predict_edb_info.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. package chart
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/shopspring/decimal"
  7. edbDataModel "hongze/hongze_yb/models/tables/edb_data"
  8. edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
  9. predictEdbConfModel "hongze/hongze_yb/models/tables/predict_edb_conf"
  10. predictEdbRuleDataModel "hongze/hongze_yb/models/tables/predict_edb_rule_data"
  11. "hongze/hongze_yb/utils"
  12. "strconv"
  13. "time"
  14. )
  15. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  16. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*predictEdbConfModel.PredictEdbConf, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*edbDataModel.EdbDataList) (predictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
  17. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  18. if err != nil {
  19. return
  20. }
  21. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  22. if err != nil {
  23. return
  24. }
  25. // 开始预测数据的时间
  26. startDate := latestDate
  27. // 如果有筛选时间的话
  28. if filtrateStartDateStr != `` {
  29. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  30. if tmpErr != nil {
  31. err = tmpErr
  32. return
  33. }
  34. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  35. if filtrateStartDate.After(latestDate) {
  36. startDate = filtrateStartDate.AddDate(0, 0, -1)
  37. }
  38. }
  39. //var dateArr []string
  40. // 对应日期的值
  41. existMap := make(map[string]float64)
  42. for _, v := range realPredictEdbInfoData {
  43. //dateArr = append(dateArr, v.DataTime)
  44. existMap[v.DataTime] = v.Value
  45. }
  46. predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
  47. //dataValue := lastDataValue
  48. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  49. for _, predictEdbConf := range predictEdbConfList {
  50. dataEndTime := endDate
  51. if predictEdbConf.EndDate.Before(dataEndTime) {
  52. dataEndTime = predictEdbConf.EndDate
  53. }
  54. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  55. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
  56. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  57. continue
  58. }
  59. switch predictEdbConf.RuleType {
  60. case 1: //1:最新
  61. var lastDataValue float64 //最新值
  62. tmpAllData := make([]*edbDataModel.EdbDataList, 0)
  63. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  64. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  65. lenTmpAllData := len(tmpAllData)
  66. if lenTmpAllData > 0 {
  67. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  68. }
  69. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(int(predictEdbConf.PredictEdbInfoID), lastDataValue, dayList, predictEdbInfoData, existMap)
  70. tmpMaxValue = lastDataValue
  71. tmpMinValue = lastDataValue
  72. case 2: //2:固定值
  73. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  74. if tmpErr != nil {
  75. err = tmpErr
  76. return
  77. }
  78. dataValue, _ := tmpValDecimal.Float64()
  79. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(int(predictEdbConf.PredictEdbInfoID), dataValue, dayList, predictEdbInfoData, existMap)
  80. tmpMaxValue = dataValue
  81. tmpMinValue = dataValue
  82. case 3: //3:同比
  83. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  84. if tmpErr != nil {
  85. err = tmpErr
  86. return
  87. }
  88. tbValue, _ := tmpValDecimal.Float64()
  89. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(int(predictEdbConf.PredictEdbInfoID), tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  90. case 4: //4:同差
  91. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  92. if tmpErr != nil {
  93. err = tmpErr
  94. return
  95. }
  96. tcValue, _ := tmpValDecimal.Float64()
  97. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(int(predictEdbConf.PredictEdbInfoID), tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  98. case 5: //5:环比
  99. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  100. if tmpErr != nil {
  101. err = tmpErr
  102. return
  103. }
  104. hbValue, _ := tmpValDecimal.Float64()
  105. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(int(predictEdbConf.PredictEdbInfoID), hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  106. case 6: //6:环差
  107. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  108. if tmpErr != nil {
  109. err = tmpErr
  110. return
  111. }
  112. hcValue, _ := tmpValDecimal.Float64()
  113. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(int(predictEdbConf.PredictEdbInfoID), hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  114. case 7: //7:N期移动均值
  115. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  116. if tmpErr != nil {
  117. err = tmpErr
  118. return
  119. }
  120. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(int(predictEdbConf.PredictEdbInfoID), nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  121. case 8: //8:N期段线性外推值
  122. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  123. if tmpErr != nil {
  124. err = tmpErr
  125. return
  126. }
  127. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(int(predictEdbConf.PredictEdbInfoID), nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  128. if err != nil {
  129. return
  130. }
  131. case 9: //9:动态环差”预测规则;
  132. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(int(predictEdbConf.PredictEdbInfoID), int(predictEdbConf.ConfigID), startDate, dataEndTime, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  133. case 10: //10:根据 给定终值后插值 规则获取预测数据
  134. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  135. if tmpErr != nil {
  136. err = tmpErr
  137. return
  138. }
  139. finalValue, _ := tmpValDecimal.Float64()
  140. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(int(predictEdbConf.PredictEdbInfoID), finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  141. case 11: //11:根据 季节性 规则获取预测数据
  142. var seasonConf SeasonConf
  143. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &seasonConf)
  144. if tmpErr != nil {
  145. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  146. return
  147. }
  148. calendar := "公历"
  149. if seasonConf.Calendar == "农历" {
  150. calendar = "农历"
  151. }
  152. yearList := make([]int, 0)
  153. //选择方式,1:连续N年;2:指定年份
  154. if seasonConf.YearType == 1 {
  155. if seasonConf.NValue < 1 {
  156. err = errors.New("连续N年不允许小于1")
  157. return
  158. }
  159. currYear := time.Now().Year()
  160. for i := 0; i < seasonConf.NValue; i++ {
  161. yearList = append(yearList, currYear-i-1)
  162. }
  163. } else {
  164. yearList = seasonConf.YearList
  165. }
  166. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(int(predictEdbConf.PredictEdbInfoID), yearList, calendar, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  167. if err != nil {
  168. return
  169. }
  170. case 12: //12:根据 移动平均同比 规则获取预测数据
  171. var moveAverageConf MoveAverageConf
  172. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  173. if tmpErr != nil {
  174. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  175. return
  176. }
  177. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(int(predictEdbConf.PredictEdbInfoID), moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  178. if err != nil {
  179. return
  180. }
  181. case 13: //13:根据 同比增速差值 规则获取预测数据
  182. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  183. if tmpErr != nil {
  184. err = tmpErr
  185. return
  186. }
  187. tbEndValue, _ := tmpValDecimal.Float64()
  188. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(int(predictEdbConf.PredictEdbInfoID), tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  189. case 14: //14:根据 一元线性拟合 规则获取预测数据
  190. var ruleConf RuleLineNhConf
  191. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  192. if err != nil {
  193. err = errors.New("一元线性拟合配置信息异常:" + err.Error())
  194. return
  195. }
  196. // 规则计算的拟合残差值map
  197. newNhccDataMap := make(map[string]float64)
  198. if predictEdbConf.PredictEdbInfoID > 0 { //已经生成的动态数据
  199. tmpPredictEdbRuleDataList, tmpErr := predictEdbRuleDataModel.GetPredictEdbRuleDataList(int(predictEdbConf.PredictEdbInfoID), int(predictEdbConf.ConfigID), "", "")
  200. if tmpErr != nil {
  201. err = tmpErr
  202. return
  203. }
  204. for _, v := range tmpPredictEdbRuleDataList {
  205. newNhccDataMap[v.DataTime.Format(utils.FormatDate)] = v.Value
  206. }
  207. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  208. newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  209. if err != nil {
  210. return
  211. }
  212. }
  213. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(int(predictEdbConf.PredictEdbInfoID), dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  214. if err != nil {
  215. return
  216. }
  217. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  218. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(int(predictEdbConf.PredictEdbInfoID), predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  219. if err != nil {
  220. return
  221. }
  222. case 16: //16:年度值倒推
  223. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(int(predictEdbConf.PredictEdbInfoID), predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  224. if err != nil {
  225. return
  226. }
  227. }
  228. // 下一个规则的开始日期
  229. {
  230. lenPredictEdbInfoData := len(predictEdbInfoData)
  231. if lenPredictEdbInfoData > 0 {
  232. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  233. if startDate.Before(tmpDataEndTime) {
  234. startDate = tmpDataEndTime
  235. }
  236. }
  237. }
  238. if tmpMinValue < minValue {
  239. minValue = tmpMinValue
  240. }
  241. if tmpMaxValue < maxValue {
  242. maxValue = tmpMaxValue
  243. }
  244. }
  245. return
  246. }
  247. // GetPredictEdbDayList 获取预测指标日期列表
  248. func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
  249. if dataDateType == `` {
  250. dataDateType = `交易日`
  251. }
  252. switch frequency {
  253. case "日度":
  254. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  255. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  256. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  257. continue
  258. }
  259. dayList = append(dayList, currDate)
  260. }
  261. case "周度":
  262. //nextDate := startDate.AddDate(0, 0, 7)
  263. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  264. dayList = append(dayList, currDate)
  265. }
  266. case "旬度":
  267. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  268. nextDate := currDate.AddDate(0, 0, 1)
  269. //每个月的10号、20号、最后一天,那么就写入
  270. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  271. dayList = append(dayList, currDate)
  272. }
  273. currDate = nextDate
  274. }
  275. case "月度":
  276. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  277. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  278. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  279. dayList = append(dayList, currDate)
  280. }
  281. currDate = currDate.AddDate(0, 0, 1)
  282. }
  283. case "季度":
  284. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  285. // 每月的最后一天
  286. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  287. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  288. // 季度日期就写入,否则不写入
  289. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  290. dayList = append(dayList, currDate)
  291. }
  292. }
  293. currDate = currDate.AddDate(0, 0, 1)
  294. }
  295. case "半年度":
  296. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  297. // 每月的最后一天
  298. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  299. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  300. // 半年度日期就写入,否则不写入
  301. if currDate.Month() == 6 || currDate.Month() == 12 {
  302. dayList = append(dayList, currDate)
  303. }
  304. }
  305. currDate = currDate.AddDate(0, 0, 1)
  306. }
  307. case "年度":
  308. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  309. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  310. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  311. dayList = append(dayList, currDate)
  312. }
  313. }
  314. }
  315. return
  316. }
  317. // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据
  318. 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) {
  319. edbInfo, err = edbInfoModel.GetEdbInfoById(edbInfoId)
  320. if err != nil {
  321. errMsg = `获取预测指标信息失败`
  322. return
  323. }
  324. dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  325. return
  326. }
  327. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
  328. func GetPredictDataListByPredictEdbInfo(edbInfo *edbInfoModel.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*edbDataModel.EdbDataList, sourceEdbInfoItem *edbInfoModel.EdbInfo, predictEdbConf *predictEdbConfModel.PredictEdbConf, err error, errMsg string) {
  329. // 非计算指标,直接从表里获取数据
  330. if edbInfo.EdbType != 1 {
  331. if !isTimeBetween {
  332. endDate = ``
  333. }
  334. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  335. }
  336. // 查找该预测指标配置
  337. predictEdbConfList, err := predictEdbConfModel.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  338. if err != nil {
  339. errMsg = "获取预测指标配置信息失败"
  340. return
  341. }
  342. if len(predictEdbConfList) == 0 {
  343. errMsg = "获取预测指标配置信息失败"
  344. err = errors.New(errMsg)
  345. return
  346. }
  347. predictEdbConf = predictEdbConfList[0]
  348. // 来源指标
  349. sourceEdbInfoItem, err = edbInfoModel.GetEdbInfoById(int(predictEdbConf.SourceEdbInfoID))
  350. if err != nil {
  351. if err == utils.ErrNoRow {
  352. errMsg = "找不到来源指标信息"
  353. err = errors.New(errMsg)
  354. }
  355. return
  356. }
  357. allDataList := make([]*edbDataModel.EdbDataList, 0)
  358. //获取指标数据(实际已生成)
  359. dataList, err = edbDataModel.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  360. if err != nil {
  361. return
  362. }
  363. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  364. if startDate != `` {
  365. allDataList, err = edbDataModel.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, "", "")
  366. if err != nil {
  367. return
  368. }
  369. } else {
  370. allDataList = dataList
  371. }
  372. // 获取预测指标未来的数据
  373. predictDataList := make([]*edbDataModel.EdbDataList, 0)
  374. endDateStr := edbInfo.EndDate.Format(utils.FormatDate) //预测指标的结束日期
  375. if isTimeBetween { //如果是时间区间,那么
  376. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  377. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  378. if reqEndDateTime.Before(edbInfo.EndDate) {
  379. endDateStr = endDate
  380. }
  381. }
  382. //predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate.Format(utils.FormatDate), sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
  383. var predictMinValue, predictMaxValue float64
  384. predictDataList, predictMinValue, predictMaxValue, err = GetChartPredictEdbInfoDataListByConfList(predictEdbConfList, startDate, sourceEdbInfoItem.LatestDate.Format(utils.FormatDate), endDateStr, edbInfo.Frequency, edbInfo.DataDateType, allDataList)
  385. if err != nil {
  386. return
  387. }
  388. dataList = append(dataList, predictDataList...)
  389. if len(predictDataList) > 0 {
  390. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  391. if edbInfo.MinValue > predictMinValue {
  392. edbInfo.MinValue = predictMinValue
  393. }
  394. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  395. if edbInfo.MaxValue < predictMaxValue {
  396. edbInfo.MaxValue = predictMaxValue
  397. }
  398. }
  399. return
  400. }
  401. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  402. func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *edbInfoModel.EdbInfo, startDate, endDate string) (dataList []*edbDataModel.EdbDataList, sourceEdbInfoItem *edbInfoModel.EdbInfo, predictEdbConf *predictEdbConfModel.PredictEdbConf, err error, errMsg string) {
  403. dataList, err = edbDataModel.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
  404. return
  405. }
  406. // GetChartDataList 通过完整的预测数据 进行 季节性图、公历、农历处理
  407. func GetChartDataList(dataList []*edbDataModel.EdbDataList, chartType int, calendar, latestDateStr, startDate string) (resultDataList interface{}, err error) {
  408. startDateReal := startDate
  409. calendarPreYear := 0
  410. if calendar == "农历" {
  411. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  412. if err != nil {
  413. fmt.Println("time.Parse:" + err.Error())
  414. }
  415. calendarPreYear = newStartDateReal.Year() - 1
  416. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  417. startDateReal = newStartDateReal.Format(utils.FormatDate)
  418. }
  419. //实际数据的截止日期
  420. latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr)
  421. if tmpErr != nil {
  422. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr))
  423. return
  424. }
  425. latestDateYear := latestDate.Year() //实际数据截止年份
  426. // 曲线图
  427. if chartType == 1 {
  428. resultDataList = dataList
  429. return
  430. }
  431. if calendar == "农历" {
  432. if len(dataList) <= 0 {
  433. resultDataList = edbDataModel.EdbDataResult{}
  434. } else {
  435. result, tmpErr := edbDataModel.AddCalculateQuarterV4(dataList)
  436. if tmpErr != nil {
  437. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  438. return
  439. }
  440. // 处理季节图的截止日期
  441. for k, edbDataItems := range result.List {
  442. var cuttingDataTimestamp int64
  443. // 切割的日期时间字符串
  444. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  445. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  446. if edbDataItems.Year >= latestDateYear {
  447. for _, tmpData := range edbDataItems.Items {
  448. if tmpData.DataTime == cuttingDataTimeStr {
  449. cuttingDataTimestamp = tmpData.DataTimestamp
  450. break
  451. }
  452. }
  453. }
  454. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  455. result.List[k] = edbDataItems
  456. }
  457. if result.List[0].Year != calendarPreYear {
  458. itemList := make([]*edbDataModel.EdbDataList, 0)
  459. items := new(edbDataModel.EdbDataItems)
  460. //items.Year = calendarPreYear
  461. items.Items = itemList
  462. newResult := new(edbDataModel.EdbDataResult)
  463. newResult.List = append(newResult.List, items)
  464. newResult.List = append(newResult.List, result.List...)
  465. resultDataList = newResult
  466. } else {
  467. resultDataList = result
  468. }
  469. }
  470. } else {
  471. currentYear := time.Now().Year()
  472. quarterDataList := make([]*edbDataModel.QuarterData, 0)
  473. quarterMap := make(map[int][]*edbDataModel.EdbDataList)
  474. var quarterArr []int
  475. for _, v := range dataList {
  476. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  477. if tmpErr != nil {
  478. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  479. return
  480. }
  481. year := itemDate.Year()
  482. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  483. timestamp := newItemDate.UnixNano() / 1e6
  484. v.DataTimestamp = timestamp
  485. if findVal, ok := quarterMap[year]; !ok {
  486. quarterArr = append(quarterArr, year)
  487. findVal = append(findVal, v)
  488. quarterMap[year] = findVal
  489. } else {
  490. findVal = append(findVal, v)
  491. quarterMap[year] = findVal
  492. }
  493. }
  494. for _, v := range quarterArr {
  495. itemList := quarterMap[v]
  496. quarterItem := new(edbDataModel.QuarterData)
  497. quarterItem.Year = strconv.Itoa(v)
  498. quarterItem.DataList = itemList
  499. //如果等于最后的实际日期,那么将切割时间戳记录
  500. if v == latestDateYear {
  501. var cuttingDataTimestamp int64
  502. for _, tmpData := range itemList {
  503. if tmpData.DataTime == latestDateStr {
  504. cuttingDataTimestamp = tmpData.DataTimestamp
  505. break
  506. }
  507. }
  508. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  509. } else if v > latestDateYear {
  510. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  511. if len(itemList) > 0 {
  512. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  513. }
  514. }
  515. quarterDataList = append(quarterDataList, quarterItem)
  516. }
  517. resultDataList = quarterDataList
  518. }
  519. return
  520. }