base_from_sci.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package models
  2. import (
  3. "fmt"
  4. "hongze/hongze_edb_lib/utils"
  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. func GetBaseFromSciDataByCondition(condition string, pars []interface{}) (list []*BaseFromSciData, err error) {
  21. o := orm.NewOrm()
  22. sql := `SELECT * FROM base_from_sci_data WHERE 1=1 `
  23. if condition != "" {
  24. sql += condition
  25. }
  26. _, err = o.Raw(sql, pars).QueryRows(&list)
  27. return
  28. }
  29. // 新增有色指标数据
  30. func AddEdbDataFromSci(edbCode string) (err error) {
  31. o := orm.NewOrm()
  32. var condition string
  33. var pars []interface{}
  34. if edbCode != "" {
  35. condition += " AND index_code=? "
  36. pars = append(pars, edbCode)
  37. }
  38. sciBaseDataAll, err := GetBaseFromSciDataByCondition(condition, pars)
  39. if err != nil && err.Error() != utils.ErrNoRow() {
  40. return
  41. }
  42. var isAdd bool
  43. addSql := ` INSERT INTO edb_data_sci(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  44. existMap := make(map[string]string)
  45. for _, sv := range sciBaseDataAll {
  46. eDate := sv.DataTime
  47. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  48. if err != nil {
  49. return err
  50. }
  51. timestamp := dataTime.UnixNano() / 1e6
  52. timeStr := fmt.Sprintf("%d", timestamp)
  53. if _, ok := existMap[eDate]; !ok {
  54. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  55. isAdd = true
  56. }
  57. existMap[eDate] = sv.Value
  58. }
  59. if isAdd {
  60. addSql = strings.TrimRight(addSql, ",")
  61. utils.FileLog.Info("addSql:" + addSql)
  62. _, err = o.Raw(addSql).Exec()
  63. if err != nil {
  64. return err
  65. }
  66. }
  67. return
  68. }
  69. // 刷新有色指标数据
  70. func RefreshEdbDataFromSci(edbInfoId int, edbCode, startDate string) (err error) {
  71. source := utils.DATA_SOURCE_SCI
  72. o := orm.NewOrm()
  73. if err != nil {
  74. return
  75. }
  76. edbInfoIdStr := strconv.Itoa(edbInfoId)
  77. //计算数据
  78. var condition string
  79. var pars []interface{}
  80. if edbCode != "" {
  81. condition += " AND index_code=? "
  82. pars = append(pars, edbCode)
  83. }
  84. if startDate != "" {
  85. condition += " AND data_time>=? "
  86. pars = append(pars, startDate)
  87. }
  88. sciDataList, err := GetBaseFromSciDataByCondition(condition, pars)
  89. if err != nil {
  90. return
  91. }
  92. // 真实数据的最大日期 , 插入规则配置的日期
  93. var realDataMaxDate, edbDataInsertConfigDate time.Time
  94. var edbDataInsertConfig *EdbDataInsertConfig
  95. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  96. {
  97. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  98. if err != nil && err.Error() != utils.ErrNoRow() {
  99. return
  100. }
  101. if edbDataInsertConfig != nil {
  102. edbDataInsertConfigDate = edbDataInsertConfig.Date
  103. }
  104. }
  105. var existCondition string
  106. var existPars []interface{}
  107. existCondition += " AND edb_info_id=? "
  108. existPars = append(existPars, edbInfoId)
  109. if startDate != "" {
  110. existCondition += " AND data_time>=? "
  111. existPars = append(existPars, startDate)
  112. }
  113. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  114. if err != nil {
  115. return err
  116. }
  117. existMap := make(map[string]*EdbInfoSearchData)
  118. for _, v := range existList {
  119. existMap[v.DataTime] = v
  120. }
  121. addSql := ` INSERT INTO edb_data_sci(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  122. var isAdd bool
  123. for _, v := range sciDataList {
  124. item := v
  125. eDate := item.DataTime
  126. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  127. if err != nil {
  128. return err
  129. }
  130. if findItem, ok := existMap[v.DataTime]; !ok {
  131. sValue := item.Value
  132. timestamp := dataTime.UnixNano() / 1e6
  133. timeStr := fmt.Sprintf("%d", timestamp)
  134. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  135. isAdd = true
  136. } else {
  137. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  138. err = ModifyEdbDataById(source, findItem.EdbDataId, item.Value)
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. }
  144. // 下面代码主要目的是处理掉手动插入的数据判断
  145. {
  146. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  147. realDataMaxDate = dataTime
  148. }
  149. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  150. isFindConfigDateRealData = true
  151. }
  152. }
  153. }
  154. // 处理手工数据补充的配置
  155. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  156. if isAdd {
  157. addSql = strings.TrimRight(addSql, ",")
  158. _, err = o.Raw(addSql).Exec()
  159. if err != nil {
  160. fmt.Println("RefreshEdbDataFromSci add Err", err.Error())
  161. return
  162. }
  163. }
  164. return
  165. }