predict_edb_info.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package data
  2. import (
  3. "errors"
  4. "hongze/hongze_chart_lib/models"
  5. "hongze/hongze_chart_lib/models/data_manage"
  6. "hongze/hongze_chart_lib/utils"
  7. "time"
  8. )
  9. // GetChartPredictEdbInfoDataList 获取图表的预测指标的未来数据
  10. func GetChartPredictEdbInfoDataList(predictEdbConf data_manage.PredictEdbConf, filtrateStartDateStr, latestDateStr string, lastDataValue float64, endDateStr, frequency string) (predictEdbInfoData []*models.EdbDataList, err error) {
  11. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  12. if err != nil {
  13. return
  14. }
  15. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  16. if err != nil {
  17. return
  18. }
  19. // 开始预测数据的时间
  20. startDate := latestDate
  21. // 如果有筛选时间的话
  22. if filtrateStartDateStr != `` {
  23. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  24. if tmpErr != nil {
  25. err = tmpErr
  26. return
  27. }
  28. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  29. if filtrateStartDate.After(latestDate) {
  30. startDate = filtrateStartDate.AddDate(0, 0, -1)
  31. }
  32. }
  33. dataValue := lastDataValue
  34. if predictEdbConf.RuleType == 2 {
  35. dataValue = predictEdbConf.FixedValue
  36. }
  37. //获取后面的预测数据
  38. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  39. predictEdbInfoData = make([]*models.EdbDataList, 0)
  40. for k, v := range dayList {
  41. predictEdbInfoData = append(predictEdbInfoData, &models.EdbDataList{
  42. EdbDataId: predictEdbConf.PredictEdbInfoId + 10000000000 + k,
  43. EdbInfoId: predictEdbConf.PredictEdbInfoId,
  44. DataTime: v.Format(utils.FormatDate),
  45. Value: dataValue,
  46. DataTimestamp: (v.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  47. })
  48. }
  49. return
  50. }
  51. // GetPredictEdbDayList 获取预测指标日期列表
  52. func getPredictEdbDayList(startDate, endDate time.Time, frequency string) (dayList []time.Time) {
  53. //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
  54. switch frequency {
  55. case "日度":
  56. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  57. //周六、日排除
  58. if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday {
  59. continue
  60. }
  61. dayList = append(dayList, currDate)
  62. }
  63. case "周度":
  64. //nextDate := startDate.AddDate(0, 0, 7)
  65. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  66. dayList = append(dayList, currDate)
  67. }
  68. case "月度":
  69. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  70. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 2, -1)
  71. if !currDate.After(endDate) {
  72. dayList = append(dayList, currDate)
  73. }
  74. }
  75. }
  76. return
  77. }
  78. // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据
  79. func GetPredictDataListByPredictEdbInfoId(edbInfoId int, startDate, endDate string, isTimeBetween bool) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  80. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  81. if err != nil {
  82. errMsg = `获取预测指标信息失败`
  83. return
  84. }
  85. return GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  86. }
  87. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
  88. func GetPredictDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  89. // 非计算指标,直接从表里获取数据
  90. if edbInfo.EdbType != 1 {
  91. if !isTimeBetween {
  92. endDate = ``
  93. }
  94. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  95. }
  96. // 查找该预测指标配置
  97. predictEdbConf, err = data_manage.GetPredictEdbConfById(edbInfo.EdbInfoId)
  98. if err != nil && err.Error() != utils.ErrNoRow() {
  99. errMsg = "获取预测指标配置信息失败"
  100. return
  101. }
  102. if predictEdbConf == nil {
  103. errMsg = "获取预测指标配置信息失败"
  104. err = errors.New(errMsg)
  105. return
  106. }
  107. // 来源指标
  108. sourceEdbInfoItem, err = data_manage.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  109. if err != nil {
  110. if err.Error() == utils.ErrNoRow() {
  111. errMsg = "找不到来源指标信息"
  112. err = errors.New(errMsg)
  113. }
  114. return
  115. }
  116. //获取指标数据(实际已生成)
  117. dataList, err = models.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  118. if err != nil {
  119. return
  120. }
  121. // 获取预测指标未来的数据
  122. predictDataList := make([]*models.EdbDataList, 0)
  123. endDateStr := edbInfo.EndDate //预测指标的结束日期
  124. if isTimeBetween { //如果是时间区间,那么
  125. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  126. endDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
  127. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  128. if reqEndDateTime.Before(endDateTime) {
  129. endDateStr = endDate
  130. }
  131. }
  132. predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate, sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
  133. if err != nil {
  134. return
  135. }
  136. dataList = append(dataList, predictDataList...)
  137. if len(predictDataList) > 0 {
  138. tmpValue := predictDataList[0]
  139. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  140. if edbInfo.MaxValue < tmpValue.Value {
  141. edbInfo.MaxValue = tmpValue.Value
  142. }
  143. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  144. if edbInfo.MinValue > tmpValue.Value {
  145. edbInfo.MinValue = tmpValue.Value
  146. }
  147. }
  148. return
  149. }
  150. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  151. func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  152. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, startDate, endDate)
  153. return
  154. }