base_from_sci.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/beego/beego/v2/client/orm"
  9. )
  10. type BaseFromSciData struct {
  11. SciDataId int `orm:"column(sci_data_id);pk"`
  12. BaseFromSciIndexId int
  13. IndexCode string
  14. DataTime string
  15. Value string
  16. CreateTime time.Time
  17. ModifyTime time.Time
  18. DataTimestamp int64
  19. }
  20. // Update 修改
  21. func (r *BaseFromSciData) Update(updateCols []string) (err error) {
  22. o := orm.NewOrm()
  23. _, err = o.Update(r, updateCols...)
  24. return
  25. }
  26. // GetIndexDataList 获取所有的指标数据
  27. func (r *BaseFromSciData) GetIndexDataList(indexCode string) (items []*BaseFromSciData, err error) {
  28. o := orm.NewOrm()
  29. sql := `SELECT * FROM base_from_sci_data WHERE 1=1 AND index_code = ? `
  30. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  31. return
  32. }
  33. // BatchAdd 批量添加指标
  34. func (r *BaseFromSciData) BatchAdd(list []*BaseFromSciData) (err error) {
  35. o := orm.NewOrm()
  36. _, err = o.InsertMulti(len(list), list)
  37. return
  38. }
  39. func GetBaseFromSciDataByCondition(condition string, pars []interface{}) (list []*BaseFromSciData, err error) {
  40. o := orm.NewOrm()
  41. sql := `SELECT * FROM base_from_sci_data WHERE 1=1 `
  42. if condition != "" {
  43. sql += condition
  44. }
  45. _, err = o.Raw(sql, pars).QueryRows(&list)
  46. return
  47. }
  48. // AddEdbDataFromSci 新增卓创(红桃3)指标数据
  49. func AddEdbDataFromSci(edbCode string) (err error) {
  50. o := orm.NewOrm()
  51. var condition string
  52. var pars []interface{}
  53. if edbCode != "" {
  54. condition += " AND index_code=? "
  55. pars = append(pars, edbCode)
  56. }
  57. sciBaseDataAll, err := GetBaseFromSciDataByCondition(condition, pars)
  58. if err != nil && err.Error() != utils.ErrNoRow() {
  59. return
  60. }
  61. var isAdd bool
  62. addSql := ` INSERT INTO edb_data_sci(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  63. existMap := make(map[string]string)
  64. for _, sv := range sciBaseDataAll {
  65. eDate := sv.DataTime
  66. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  67. if err != nil {
  68. return err
  69. }
  70. timestamp := dataTime.UnixNano() / 1e6
  71. timeStr := fmt.Sprintf("%d", timestamp)
  72. if _, ok := existMap[eDate]; !ok {
  73. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  74. isAdd = true
  75. }
  76. existMap[eDate] = sv.Value
  77. }
  78. if isAdd {
  79. addSql = strings.TrimRight(addSql, ",")
  80. utils.FileLog.Info("addSql:" + addSql)
  81. _, err = o.Raw(addSql).Exec()
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return
  87. }
  88. // RefreshEdbDataFromSci 刷新卓创(红桃3)指标数据
  89. func RefreshEdbDataFromSci(edbInfoId int, edbCode, startDate string) (err error) {
  90. source := utils.DATA_SOURCE_SCI
  91. o := orm.NewOrm()
  92. if err != nil {
  93. return
  94. }
  95. edbInfoIdStr := strconv.Itoa(edbInfoId)
  96. //计算数据
  97. var condition string
  98. var pars []interface{}
  99. if edbCode != "" {
  100. condition += " AND index_code=? "
  101. pars = append(pars, edbCode)
  102. }
  103. if startDate != "" {
  104. condition += " AND data_time>=? "
  105. pars = append(pars, startDate)
  106. }
  107. sciDataList, err := GetBaseFromSciDataByCondition(condition, pars)
  108. if err != nil {
  109. return
  110. }
  111. // 真实数据的最大日期 , 插入规则配置的日期
  112. var realDataMaxDate, edbDataInsertConfigDate time.Time
  113. var edbDataInsertConfig *EdbDataInsertConfig
  114. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  115. {
  116. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  117. if err != nil && err.Error() != utils.ErrNoRow() {
  118. return
  119. }
  120. if edbDataInsertConfig != nil {
  121. edbDataInsertConfigDate = edbDataInsertConfig.Date
  122. }
  123. }
  124. var existCondition string
  125. var existPars []interface{}
  126. existCondition += " AND edb_info_id=? "
  127. existPars = append(existPars, edbInfoId)
  128. if startDate != "" {
  129. existCondition += " AND data_time>=? "
  130. existPars = append(existPars, startDate)
  131. }
  132. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  133. if err != nil {
  134. return err
  135. }
  136. existMap := make(map[string]*EdbInfoSearchData)
  137. for _, v := range existList {
  138. existMap[v.DataTime] = v
  139. }
  140. addSql := ` INSERT INTO edb_data_sci(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  141. var isAdd bool
  142. for _, v := range sciDataList {
  143. item := v
  144. eDate := item.DataTime
  145. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  146. if err != nil {
  147. return err
  148. }
  149. if findItem, ok := existMap[v.DataTime]; !ok {
  150. sValue := item.Value
  151. timestamp := dataTime.UnixNano() / 1e6
  152. timeStr := fmt.Sprintf("%d", timestamp)
  153. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  154. isAdd = true
  155. } else {
  156. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  157. err = ModifyEdbDataById(source, findItem.EdbDataId, item.Value)
  158. if err != nil {
  159. return err
  160. }
  161. }
  162. }
  163. // 下面代码主要目的是处理掉手动插入的数据判断
  164. {
  165. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  166. realDataMaxDate = dataTime
  167. }
  168. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  169. isFindConfigDateRealData = true
  170. }
  171. }
  172. }
  173. // 处理手工数据补充的配置
  174. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  175. if isAdd {
  176. addSql = strings.TrimRight(addSql, ",")
  177. _, err = o.Raw(addSql).Exec()
  178. if err != nil {
  179. fmt.Println("RefreshEdbDataFromSci add Err", err.Error())
  180. return
  181. }
  182. }
  183. return
  184. }
  185. // HandleSciExcelDataReq 卓创(红桃3)的excel数据
  186. type HandleSciExcelDataReq struct {
  187. DataMap map[string]map[string]string
  188. IndexNameList []string
  189. ThirdIndexIdList []string
  190. FrequencyList []string
  191. UnitList []string
  192. }
  193. // BaseFromSciIndex 红桃3指标表格
  194. type BaseFromSciIndex struct {
  195. BaseFromSciIndexId int `orm:"column(base_from_sci_index_id);pk" json:"base_from_sci_index_id"` //序号
  196. ClassifyId int `gorm:"column:classify_id" json:"classify_id"`
  197. IndexCode string `gorm:"column:index_code" json:"index_code" description:"指标编码"`
  198. IndexName string `gorm:"column:index_name" json:"index_name" description:"指标名称"`
  199. Frequency string `gorm:"column:frequency" json:"frequency"`
  200. Unit string `gorm:"column:unit" json:"unit"`
  201. StartDate time.Time `gorm:"column:start_date" json:"start_date"`
  202. EndDate time.Time `gorm:"column:end_date" json:"end_date"`
  203. CreateTime time.Time `gorm:"autoCreateTime;column:create_time" json:"create_time"` //创建时间
  204. ModifyTime time.Time `gorm:"autoUpdateTime:milli;column:modify_time" json:"modify_time"` //最后更新时间
  205. }
  206. // TableName get sql table name.获取数据库表名
  207. func (r *BaseFromSciIndex) TableName() string {
  208. return "base_from_sci_index"
  209. }
  210. // Update 修改
  211. func (r *BaseFromSciIndex) Update(updateCols []string) (err error) {
  212. o := orm.NewOrm()
  213. _, err = o.Update(r, updateCols...)
  214. return
  215. }
  216. // GetAllIndex 获取所有的指标
  217. func (r *BaseFromSciIndex) GetAllIndex() (items []*BaseFromSciIndex, err error) {
  218. o := orm.NewOrm()
  219. sql := `SELECT * FROM base_from_sci_index WHERE 1=1 `
  220. _, err = o.Raw(sql).QueryRows(&items)
  221. return
  222. }
  223. // BatchAdd 批量添加指标
  224. func (r *BaseFromSciIndex) BatchAdd(list []*BaseFromSciIndex) (err error) {
  225. o := orm.NewOrm()
  226. _, err = o.InsertMulti(len(list), list)
  227. return
  228. }