predict_edb_info.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_chart_lib/models"
  6. "eta/eta_chart_lib/models/data_manage"
  7. "eta/eta_chart_lib/utils"
  8. "github.com/shopspring/decimal"
  9. "strconv"
  10. "time"
  11. )
  12. // GetPredictEdbDayList 获取预测指标日期列表
  13. func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
  14. if dataDateType == `` {
  15. dataDateType = `交易日`
  16. }
  17. switch frequency {
  18. case "日度":
  19. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  20. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  21. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  22. continue
  23. }
  24. dayList = append(dayList, currDate)
  25. }
  26. case "周度":
  27. //nextDate := startDate.AddDate(0, 0, 7)
  28. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  29. dayList = append(dayList, currDate)
  30. }
  31. case "旬度":
  32. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  33. nextDate := currDate.AddDate(0, 0, 1)
  34. //每个月的10号、20号、最后一天,那么就写入
  35. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  36. dayList = append(dayList, currDate)
  37. }
  38. currDate = nextDate
  39. }
  40. case "月度":
  41. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  42. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  43. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  44. dayList = append(dayList, currDate)
  45. }
  46. currDate = currDate.AddDate(0, 0, 1)
  47. }
  48. case "季度":
  49. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  50. // 每月的最后一天
  51. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  52. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  53. // 季度日期就写入,否则不写入
  54. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  55. dayList = append(dayList, currDate)
  56. }
  57. }
  58. currDate = currDate.AddDate(0, 0, 1)
  59. }
  60. case "半年度":
  61. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  62. // 每月的最后一天
  63. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  64. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  65. // 半年度日期就写入,否则不写入
  66. if currDate.Month() == 6 || currDate.Month() == 12 {
  67. dayList = append(dayList, currDate)
  68. }
  69. }
  70. currDate = currDate.AddDate(0, 0, 1)
  71. }
  72. case "年度":
  73. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  74. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  75. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  76. dayList = append(dayList, currDate)
  77. }
  78. }
  79. }
  80. return
  81. }
  82. // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据(日期正序返回)
  83. func GetPredictDataListByPredictEdbInfoId(edbInfoId int, startDate, endDate string, isTimeBetween bool) (edbInfo *data_manage.EdbInfo, dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  84. edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
  85. if err != nil {
  86. errMsg = `获取预测指标信息失败`
  87. return
  88. }
  89. dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  90. return
  91. }
  92. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
  93. 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) {
  94. // 非计算指标,直接从表里获取数据
  95. if edbInfo.EdbType != 1 {
  96. if !isTimeBetween {
  97. endDate = ``
  98. }
  99. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  100. }
  101. // 查找该预测指标配置
  102. predictEdbConfList, err := data_manage.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  103. if err != nil && err.Error() != utils.ErrNoRow() {
  104. errMsg = "获取预测指标配置信息失败"
  105. return
  106. }
  107. if len(predictEdbConfList) == 0 {
  108. errMsg = "获取预测指标配置信息失败"
  109. err = errors.New(errMsg)
  110. return
  111. }
  112. predictEdbConf = predictEdbConfList[0]
  113. if predictEdbConf == nil {
  114. errMsg = "获取预测指标配置信息失败"
  115. err = errors.New(errMsg)
  116. return
  117. }
  118. // 来源指标
  119. sourceEdbInfoItem, err = data_manage.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  120. if err != nil {
  121. if err.Error() == utils.ErrNoRow() {
  122. errMsg = "找不到来源指标信息"
  123. err = errors.New(errMsg)
  124. }
  125. return
  126. }
  127. // 所有数据
  128. allDataList := make([]*models.EdbDataList, 0)
  129. //获取指标数据(实际已生成)
  130. dataList, err = models.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  131. if err != nil {
  132. return
  133. }
  134. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  135. if startDate != `` {
  136. allDataList, err = models.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, "", "")
  137. if err != nil {
  138. return
  139. }
  140. } else {
  141. allDataList = dataList
  142. }
  143. // 获取预测指标未来的数据
  144. predictDataList := make([]*models.EdbDataList, 0)
  145. endDateStr := edbInfo.EndDate //预测指标的结束日期
  146. if isTimeBetween && endDate != `` { //如果是时间区间,同时截止日期不为空的情况,那么
  147. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  148. endDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
  149. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  150. if reqEndDateTime.Before(endDateTime) {
  151. endDateStr = endDate
  152. }
  153. }
  154. //predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate, sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
  155. var predictMinValue, predictMaxValue float64
  156. predictDataList, predictMinValue, predictMaxValue, err = GetChartPredictEdbInfoDataListByConfList(predictEdbConfList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, edbInfo.Frequency, edbInfo.DataDateType, allDataList)
  157. if err != nil {
  158. return
  159. }
  160. dataList = append(dataList, predictDataList...)
  161. if len(predictDataList) > 0 {
  162. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  163. if edbInfo.MinValue > predictMinValue {
  164. edbInfo.MinValue = predictMinValue
  165. }
  166. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  167. if edbInfo.MaxValue < predictMaxValue {
  168. edbInfo.MaxValue = predictMaxValue
  169. }
  170. }
  171. return
  172. }
  173. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  174. func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  175. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
  176. return
  177. }
  178. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  179. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*data_manage.PredictEdbConf, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*models.EdbDataList) (predictEdbInfoData []*models.EdbDataList, minValue, maxValue float64, err error) {
  180. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  181. if err != nil {
  182. return
  183. }
  184. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  185. if err != nil {
  186. return
  187. }
  188. // 开始预测数据的时间
  189. startDate := latestDate
  190. // 如果有筛选时间的话
  191. if filtrateStartDateStr != `` {
  192. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  193. if tmpErr != nil {
  194. err = tmpErr
  195. return
  196. }
  197. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  198. if filtrateStartDate.After(latestDate) {
  199. startDate = filtrateStartDate.AddDate(0, 0, -1)
  200. }
  201. }
  202. //var dateArr []string
  203. // 对应日期的值
  204. existMap := make(map[string]float64)
  205. for _, v := range realPredictEdbInfoData {
  206. //dateArr = append(dateArr, v.DataTime)
  207. existMap[v.DataTime] = v.Value
  208. }
  209. predictEdbInfoData = make([]*models.EdbDataList, 0)
  210. //dataValue := lastDataValue
  211. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  212. for _, predictEdbConf := range predictEdbConfList {
  213. dataEndTime := endDate
  214. if predictEdbConf.EndDate.Before(dataEndTime) {
  215. dataEndTime = predictEdbConf.EndDate
  216. }
  217. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  218. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
  219. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  220. continue
  221. }
  222. switch predictEdbConf.RuleType {
  223. case 1: //1:最新
  224. var lastDataValue float64 //最新值
  225. tmpAllData := make([]*models.EdbDataList, 0)
  226. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  227. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  228. lenTmpAllData := len(tmpAllData)
  229. if lenTmpAllData > 0 {
  230. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  231. }
  232. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  233. tmpMaxValue = lastDataValue
  234. tmpMinValue = lastDataValue
  235. case 2: //2:固定值
  236. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  237. if tmpErr != nil {
  238. err = tmpErr
  239. return
  240. }
  241. dataValue, _ := tmpValDecimal.Float64()
  242. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  243. tmpMaxValue = dataValue
  244. tmpMinValue = dataValue
  245. case 3: //3:同比
  246. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  247. if tmpErr != nil {
  248. err = tmpErr
  249. return
  250. }
  251. tbValue, _ := tmpValDecimal.Float64()
  252. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  253. case 4: //4:同差
  254. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  255. if tmpErr != nil {
  256. err = tmpErr
  257. return
  258. }
  259. tcValue, _ := tmpValDecimal.Float64()
  260. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  261. case 5: //5:环比
  262. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  263. if tmpErr != nil {
  264. err = tmpErr
  265. return
  266. }
  267. hbValue, _ := tmpValDecimal.Float64()
  268. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  269. case 6: //6:环差
  270. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  271. if tmpErr != nil {
  272. err = tmpErr
  273. return
  274. }
  275. hcValue, _ := tmpValDecimal.Float64()
  276. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  277. case 7: //7:N期移动均值
  278. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  279. if tmpErr != nil {
  280. err = tmpErr
  281. return
  282. }
  283. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  284. case 8: //8:N期段线性外推值
  285. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  286. if tmpErr != nil {
  287. err = tmpErr
  288. return
  289. }
  290. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  291. case 9: //9:动态环差”预测规则;
  292. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate, dataEndTime, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  293. case 10: //10:根据 给定终值后插值 规则获取预测数据
  294. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  295. if tmpErr != nil {
  296. err = tmpErr
  297. return
  298. }
  299. finalValue, _ := tmpValDecimal.Float64()
  300. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  301. case 11: //11:根据 季节性 规则获取预测数据
  302. var seasonConf SeasonConf
  303. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &seasonConf)
  304. if tmpErr != nil {
  305. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  306. return
  307. }
  308. calendar := "公历"
  309. if seasonConf.Calendar == "农历" {
  310. calendar = "农历"
  311. }
  312. yearList := make([]int, 0)
  313. //选择方式,1:连续N年;2:指定年份
  314. if seasonConf.YearType == 1 {
  315. if seasonConf.NValue < 1 {
  316. err = errors.New("连续N年不允许小于1")
  317. return
  318. }
  319. currYear := time.Now().Year()
  320. for i := 0; i < seasonConf.NValue; i++ {
  321. yearList = append(yearList, currYear-i-1)
  322. }
  323. } else {
  324. yearList = seasonConf.YearList
  325. }
  326. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, yearList, calendar, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  327. if err != nil {
  328. return
  329. }
  330. case 12: //12:根据 移动平均同比 规则获取预测数据
  331. var moveAverageConf MoveAverageConf
  332. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  333. if tmpErr != nil {
  334. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  335. return
  336. }
  337. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  338. if err != nil {
  339. return
  340. }
  341. case 13: //13:根据 同比增速差值 规则获取预测数据
  342. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  343. if tmpErr != nil {
  344. err = tmpErr
  345. return
  346. }
  347. tbEndValue, _ := tmpValDecimal.Float64()
  348. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  349. case 14: //14:根据 一元线性拟合 规则获取预测数据
  350. var ruleConf RuleLineNhConf
  351. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  352. if err != nil {
  353. err = errors.New("一元线性拟合配置信息异常:" + err.Error())
  354. return
  355. }
  356. // 规则计算的拟合残差值map
  357. newNhccDataMap := make(map[string]float64)
  358. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  359. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, "", "")
  360. if tmpErr != nil {
  361. err = tmpErr
  362. return
  363. }
  364. for _, v := range tmpPredictEdbRuleDataList {
  365. newNhccDataMap[v.DataTime] = v.Value
  366. }
  367. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  368. newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  369. if err != nil {
  370. return
  371. }
  372. }
  373. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  374. if err != nil {
  375. return
  376. }
  377. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  378. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  379. if err != nil {
  380. return
  381. }
  382. case 16: //16:年度值倒推
  383. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  384. if err != nil {
  385. return
  386. }
  387. }
  388. // 下一个规则的开始日期
  389. {
  390. lenPredictEdbInfoData := len(predictEdbInfoData)
  391. if lenPredictEdbInfoData > 0 {
  392. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  393. if startDate.Before(tmpDataEndTime) {
  394. startDate = tmpDataEndTime
  395. }
  396. }
  397. }
  398. if tmpMinValue < minValue {
  399. minValue = tmpMinValue
  400. }
  401. if tmpMaxValue < maxValue {
  402. maxValue = tmpMaxValue
  403. }
  404. }
  405. return
  406. }