predict_edb_info_rule.go 39 KB

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