base_from_gn.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type BridgeGnIndexDataParams struct {
  11. IndexCode string `json:"index_code" form:"index_code" description:"指标编码"`
  12. StartDate string `json:"start_date" form:"start_date" description:"开始日期"`
  13. EndDate string `json:"end_date" form:"end_date" description:"结束日期"`
  14. }
  15. type BridgeGnIndexAndData struct {
  16. Val float64 `json:"val"`
  17. DataTime string `json:"data_time"`
  18. UpdateTime string `json:"update_time"`
  19. }
  20. type BridgeGnResultIndexData struct {
  21. Code int `json:"code" description:"状态码"`
  22. Msg string `json:"msg" description:"提示信息"`
  23. Data []BridgeGnIndexAndData `json:"data" description:"返回数据"`
  24. }
  25. func RefreshEdbDataFromGn(source, subSource, edbInfoId int, tableName, edbCode, startDate string, dataList []BridgeGnIndexAndData) (err error) {
  26. if source <= 0 {
  27. err = fmt.Errorf("指标来源有误")
  28. return
  29. }
  30. if edbInfoId <= 0 {
  31. err = fmt.Errorf("指标ID有误")
  32. return
  33. }
  34. if tableName == "" {
  35. err = fmt.Errorf("数据表名为空")
  36. return
  37. }
  38. var realDataMaxDate, edbDataInsertConfigDate time.Time
  39. var edbDataInsertConfig *EdbDataInsertConfig
  40. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  41. {
  42. conf, e := GetEdbDataInsertConfigByEdbId(edbInfoId)
  43. if e != nil && e.Error() != utils.ErrNoRow() {
  44. return
  45. }
  46. edbDataInsertConfig = conf
  47. if edbDataInsertConfig != nil {
  48. edbDataInsertConfigDate = edbDataInsertConfig.Date
  49. }
  50. }
  51. cond := ` AND edb_info_id = ?`
  52. pars := make([]interface{}, 0)
  53. pars = append(pars, edbInfoId)
  54. var startDateTime time.Time
  55. if startDate != "" {
  56. cond += ` AND data_time >= ?`
  57. pars = append(pars, startDate)
  58. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  59. }
  60. existList, e := GetEdbDataByCondition(source, subSource, cond, pars)
  61. if e != nil {
  62. err = fmt.Errorf("获取指标已有数据失败, Err: %s", e.Error())
  63. return
  64. }
  65. existMap := make(map[string]*EdbInfoSearchData)
  66. for _, v := range existList {
  67. existMap[v.DataTime] = v
  68. }
  69. hasNew := false
  70. strEdbInfoId := strconv.Itoa(edbInfoId)
  71. addExists := make(map[string]bool)
  72. sqlInsert := fmt.Sprintf(`INSERT INTO %s(edb_info_id, edb_code, data_time, value, create_time, modify_time, data_timestamp) VALUES `, tableName)
  73. for _, v := range dataList {
  74. currDataTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  75. if tmpErr != nil {
  76. err = tmpErr
  77. return
  78. }
  79. val := utils.SubFloatToString(v.Val, 30)
  80. stamp := fmt.Sprint(currDataTime.UnixMilli())
  81. dataTime := currDataTime.Format(utils.FormatDate)
  82. if !startDateTime.IsZero() && currDataTime.Before(startDateTime) {
  83. t, e := GetEdbDataByDate(source, subSource, edbCode, dataTime)
  84. if e == nil && t != nil {
  85. existMap[t.DataTime] = t
  86. }
  87. }
  88. {
  89. if realDataMaxDate.IsZero() || currDataTime.After(realDataMaxDate) {
  90. realDataMaxDate = currDataTime
  91. }
  92. if edbDataInsertConfigDate.IsZero() || currDataTime.Equal(edbDataInsertConfigDate) {
  93. isFindConfigDateRealData = true
  94. }
  95. }
  96. exist, ok := existMap[dataTime]
  97. if !ok {
  98. if _, o := addExists[dataTime]; !o {
  99. hasNew = true
  100. sqlInsert += GetAddSql(strEdbInfoId, edbCode, dataTime, stamp, val)
  101. addExists[dataTime] = true
  102. }
  103. continue
  104. }
  105. if exist != nil && utils.SubFloatToString(exist.Value, 30) != val {
  106. if e = ModifyEdbDataById(source, subSource, exist.EdbDataId, val); e != nil {
  107. err = fmt.Errorf("modify edb data err: %s", e.Error())
  108. return
  109. }
  110. }
  111. }
  112. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  113. if !hasNew {
  114. return
  115. }
  116. sqlInsert = strings.TrimRight(sqlInsert, ",")
  117. e = global.DEFAULT_DmSQL.Exec(sqlInsert).Error
  118. if e != nil {
  119. err = fmt.Errorf("insert edb data err: %s", e.Error())
  120. return
  121. }
  122. return
  123. }