predict_edb_data_calculate_zjpj.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package models
  2. import (
  3. "errors"
  4. "eta_gn/eta_index_lib/global"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  7. "gorm.io/gorm"
  8. "strings"
  9. "time"
  10. )
  11. // EdbDataPredictCalculateZjpj 直接拼接数据结构体
  12. type EdbDataPredictCalculateZjpj struct {
  13. EdbDataId int `gorm:"primaryKey;autoIncrement;column:edb_data_id"`
  14. EdbInfoId int
  15. EdbCode string
  16. DataTime string
  17. Value float64
  18. Status int
  19. CreateTime time.Time
  20. ModifyTime time.Time
  21. DataTimestamp int64
  22. }
  23. // RefreshAllPredictCalculateZjpj 刷新所有 直接拼接 数据
  24. func RefreshAllPredictCalculateZjpj(edbInfo *EdbInfo) (latestDateStr string, latestValue float64, err error) {
  25. to := global.DEFAULT_DmSQL.Begin()
  26. defer func() {
  27. if err != nil {
  28. to.Rollback()
  29. } else {
  30. to.Commit()
  31. }
  32. }()
  33. //查询关联指标信息
  34. var existCondition string
  35. var existPars []interface{}
  36. existCondition += " AND edb_info_id=? "
  37. existPars = append(existPars, edbInfo.EdbInfoId)
  38. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  39. if err != nil {
  40. err = fmt.Errorf("判断指标是否改变失败,Err:" + err.Error())
  41. return
  42. }
  43. var existItemA, existItemB *EdbInfoCalculateMapping
  44. for _, existItem := range existList {
  45. if existItem.FromTag == "A" {
  46. existItemA = existItem
  47. } else if existItem.FromTag == "B" {
  48. existItemB = existItem
  49. }
  50. }
  51. if existItemA == nil {
  52. err = errors.New("原拼接日期之前的指标不存在")
  53. return
  54. }
  55. if existItemB == nil {
  56. err = errors.New("原拼接日期之后的指标不存在")
  57. return
  58. }
  59. fromEdbInfo, err := GetEdbInfoById(existItemA.FromEdbInfoId)
  60. if err != nil {
  61. err = fmt.Errorf("GetEdbInfoById Err:" + err.Error())
  62. return
  63. }
  64. secondEdbInfo, err := GetEdbInfoById(existItemB.FromEdbInfoId)
  65. if err != nil {
  66. err = fmt.Errorf("GetEdbInfoById Err:" + err.Error())
  67. return
  68. }
  69. // 刷新数据
  70. latestDateStr, latestValue, err = refreshAllPredictCalculateZjpj(to, edbInfo, fromEdbInfo, secondEdbInfo)
  71. return
  72. }
  73. // refreshAllPredictCalculateZjpj 刷新所有 直接拼接 数据
  74. func refreshAllPredictCalculateZjpj(to *gorm.DB, edbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (latestDateStr string, latestValue float64, err error) {
  75. //查询当前指标现有的数据
  76. var dataList []*EdbDataPredictCalculateZjpj
  77. sql := ` SELECT * FROM edb_data_predict_calculate_zjpj WHERE edb_info_id=? ORDER BY data_time DESC `
  78. err = to.Raw(sql, edbInfo.EdbInfoId).Scan(&dataList).Error
  79. if err != nil {
  80. return
  81. }
  82. if edbInfo.CalculateFormula <= secondEdbInfo.LatestDate {
  83. latestDateStr = secondEdbInfo.LatestDate
  84. } else {
  85. if edbInfo.CalculateFormula >= firstEdbInfo.LatestDate {
  86. latestDateStr = firstEdbInfo.LatestDate
  87. } else {
  88. latestDateStr = edbInfo.CalculateFormula
  89. }
  90. }
  91. var dateArr []string
  92. dataMap := make(map[string]*EdbDataPredictCalculateZjpj)
  93. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  94. for _, v := range dataList {
  95. dateArr = append(dateArr, v.DataTime)
  96. dataMap[v.DataTime] = v
  97. removeDataTimeMap[v.DataTime] = 1
  98. }
  99. addDataList := make([]*EdbDataPredictCalculateZjpj, 0)
  100. //第一个指标
  101. {
  102. var firstDataList []*EdbInfoSearchData
  103. firstDataList, err = GetPredictEdbDataListAllByStartDate(firstEdbInfo, 0, "")
  104. if err != nil {
  105. return
  106. }
  107. for _, v := range firstDataList {
  108. if v.DataTime >= edbInfo.CalculateFormula {
  109. continue
  110. }
  111. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  112. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  113. delete(removeDataTimeMap, v.DataTime)
  114. }
  115. if latestDateStr == v.DataTime {
  116. latestValue = v.Value
  117. }
  118. //时间戳
  119. if edbData, ok := dataMap[v.DataTime]; ok {
  120. if edbData.Value != v.Value {
  121. //更新指标数据
  122. edbData.Value = v.Value
  123. _ = to.Model(edbData).Select([]string{"Value"}).Updates(edbData).Error
  124. }
  125. } else {
  126. //时间戳
  127. currentDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  128. timestamp := currentDate.UnixNano() / 1e6
  129. edbDataZjpj := &EdbDataPredictCalculateZjpj{
  130. EdbInfoId: edbInfo.EdbInfoId,
  131. EdbCode: edbInfo.EdbCode,
  132. DataTime: v.DataTime,
  133. Value: v.Value,
  134. Status: 1,
  135. CreateTime: time.Now(),
  136. ModifyTime: time.Now(),
  137. DataTimestamp: timestamp,
  138. }
  139. addDataList = append(addDataList, edbDataZjpj)
  140. }
  141. }
  142. }
  143. //第二个指标
  144. {
  145. var secondDataList []*EdbInfoSearchData
  146. secondDataList, err = GetPredictEdbDataListAllByStartDate(secondEdbInfo, 0, edbInfo.CalculateFormula)
  147. if err != nil {
  148. return
  149. }
  150. for _, v := range secondDataList {
  151. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  152. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  153. delete(removeDataTimeMap, v.DataTime)
  154. }
  155. if latestDateStr == v.DataTime {
  156. latestValue = v.Value
  157. }
  158. if edbData, ok := dataMap[v.DataTime]; ok {
  159. if edbData.Value != v.Value {
  160. //更新指标数据
  161. edbData.Value = v.Value
  162. edbData.ModifyTime = time.Now()
  163. tmpErr := to.Model(edbData).Select([]string{"Value", "ModifyTime"}).Updates(edbData).Error
  164. if tmpErr != nil {
  165. fmt.Println("tmpErr:", tmpErr)
  166. err = tmpErr
  167. return
  168. }
  169. }
  170. } else {
  171. //时间戳
  172. currentDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  173. timestamp := currentDate.UnixNano() / 1e6
  174. edbDataZjpj := &EdbDataPredictCalculateZjpj{
  175. EdbInfoId: edbInfo.EdbInfoId,
  176. EdbCode: edbInfo.EdbCode,
  177. DataTime: v.DataTime,
  178. Value: v.Value,
  179. Status: 1,
  180. CreateTime: time.Now(),
  181. ModifyTime: time.Now(),
  182. DataTimestamp: timestamp,
  183. }
  184. addDataList = append(addDataList, edbDataZjpj)
  185. }
  186. }
  187. }
  188. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  189. {
  190. removeDateList := make([]string, 0)
  191. for dateTime := range removeDataTimeMap {
  192. removeDateList = append(removeDateList, dateTime)
  193. }
  194. if len(removeDateList) > 0 {
  195. removeDateStr := strings.Join(removeDateList, `','`)
  196. removeDateStr = `'` + removeDateStr + `'`
  197. //如果拼接指标变更了,那么需要删除所有的指标数据
  198. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  199. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  200. err = to.Exec(sql, edbInfo.EdbInfoId).Error
  201. if err != nil {
  202. err = fmt.Errorf("删除不存在的直接拼接指标数据失败,Err:" + err.Error())
  203. return
  204. }
  205. }
  206. }
  207. //数据入库
  208. if len(addDataList) > 0 {
  209. tmpErr := to.CreateInBatches(addDataList, 500).Error
  210. if tmpErr != nil {
  211. err = tmpErr
  212. return
  213. }
  214. }
  215. return
  216. }