predict_edb_info_rule.go 38 KB

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