predict_edb_info_rule.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. package data
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/nosixtools/solarlunar"
  6. "github.com/shopspring/decimal"
  7. "hongze/hongze_chart_lib/models"
  8. "hongze/hongze_chart_lib/models/data_manage"
  9. "hongze/hongze_chart_lib/utils"
  10. "time"
  11. )
  12. // GetChartPredictEdbInfoDataListByRule1 根据规则1获取预测数据
  13. func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList) {
  14. newPredictEdbInfoData = predictEdbInfoData
  15. //获取后面的预测数据
  16. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  17. predictEdbInfoData = make([]*models.EdbDataList, 0)
  18. for k, v := range dayList {
  19. newPredictEdbInfoData = append(newPredictEdbInfoData, &models.EdbDataList{
  20. EdbDataId: edbInfoId + 10000000000 + k,
  21. EdbInfoId: edbInfoId,
  22. DataTime: v.Format(utils.FormatDate),
  23. Value: dataValue,
  24. DataTimestamp: (v.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  25. })
  26. existMap[v.Format(utils.FormatDate)] = dataValue
  27. }
  28. return
  29. }
  30. // GetChartPredictEdbInfoDataListByRuleTb 根据同比值规则获取预测数据
  31. // 2.1 同比: 在未来某一个时间段内,给定一个固定的同比增速a,用去年同期值X乘以同比增速(1+a),得到预测值Y=X(1+a)
  32. // 例: 今年1-3月值,100,100,120。给定同比增速a=0.1,则明年1-3月预测值为: 100*1.1=110,100*1.1=110,120*1.1=132。
  33. func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  34. allDataList := make([]*models.EdbDataList, 0)
  35. allDataList = append(allDataList, realPredictEdbInfoData...)
  36. allDataList = append(allDataList, predictEdbInfoData...)
  37. newPredictEdbInfoData = predictEdbInfoData
  38. index := len(allDataList)
  39. //获取后面的预测数据
  40. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  41. predictEdbInfoData = make([]*models.EdbDataList, 0)
  42. for k, currentDate := range dayList {
  43. tmpData := &models.EdbDataList{
  44. EdbDataId: edbInfoId + 10000000000 + index + k,
  45. EdbInfoId: edbInfoId,
  46. DataTime: currentDate.Format(utils.FormatDate),
  47. //Value: dataValue,
  48. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  49. }
  50. var val float64
  51. var calculateStatus bool //计算结果
  52. //currentItem := existMap[av]
  53. //上一年的日期
  54. preDate := currentDate.AddDate(-1, 0, 0)
  55. preDateStr := preDate.Format(utils.FormatDate)
  56. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  57. val = TbzDiv(preValue, tbValue)
  58. calculateStatus = true
  59. } else {
  60. switch frequency {
  61. case "月度":
  62. //向上和向下,各找一个月
  63. nextDateDay := preDate
  64. preDateDay := preDate
  65. for i := 0; i <= 35; i++ {
  66. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  67. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  68. val = TbzDiv(preValue, tbValue)
  69. calculateStatus = true
  70. break
  71. } else {
  72. preDateDayStr := preDateDay.Format(utils.FormatDate)
  73. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  74. val = TbzDiv(preValue, tbValue)
  75. calculateStatus = true
  76. break
  77. }
  78. }
  79. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  80. preDateDay = preDateDay.AddDate(0, 0, -1)
  81. }
  82. case "季度", "年度":
  83. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  84. val = TbzDiv(preValue, tbValue)
  85. calculateStatus = true
  86. break
  87. }
  88. default:
  89. nextDateDay := preDate
  90. preDateDay := preDate
  91. for i := 0; i < 35; i++ {
  92. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  93. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  94. val = TbzDiv(preValue, tbValue)
  95. calculateStatus = true
  96. break
  97. } else {
  98. preDateDayStr := preDateDay.Format(utils.FormatDate)
  99. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  100. val = TbzDiv(preValue, tbValue)
  101. calculateStatus = true
  102. break
  103. } else {
  104. //fmt.Println("pre not find:", preDateStr, "i:", i)
  105. }
  106. }
  107. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  108. preDateDay = preDateDay.AddDate(0, 0, -1)
  109. }
  110. }
  111. }
  112. if calculateStatus {
  113. tmpData.Value = val
  114. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  115. allDataList = append(allDataList, tmpData)
  116. existMap[tmpData.DataTime] = val
  117. // 最大最小值
  118. if val < minValue {
  119. minValue = val
  120. }
  121. if val > maxValue {
  122. maxValue = val
  123. }
  124. }
  125. }
  126. return
  127. }
  128. // TbzDiv 同比值计算
  129. // @params a float64 去年同期值
  130. // @params b float64 固定同比增速
  131. func TbzDiv(a, b float64) (result float64) {
  132. if b != 0 {
  133. // 去年同期值
  134. af := decimal.NewFromFloat(a)
  135. // 同比增速
  136. bf := decimal.NewFromFloat(b)
  137. // 默认1
  138. cf := decimal.NewFromFloat(1)
  139. // 总增速
  140. val := bf.Add(cf)
  141. // 计算
  142. result, _ = val.Mul(af).RoundCeil(4).Float64()
  143. } else {
  144. result = 0
  145. }
  146. return
  147. }
  148. // GetChartPredictEdbInfoDataListByRuleTc 根据同差值规则获取预测数据
  149. // 2.2 同差: 在未来某一个时间段内,给定一个固定的同比增加值a,用去年同期值X加上同比增加值A,得到预测值Y=X+a
  150. // 例: 今年1-3月值,100,100,120。给定同比增加值a=10,则明年1-3月预测值为: 100+10=110,100+10=110,120+10=130
  151. func GetChartPredictEdbInfoDataListByRuleTc(edbInfoId int, tcValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  152. allDataList := make([]*models.EdbDataList, 0)
  153. allDataList = append(allDataList, realPredictEdbInfoData...)
  154. allDataList = append(allDataList, predictEdbInfoData...)
  155. newPredictEdbInfoData = predictEdbInfoData
  156. index := len(allDataList)
  157. //获取后面的预测数据
  158. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  159. predictEdbInfoData = make([]*models.EdbDataList, 0)
  160. for k, currentDate := range dayList {
  161. tmpData := &models.EdbDataList{
  162. EdbDataId: edbInfoId + 10000000000 + index + k,
  163. EdbInfoId: edbInfoId,
  164. DataTime: currentDate.Format(utils.FormatDate),
  165. //Value: dataValue,
  166. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  167. }
  168. var val float64
  169. var calculateStatus bool //计算结果
  170. //currentItem := existMap[av]
  171. //上一年的日期
  172. preDate := currentDate.AddDate(-1, 0, 0)
  173. preDateStr := preDate.Format(utils.FormatDate)
  174. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  175. val = TczDiv(preValue, tcValue)
  176. calculateStatus = true
  177. } else {
  178. switch frequency {
  179. case "月度":
  180. //向上和向下,各找一个月
  181. nextDateDay := preDate
  182. preDateDay := preDate
  183. for i := 0; i <= 35; i++ {
  184. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  185. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  186. val = TczDiv(preValue, tcValue)
  187. calculateStatus = true
  188. break
  189. } else {
  190. preDateDayStr := preDateDay.Format(utils.FormatDate)
  191. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  192. val = TczDiv(preValue, tcValue)
  193. calculateStatus = true
  194. break
  195. }
  196. }
  197. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  198. preDateDay = preDateDay.AddDate(0, 0, -1)
  199. }
  200. case "季度", "年度":
  201. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  202. val = TczDiv(preValue, tcValue)
  203. calculateStatus = true
  204. break
  205. }
  206. default:
  207. nextDateDay := preDate
  208. preDateDay := preDate
  209. for i := 0; i < 35; i++ {
  210. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  211. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  212. val = TczDiv(preValue, tcValue)
  213. calculateStatus = true
  214. break
  215. } else {
  216. preDateDayStr := preDateDay.Format(utils.FormatDate)
  217. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  218. val = TczDiv(preValue, tcValue)
  219. calculateStatus = true
  220. break
  221. } else {
  222. //fmt.Println("pre not find:", preDateStr, "i:", i)
  223. }
  224. }
  225. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  226. preDateDay = preDateDay.AddDate(0, 0, -1)
  227. }
  228. }
  229. }
  230. if calculateStatus {
  231. tmpData.Value = val
  232. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  233. allDataList = append(allDataList, tmpData)
  234. existMap[tmpData.DataTime] = val
  235. // 最大最小值
  236. if val < minValue {
  237. minValue = val
  238. }
  239. if val > maxValue {
  240. maxValue = val
  241. }
  242. }
  243. }
  244. return
  245. }
  246. // TczDiv 环差值计算
  247. // @params a float64 上一期值
  248. // @params b float64 固定的环比增加值
  249. func TczDiv(a, b float64) (result float64) {
  250. if b != 0 {
  251. // 上一期值
  252. af := decimal.NewFromFloat(a)
  253. // 固定的环比增加值
  254. bf := decimal.NewFromFloat(b)
  255. // 计算
  256. result, _ = af.Add(bf).RoundCeil(4).Float64()
  257. } else {
  258. result = 0
  259. }
  260. return
  261. }
  262. // GetChartPredictEdbInfoDataListByRuleHb 根据环比值规则获取预测数据
  263. // 环比:在未来某一个时间段内,给定一个固定的环比增速a,用上一期值X乘以环比增速(1+a),得到预测值Y=X(1+a)
  264. // 例: 最近1期值为100,给定环比增速a=0.2,则未来3期预测值为: 100*1.2=120,120*1.2=144,144*1.2=172.8
  265. func GetChartPredictEdbInfoDataListByRuleHb(edbInfoId int, hbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  266. allDataList := make([]*models.EdbDataList, 0)
  267. allDataList = append(allDataList, realPredictEdbInfoData...)
  268. allDataList = append(allDataList, predictEdbInfoData...)
  269. newPredictEdbInfoData = predictEdbInfoData
  270. index := len(allDataList)
  271. //获取后面的预测数据
  272. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  273. for k, currentDate := range dayList {
  274. tmpK := index + k - 1 //上1期的值
  275. // 环比值计算
  276. val := HbzDiv(allDataList[tmpK].Value, hbValue)
  277. currentDateStr := currentDate.Format(utils.FormatDate)
  278. tmpData := &models.EdbDataList{
  279. EdbDataId: edbInfoId + 10000000000 + index + k,
  280. EdbInfoId: edbInfoId,
  281. DataTime: currentDateStr,
  282. Value: val,
  283. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  284. }
  285. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  286. allDataList = append(allDataList, tmpData)
  287. existMap[currentDateStr] = val
  288. // 最大最小值
  289. if val < minValue {
  290. minValue = val
  291. }
  292. if val > maxValue {
  293. maxValue = val
  294. }
  295. }
  296. return
  297. }
  298. // HbzDiv 环比值计算
  299. // @params a float64 上一期值
  300. // @params b float64 固定的环比增速
  301. func HbzDiv(a, b float64) (result float64) {
  302. if b != 0 {
  303. // 上一期值
  304. af := decimal.NewFromFloat(a)
  305. // 固定的环比增速
  306. bf := decimal.NewFromFloat(b)
  307. // 默认1
  308. cf := decimal.NewFromFloat(1)
  309. // 总增速
  310. val := bf.Add(cf)
  311. // 计算
  312. result, _ = val.Mul(af).RoundCeil(4).Float64()
  313. } else {
  314. result = 0
  315. }
  316. return
  317. }
  318. // GetChartPredictEdbInfoDataListByRuleHc 根据环差值规则获取预测数据
  319. // 2.4 环差:在未来某一个时间段内,给定一个固定的环比增加值a,用上一期值X加上环比增加值a,得到预测值Y=X+a
  320. // 例: 最近1期值为100,给定环比增加值a=10,则未来3期预测值为: 100+10=110,110+10=120,120+10=130
  321. func GetChartPredictEdbInfoDataListByRuleHc(edbInfoId int, hcValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  322. allDataList := make([]*models.EdbDataList, 0)
  323. allDataList = append(allDataList, realPredictEdbInfoData...)
  324. allDataList = append(allDataList, predictEdbInfoData...)
  325. newPredictEdbInfoData = predictEdbInfoData
  326. index := len(allDataList)
  327. //获取后面的预测数据
  328. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  329. for k, currentDate := range dayList {
  330. tmpK := index + k - 1 //上1期的值
  331. // 环差别值计算
  332. val := HczDiv(allDataList[tmpK].Value, hcValue)
  333. currentDateStr := currentDate.Format(utils.FormatDate)
  334. tmpData := &models.EdbDataList{
  335. EdbDataId: edbInfoId + 10000000000 + index + k,
  336. EdbInfoId: edbInfoId,
  337. DataTime: currentDateStr,
  338. Value: val,
  339. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  340. }
  341. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  342. allDataList = append(allDataList, tmpData)
  343. existMap[currentDateStr] = val
  344. // 最大最小值
  345. if val < minValue {
  346. minValue = val
  347. }
  348. if val > maxValue {
  349. maxValue = val
  350. }
  351. }
  352. return
  353. }
  354. // HczDiv 环差值计算
  355. // @params a float64 上一期值
  356. // @params b float64 固定的环比增加值
  357. func HczDiv(a, b float64) (result float64) {
  358. if b != 0 {
  359. // 上一期值
  360. af := decimal.NewFromFloat(a)
  361. // 固定的环比增加值
  362. bf := decimal.NewFromFloat(b)
  363. // 计算
  364. result, _ = af.Add(bf).RoundCeil(4).Float64()
  365. } else {
  366. result = 0
  367. }
  368. return
  369. }
  370. // GetChartPredictEdbInfoDataListByRuleNMoveMeanValue 根据N期移动均值规则获取预测数据
  371. // 2.5 N期移动均值:在未来某一个时间段内,下一期值等于过去N期值得平均值。
  372. // 例:最近3期值(N=3),为95,98,105则未来第1期值为 1/3*(95+98+105)=99.33, 未来第2期值为 1/3*(98+105+99.33)=100.78依次类推。
  373. func GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  374. allDataList := make([]*models.EdbDataList, 0)
  375. allDataList = append(allDataList, realPredictEdbInfoData...)
  376. allDataList = append(allDataList, predictEdbInfoData...)
  377. newPredictEdbInfoData = predictEdbInfoData
  378. lenAllData := len(allDataList)
  379. if lenAllData < nValue || lenAllData <= 0 {
  380. return
  381. }
  382. if nValue <= 0 {
  383. return
  384. }
  385. // 分母
  386. decimalN := decimal.NewFromInt(int64(nValue))
  387. //获取后面的预测数据
  388. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  389. for k, currentDate := range dayList {
  390. tmpIndex := lenAllData + k - 1 //上1期的值
  391. // 数据集合中的最后一个数据
  392. tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
  393. for tmpK := 2; tmpK <= nValue; tmpK++ {
  394. tmpIndex2 := tmpIndex - tmpK //上N期的值
  395. tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
  396. tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
  397. }
  398. // N期移动均值计算
  399. val, _ := tmpDecimalVal.Div(decimalN).RoundCeil(4).Float64()
  400. currentDateStr := currentDate.Format(utils.FormatDate)
  401. tmpData := &models.EdbDataList{
  402. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  403. EdbInfoId: edbInfoId,
  404. DataTime: currentDateStr,
  405. Value: val,
  406. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  407. }
  408. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  409. allDataList = append(allDataList, tmpData)
  410. existMap[currentDateStr] = val
  411. // 最大最小值
  412. if val < minValue {
  413. minValue = val
  414. }
  415. if val > maxValue {
  416. maxValue = val
  417. }
  418. }
  419. return
  420. }
  421. // GetChartPredictEdbInfoDataListByRuleNLinearRegression 根据N期移动均值规则获取预测数据
  422. // 2.6N期段线性外推值:给出过去N期值所确定的线性回归方程(Y=aX+b)在未来一段时间内的推算值。回归方程虽然比较复杂,但各种编程语言应该都有现成的模块或函数,应该无需自己编写。
  423. // 例1:过去5期值(N=5)分别为:3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:13,15,17。
  424. //
  425. // 例2:过去6期值(N=6)分别为:3,3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:12.33,14.05,15.76。例1和例2的区别在于,多加了一期数据,导致回归方程发生改变,从而预测值不同。
  426. func GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  427. //var errMsg string
  428. //defer func() {
  429. // if errMsg != `` {
  430. // go alarm_msg.SendAlarmMsg("更新上海的token失败;ERR:"+err.Error(), 3)
  431. // }
  432. //}()
  433. allDataList := make([]*models.EdbDataList, 0)
  434. allDataList = append(allDataList, realPredictEdbInfoData...)
  435. allDataList = append(allDataList, predictEdbInfoData...)
  436. newPredictEdbInfoData = predictEdbInfoData
  437. lenAllData := len(allDataList)
  438. if lenAllData < nValue || lenAllData <= 0 {
  439. return
  440. }
  441. if nValue <= 1 {
  442. return
  443. }
  444. //获取后面的预测数据
  445. // 获取线性方程公式的a、b的值
  446. coordinateData := make([]Coordinate, 0)
  447. for tmpK := nValue; tmpK > 0; tmpK-- {
  448. tmpIndex2 := lenAllData - tmpK //上N期的值
  449. tmpCoordinate := Coordinate{
  450. X: float64(nValue - tmpK + 1),
  451. Y: allDataList[tmpIndex2].Value,
  452. }
  453. coordinateData = append(coordinateData, tmpCoordinate)
  454. }
  455. a, b := getLinearResult(coordinateData)
  456. //fmt.Println("a:", a, ";======b:", b)
  457. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  458. for k, currentDate := range dayList {
  459. tmpK := nValue + k + 1
  460. aDecimal := decimal.NewFromFloat(a)
  461. xDecimal := decimal.NewFromInt(int64(tmpK))
  462. bDecimal := decimal.NewFromFloat(b)
  463. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
  464. currentDateStr := currentDate.Format(utils.FormatDate)
  465. tmpData := &models.EdbDataList{
  466. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  467. EdbInfoId: edbInfoId,
  468. DataTime: currentDateStr,
  469. Value: val,
  470. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  471. }
  472. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  473. allDataList = append(allDataList, tmpData)
  474. existMap[currentDateStr] = val
  475. // 最大最小值
  476. if val < minValue {
  477. minValue = val
  478. }
  479. if val > maxValue {
  480. maxValue = val
  481. }
  482. }
  483. return
  484. }
  485. // Series is a container for a series of data
  486. type Series []Coordinate
  487. // Coordinate holds the data in a series
  488. type Coordinate struct {
  489. X, Y float64
  490. }
  491. func getLinearResult(s []Coordinate) (gradient, intercept float64) {
  492. if len(s) <= 1 {
  493. return
  494. }
  495. // Placeholder for the math to be done
  496. var sum [5]float64
  497. // Loop over data keeping index in place
  498. i := 0
  499. for ; i < len(s); i++ {
  500. sum[0] += s[i].X
  501. sum[1] += s[i].Y
  502. sum[2] += s[i].X * s[i].X
  503. sum[3] += s[i].X * s[i].Y
  504. sum[4] += s[i].Y * s[i].Y
  505. }
  506. // Find gradient and intercept
  507. f := float64(i)
  508. gradient = (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
  509. intercept = (sum[1] / f) - (gradient * sum[0] / f)
  510. //fmt.Println("gradient:", gradient, ";intercept:", intercept)
  511. // Create the new regression series
  512. //for j := 0; j < len(s); j++ {
  513. // regressions = append(regressions, Coordinate{
  514. // X: s[j].X,
  515. // Y: s[j].X*gradient + intercept,
  516. // })
  517. //}
  518. return
  519. }
  520. // GetChartPredictEdbInfoDataListByRuleTrendsHC 根据动态环比增加值的计算规则获取预测数据
  521. // 研究员有对预测指标进行动态环差计算的需求,即预测指标使用环差规则进行预测时,环比增加值不是固定值,而是由几个预测指标计算得出的动态变化的值;
  522. //需求说明:
  523. //1、增加“动态环差”预测规则;
  524. //2、环比增加值在弹窗设置;
  525. //3、动态环差预测举例:
  526. //指标A实际最新数据为2022-10-27(100);
  527. //预测指标B预测数据为2022-10-28(240)、2022-10-29(300);
  528. //预测指标C预测数据为2022-10-28(260)、2022-10-29(310);
  529. //计算公式为B-C;
  530. //则指标A至2022-10-29的预测值为2022-10-28(100+(240-260)=80)、2022-10-29(80+(300-310)=90);
  531. //注:动态环比增加值的计算遵从计算指标的计算规则,即用于计算的指标若有部分指标缺少部分日期数据,则这部分日期数据不做计算,为空;若动态环比增加值某一天为空,则往前追溯最近一期有值的环比增加值作为该天的数值参与计算;
  532. func GetChartPredictEdbInfoDataListByRuleTrendsHC(edbInfoId, configId int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  533. allDataList := make([]*models.EdbDataList, 0)
  534. allDataList = append(allDataList, realPredictEdbInfoData...)
  535. allDataList = append(allDataList, predictEdbInfoData...)
  536. newPredictEdbInfoData = predictEdbInfoData
  537. lenAllData := len(allDataList)
  538. if lenAllData <= 0 {
  539. return
  540. }
  541. hcDataMap := make(map[string]float64) //规则计算的环差值map
  542. //已经生成的动态数据
  543. tmpPredictEdbRuleDataList, err := data_manage.GetPredictEdbRuleDataList(edbInfoId, configId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  544. if err != nil {
  545. return
  546. }
  547. for _, v := range tmpPredictEdbRuleDataList {
  548. hcDataMap[v.DataTime] = v.Value
  549. }
  550. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  551. for k, currentDate := range dayList {
  552. // 最近一条数据
  553. tmpLenAllDataList := len(allDataList)
  554. lastValue := allDataList[tmpLenAllDataList-1].Value
  555. // 动态环差值数据
  556. currentDateStr := currentDate.Format(utils.FormatDate)
  557. hcVal, ok := hcDataMap[currentDateStr]
  558. if !ok {
  559. continue
  560. }
  561. lastValueDecimal := decimal.NewFromFloat(lastValue)
  562. hcValDecimal := decimal.NewFromFloat(hcVal)
  563. val, _ := lastValueDecimal.Add(hcValDecimal).RoundCeil(4).Float64()
  564. tmpData := &models.EdbDataList{
  565. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  566. EdbInfoId: edbInfoId,
  567. DataTime: currentDateStr,
  568. Value: val,
  569. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  570. }
  571. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  572. allDataList = append(allDataList, tmpData)
  573. existMap[currentDateStr] = val
  574. // 最大最小值
  575. if val < minValue {
  576. minValue = val
  577. }
  578. if val > maxValue {
  579. maxValue = val
  580. }
  581. }
  582. return
  583. }
  584. // GetChartPredictEdbInfoDataListByRuleFinalValueHc 根据 给定终值后插值 规则获取预测数据
  585. // 项目背景:
  586. //假设螺纹产量在2023年1月1号的预测值是255万吨,从当下到2023年1月1号,螺纹产量将会线性变化,那么每一期的螺纹产量是多少?
  587. //算法:从当下(2022/10/28)到2023/1/1号,一共65天,从当前值(305.02)到255,差值-50.02,
  588. //则每日环差为-50.02/65=-0.7695。因为数据点是周度频率,每周环差为,-0.3849*7=-5.3868。
  589. //从以上计算过程可看出,“给定终值后差值”的算法,是在“环差”算法的基础上,做的一个改动。即这个”环差值”=【(终值-最新值)/终值与最新值得日期差】*数据频率
  590. //需求说明:
  591. //1、增加一个预测规则,名为“给定终值后插值”,给定预测截止日期和预测终值,计算最新数据日期至预测截止日期的时间差T,计算最新数据和预测终值的数据差S,数据频率与指标频度有关,日度=1,周度=7,旬度=10,月度=30,季度=90,年度=365,环差值=S/T*频率,预测数值=前一天数值+环差值;
  592. //2、最新数据值和日期改动后,需重新计算环差值和预测数值;
  593. func GetChartPredictEdbInfoDataListByRuleFinalValueHc(edbInfoId int, finalValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  594. allDataList := make([]*models.EdbDataList, 0)
  595. allDataList = append(allDataList, realPredictEdbInfoData...)
  596. allDataList = append(allDataList, predictEdbInfoData...)
  597. newPredictEdbInfoData = predictEdbInfoData
  598. index := len(allDataList)
  599. //获取后面的预测日期
  600. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  601. lenDay := len(dayList)
  602. if lenDay <= 0 {
  603. return
  604. }
  605. var hcValue float64
  606. lastValueDeciamal := decimal.NewFromFloat(allDataList[index-1].Value) // 实际数据的最后一个值
  607. finalValueDeciamal := decimal.NewFromFloat(finalValue) // 给定的终止数据
  608. dayDecimal := decimal.NewFromInt(int64(lenDay)) // 需要作为分母的期数
  609. hcValue, _ = finalValueDeciamal.Sub(lastValueDeciamal).Div(dayDecimal).Float64() // 计算出来的环差值
  610. //获取后面的预测数据
  611. predictEdbInfoData = make([]*models.EdbDataList, 0)
  612. lastK := lenDay - 1 // 最后的日期
  613. for k, currentDate := range dayList {
  614. tmpK := index + k - 1 //上1期的值
  615. var val float64
  616. // 环差别值计算
  617. if k == lastK { //如果是最后一天,那么就用最终值,否则就计算
  618. val = finalValue
  619. } else {
  620. val = HczDiv(allDataList[tmpK].Value, hcValue)
  621. }
  622. currentDateStr := currentDate.Format(utils.FormatDate)
  623. tmpData := &models.EdbDataList{
  624. EdbDataId: edbInfoId + 10000000000 + index + k,
  625. EdbInfoId: edbInfoId,
  626. DataTime: currentDateStr,
  627. Value: val,
  628. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  629. }
  630. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  631. allDataList = append(allDataList, tmpData)
  632. existMap[currentDateStr] = val
  633. // 最大最小值
  634. if val < minValue {
  635. minValue = val
  636. }
  637. if val > maxValue {
  638. maxValue = val
  639. }
  640. }
  641. return
  642. }
  643. // SeasonConf 季节性规则的配置
  644. type SeasonConf struct {
  645. Calendar string `description:"公历、农历"`
  646. YearType int `description:"选择方式,1:连续N年;2:指定年份"`
  647. NValue int `description:"连续N年"`
  648. YearList []int `description:"指定年份列表"`
  649. }
  650. // GetChartPredictEdbInfoDataListByRuleSeason 根据 季节性 规则获取预测数据
  651. // ETA预测规则:季节性
  652. //已知选定指标A最近更新日期: 2022-12-6 200
  653. //设置预测截止日期2023-01-06
  654. //1、选择过去N年,N=3
  655. //则过去N年为2021、2020、2019
  656. //指标A日期 实际值 指标A日期
  657. //2019/12/5 150 2019/12/6
  658. //2020/12/5 180 2020/12/6
  659. //2021/12/5 210 2021/12/6
  660. //2019/12/31 200 2020/1/1
  661. //2020/12/31 210 2021/1/1
  662. //2021/12/31 250 2022/1/1
  663. //
  664. //计算12.7预测值,求过去N年环差均值=[(100-150)+(160-180)+(250-210)]/3=-10
  665. //则12.7预测值=12.6值+过去N年环差均值=200-10=190
  666. //以此类推...
  667. //
  668. //计算2023.1.2预测值,求过去N年环差均值=[(300-200)+(220-210)+(260-250)]/3=40
  669. //则2023.1.2预测值=2023.1.1值+过去N年环差均值
  670. func GetChartPredictEdbInfoDataListByRuleSeason(edbInfoId int, yearsList []int, calendar string, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64, err error) {
  671. allDataList := make([]*models.EdbDataList, 0)
  672. allDataList = append(allDataList, realPredictEdbInfoData...)
  673. allDataList = append(allDataList, predictEdbInfoData...)
  674. newPredictEdbInfoData = predictEdbInfoData
  675. // 获取每个年份的日期数据需要平移的天数
  676. moveDayMap := make(map[int]int, 0) // 每个年份的春节公历
  677. {
  678. if calendar == "公历" {
  679. for _, year := range yearsList {
  680. moveDayMap[year] = 0 //公历就不平移了
  681. }
  682. } else {
  683. currentDay := time.Now()
  684. if currentDay.Month() >= 11 { //如果大于等于11月份,那么用的是下一年的春节
  685. currentDay = currentDay.AddDate(1, 0, 0)
  686. }
  687. currentYear := currentDay.Year()
  688. currentYearCjnl := fmt.Sprintf("%d-01-01", currentYear) //当年的春节农历
  689. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当年的春节公历
  690. currentYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, currentYearCjgl, time.Local)
  691. if tmpErr != nil {
  692. err = errors.New("当前春节公历日期转换失败:" + tmpErr.Error())
  693. return
  694. }
  695. // 指定的年份
  696. for _, year := range yearsList {
  697. tmpYearCjnl := fmt.Sprintf("%d-01-01", year) //指定年的春节农历
  698. tmpYearCjgl := solarlunar.LunarToSolar(tmpYearCjnl, false) //指定年的春节公历
  699. //moveDayList = append(moveDayList, 0) //公历就不平移了
  700. tmpYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpYearCjgl, time.Local)
  701. if tmpErr != nil {
  702. err = errors.New(fmt.Sprintf("%d公历日期转换失败:%s", year, tmpErr.Error()))
  703. return
  704. }
  705. tmpCurrentYearCjglTime := currentYearCjglTime.AddDate(year-currentYear, 0, 0)
  706. moveDay := utils.GetTimeSubDay(tmpYearCjglTime, tmpCurrentYearCjglTime)
  707. moveDayMap[year] = moveDay //公历平移
  708. }
  709. }
  710. }
  711. index := len(allDataList)
  712. //获取后面的预测日期
  713. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  714. //获取后面的预测数据
  715. predictEdbInfoData = make([]*models.EdbDataList, 0)
  716. for k, currentDate := range dayList {
  717. tmpHistoryVal := decimal.NewFromFloat(0) //往期的差值总和
  718. tmpHistoryValNum := 0 // 往期差值计算的数量
  719. tmpLenAllDataList := len(allDataList)
  720. tmpK := tmpLenAllDataList - 1 //上1期数据的下标
  721. lastDayStr := allDataList[tmpK].DataTime
  722. lastDayVal := allDataList[tmpK].Value
  723. lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, lastDayStr, time.Local)
  724. if tmpErr != nil {
  725. err = errors.New("获取上期日期转换失败:" + tmpErr.Error())
  726. }
  727. for _, year := range yearsList {
  728. moveDay := moveDayMap[year] //需要移动的天数
  729. var tmpHistoryCurrentVal, tmpHistoryLastVal float64
  730. var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
  731. //前几年当日的日期
  732. tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, moveDay)
  733. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  734. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
  735. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  736. tmpHistoryCurrentVal = val
  737. isFindHistoryCurrent = true
  738. break
  739. } else {
  740. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
  741. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  742. tmpHistoryCurrentVal = val
  743. isFindHistoryCurrent = true
  744. break
  745. }
  746. }
  747. }
  748. //前几年上一期的日期
  749. tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, moveDay)
  750. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  751. tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
  752. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  753. tmpHistoryLastVal = val
  754. isFindHistoryLast = true
  755. break
  756. } else {
  757. tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
  758. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  759. tmpHistoryLastVal = val
  760. isFindHistoryLast = true
  761. break
  762. }
  763. }
  764. }
  765. // 如果两个日期对应的数据都找到了,那么计算两期的差值
  766. if isFindHistoryCurrent && isFindHistoryLast {
  767. af := decimal.NewFromFloat(tmpHistoryCurrentVal)
  768. bf := decimal.NewFromFloat(tmpHistoryLastVal)
  769. tmpHistoryVal = tmpHistoryVal.Add(af.Sub(bf))
  770. tmpHistoryValNum++
  771. }
  772. }
  773. //计算的差值与选择的年份数量不一致,那么当前日期不计算
  774. if tmpHistoryValNum != len(yearsList) {
  775. continue
  776. }
  777. lastDayValDec := decimal.NewFromFloat(lastDayVal)
  778. val, _ := tmpHistoryVal.Div(decimal.NewFromInt(int64(tmpHistoryValNum))).Add(lastDayValDec).RoundCeil(4).Float64()
  779. currentDateStr := currentDate.Format(utils.FormatDate)
  780. tmpData := &models.EdbDataList{
  781. EdbDataId: edbInfoId + 10000000000 + index + k,
  782. EdbInfoId: edbInfoId,
  783. DataTime: currentDateStr,
  784. Value: val,
  785. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  786. }
  787. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  788. allDataList = append(allDataList, tmpData)
  789. existMap[currentDateStr] = val
  790. // 最大最小值
  791. if val < minValue {
  792. minValue = val
  793. }
  794. if val > maxValue {
  795. maxValue = val
  796. }
  797. }
  798. return
  799. }
  800. // MoveAverageConf 移动平均同比规则的配置
  801. type MoveAverageConf struct {
  802. Year int `description:"指定年份"`
  803. NValue int `description:"N期的数据"`
  804. }
  805. // GetChartPredictEdbInfoDataListByRuleMoveAverageTb 根据 移动平均同比 规则获取预测数据
  806. // ETA预测规则:季节性
  807. //2、选择指定N年,N=3
  808. //指定N年为2012、2015、2018
  809. //指标A日期 实际值 指标A日期 实际值
  810. //2012/12/5 150 2012/12/6 130
  811. //2015/12/5 180 2015/12/6 150
  812. //2018/12/5 210 2018/12/6 260
  813. //2012/12/31 200 2013/1/1 200
  814. //2015/12/31 210 2016/1/1 250
  815. //2018/12/31 250 2019/1/1 270
  816. //计算12.7预测值,求过去N年环差均值=[(130-150)+(150-180)+(290-210)]/3=10
  817. //则12.7预测值=12.6值+过去N年环差均值=200+10=210
  818. //以此类推...
  819. //计算2023.1.2预测值,求过去N年环差均值=[(200-200)+(250-210)+(270-250)]/3=16.67
  820. //则2023.1.2预测值=2023.1.1值+过去N年环差均值
  821. func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, year int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64, err error) {
  822. allDataList := make([]*models.EdbDataList, 0)
  823. allDataList = append(allDataList, realPredictEdbInfoData...)
  824. allDataList = append(allDataList, predictEdbInfoData...)
  825. newPredictEdbInfoData = predictEdbInfoData
  826. lenAllData := len(allDataList)
  827. if lenAllData < nValue || lenAllData <= 0 {
  828. return
  829. }
  830. if nValue <= 0 {
  831. return
  832. }
  833. // 分母
  834. decimalN := decimal.NewFromInt(int64(nValue))
  835. //获取后面的预测数据
  836. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  837. for k, currentDate := range dayList {
  838. tmpLenAllDataList := len(allDataList)
  839. tmpIndex := tmpLenAllDataList - 1 //上1期数据的下标
  840. averageDateList := make([]string, 0) //计算平均数的日期
  841. // 数据集合中的最后一个数据
  842. tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
  843. averageDateList = append(averageDateList, allDataList[tmpIndex].DataTime)
  844. for tmpK := 2; tmpK <= nValue; tmpK++ {
  845. tmpIndex2 := tmpIndex - tmpK //上N期的值
  846. tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
  847. tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
  848. averageDateList = append(averageDateList, allDataList[tmpIndex2].DataTime)
  849. }
  850. // 最近的N期平均值
  851. tmpAverageVal := tmpDecimalVal.Div(decimalN)
  852. var tmpHistoryCurrentVal float64 // 前几年当日的数据值
  853. var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
  854. tmpHistoryDecimalVal := decimal.NewFromFloat(0) //前几年N期数据总值
  855. {
  856. // 前几年N期汇总期数
  857. tmpHistoryValNum := 0
  858. {
  859. //前几年当日的日期
  860. tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, 0)
  861. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  862. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
  863. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  864. tmpHistoryCurrentVal = val
  865. isFindHistoryCurrent = true
  866. break
  867. } else {
  868. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
  869. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  870. tmpHistoryCurrentVal = val
  871. isFindHistoryCurrent = true
  872. break
  873. }
  874. }
  875. }
  876. }
  877. for _, averageDate := range averageDateList {
  878. lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, averageDate, time.Local)
  879. if tmpErr != nil {
  880. err = tmpErr
  881. return
  882. }
  883. //前几年上一期的日期
  884. tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, 0)
  885. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  886. tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
  887. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  888. tmpDecimalVal2 := decimal.NewFromFloat(val)
  889. tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
  890. tmpHistoryValNum++
  891. break
  892. } else {
  893. tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
  894. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  895. tmpDecimalVal2 := decimal.NewFromFloat(val)
  896. tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
  897. tmpHistoryValNum++
  898. break
  899. }
  900. }
  901. }
  902. }
  903. // 汇总期数与配置的N期数量一致
  904. if tmpHistoryValNum == nValue {
  905. isFindHistoryLast = true
  906. }
  907. }
  908. // 如果没有找到前几年的汇总数据,或者没有找到前几年当日的数据,那么退出当前循环,进入下一循环
  909. if !isFindHistoryLast || !isFindHistoryCurrent {
  910. continue
  911. }
  912. // 计算最近N期同比值
  913. tbVal := tmpAverageVal.Div(tmpHistoryDecimalVal)
  914. // 预测值结果 = 同比年份同期值(tmpHistoryCurrentVal的值)* 同比值(tbVal的值)
  915. val, _ := decimal.NewFromFloat(tmpHistoryCurrentVal).Mul(tbVal).RoundCeil(4).Float64()
  916. currentDateStr := currentDate.Format(utils.FormatDate)
  917. tmpData := &models.EdbDataList{
  918. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  919. EdbInfoId: edbInfoId,
  920. DataTime: currentDateStr,
  921. Value: val,
  922. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  923. }
  924. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  925. allDataList = append(allDataList, tmpData)
  926. existMap[currentDateStr] = val
  927. // 最大最小值
  928. if val < minValue {
  929. minValue = val
  930. }
  931. if val > maxValue {
  932. maxValue = val
  933. }
  934. }
  935. return
  936. }