predict_edb_info.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 !isTimeBetween {
  96. endDate = ``
  97. }
  98. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  99. }
  100. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  101. func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  102. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
  103. return
  104. }
  105. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  106. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*data_manage.PredictEdbConf, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*models.EdbDataList) (predictEdbInfoData []*models.EdbDataList, minValue, maxValue float64, err error) {
  107. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  108. if err != nil {
  109. return
  110. }
  111. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  112. if err != nil {
  113. return
  114. }
  115. // 开始预测数据的时间
  116. startDate := latestDate
  117. // 如果有筛选时间的话
  118. if filtrateStartDateStr != `` {
  119. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  120. if tmpErr != nil {
  121. err = tmpErr
  122. return
  123. }
  124. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  125. if filtrateStartDate.After(latestDate) {
  126. startDate = filtrateStartDate.AddDate(0, 0, -1)
  127. }
  128. }
  129. //var dateArr []string
  130. // 对应日期的值
  131. existMap := make(map[string]float64)
  132. for _, v := range realPredictEdbInfoData {
  133. //dateArr = append(dateArr, v.DataTime)
  134. existMap[v.DataTime] = v.Value
  135. }
  136. predictEdbInfoData = make([]*models.EdbDataList, 0)
  137. //dataValue := lastDataValue
  138. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  139. for _, predictEdbConf := range predictEdbConfList {
  140. dataEndTime := endDate
  141. if predictEdbConf.EndDate.Before(dataEndTime) {
  142. dataEndTime = predictEdbConf.EndDate
  143. }
  144. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  145. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
  146. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  147. continue
  148. }
  149. switch predictEdbConf.RuleType {
  150. case 1: //1:最新
  151. var lastDataValue float64 //最新值
  152. tmpAllData := make([]*models.EdbDataList, 0)
  153. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  154. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  155. lenTmpAllData := len(tmpAllData)
  156. if lenTmpAllData > 0 {
  157. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  158. }
  159. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  160. tmpMaxValue = lastDataValue
  161. tmpMinValue = lastDataValue
  162. case 2: //2:固定值
  163. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  164. if tmpErr != nil {
  165. err = tmpErr
  166. return
  167. }
  168. dataValue, _ := tmpValDecimal.Float64()
  169. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  170. tmpMaxValue = dataValue
  171. tmpMinValue = dataValue
  172. case 3: //3:同比
  173. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  174. if tmpErr != nil {
  175. err = tmpErr
  176. return
  177. }
  178. tbValue, _ := tmpValDecimal.Float64()
  179. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  180. case 4: //4:同差
  181. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  182. if tmpErr != nil {
  183. err = tmpErr
  184. return
  185. }
  186. tcValue, _ := tmpValDecimal.Float64()
  187. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  188. case 5: //5:环比
  189. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  190. if tmpErr != nil {
  191. err = tmpErr
  192. return
  193. }
  194. hbValue, _ := tmpValDecimal.Float64()
  195. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  196. case 6: //6:环差
  197. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  198. if tmpErr != nil {
  199. err = tmpErr
  200. return
  201. }
  202. hcValue, _ := tmpValDecimal.Float64()
  203. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  204. case 7: //7:N期移动均值
  205. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  206. if tmpErr != nil {
  207. err = tmpErr
  208. return
  209. }
  210. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  211. case 8: //8:N期段线性外推值
  212. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  213. if tmpErr != nil {
  214. err = tmpErr
  215. return
  216. }
  217. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  218. case 9: //9:动态环差”预测规则;
  219. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate, dataEndTime, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  220. case 10: //10:根据 给定终值后插值 规则获取预测数据
  221. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  222. if tmpErr != nil {
  223. err = tmpErr
  224. return
  225. }
  226. finalValue, _ := tmpValDecimal.Float64()
  227. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  228. case 11: //11:根据 季节性 规则获取预测数据
  229. var seasonConf SeasonConf
  230. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &seasonConf)
  231. if tmpErr != nil {
  232. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  233. return
  234. }
  235. calendar := "公历"
  236. if seasonConf.Calendar == "农历" {
  237. calendar = "农历"
  238. }
  239. yearList := make([]int, 0)
  240. //选择方式,1:连续N年;2:指定年份
  241. if seasonConf.YearType == 1 {
  242. if seasonConf.NValue < 1 {
  243. err = errors.New("连续N年不允许小于1")
  244. return
  245. }
  246. currYear := time.Now().Year()
  247. for i := 0; i < seasonConf.NValue; i++ {
  248. yearList = append(yearList, currYear-i-1)
  249. }
  250. } else {
  251. yearList = seasonConf.YearList
  252. }
  253. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, yearList, calendar, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  254. if err != nil {
  255. return
  256. }
  257. case 12: //12:根据 移动平均同比 规则获取预测数据
  258. var moveAverageConf MoveAverageConf
  259. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  260. if tmpErr != nil {
  261. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  262. return
  263. }
  264. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  265. if err != nil {
  266. return
  267. }
  268. case 13: //13:根据 同比增速差值 规则获取预测数据
  269. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  270. if tmpErr != nil {
  271. err = tmpErr
  272. return
  273. }
  274. tbEndValue, _ := tmpValDecimal.Float64()
  275. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  276. case 14: //14:根据 一元线性拟合 规则获取预测数据
  277. var ruleConf RuleLineNhConf
  278. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  279. if err != nil {
  280. err = errors.New("一元线性拟合配置信息异常:" + err.Error())
  281. return
  282. }
  283. // 规则计算的拟合残差值map
  284. newNhccDataMap := make(map[string]float64)
  285. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  286. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, "", "")
  287. if tmpErr != nil {
  288. err = tmpErr
  289. return
  290. }
  291. for _, v := range tmpPredictEdbRuleDataList {
  292. newNhccDataMap[v.DataTime] = v.Value
  293. }
  294. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  295. newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  296. if err != nil {
  297. return
  298. }
  299. }
  300. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  301. if err != nil {
  302. return
  303. }
  304. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  305. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  306. if err != nil {
  307. return
  308. }
  309. case 16: //16:年度值倒推
  310. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  311. if err != nil {
  312. return
  313. }
  314. }
  315. // 下一个规则的开始日期
  316. {
  317. lenPredictEdbInfoData := len(predictEdbInfoData)
  318. if lenPredictEdbInfoData > 0 {
  319. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  320. if startDate.Before(tmpDataEndTime) {
  321. startDate = tmpDataEndTime
  322. }
  323. }
  324. }
  325. if tmpMinValue < minValue {
  326. minValue = tmpMinValue
  327. }
  328. if tmpMaxValue < maxValue {
  329. maxValue = tmpMaxValue
  330. }
  331. }
  332. return
  333. }