base_from_gn.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // BridgeGnIndexDataParams 桥接服务-获取国能指标数据入参
  11. type BridgeGnIndexDataParams struct {
  12. IndexCode string `json:"index_code" form:"index_code" description:"指标编码"`
  13. StartDate string `json:"start_date" form:"start_date" description:"开始日期"`
  14. EndDate string `json:"end_date" form:"end_date" description:"结束日期"`
  15. }
  16. // BridgeGnIndexAndData 桥接服务-国能指标和数据
  17. type BridgeGnIndexAndData struct {
  18. Val float64 `json:"val"`
  19. DataTime string `json:"data_time"`
  20. UpdateTime string `json:"update_time"`
  21. }
  22. // BridgeGnResultIndexData 桥接服务-获取国能指标数据响应体
  23. type BridgeGnResultIndexData struct {
  24. Code int `json:"code" description:"状态码"`
  25. Msg string `json:"msg" description:"提示信息"`
  26. Data []BridgeGnIndexAndData `json:"data" description:"返回数据"`
  27. }
  28. // RefreshEdbDataFromGn
  29. // @Description: 刷新国能指标数据
  30. // @param source
  31. // @param subSource
  32. // @param edbInfoId
  33. // @param tableName
  34. // @param edbCode
  35. // @param startDate
  36. // @param dataList
  37. // @return err
  38. func RefreshEdbDataFromGn(source, subSource, edbInfoId int, tableName, edbCode, startDate string, dataList []BridgeGnIndexAndData) (err error) {
  39. if source <= 0 {
  40. err = fmt.Errorf("指标来源有误")
  41. return
  42. }
  43. if edbInfoId <= 0 {
  44. err = fmt.Errorf("指标ID有误")
  45. return
  46. }
  47. if tableName == "" {
  48. err = fmt.Errorf("数据表名为空")
  49. return
  50. }
  51. // 真实数据的最大日期, 插入规则配置的日期
  52. var realDataMaxDate, edbDataInsertConfigDate time.Time
  53. var edbDataInsertConfig *EdbDataInsertConfig
  54. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  55. {
  56. conf, e := GetEdbDataInsertConfigByEdbId(edbInfoId)
  57. if e != nil && e.Error() != utils.ErrNoRow() {
  58. return
  59. }
  60. edbDataInsertConfig = conf
  61. if edbDataInsertConfig != nil {
  62. edbDataInsertConfigDate = edbDataInsertConfig.Date
  63. }
  64. }
  65. // 获取已有数据
  66. cond := ` AND edb_info_id = ?`
  67. pars := make([]interface{}, 0)
  68. pars = append(pars, edbInfoId)
  69. var startDateTime time.Time
  70. if startDate != "" {
  71. cond += ` AND data_time >= ?`
  72. pars = append(pars, startDate)
  73. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  74. }
  75. existList, e := GetEdbDataByCondition(source, subSource, cond, pars)
  76. if e != nil {
  77. err = fmt.Errorf("获取指标已有数据失败, Err: %s", e.Error())
  78. return
  79. }
  80. existMap := make(map[string]*EdbInfoSearchData)
  81. for _, v := range existList {
  82. existMap[v.DataTime] = v
  83. }
  84. // 比对数据
  85. hasNew := false
  86. strEdbInfoId := strconv.Itoa(edbInfoId)
  87. addExists := make(map[string]bool)
  88. sqlInsert := fmt.Sprintf(`INSERT INTO %s(edb_info_id, edb_code, data_time, value, create_time, modify_time, data_timestamp) VALUES `, tableName)
  89. for _, v := range dataList {
  90. currDataTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  91. if tmpErr != nil {
  92. err = tmpErr
  93. return
  94. }
  95. val := utils.SubFloatToString(v.Val, 30)
  96. stamp := fmt.Sprint(currDataTime.UnixMilli())
  97. dataTime := currDataTime.Format(utils.FormatDate)
  98. // 如果传入的开始时间是空的, 且当前数据日期早于传入的开始日期, 那么需要判断下当前日期的数据是否存在
  99. if !startDateTime.IsZero() && currDataTime.Before(startDateTime) {
  100. t, e := GetEdbDataByDate(source, subSource, edbCode, dataTime)
  101. if e == nil && t != nil {
  102. existMap[t.DataTime] = t
  103. }
  104. }
  105. // 下面代码主要目的是处理掉手动插入的数据判断
  106. {
  107. if realDataMaxDate.IsZero() || currDataTime.After(realDataMaxDate) {
  108. realDataMaxDate = currDataTime
  109. }
  110. if edbDataInsertConfigDate.IsZero() || currDataTime.Equal(edbDataInsertConfigDate) {
  111. isFindConfigDateRealData = true
  112. }
  113. }
  114. // 新增数据
  115. exist, ok := existMap[dataTime]
  116. if !ok {
  117. // 不在历史数据中且与新增中的数据不重复
  118. if _, o := addExists[dataTime]; !o {
  119. hasNew = true
  120. sqlInsert += GetAddSql(strEdbInfoId, edbCode, dataTime, stamp, val)
  121. addExists[dataTime] = true
  122. }
  123. continue
  124. }
  125. // 更新数据
  126. if exist != nil && utils.SubFloatToString(exist.Value, 30) != val {
  127. if e = ModifyEdbDataById(source, subSource, exist.EdbDataId, val); e != nil {
  128. err = fmt.Errorf("modify edb data err: %s", e.Error())
  129. return
  130. }
  131. }
  132. }
  133. // 处理手工数据补充的配置
  134. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  135. // 执行新增
  136. if !hasNew {
  137. return
  138. }
  139. sqlInsert = strings.TrimRight(sqlInsert, ",")
  140. e = global.DEFAULT_DmSQL.Exec(sqlInsert).Error
  141. if e != nil {
  142. err = fmt.Errorf("insert edb data err: %s", e.Error())
  143. return
  144. }
  145. return
  146. }