predict_edb_info.go 24 KB

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