predict_edb_info_rule.go 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. package chart
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/nosixtools/solarlunar"
  6. "github.com/shopspring/decimal"
  7. edbDataModel "hongze/hongze_yb/models/tables/edb_data"
  8. predictEdbRuleDataModel "hongze/hongze_yb/models/tables/predict_edb_rule_data"
  9. "hongze/hongze_yb/utils"
  10. "math"
  11. "time"
  12. )
  13. // GetChartPredictEdbInfoDataListByRule1 根据规则1获取预测数据
  14. func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList) {
  15. newPredictEdbInfoData = predictEdbInfoData
  16. //获取后面的预测数据
  17. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  18. predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
  19. for k, v := range dayList {
  20. newPredictEdbInfoData = append(newPredictEdbInfoData, &edbDataModel.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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
  35. allDataList := make([]*edbDataModel.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([]*edbDataModel.EdbDataList, 0)
  43. for k, currentDate := range dayList {
  44. tmpData := &edbDataModel.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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
  153. allDataList := make([]*edbDataModel.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([]*edbDataModel.EdbDataList, 0)
  161. for k, currentDate := range dayList {
  162. tmpData := &edbDataModel.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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
  267. allDataList := make([]*edbDataModel.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 := &edbDataModel.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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
  323. allDataList := make([]*edbDataModel.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 := &edbDataModel.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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
  375. allDataList := make([]*edbDataModel.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 := &edbDataModel.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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
  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([]*edbDataModel.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. if math.IsNaN(a) || math.IsNaN(b) {
  458. err = errors.New("线性方程公式生成失败")
  459. return
  460. }
  461. //fmt.Println("a:", a, ";======b:", b)
  462. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  463. for k, currentDate := range dayList {
  464. tmpK := nValue + k + 1
  465. aDecimal := decimal.NewFromFloat(a)
  466. xDecimal := decimal.NewFromInt(int64(tmpK))
  467. bDecimal := decimal.NewFromFloat(b)
  468. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
  469. currentDateStr := currentDate.Format(utils.FormatDate)
  470. tmpData := &edbDataModel.EdbDataList{
  471. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  472. EdbInfoId: edbInfoId,
  473. DataTime: currentDateStr,
  474. Value: val,
  475. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  476. }
  477. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  478. allDataList = append(allDataList, tmpData)
  479. existMap[currentDateStr] = val
  480. // 最大最小值
  481. if val < minValue {
  482. minValue = val
  483. }
  484. if val > maxValue {
  485. maxValue = val
  486. }
  487. }
  488. return
  489. }
  490. // Series is a container for a series of data
  491. type Series []Coordinate
  492. // Coordinate holds the data in a series
  493. type Coordinate struct {
  494. X, Y float64
  495. }
  496. func getLinearResult(s []Coordinate) (gradient, intercept float64) {
  497. if len(s) <= 1 {
  498. return
  499. }
  500. // Placeholder for the math to be done
  501. var sum [5]float64
  502. // Loop over data keeping index in place
  503. i := 0
  504. for ; i < len(s); i++ {
  505. sum[0] += s[i].X
  506. sum[1] += s[i].Y
  507. sum[2] += s[i].X * s[i].X
  508. sum[3] += s[i].X * s[i].Y
  509. sum[4] += s[i].Y * s[i].Y
  510. }
  511. // Find gradient and intercept
  512. f := float64(i)
  513. gradient = (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
  514. intercept = (sum[1] / f) - (gradient * sum[0] / f)
  515. //fmt.Println("gradient:", gradient, ";intercept:", intercept)
  516. // Create the new regression series
  517. //for j := 0; j < len(s); j++ {
  518. // regressions = append(regressions, Coordinate{
  519. // X: s[j].X,
  520. // Y: s[j].X*gradient + intercept,
  521. // })
  522. //}
  523. return
  524. }
  525. // GetChartPredictEdbInfoDataListByRuleTrendsHC 根据动态环比增加值的计算规则获取预测数据
  526. // 研究员有对预测指标进行动态环差计算的需求,即预测指标使用环差规则进行预测时,环比增加值不是固定值,而是由几个预测指标计算得出的动态变化的值;
  527. //需求说明:
  528. //1、增加“动态环差”预测规则;
  529. //2、环比增加值在弹窗设置;
  530. //3、动态环差预测举例:
  531. //指标A实际最新数据为2022-10-27(100);
  532. //预测指标B预测数据为2022-10-28(240)、2022-10-29(300);
  533. //预测指标C预测数据为2022-10-28(260)、2022-10-29(310);
  534. //计算公式为B-C;
  535. //则指标A至2022-10-29的预测值为2022-10-28(100+(240-260)=80)、2022-10-29(80+(300-310)=90);
  536. //注:动态环比增加值的计算遵从计算指标的计算规则,即用于计算的指标若有部分指标缺少部分日期数据,则这部分日期数据不做计算,为空;若动态环比增加值某一天为空,则往前追溯最近一期有值的环比增加值作为该天的数值参与计算;
  537. func GetChartPredictEdbInfoDataListByRuleTrendsHC(edbInfoId, configId int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
  538. allDataList := make([]*edbDataModel.EdbDataList, 0)
  539. allDataList = append(allDataList, realPredictEdbInfoData...)
  540. allDataList = append(allDataList, predictEdbInfoData...)
  541. newPredictEdbInfoData = predictEdbInfoData
  542. lenAllData := len(allDataList)
  543. if lenAllData <= 0 {
  544. return
  545. }
  546. hcDataMap := make(map[string]float64) //规则计算的环差值map
  547. //已经生成的动态数据
  548. tmpPredictEdbRuleDataList, err := predictEdbRuleDataModel.GetPredictEdbRuleDataList(edbInfoId, configId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  549. if err != nil {
  550. return
  551. }
  552. for _, v := range tmpPredictEdbRuleDataList {
  553. hcDataMap[v.DataTime.Format(utils.FormatDate)] = v.Value
  554. }
  555. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  556. for k, currentDate := range dayList {
  557. // 最近一条数据
  558. tmpLenAllDataList := len(allDataList)
  559. lastValue := allDataList[tmpLenAllDataList-1].Value
  560. // 动态环差值数据
  561. currentDateStr := currentDate.Format(utils.FormatDate)
  562. hcVal, ok := hcDataMap[currentDateStr]
  563. if !ok {
  564. continue
  565. }
  566. lastValueDecimal := decimal.NewFromFloat(lastValue)
  567. hcValDecimal := decimal.NewFromFloat(hcVal)
  568. val, _ := lastValueDecimal.Add(hcValDecimal).RoundCeil(4).Float64()
  569. tmpData := &edbDataModel.EdbDataList{
  570. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  571. EdbInfoId: edbInfoId,
  572. DataTime: currentDateStr,
  573. Value: val,
  574. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  575. }
  576. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  577. allDataList = append(allDataList, tmpData)
  578. existMap[currentDateStr] = val
  579. // 最大最小值
  580. if val < minValue {
  581. minValue = val
  582. }
  583. if val > maxValue {
  584. maxValue = val
  585. }
  586. }
  587. return
  588. }
  589. // GetChartPredictEdbInfoDataListByRuleFinalValueHc 根据 给定终值后插值 规则获取预测数据
  590. // 项目背景:
  591. //假设螺纹产量在2023年1月1号的预测值是255万吨,从当下到2023年1月1号,螺纹产量将会线性变化,那么每一期的螺纹产量是多少?
  592. //算法:从当下(2022/10/28)到2023/1/1号,一共65天,从当前值(305.02)到255,差值-50.02,
  593. //则每日环差为-50.02/65=-0.7695。因为数据点是周度频率,每周环差为,-0.3849*7=-5.3868。
  594. //从以上计算过程可看出,“给定终值后差值”的算法,是在“环差”算法的基础上,做的一个改动。即这个”环差值”=【(终值-最新值)/终值与最新值得日期差】*数据频率
  595. //需求说明:
  596. //1、增加一个预测规则,名为“给定终值后插值”,给定预测截止日期和预测终值,计算最新数据日期至预测截止日期的时间差T,计算最新数据和预测终值的数据差S,数据频率与指标频度有关,日度=1,周度=7,旬度=10,月度=30,季度=90,年度=365,环差值=S/T*频率,预测数值=前一天数值+环差值;
  597. //2、最新数据值和日期改动后,需重新计算环差值和预测数值;
  598. func GetChartPredictEdbInfoDataListByRuleFinalValueHc(edbInfoId int, finalValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
  599. allDataList := make([]*edbDataModel.EdbDataList, 0)
  600. allDataList = append(allDataList, realPredictEdbInfoData...)
  601. allDataList = append(allDataList, predictEdbInfoData...)
  602. newPredictEdbInfoData = predictEdbInfoData
  603. index := len(allDataList)
  604. //获取后面的预测日期
  605. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  606. lenDay := len(dayList)
  607. if lenDay <= 0 {
  608. return
  609. }
  610. var hcValue float64
  611. lastValueDeciamal := decimal.NewFromFloat(allDataList[index-1].Value) // 实际数据的最后一个值
  612. finalValueDeciamal := decimal.NewFromFloat(finalValue) // 给定的终止数据
  613. dayDecimal := decimal.NewFromInt(int64(lenDay)) // 需要作为分母的期数
  614. hcValue, _ = finalValueDeciamal.Sub(lastValueDeciamal).Div(dayDecimal).Float64() // 计算出来的环差值
  615. //获取后面的预测数据
  616. predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
  617. lastK := lenDay - 1 // 最后的日期
  618. for k, currentDate := range dayList {
  619. tmpK := index + k - 1 //上1期的值
  620. var val float64
  621. // 环差别值计算
  622. if k == lastK { //如果是最后一天,那么就用最终值,否则就计算
  623. val = finalValue
  624. } else {
  625. val = HczDiv(allDataList[tmpK].Value, hcValue)
  626. }
  627. currentDateStr := currentDate.Format(utils.FormatDate)
  628. tmpData := &edbDataModel.EdbDataList{
  629. EdbDataId: edbInfoId + 10000000000 + index + k,
  630. EdbInfoId: edbInfoId,
  631. DataTime: currentDateStr,
  632. Value: val,
  633. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  634. }
  635. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  636. allDataList = append(allDataList, tmpData)
  637. existMap[currentDateStr] = val
  638. // 最大最小值
  639. if val < minValue {
  640. minValue = val
  641. }
  642. if val > maxValue {
  643. maxValue = val
  644. }
  645. }
  646. return
  647. }
  648. // SeasonConf 季节性规则的配置
  649. type SeasonConf struct {
  650. Calendar string `description:"公历、农历"`
  651. YearType int `description:"选择方式,1:连续N年;2:指定年份"`
  652. NValue int `description:"连续N年"`
  653. YearList []int `description:"指定年份列表"`
  654. }
  655. // GetChartPredictEdbInfoDataListByRuleSeason 根据 季节性 规则获取预测数据
  656. // ETA预测规则:季节性
  657. //已知选定指标A最近更新日期: 2022-12-6 200
  658. //设置预测截止日期2023-01-06
  659. //1、选择过去N年,N=3
  660. //则过去N年为2021、2020、2019
  661. //指标A日期 实际值 指标A日期
  662. //2019/12/5 150 2019/12/6
  663. //2020/12/5 180 2020/12/6
  664. //2021/12/5 210 2021/12/6
  665. //2019/12/31 200 2020/1/1
  666. //2020/12/31 210 2021/1/1
  667. //2021/12/31 250 2022/1/1
  668. //
  669. //计算12.7预测值,求过去N年环差均值=[(100-150)+(160-180)+(250-210)]/3=-10
  670. //则12.7预测值=12.6值+过去N年环差均值=200-10=190
  671. //以此类推...
  672. //
  673. //计算2023.1.2预测值,求过去N年环差均值=[(300-200)+(220-210)+(260-250)]/3=40
  674. //则2023.1.2预测值=2023.1.1值+过去N年环差均值
  675. func GetChartPredictEdbInfoDataListByRuleSeason(edbInfoId int, yearsList []int, calendar string, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
  676. allDataList := make([]*edbDataModel.EdbDataList, 0)
  677. allDataList = append(allDataList, realPredictEdbInfoData...)
  678. allDataList = append(allDataList, predictEdbInfoData...)
  679. newPredictEdbInfoData = predictEdbInfoData
  680. // 获取每个年份的日期数据需要平移的天数
  681. moveDayMap := make(map[int]int, 0) // 每个年份的春节公历
  682. {
  683. if calendar == "公历" {
  684. for _, year := range yearsList {
  685. moveDayMap[year] = 0 //公历就不平移了
  686. }
  687. } else {
  688. currentDay := time.Now()
  689. if currentDay.Month() >= 11 { //如果大于等于11月份,那么用的是下一年的春节
  690. currentDay = currentDay.AddDate(1, 0, 0)
  691. }
  692. currentYear := currentDay.Year()
  693. currentYearCjnl := fmt.Sprintf("%d-01-01", currentYear) //当年的春节农历
  694. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当年的春节公历
  695. currentYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, currentYearCjgl, time.Local)
  696. if tmpErr != nil {
  697. err = errors.New("当前春节公历日期转换失败:" + tmpErr.Error())
  698. return
  699. }
  700. // 指定的年份
  701. for _, year := range yearsList {
  702. tmpYearCjnl := fmt.Sprintf("%d-01-01", year) //指定年的春节农历
  703. tmpYearCjgl := solarlunar.LunarToSolar(tmpYearCjnl, false) //指定年的春节公历
  704. //moveDayList = append(moveDayList, 0) //公历就不平移了
  705. tmpYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpYearCjgl, time.Local)
  706. if tmpErr != nil {
  707. err = errors.New(fmt.Sprintf("%d公历日期转换失败:%s", year, tmpErr.Error()))
  708. return
  709. }
  710. tmpCurrentYearCjglTime := currentYearCjglTime.AddDate(year-currentYear, 0, 0)
  711. moveDay := utils.GetTimeSubDay(tmpYearCjglTime, tmpCurrentYearCjglTime)
  712. moveDayMap[year] = moveDay //公历平移
  713. }
  714. }
  715. }
  716. index := len(allDataList)
  717. //获取后面的预测日期
  718. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  719. //获取后面的预测数据
  720. predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
  721. for k, currentDate := range dayList {
  722. tmpHistoryVal := decimal.NewFromFloat(0) //往期的差值总和
  723. tmpHistoryValNum := 0 // 往期差值计算的数量
  724. tmpLenAllDataList := len(allDataList)
  725. tmpK := tmpLenAllDataList - 1 //上1期数据的下标
  726. lastDayStr := allDataList[tmpK].DataTime
  727. lastDayVal := allDataList[tmpK].Value
  728. lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, lastDayStr, time.Local)
  729. if tmpErr != nil {
  730. err = errors.New("获取上期日期转换失败:" + tmpErr.Error())
  731. }
  732. for _, year := range yearsList {
  733. moveDay := moveDayMap[year] //需要移动的天数
  734. var tmpHistoryCurrentVal, tmpHistoryLastVal float64
  735. var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
  736. //前几年当日的日期
  737. tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, moveDay)
  738. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  739. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
  740. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  741. tmpHistoryCurrentVal = val
  742. isFindHistoryCurrent = true
  743. break
  744. } else {
  745. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
  746. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  747. tmpHistoryCurrentVal = val
  748. isFindHistoryCurrent = true
  749. break
  750. }
  751. }
  752. }
  753. //前几年上一期的日期
  754. tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, moveDay)
  755. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  756. tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
  757. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  758. tmpHistoryLastVal = val
  759. isFindHistoryLast = true
  760. break
  761. } else {
  762. tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
  763. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  764. tmpHistoryLastVal = val
  765. isFindHistoryLast = true
  766. break
  767. }
  768. }
  769. }
  770. // 如果两个日期对应的数据都找到了,那么计算两期的差值
  771. if isFindHistoryCurrent && isFindHistoryLast {
  772. af := decimal.NewFromFloat(tmpHistoryCurrentVal)
  773. bf := decimal.NewFromFloat(tmpHistoryLastVal)
  774. tmpHistoryVal = tmpHistoryVal.Add(af.Sub(bf))
  775. tmpHistoryValNum++
  776. }
  777. }
  778. //计算的差值与选择的年份数量不一致,那么当前日期不计算
  779. if tmpHistoryValNum != len(yearsList) {
  780. continue
  781. }
  782. lastDayValDec := decimal.NewFromFloat(lastDayVal)
  783. val, _ := tmpHistoryVal.Div(decimal.NewFromInt(int64(tmpHistoryValNum))).Add(lastDayValDec).RoundCeil(4).Float64()
  784. currentDateStr := currentDate.Format(utils.FormatDate)
  785. tmpData := &edbDataModel.EdbDataList{
  786. EdbDataId: edbInfoId + 10000000000 + index + k,
  787. EdbInfoId: edbInfoId,
  788. DataTime: currentDateStr,
  789. Value: val,
  790. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  791. }
  792. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  793. allDataList = append(allDataList, tmpData)
  794. existMap[currentDateStr] = val
  795. // 最大最小值
  796. if val < minValue {
  797. minValue = val
  798. }
  799. if val > maxValue {
  800. maxValue = val
  801. }
  802. }
  803. return
  804. }
  805. // MoveAverageConf 移动平均同比规则的配置
  806. type MoveAverageConf struct {
  807. Year int `description:"指定年份"`
  808. NValue int `description:"N期的数据"`
  809. }
  810. // GetChartPredictEdbInfoDataListByRuleMoveAverageTb 根据 移动平均同比 规则获取预测数据
  811. // ETA预测规则:季节性
  812. //2、选择指定N年,N=3
  813. //指定N年为2012、2015、2018
  814. //指标A日期 实际值 指标A日期 实际值
  815. //2012/12/5 150 2012/12/6 130
  816. //2015/12/5 180 2015/12/6 150
  817. //2018/12/5 210 2018/12/6 260
  818. //2012/12/31 200 2013/1/1 200
  819. //2015/12/31 210 2016/1/1 250
  820. //2018/12/31 250 2019/1/1 270
  821. //计算12.7预测值,求过去N年环差均值=[(130-150)+(150-180)+(290-210)]/3=10
  822. //则12.7预测值=12.6值+过去N年环差均值=200+10=210
  823. //以此类推...
  824. //计算2023.1.2预测值,求过去N年环差均值=[(200-200)+(250-210)+(270-250)]/3=16.67
  825. //则2023.1.2预测值=2023.1.1值+过去N年环差均值
  826. func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, year int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
  827. allDataList := make([]*edbDataModel.EdbDataList, 0)
  828. allDataList = append(allDataList, realPredictEdbInfoData...)
  829. allDataList = append(allDataList, predictEdbInfoData...)
  830. newPredictEdbInfoData = predictEdbInfoData
  831. lenAllData := len(allDataList)
  832. if lenAllData < nValue || lenAllData <= 0 {
  833. return
  834. }
  835. if nValue <= 0 {
  836. return
  837. }
  838. // 分母
  839. decimalN := decimal.NewFromInt(int64(nValue))
  840. //获取后面的预测数据
  841. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  842. for k, currentDate := range dayList {
  843. tmpLenAllDataList := len(allDataList)
  844. tmpIndex := tmpLenAllDataList - 1 //上1期数据的下标
  845. averageDateList := make([]string, 0) //计算平均数的日期
  846. // 数据集合中的最后一个数据
  847. tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
  848. averageDateList = append(averageDateList, allDataList[tmpIndex].DataTime)
  849. for tmpK := 2; tmpK <= nValue; tmpK++ {
  850. tmpIndex2 := tmpIndex - tmpK //上N期的值
  851. tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
  852. tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
  853. averageDateList = append(averageDateList, allDataList[tmpIndex2].DataTime)
  854. }
  855. // 最近的N期平均值
  856. tmpAverageVal := tmpDecimalVal.Div(decimalN)
  857. var tmpHistoryCurrentVal float64 // 前几年当日的数据值
  858. var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
  859. tmpHistoryDecimalVal := decimal.NewFromFloat(0) //前几年N期数据总值
  860. {
  861. // 前几年N期汇总期数
  862. tmpHistoryValNum := 0
  863. {
  864. //前几年当日的日期
  865. tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, 0)
  866. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  867. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
  868. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  869. tmpHistoryCurrentVal = val
  870. isFindHistoryCurrent = true
  871. break
  872. } else {
  873. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
  874. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  875. tmpHistoryCurrentVal = val
  876. isFindHistoryCurrent = true
  877. break
  878. }
  879. }
  880. }
  881. }
  882. for _, averageDate := range averageDateList {
  883. lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, averageDate, time.Local)
  884. if tmpErr != nil {
  885. err = tmpErr
  886. return
  887. }
  888. //前几年上一期的日期
  889. tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, 0)
  890. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  891. tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
  892. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  893. tmpDecimalVal2 := decimal.NewFromFloat(val)
  894. tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
  895. tmpHistoryValNum++
  896. break
  897. } else {
  898. tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
  899. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  900. tmpDecimalVal2 := decimal.NewFromFloat(val)
  901. tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
  902. tmpHistoryValNum++
  903. break
  904. }
  905. }
  906. }
  907. }
  908. // 汇总期数与配置的N期数量一致
  909. if tmpHistoryValNum == nValue {
  910. isFindHistoryLast = true
  911. }
  912. }
  913. // 如果没有找到前几年的汇总数据,或者没有找到前几年当日的数据,那么退出当前循环,进入下一循环
  914. if !isFindHistoryLast || !isFindHistoryCurrent {
  915. continue
  916. }
  917. // 计算最近N期同比值
  918. tbVal := tmpAverageVal.Div(tmpHistoryDecimalVal)
  919. // 预测值结果 = 同比年份同期值(tmpHistoryCurrentVal的值)* 同比值(tbVal的值)
  920. val, _ := decimal.NewFromFloat(tmpHistoryCurrentVal).Mul(tbVal).RoundCeil(4).Float64()
  921. currentDateStr := currentDate.Format(utils.FormatDate)
  922. tmpData := &edbDataModel.EdbDataList{
  923. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  924. EdbInfoId: edbInfoId,
  925. DataTime: currentDateStr,
  926. Value: val,
  927. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  928. }
  929. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  930. allDataList = append(allDataList, tmpData)
  931. existMap[currentDateStr] = val
  932. // 最大最小值
  933. if val < minValue {
  934. minValue = val
  935. }
  936. if val > maxValue {
  937. maxValue = val
  938. }
  939. }
  940. return
  941. }