base_from_lt.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //路透
  12. // 新增路透指标数据
  13. func AddEdbDataFromLt(edbCode string, ltDataList map[int64]interface{}) (err error) {
  14. o := orm.NewOrm()
  15. if len(ltDataList) > 0 {
  16. var isAdd bool
  17. addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  18. for timestampInt, edbValue := range ltDataList {
  19. dataTime := time.Unix(timestampInt/1000, 0)
  20. //校验数据类型对不对
  21. valType := reflect.TypeOf(edbValue)
  22. if valType == nil {
  23. continue
  24. }
  25. if valType.String() != "float64" {
  26. continue
  27. }
  28. sValue := edbValue.(float64)
  29. eDate := dataTime.Format(utils.FormatDate)
  30. if err != nil {
  31. return err
  32. }
  33. timestamp := dataTime.UnixNano() / 1e6
  34. timeStr := fmt.Sprintf("%d", timestamp)
  35. addSql += GetAddSql("0", edbCode, eDate, timeStr, utils.SubFloatToString(sValue, 20))
  36. isAdd = true
  37. }
  38. if isAdd {
  39. addSql = strings.TrimRight(addSql, ",")
  40. _, err = o.Raw(addSql).Exec()
  41. if err != nil {
  42. fmt.Println("RefreshAllEdbDataByPb add Err", err.Error())
  43. return
  44. }
  45. }
  46. }
  47. return
  48. }
  49. // 刷新路透指标数据
  50. func RefreshEdbDataFromLt(edbInfoId int, edbCode, startDate string, ltDataList map[int64]interface{}) (err error) {
  51. source := utils.DATA_SOURCE_LT
  52. subSource := utils.DATA_SUB_SOURCE_EDB
  53. o := orm.NewOrm()
  54. // 真实数据的最大日期 , 插入规则配置的日期
  55. var realDataMaxDate, edbDataInsertConfigDate time.Time
  56. var edbDataInsertConfig *EdbDataInsertConfig
  57. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  58. {
  59. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  60. if err != nil && err.Error() != utils.ErrNoRow() {
  61. return
  62. }
  63. if edbDataInsertConfig != nil {
  64. edbDataInsertConfigDate = edbDataInsertConfig.Date
  65. }
  66. }
  67. var existCondition string
  68. var existPars []interface{}
  69. existCondition += " AND edb_info_id=? "
  70. existPars = append(existPars, edbInfoId)
  71. if startDate != "" {
  72. existCondition += " AND data_time>=? "
  73. existPars = append(existPars, startDate)
  74. }
  75. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  76. if err != nil {
  77. return err
  78. }
  79. existMap := make(map[string]*EdbInfoSearchData)
  80. for _, v := range existList {
  81. existMap[v.DataTime] = v
  82. }
  83. edbInfoIdStr := strconv.Itoa(edbInfoId)
  84. addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  85. var isAdd bool
  86. for timestampInt, edbValue := range ltDataList {
  87. dataTime := time.Unix(timestampInt/1000, 0)
  88. //校验数据类型对不对
  89. valType := reflect.TypeOf(edbValue)
  90. if valType == nil {
  91. continue
  92. }
  93. if valType.String() != "float64" {
  94. continue
  95. }
  96. sValue := edbValue.(float64)
  97. eDate := dataTime.Format(utils.FormatDate)
  98. if err != nil {
  99. return err
  100. }
  101. dataTime, _ = time.ParseInLocation(utils.FormatDate, eDate, time.Local) //这么弄的主要目的是为了北京时间
  102. saveValue := utils.SubFloatToString(sValue, 30)
  103. if findItem, ok := existMap[eDate]; !ok {
  104. timestamp := dataTime.UnixNano() / 1e6
  105. timeStr := fmt.Sprintf("%d", timestamp)
  106. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  107. isAdd = true
  108. } else {
  109. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != saveValue {
  110. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, saveValue)
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. }
  116. // 下面代码主要目的是处理掉手动插入的数据判断
  117. {
  118. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  119. realDataMaxDate = dataTime
  120. }
  121. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  122. isFindConfigDateRealData = true
  123. }
  124. }
  125. }
  126. // 处理手工数据补充的配置
  127. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  128. if isAdd {
  129. addSql = strings.TrimRight(addSql, ",")
  130. _, err = o.Raw(addSql).Exec()
  131. if err != nil {
  132. fmt.Println("RefreshAllEdbDataByLt add Err", err.Error())
  133. return
  134. }
  135. }
  136. return
  137. }