base_from_jiayue.go 8.3 KB

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