predict_edb_info.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package chart
  2. import (
  3. "errors"
  4. "github.com/shopspring/decimal"
  5. edbDataModel "hongze/hongze_yb/models/tables/edb_data"
  6. edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
  7. predictEdbConfModel "hongze/hongze_yb/models/tables/predict_edb_conf"
  8. "hongze/hongze_yb/utils"
  9. "strconv"
  10. "time"
  11. )
  12. // GetChartPredictEdbInfoDataList 获取图表的预测指标的未来数据
  13. func GetChartPredictEdbInfoDataList(predictEdbConf predictEdbConfModel.PredictEdbConf, filtrateStartDateStr, latestDateStr string, lastDataValue float64, endDateStr, frequency string) (predictEdbInfoData []*edbDataModel.EdbDataList, err error) {
  14. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  15. if err != nil {
  16. return
  17. }
  18. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  19. if err != nil {
  20. return
  21. }
  22. // 开始预测数据的时间
  23. startDate := latestDate
  24. // 如果有筛选时间的话
  25. if filtrateStartDateStr != `` {
  26. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  27. if tmpErr != nil {
  28. err = tmpErr
  29. return
  30. }
  31. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  32. if filtrateStartDate.After(latestDate) {
  33. startDate = filtrateStartDate.AddDate(0, 0, -1)
  34. }
  35. }
  36. dataValue := lastDataValue
  37. if predictEdbConf.RuleType == 2 {
  38. dataValue = predictEdbConf.FixedValue
  39. }
  40. //获取后面的预测数据
  41. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  42. predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
  43. for k, v := range dayList {
  44. predictEdbInfoData = append(predictEdbInfoData, &edbDataModel.EdbDataList{
  45. EdbDataId: int(predictEdbConf.PredictEdbInfoID) + 10000000000 + k,
  46. EdbInfoId: int(predictEdbConf.PredictEdbInfoID),
  47. DataTime: v.Format(utils.FormatDate),
  48. Value: dataValue,
  49. DataTimestamp: (v.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  50. })
  51. }
  52. return
  53. }
  54. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  55. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*predictEdbConfModel.PredictEdbConf, filtrateStartDateStr, latestDateStr, endDateStr, frequency string, realPredictEdbInfoData []*edbDataModel.EdbDataList) (predictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
  56. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  57. if err != nil {
  58. return
  59. }
  60. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  61. if err != nil {
  62. return
  63. }
  64. // 开始预测数据的时间
  65. startDate := latestDate
  66. // 如果有筛选时间的话
  67. if filtrateStartDateStr != `` {
  68. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  69. if tmpErr != nil {
  70. err = tmpErr
  71. return
  72. }
  73. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  74. if filtrateStartDate.After(latestDate) {
  75. startDate = filtrateStartDate.AddDate(0, 0, -1)
  76. }
  77. }
  78. //var dateArr []string
  79. // 对应日期的值
  80. existMap := make(map[string]float64)
  81. for _, v := range realPredictEdbInfoData {
  82. //dateArr = append(dateArr, v.DataTime)
  83. existMap[v.DataTime] = v.Value
  84. }
  85. predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
  86. //dataValue := lastDataValue
  87. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  88. for _, predictEdbConf := range predictEdbConfList {
  89. dataEndTime := endDate
  90. if predictEdbConf.EndDate.Before(dataEndTime) {
  91. dataEndTime = predictEdbConf.EndDate
  92. }
  93. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  94. switch predictEdbConf.RuleType {
  95. case 1: //1:最新
  96. var lastDataValue float64 //最新值
  97. tmpAllData := make([]*edbDataModel.EdbDataList, 0)
  98. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  99. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  100. lenTmpAllData := len(tmpAllData)
  101. if lenTmpAllData > 0 {
  102. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  103. }
  104. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(int(predictEdbConf.PredictEdbInfoID), lastDataValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
  105. tmpMaxValue = lastDataValue
  106. tmpMinValue = lastDataValue
  107. case 2: //2:固定值
  108. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  109. if tmpErr != nil {
  110. err = tmpErr
  111. return
  112. }
  113. dataValue, _ := tmpValDecimal.Float64()
  114. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(int(predictEdbConf.PredictEdbInfoID), dataValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
  115. tmpMaxValue = dataValue
  116. tmpMinValue = dataValue
  117. case 3: //3:同比
  118. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  119. if tmpErr != nil {
  120. err = tmpErr
  121. return
  122. }
  123. tbValue, _ := tmpValDecimal.Float64()
  124. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(int(predictEdbConf.PredictEdbInfoID), tbValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  125. case 4: //4:同差
  126. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  127. if tmpErr != nil {
  128. err = tmpErr
  129. return
  130. }
  131. tcValue, _ := tmpValDecimal.Float64()
  132. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(int(predictEdbConf.PredictEdbInfoID), tcValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  133. case 5: //5:环比
  134. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  135. if tmpErr != nil {
  136. err = tmpErr
  137. return
  138. }
  139. hbValue, _ := tmpValDecimal.Float64()
  140. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(int(predictEdbConf.PredictEdbInfoID), hbValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  141. case 6: //6:环差
  142. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  143. if tmpErr != nil {
  144. err = tmpErr
  145. return
  146. }
  147. hcValue, _ := tmpValDecimal.Float64()
  148. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(int(predictEdbConf.PredictEdbInfoID), hcValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  149. case 7: //7:N期移动均值
  150. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  151. if tmpErr != nil {
  152. err = tmpErr
  153. return
  154. }
  155. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(int(predictEdbConf.PredictEdbInfoID), nValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  156. case 8: //8:N期段线性外推值
  157. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  158. if tmpErr != nil {
  159. err = tmpErr
  160. return
  161. }
  162. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNLinearRegression(int(predictEdbConf.PredictEdbInfoID), nValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  163. case 9: //9:动态环差”预测规则;
  164. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(int(predictEdbConf.PredictEdbInfoID), int(predictEdbConf.ConfigID), startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  165. case 10: //10:根据 给定终值后插值 规则获取预测数据
  166. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  167. if tmpErr != nil {
  168. err = tmpErr
  169. return
  170. }
  171. finalValue, _ := tmpValDecimal.Float64()
  172. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(int(predictEdbConf.PredictEdbInfoID), finalValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  173. }
  174. //startDate = dataEndTime.AddDate(0, 0, 1)
  175. startDate = dataEndTime
  176. if tmpMinValue < minValue {
  177. minValue = tmpMinValue
  178. }
  179. if tmpMaxValue < maxValue {
  180. maxValue = tmpMaxValue
  181. }
  182. }
  183. return
  184. }
  185. // GetPredictEdbDayList 获取预测指标日期列表
  186. func getPredictEdbDayList(startDate, endDate time.Time, frequency string) (dayList []time.Time) {
  187. //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
  188. switch frequency {
  189. case "日度":
  190. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  191. //周六、日排除
  192. if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday {
  193. continue
  194. }
  195. dayList = append(dayList, currDate)
  196. }
  197. case "周度":
  198. //nextDate := startDate.AddDate(0, 0, 7)
  199. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  200. dayList = append(dayList, currDate)
  201. }
  202. case "月度":
  203. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  204. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 2, -1)
  205. if !currDate.After(endDate) {
  206. dayList = append(dayList, currDate)
  207. }
  208. }
  209. }
  210. return
  211. }
  212. // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据
  213. func GetPredictDataListByPredictEdbInfoId(edbInfoId int, startDate, endDate string, isTimeBetween bool) (edbInfo *edbInfoModel.EdbInfo, dataList []*edbDataModel.EdbDataList, sourceEdbInfoItem *edbInfoModel.EdbInfo, predictEdbConf *predictEdbConfModel.PredictEdbConf, err error, errMsg string) {
  214. edbInfo, err = edbInfoModel.GetEdbInfoById(edbInfoId)
  215. if err != nil {
  216. errMsg = `获取预测指标信息失败`
  217. return
  218. }
  219. dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  220. return
  221. }
  222. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
  223. func GetPredictDataListByPredictEdbInfo(edbInfo *edbInfoModel.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*edbDataModel.EdbDataList, sourceEdbInfoItem *edbInfoModel.EdbInfo, predictEdbConf *predictEdbConfModel.PredictEdbConf, err error, errMsg string) {
  224. // 非计算指标,直接从表里获取数据
  225. if edbInfo.EdbType != 1 {
  226. if !isTimeBetween {
  227. endDate = ``
  228. }
  229. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  230. }
  231. // 查找该预测指标配置
  232. predictEdbConfList, err := predictEdbConfModel.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  233. if err != nil {
  234. errMsg = "获取预测指标配置信息失败"
  235. return
  236. }
  237. if len(predictEdbConfList) == 0 {
  238. errMsg = "获取预测指标配置信息失败"
  239. err = errors.New(errMsg)
  240. return
  241. }
  242. predictEdbConf = predictEdbConfList[0]
  243. // 来源指标
  244. sourceEdbInfoItem, err = edbInfoModel.GetEdbInfoById(int(predictEdbConf.SourceEdbInfoID))
  245. if err != nil {
  246. if err == utils.ErrNoRow {
  247. errMsg = "找不到来源指标信息"
  248. err = errors.New(errMsg)
  249. }
  250. return
  251. }
  252. allDataList := make([]*edbDataModel.EdbDataList, 0)
  253. //获取指标数据(实际已生成)
  254. dataList, err = edbDataModel.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  255. if err != nil {
  256. return
  257. }
  258. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  259. if startDate != `` {
  260. allDataList, err = edbDataModel.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, "", "")
  261. if err != nil {
  262. return
  263. }
  264. } else {
  265. allDataList = dataList
  266. }
  267. // 获取预测指标未来的数据
  268. predictDataList := make([]*edbDataModel.EdbDataList, 0)
  269. endDateStr := edbInfo.EndDate.Format(utils.FormatDate) //预测指标的结束日期
  270. if isTimeBetween { //如果是时间区间,那么
  271. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  272. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  273. if reqEndDateTime.Before(edbInfo.EndDate) {
  274. endDateStr = endDate
  275. }
  276. }
  277. //predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate.Format(utils.FormatDate), sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
  278. var predictMinValue, predictMaxValue float64
  279. predictDataList, predictMinValue, predictMaxValue, err = GetChartPredictEdbInfoDataListByConfList(predictEdbConfList, startDate, sourceEdbInfoItem.LatestDate.Format(utils.FormatDate), endDateStr, edbInfo.Frequency, allDataList)
  280. if err != nil {
  281. return
  282. }
  283. dataList = append(dataList, predictDataList...)
  284. if len(predictDataList) > 0 {
  285. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  286. if edbInfo.MinValue > predictMinValue {
  287. edbInfo.MinValue = predictMinValue
  288. }
  289. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  290. if edbInfo.MaxValue < predictMaxValue {
  291. edbInfo.MaxValue = predictMaxValue
  292. }
  293. }
  294. return
  295. }
  296. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  297. func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *edbInfoModel.EdbInfo, startDate, endDate string) (dataList []*edbDataModel.EdbDataList, sourceEdbInfoItem *edbInfoModel.EdbInfo, predictEdbConf *predictEdbConfModel.PredictEdbConf, err error, errMsg string) {
  298. dataList, err = edbDataModel.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, startDate, endDate)
  299. return
  300. }