base_from_jiayue.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // BridgeJiaYueIndexDataParams 桥接服务-获取嘉悦指标数据入参
  11. type BridgeJiaYueIndexDataParams struct {
  12. IndexCode string `json:"index_code" form:"index_code" description:"指标编码"`
  13. SourceExtend string `json:"source_extend" form:"source_extend" description:"来源"`
  14. StartDate string `json:"start_date" form:"start_date" description:"开始日期"`
  15. EndDate string `json:"end_date" form:"end_date" description:"结束日期"`
  16. }
  17. // BridgeJiaYueResultIndexData 桥接服务-获取嘉悦指标数据响应体
  18. type BridgeJiaYueResultIndexData struct {
  19. Code int `json:"code" description:"状态码"`
  20. Msg string `json:"msg" description:"提示信息"`
  21. Data BridgeJiaYueIndexAndData `json:"data" description:"返回数据"`
  22. }
  23. // BridgeJiaYueIndexAndData 桥接服务-嘉悦指标和数据
  24. type BridgeJiaYueIndexAndData struct {
  25. Id int `description:"指标自增ID" json:"id"`
  26. IndexCode string `description:"指标编码" json:"index_code"`
  27. IndexName string `description:"指标名称" json:"index_name"`
  28. Unit string `description:"单位" json:"unit"`
  29. Frequency string `description:"频度" json:"frequency"`
  30. LastDate time.Time `description:"指标最新时间" json:"last_date"`
  31. LastUpdateTime time.Time `description:"最新更新时间" json:"last_update_time"`
  32. Status int `description:"指标状态" json:"status"`
  33. IndexData []BridgeJiaYueIndexData `description:"指标数据" json:"index_data"`
  34. }
  35. // BridgeJiaYueIndexData 桥接服务-嘉悦指标数据
  36. type BridgeJiaYueIndexData struct {
  37. Val float64 `json:"val"`
  38. DataTime time.Time `json:"data_time"`
  39. UpdateTime time.Time `json:"update_time"`
  40. }
  41. // AddEdbDataFromJiaYue 新增嘉悦指标数据
  42. func AddEdbDataFromJiaYue(tableName, edbCode string, dataList []BridgeJiaYueIndexData) (err error) {
  43. if tableName == "" {
  44. err = fmt.Errorf("数据表名为空")
  45. return
  46. }
  47. if edbCode == "" {
  48. err = fmt.Errorf("指标编码为空")
  49. return
  50. }
  51. if len(dataList) == 0 {
  52. return
  53. }
  54. sql := fmt.Sprintf(`INSERT INTO %s(edb_info_id, edb_code, data_time, value, create_time, modify_time, data_timestamp) VALUES `, tableName)
  55. for _, v := range dataList {
  56. val := utils.SubFloatToString(v.Val, 20)
  57. stamp := fmt.Sprint(v.DataTime.UnixMilli())
  58. sql += GetAddSql("0", edbCode, v.DataTime.Format(utils.FormatDate), stamp, val)
  59. }
  60. sql = strings.TrimRight(sql, ",")
  61. // 新增入库
  62. o := orm.NewOrm()
  63. _, e := o.Raw(sql).Exec()
  64. if e != nil {
  65. err = fmt.Errorf("insert data err: %s", e.Error())
  66. }
  67. return
  68. }
  69. // RefreshEdbDataFromJiaYue 刷新嘉悦指标数据
  70. func RefreshEdbDataFromJiaYue(source, edbInfoId int, tableName, edbCode, startDate string, dataList []BridgeJiaYueIndexData) (err error) {
  71. if source <= 0 {
  72. err = fmt.Errorf("指标来源有误")
  73. return
  74. }
  75. if edbInfoId <= 0 {
  76. err = fmt.Errorf("指标ID有误")
  77. return
  78. }
  79. if tableName == "" {
  80. err = fmt.Errorf("数据表名为空")
  81. return
  82. }
  83. // 真实数据的最大日期, 插入规则配置的日期
  84. var realDataMaxDate, edbDataInsertConfigDate time.Time
  85. var edbDataInsertConfig *EdbDataInsertConfig
  86. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  87. {
  88. conf, e := GetEdbDataInsertConfigByEdbId(edbInfoId)
  89. if e != nil && e.Error() != utils.ErrNoRow() {
  90. return
  91. }
  92. edbDataInsertConfig = conf
  93. if edbDataInsertConfig != nil {
  94. edbDataInsertConfigDate = edbDataInsertConfig.Date
  95. }
  96. }
  97. // 获取已有数据
  98. cond := ` AND edb_info_id = ?`
  99. pars := make([]interface{}, 0)
  100. pars = append(pars, edbInfoId)
  101. var startDateTime time.Time
  102. if startDate != "" {
  103. cond += ` AND data_time >= ?`
  104. pars = append(pars, startDate)
  105. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  106. }
  107. existList, e := GetEdbDataByCondition(source, cond, pars)
  108. if e != nil {
  109. err = fmt.Errorf("获取指标已有数据失败, Err: %s", e.Error())
  110. return
  111. }
  112. existMap := make(map[string]*EdbInfoSearchData)
  113. for _, v := range existList {
  114. existMap[v.DataTime] = v
  115. }
  116. // 比对数据
  117. hasNew := false
  118. strEdbInfoId := strconv.Itoa(edbInfoId)
  119. addExists := make(map[string]bool)
  120. sqlInsert := fmt.Sprintf(`INSERT INTO %s(edb_info_id, edb_code, data_time, value, create_time, modify_time, data_timestamp) VALUES `, tableName)
  121. for _, v := range dataList {
  122. val := utils.SubFloatToString(v.Val, 30)
  123. stamp := fmt.Sprint(v.DataTime.UnixMilli())
  124. dataTime := v.DataTime.Format(utils.FormatDate)
  125. // 如果传入的开始时间是空的, 且当前数据日期早于传入的开始日期, 那么需要判断下当前日期的数据是否存在
  126. if !startDateTime.IsZero() && v.DataTime.Before(startDateTime) {
  127. t, e := GetEdbDataByDate(source, edbCode, dataTime)
  128. if e == nil && t != nil {
  129. existMap[t.DataTime] = t
  130. }
  131. }
  132. // 下面代码主要目的是处理掉手动插入的数据判断
  133. {
  134. if realDataMaxDate.IsZero() || v.DataTime.After(realDataMaxDate) {
  135. realDataMaxDate = v.DataTime
  136. }
  137. if edbDataInsertConfigDate.IsZero() || v.DataTime.Equal(edbDataInsertConfigDate) {
  138. isFindConfigDateRealData = true
  139. }
  140. }
  141. // 新增数据
  142. exist, ok := existMap[dataTime]
  143. if !ok {
  144. // 不在历史数据中且与新增中的数据不重复
  145. if _, o := addExists[dataTime]; !o {
  146. hasNew = true
  147. sqlInsert += GetAddSql(strEdbInfoId, edbCode, dataTime, stamp, val)
  148. addExists[dataTime] = true
  149. }
  150. continue
  151. }
  152. // 更新数据
  153. if exist != nil && utils.SubFloatToString(exist.Value, 30) != val {
  154. if e = ModifyEdbDataById(source, exist.EdbDataId, val); e != nil {
  155. err = fmt.Errorf("modify edb data err: %s", e.Error())
  156. return
  157. }
  158. }
  159. }
  160. // 处理手工数据补充的配置
  161. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  162. // 执行新增
  163. if !hasNew {
  164. return
  165. }
  166. o := orm.NewOrm()
  167. sqlInsert = strings.TrimRight(sqlInsert, ",")
  168. _, e = o.Raw(sqlInsert).Exec()
  169. if e != nil {
  170. err = fmt.Errorf("insert edb data err: %s", e.Error())
  171. return
  172. }
  173. return
  174. }