base_from_lt.go 4.3 KB

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