predict_edb_info_rule.go 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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. "math"
  11. "strings"
  12. "time"
  13. )
  14. // GetChartPredictEdbInfoDataListByRule1 根据规则1获取预测数据
  15. func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList) {
  16. newPredictEdbInfoData = predictEdbInfoData
  17. //获取后面的预测数据
  18. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  19. predictEdbInfoData = make([]*models.EdbDataList, 0)
  20. for k, v := range dayList {
  21. newPredictEdbInfoData = append(newPredictEdbInfoData, &models.EdbDataList{
  22. EdbDataId: edbInfoId + 10000000000 + k,
  23. EdbInfoId: edbInfoId,
  24. DataTime: v.Format(utils.FormatDate),
  25. Value: dataValue,
  26. DataTimestamp: (v.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  27. })
  28. existMap[v.Format(utils.FormatDate)] = dataValue
  29. }
  30. return
  31. }
  32. // GetChartPredictEdbInfoDataListByRuleTb 根据同比值规则获取预测数据
  33. // 2.1 同比: 在未来某一个时间段内,给定一个固定的同比增速a,用去年同期值X乘以同比增速(1+a),得到预测值Y=X(1+a)
  34. // 例: 今年1-3月值,100,100,120。给定同比增速a=0.1,则明年1-3月预测值为: 100*1.1=110,100*1.1=110,120*1.1=132。
  35. 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) {
  36. allDataList := make([]*models.EdbDataList, 0)
  37. allDataList = append(allDataList, realPredictEdbInfoData...)
  38. allDataList = append(allDataList, predictEdbInfoData...)
  39. newPredictEdbInfoData = predictEdbInfoData
  40. index := len(allDataList)
  41. //获取后面的预测数据
  42. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  43. predictEdbInfoData = make([]*models.EdbDataList, 0)
  44. for k, currentDate := range dayList {
  45. tmpData := &models.EdbDataList{
  46. EdbDataId: edbInfoId + 10000000000 + index + k,
  47. EdbInfoId: edbInfoId,
  48. DataTime: currentDate.Format(utils.FormatDate),
  49. //Value: dataValue,
  50. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  51. }
  52. var val float64
  53. var calculateStatus bool //计算结果
  54. //currentItem := existMap[av]
  55. //上一年的日期
  56. preDate := currentDate.AddDate(-1, 0, 0)
  57. preDateStr := preDate.Format(utils.FormatDate)
  58. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  59. val = TbzDiv(preValue, tbValue)
  60. calculateStatus = true
  61. } else {
  62. switch frequency {
  63. case "月度":
  64. //向上和向下,各找一个月
  65. nextDateDay := preDate
  66. preDateDay := preDate
  67. for i := 0; i <= 35; i++ {
  68. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  69. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  70. val = TbzDiv(preValue, tbValue)
  71. calculateStatus = true
  72. break
  73. } else {
  74. preDateDayStr := preDateDay.Format(utils.FormatDate)
  75. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  76. val = TbzDiv(preValue, tbValue)
  77. calculateStatus = true
  78. break
  79. }
  80. }
  81. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  82. preDateDay = preDateDay.AddDate(0, 0, -1)
  83. }
  84. case "季度", "年度":
  85. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  86. val = TbzDiv(preValue, tbValue)
  87. calculateStatus = true
  88. break
  89. }
  90. default:
  91. nextDateDay := preDate
  92. preDateDay := preDate
  93. for i := 0; i < 35; i++ {
  94. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  95. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  96. val = TbzDiv(preValue, tbValue)
  97. calculateStatus = true
  98. break
  99. } else {
  100. preDateDayStr := preDateDay.Format(utils.FormatDate)
  101. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  102. val = TbzDiv(preValue, tbValue)
  103. calculateStatus = true
  104. break
  105. } else {
  106. //fmt.Println("pre not find:", preDateStr, "i:", i)
  107. }
  108. }
  109. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  110. preDateDay = preDateDay.AddDate(0, 0, -1)
  111. }
  112. }
  113. }
  114. if calculateStatus {
  115. tmpData.Value = val
  116. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  117. allDataList = append(allDataList, tmpData)
  118. existMap[tmpData.DataTime] = val
  119. // 最大最小值
  120. if val < minValue {
  121. minValue = val
  122. }
  123. if val > maxValue {
  124. maxValue = val
  125. }
  126. }
  127. }
  128. return
  129. }
  130. // TbzDiv 同比值计算
  131. // @params a float64 去年同期值
  132. // @params b float64 固定同比增速
  133. func TbzDiv(a, b float64) (result float64) {
  134. if b != 0 {
  135. // 去年同期值
  136. af := decimal.NewFromFloat(a)
  137. // 同比增速
  138. bf := decimal.NewFromFloat(b)
  139. // 默认1
  140. cf := decimal.NewFromFloat(1)
  141. // 总增速
  142. val := bf.Add(cf)
  143. // 计算
  144. result, _ = val.Mul(af).RoundCeil(4).Float64()
  145. } else {
  146. result = 0
  147. }
  148. return
  149. }
  150. // GetChartPredictEdbInfoDataListByRuleTc 根据同差值规则获取预测数据
  151. // 2.2 同差: 在未来某一个时间段内,给定一个固定的同比增加值a,用去年同期值X加上同比增加值A,得到预测值Y=X+a
  152. // 例: 今年1-3月值,100,100,120。给定同比增加值a=10,则明年1-3月预测值为: 100+10=110,100+10=110,120+10=130
  153. 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) {
  154. allDataList := make([]*models.EdbDataList, 0)
  155. allDataList = append(allDataList, realPredictEdbInfoData...)
  156. allDataList = append(allDataList, predictEdbInfoData...)
  157. newPredictEdbInfoData = predictEdbInfoData
  158. index := len(allDataList)
  159. //获取后面的预测数据
  160. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  161. predictEdbInfoData = make([]*models.EdbDataList, 0)
  162. for k, currentDate := range dayList {
  163. tmpData := &models.EdbDataList{
  164. EdbDataId: edbInfoId + 10000000000 + index + k,
  165. EdbInfoId: edbInfoId,
  166. DataTime: currentDate.Format(utils.FormatDate),
  167. //Value: dataValue,
  168. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  169. }
  170. var val float64
  171. var calculateStatus bool //计算结果
  172. //currentItem := existMap[av]
  173. //上一年的日期
  174. preDate := currentDate.AddDate(-1, 0, 0)
  175. preDateStr := preDate.Format(utils.FormatDate)
  176. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  177. val = TczDiv(preValue, tcValue)
  178. calculateStatus = true
  179. } else {
  180. switch frequency {
  181. case "月度":
  182. //向上和向下,各找一个月
  183. nextDateDay := preDate
  184. preDateDay := preDate
  185. for i := 0; i <= 35; i++ {
  186. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  187. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  188. val = TczDiv(preValue, tcValue)
  189. calculateStatus = true
  190. break
  191. } else {
  192. preDateDayStr := preDateDay.Format(utils.FormatDate)
  193. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  194. val = TczDiv(preValue, tcValue)
  195. calculateStatus = true
  196. break
  197. }
  198. }
  199. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  200. preDateDay = preDateDay.AddDate(0, 0, -1)
  201. }
  202. case "季度", "年度":
  203. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  204. val = TczDiv(preValue, tcValue)
  205. calculateStatus = true
  206. break
  207. }
  208. default:
  209. nextDateDay := preDate
  210. preDateDay := preDate
  211. for i := 0; i < 35; i++ {
  212. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  213. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  214. val = TczDiv(preValue, tcValue)
  215. calculateStatus = true
  216. break
  217. } else {
  218. preDateDayStr := preDateDay.Format(utils.FormatDate)
  219. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  220. val = TczDiv(preValue, tcValue)
  221. calculateStatus = true
  222. break
  223. } else {
  224. //fmt.Println("pre not find:", preDateStr, "i:", i)
  225. }
  226. }
  227. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  228. preDateDay = preDateDay.AddDate(0, 0, -1)
  229. }
  230. }
  231. }
  232. if calculateStatus {
  233. tmpData.Value = val
  234. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  235. allDataList = append(allDataList, tmpData)
  236. existMap[tmpData.DataTime] = val
  237. // 最大最小值
  238. if val < minValue {
  239. minValue = val
  240. }
  241. if val > maxValue {
  242. maxValue = val
  243. }
  244. }
  245. }
  246. return
  247. }
  248. // TczDiv 环差值计算
  249. // @params a float64 上一期值
  250. // @params b float64 固定的环比增加值
  251. func TczDiv(a, b float64) (result float64) {
  252. if b != 0 {
  253. // 上一期值
  254. af := decimal.NewFromFloat(a)
  255. // 固定的环比增加值
  256. bf := decimal.NewFromFloat(b)
  257. // 计算
  258. result, _ = af.Add(bf).RoundCeil(4).Float64()
  259. } else {
  260. result = 0
  261. }
  262. return
  263. }
  264. // GetChartPredictEdbInfoDataListByRuleHb 根据环比值规则获取预测数据
  265. // 环比:在未来某一个时间段内,给定一个固定的环比增速a,用上一期值X乘以环比增速(1+a),得到预测值Y=X(1+a)
  266. // 例: 最近1期值为100,给定环比增速a=0.2,则未来3期预测值为: 100*1.2=120,120*1.2=144,144*1.2=172.8
  267. 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) {
  268. allDataList := make([]*models.EdbDataList, 0)
  269. allDataList = append(allDataList, realPredictEdbInfoData...)
  270. allDataList = append(allDataList, predictEdbInfoData...)
  271. newPredictEdbInfoData = predictEdbInfoData
  272. index := len(allDataList)
  273. //获取后面的预测数据
  274. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  275. for k, currentDate := range dayList {
  276. tmpK := index + k - 1 //上1期的值
  277. // 环比值计算
  278. val := HbzDiv(allDataList[tmpK].Value, hbValue)
  279. currentDateStr := currentDate.Format(utils.FormatDate)
  280. tmpData := &models.EdbDataList{
  281. EdbDataId: edbInfoId + 10000000000 + index + k,
  282. EdbInfoId: edbInfoId,
  283. DataTime: currentDateStr,
  284. Value: val,
  285. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  286. }
  287. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  288. allDataList = append(allDataList, tmpData)
  289. existMap[currentDateStr] = val
  290. // 最大最小值
  291. if val < minValue {
  292. minValue = val
  293. }
  294. if val > maxValue {
  295. maxValue = val
  296. }
  297. }
  298. return
  299. }
  300. // HbzDiv 环比值计算
  301. // @params a float64 上一期值
  302. // @params b float64 固定的环比增速
  303. func HbzDiv(a, b float64) (result float64) {
  304. if b != 0 {
  305. // 上一期值
  306. af := decimal.NewFromFloat(a)
  307. // 固定的环比增速
  308. bf := decimal.NewFromFloat(b)
  309. // 默认1
  310. cf := decimal.NewFromFloat(1)
  311. // 总增速
  312. val := bf.Add(cf)
  313. // 计算
  314. result, _ = val.Mul(af).RoundCeil(4).Float64()
  315. } else {
  316. result = 0
  317. }
  318. return
  319. }
  320. // GetChartPredictEdbInfoDataListByRuleHc 根据环差值规则获取预测数据
  321. // 2.4 环差:在未来某一个时间段内,给定一个固定的环比增加值a,用上一期值X加上环比增加值a,得到预测值Y=X+a
  322. // 例: 最近1期值为100,给定环比增加值a=10,则未来3期预测值为: 100+10=110,110+10=120,120+10=130
  323. 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) {
  324. allDataList := make([]*models.EdbDataList, 0)
  325. allDataList = append(allDataList, realPredictEdbInfoData...)
  326. allDataList = append(allDataList, predictEdbInfoData...)
  327. newPredictEdbInfoData = predictEdbInfoData
  328. index := len(allDataList)
  329. //获取后面的预测数据
  330. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  331. for k, currentDate := range dayList {
  332. tmpK := index + k - 1 //上1期的值
  333. // 环差别值计算
  334. val := HczDiv(allDataList[tmpK].Value, hcValue)
  335. currentDateStr := currentDate.Format(utils.FormatDate)
  336. tmpData := &models.EdbDataList{
  337. EdbDataId: edbInfoId + 10000000000 + index + k,
  338. EdbInfoId: edbInfoId,
  339. DataTime: currentDateStr,
  340. Value: val,
  341. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  342. }
  343. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  344. allDataList = append(allDataList, tmpData)
  345. existMap[currentDateStr] = val
  346. // 最大最小值
  347. if val < minValue {
  348. minValue = val
  349. }
  350. if val > maxValue {
  351. maxValue = val
  352. }
  353. }
  354. return
  355. }
  356. // HczDiv 环差值计算
  357. // @params a float64 上一期值
  358. // @params b float64 固定的环比增加值
  359. func HczDiv(a, b float64) (result float64) {
  360. if b != 0 {
  361. // 上一期值
  362. af := decimal.NewFromFloat(a)
  363. // 固定的环比增加值
  364. bf := decimal.NewFromFloat(b)
  365. // 计算
  366. result, _ = af.Add(bf).RoundCeil(4).Float64()
  367. } else {
  368. result = 0
  369. }
  370. return
  371. }
  372. // GetChartPredictEdbInfoDataListByRuleNMoveMeanValue 根据N期移动均值规则获取预测数据
  373. // 2.5 N期移动均值:在未来某一个时间段内,下一期值等于过去N期值得平均值。
  374. // 例:最近3期值(N=3),为95,98,105则未来第1期值为 1/3*(95+98+105)=99.33, 未来第2期值为 1/3*(98+105+99.33)=100.78依次类推。
  375. 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) {
  376. allDataList := make([]*models.EdbDataList, 0)
  377. allDataList = append(allDataList, realPredictEdbInfoData...)
  378. allDataList = append(allDataList, predictEdbInfoData...)
  379. newPredictEdbInfoData = predictEdbInfoData
  380. lenAllData := len(allDataList)
  381. if lenAllData < nValue || lenAllData <= 0 {
  382. return
  383. }
  384. if nValue <= 0 {
  385. return
  386. }
  387. // 分母
  388. decimalN := decimal.NewFromInt(int64(nValue))
  389. //获取后面的预测数据
  390. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  391. for k, currentDate := range dayList {
  392. tmpIndex := lenAllData + k - 1 //上1期的值
  393. // 数据集合中的最后一个数据
  394. tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
  395. for tmpK := 2; tmpK <= nValue; tmpK++ {
  396. tmpIndex2 := tmpIndex - tmpK //上N期的值
  397. tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
  398. tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
  399. }
  400. // N期移动均值计算
  401. val, _ := tmpDecimalVal.Div(decimalN).RoundCeil(4).Float64()
  402. currentDateStr := currentDate.Format(utils.FormatDate)
  403. tmpData := &models.EdbDataList{
  404. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  405. EdbInfoId: edbInfoId,
  406. DataTime: currentDateStr,
  407. Value: val,
  408. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  409. }
  410. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  411. allDataList = append(allDataList, tmpData)
  412. existMap[currentDateStr] = val
  413. // 最大最小值
  414. if val < minValue {
  415. minValue = val
  416. }
  417. if val > maxValue {
  418. maxValue = val
  419. }
  420. }
  421. return
  422. }
  423. // GetChartPredictEdbInfoDataListByRuleNLinearRegression 根据N期移动均值规则获取预测数据
  424. // 2.6N期段线性外推值:给出过去N期值所确定的线性回归方程(Y=aX+b)在未来一段时间内的推算值。回归方程虽然比较复杂,但各种编程语言应该都有现成的模块或函数,应该无需自己编写。
  425. // 例1:过去5期值(N=5)分别为:3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:13,15,17。
  426. //
  427. // 例2:过去6期值(N=6)分别为:3,3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:12.33,14.05,15.76。例1和例2的区别在于,多加了一期数据,导致回归方程发生改变,从而预测值不同。
  428. 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) {
  429. //var errMsg string
  430. //defer func() {
  431. // if errMsg != `` {
  432. // go alarm_msg.SendAlarmMsg("更新上海的token失败;ERR:"+err.Error(), 3)
  433. // }
  434. //}()
  435. allDataList := make([]*models.EdbDataList, 0)
  436. allDataList = append(allDataList, realPredictEdbInfoData...)
  437. allDataList = append(allDataList, predictEdbInfoData...)
  438. newPredictEdbInfoData = predictEdbInfoData
  439. lenAllData := len(allDataList)
  440. if lenAllData < nValue || lenAllData <= 0 {
  441. return
  442. }
  443. if nValue <= 1 {
  444. return
  445. }
  446. //获取后面的预测数据
  447. // 获取线性方程公式的a、b的值
  448. coordinateData := make([]Coordinate, 0)
  449. for tmpK := nValue; tmpK > 0; tmpK-- {
  450. tmpIndex2 := lenAllData - tmpK //上N期的值
  451. tmpCoordinate := Coordinate{
  452. X: float64(nValue - tmpK + 1),
  453. Y: allDataList[tmpIndex2].Value,
  454. }
  455. coordinateData = append(coordinateData, tmpCoordinate)
  456. }
  457. a, b := getLinearResult(coordinateData)
  458. //fmt.Println("a:", a, ";======b:", b)
  459. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  460. for k, currentDate := range dayList {
  461. tmpK := nValue + k + 1
  462. aDecimal := decimal.NewFromFloat(a)
  463. xDecimal := decimal.NewFromInt(int64(tmpK))
  464. bDecimal := decimal.NewFromFloat(b)
  465. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
  466. currentDateStr := currentDate.Format(utils.FormatDate)
  467. tmpData := &models.EdbDataList{
  468. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  469. EdbInfoId: edbInfoId,
  470. DataTime: currentDateStr,
  471. Value: val,
  472. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  473. }
  474. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  475. allDataList = append(allDataList, tmpData)
  476. existMap[currentDateStr] = val
  477. // 最大最小值
  478. if val < minValue {
  479. minValue = val
  480. }
  481. if val > maxValue {
  482. maxValue = val
  483. }
  484. }
  485. return
  486. }
  487. // Series is a container for a series of data
  488. type Series []Coordinate
  489. // Coordinate holds the data in a series
  490. type Coordinate struct {
  491. X, Y float64
  492. }
  493. func getLinearResult(s []Coordinate) (gradient, intercept float64) {
  494. if len(s) <= 1 {
  495. return
  496. }
  497. // Placeholder for the math to be done
  498. var sum [5]float64
  499. // Loop over data keeping index in place
  500. i := 0
  501. for ; i < len(s); i++ {
  502. sum[0] += s[i].X
  503. sum[1] += s[i].Y
  504. sum[2] += s[i].X * s[i].X
  505. sum[3] += s[i].X * s[i].Y
  506. sum[4] += s[i].Y * s[i].Y
  507. }
  508. // Find gradient and intercept
  509. f := float64(i)
  510. gradient = (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
  511. intercept = (sum[1] / f) - (gradient * sum[0] / f)
  512. //fmt.Println("gradient:", gradient, ";intercept:", intercept)
  513. // Create the new regression series
  514. //for j := 0; j < len(s); j++ {
  515. // regressions = append(regressions, Coordinate{
  516. // X: s[j].X,
  517. // Y: s[j].X*gradient + intercept,
  518. // })
  519. //}
  520. return
  521. }
  522. // GetChartPredictEdbInfoDataListByRuleTrendsHC 根据动态环比增加值的计算规则获取预测数据
  523. //
  524. // 研究员有对预测指标进行动态环差计算的需求,即预测指标使用环差规则进行预测时,环比增加值不是固定值,而是由几个预测指标计算得出的动态变化的值;
  525. // 需求说明:
  526. // 1、增加“动态环差”预测规则;
  527. // 2、环比增加值在弹窗设置;
  528. // 3、动态环差预测举例:
  529. // 指标A实际最新数据为2022-10-27(100);
  530. // 预测指标B预测数据为2022-10-28(240)、2022-10-29(300);
  531. // 预测指标C预测数据为2022-10-28(260)、2022-10-29(310);
  532. // 计算公式为B-C;
  533. // 则指标A至2022-10-29的预测值为2022-10-28(100+(240-260)=80)、2022-10-29(80+(300-310)=90);
  534. // 注:动态环比增加值的计算遵从计算指标的计算规则,即用于计算的指标若有部分指标缺少部分日期数据,则这部分日期数据不做计算,为空;若动态环比增加值某一天为空,则往前追溯最近一期有值的环比增加值作为该天的数值参与计算;
  535. 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) {
  536. allDataList := make([]*models.EdbDataList, 0)
  537. allDataList = append(allDataList, realPredictEdbInfoData...)
  538. allDataList = append(allDataList, predictEdbInfoData...)
  539. newPredictEdbInfoData = predictEdbInfoData
  540. lenAllData := len(allDataList)
  541. if lenAllData <= 0 {
  542. return
  543. }
  544. hcDataMap := make(map[string]float64) //规则计算的环差值map
  545. //已经生成的动态数据
  546. tmpPredictEdbRuleDataList, err := data_manage.GetPredictEdbRuleDataList(edbInfoId, configId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  547. if err != nil {
  548. return
  549. }
  550. for _, v := range tmpPredictEdbRuleDataList {
  551. hcDataMap[v.DataTime] = v.Value
  552. }
  553. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  554. for k, currentDate := range dayList {
  555. // 最近一条数据
  556. tmpLenAllDataList := len(allDataList)
  557. lastValue := allDataList[tmpLenAllDataList-1].Value
  558. // 动态环差值数据
  559. currentDateStr := currentDate.Format(utils.FormatDate)
  560. hcVal, ok := hcDataMap[currentDateStr]
  561. if !ok {
  562. continue
  563. }
  564. lastValueDecimal := decimal.NewFromFloat(lastValue)
  565. hcValDecimal := decimal.NewFromFloat(hcVal)
  566. val, _ := lastValueDecimal.Add(hcValDecimal).RoundCeil(4).Float64()
  567. tmpData := &models.EdbDataList{
  568. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  569. EdbInfoId: edbInfoId,
  570. DataTime: currentDateStr,
  571. Value: val,
  572. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  573. }
  574. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  575. allDataList = append(allDataList, tmpData)
  576. existMap[currentDateStr] = val
  577. // 最大最小值
  578. if val < minValue {
  579. minValue = val
  580. }
  581. if val > maxValue {
  582. maxValue = val
  583. }
  584. }
  585. return
  586. }
  587. // GetChartPredictEdbInfoDataListByRuleFinalValueHc 根据 给定终值后插值 规则获取预测数据
  588. //
  589. // 项目背景:
  590. // 假设螺纹产量在2023年1月1号的预测值是255万吨,从当下到2023年1月1号,螺纹产量将会线性变化,那么每一期的螺纹产量是多少?
  591. // 算法:从当下(2022/10/28)到2023/1/1号,一共65天,从当前值(305.02)到255,差值-50.02,
  592. // 则每日环差为-50.02/65=-0.7695。因为数据点是周度频率,每周环差为,-0.3849*7=-5.3868。
  593. // 从以上计算过程可看出,“给定终值后差值”的算法,是在“环差”算法的基础上,做的一个改动。即这个”环差值”=【(终值-最新值)/终值与最新值得日期差】*数据频率
  594. // 需求说明:
  595. // 1、增加一个预测规则,名为“给定终值后插值”,给定预测截止日期和预测终值,计算最新数据日期至预测截止日期的时间差T,计算最新数据和预测终值的数据差S,数据频率与指标频度有关,日度=1,周度=7,旬度=10,月度=30,季度=90,年度=365,环差值=S/T*频率,预测数值=前一天数值+环差值;
  596. // 2、最新数据值和日期改动后,需重新计算环差值和预测数值;
  597. 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) {
  598. allDataList := make([]*models.EdbDataList, 0)
  599. allDataList = append(allDataList, realPredictEdbInfoData...)
  600. allDataList = append(allDataList, predictEdbInfoData...)
  601. newPredictEdbInfoData = predictEdbInfoData
  602. index := len(allDataList)
  603. //获取后面的预测日期
  604. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  605. lenDay := len(dayList)
  606. if lenDay <= 0 {
  607. return
  608. }
  609. var hcValue float64
  610. lastValueDeciamal := decimal.NewFromFloat(allDataList[index-1].Value) // 实际数据的最后一个值
  611. finalValueDeciamal := decimal.NewFromFloat(finalValue) // 给定的终止数据
  612. dayDecimal := decimal.NewFromInt(int64(lenDay)) // 需要作为分母的期数
  613. hcValue, _ = finalValueDeciamal.Sub(lastValueDeciamal).Div(dayDecimal).Float64() // 计算出来的环差值
  614. //获取后面的预测数据
  615. predictEdbInfoData = make([]*models.EdbDataList, 0)
  616. lastK := lenDay - 1 // 最后的日期
  617. for k, currentDate := range dayList {
  618. tmpK := index + k - 1 //上1期的值
  619. var val float64
  620. // 环差别值计算
  621. if k == lastK { //如果是最后一天,那么就用最终值,否则就计算
  622. val = finalValue
  623. } else {
  624. val = HczDiv(allDataList[tmpK].Value, hcValue)
  625. }
  626. currentDateStr := currentDate.Format(utils.FormatDate)
  627. tmpData := &models.EdbDataList{
  628. EdbDataId: edbInfoId + 10000000000 + index + k,
  629. EdbInfoId: edbInfoId,
  630. DataTime: currentDateStr,
  631. Value: val,
  632. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  633. }
  634. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  635. allDataList = append(allDataList, tmpData)
  636. existMap[currentDateStr] = val
  637. // 最大最小值
  638. if val < minValue {
  639. minValue = val
  640. }
  641. if val > maxValue {
  642. maxValue = val
  643. }
  644. }
  645. return
  646. }
  647. // SeasonConf 季节性规则的配置
  648. type SeasonConf struct {
  649. Calendar string `description:"公历、农历"`
  650. YearType int `description:"选择方式,1:连续N年;2:指定年份"`
  651. NValue int `description:"连续N年"`
  652. YearList []int `description:"指定年份列表"`
  653. }
  654. // GetChartPredictEdbInfoDataListByRuleSeason 根据 季节性 规则获取预测数据
  655. //
  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 []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64, err error) {
  676. allDataList := make([]*models.EdbDataList, 0)
  677. allDataList = append(allDataList, realPredictEdbInfoData...)
  678. allDataList = append(allDataList, predictEdbInfoData...)
  679. newPredictEdbInfoData = predictEdbInfoData
  680. // 插值法数据处理
  681. handleDataMap := make(map[string]float64)
  682. err = HandleDataByLinearRegression(allDataList, handleDataMap)
  683. if err != nil {
  684. return
  685. }
  686. // 获取每个年份的日期数据需要平移的天数
  687. moveDayMap := make(map[int]int, 0) // 每个年份的春节公历
  688. {
  689. if calendar == "公历" {
  690. for _, year := range yearsList {
  691. moveDayMap[year] = 0 //公历就不平移了
  692. }
  693. } else {
  694. currentDay := time.Now()
  695. if currentDay.Month() >= 11 { //如果大于等于11月份,那么用的是下一年的春节
  696. currentDay = currentDay.AddDate(1, 0, 0)
  697. }
  698. currentYear := currentDay.Year()
  699. currentYearCjnl := fmt.Sprintf("%d-01-01", currentYear) //当年的春节农历
  700. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当年的春节公历
  701. currentYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, currentYearCjgl, time.Local)
  702. if tmpErr != nil {
  703. err = errors.New("当前春节公历日期转换失败:" + tmpErr.Error())
  704. return
  705. }
  706. // 指定的年份
  707. for _, year := range yearsList {
  708. tmpYearCjnl := fmt.Sprintf("%d-01-01", year) //指定年的春节农历
  709. tmpYearCjgl := solarlunar.LunarToSolar(tmpYearCjnl, false) //指定年的春节公历
  710. //moveDayList = append(moveDayList, 0) //公历就不平移了
  711. tmpYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpYearCjgl, time.Local)
  712. if tmpErr != nil {
  713. err = errors.New(fmt.Sprintf("%d公历日期转换失败:%s", year, tmpErr.Error()))
  714. return
  715. }
  716. tmpCurrentYearCjglTime := currentYearCjglTime.AddDate(year-currentYear, 0, 0)
  717. moveDay := utils.GetTimeSubDay(tmpYearCjglTime, tmpCurrentYearCjglTime)
  718. moveDayMap[year] = moveDay //公历平移
  719. }
  720. }
  721. }
  722. index := len(allDataList)
  723. //获取后面的预测日期
  724. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  725. //获取后面的预测数据
  726. predictEdbInfoData = make([]*models.EdbDataList, 0)
  727. for k, currentDate := range dayList {
  728. // 如果遇到闰二月,如2.29,去掉该天数据
  729. if strings.Contains(currentDate.Format(utils.FormatDate), "02-29") {
  730. continue
  731. }
  732. tmpHistoryVal := decimal.NewFromFloat(0) //往期的差值总和
  733. tmpHistoryValNum := 0 // 往期差值计算的数量
  734. tmpLenAllDataList := len(allDataList)
  735. tmpK := tmpLenAllDataList - 1 //上1期数据的下标
  736. lastDayData := allDataList[tmpK] // 上1期的数据
  737. lastDayStr := lastDayData.DataTime
  738. lastDayVal := lastDayData.Value
  739. lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, lastDayStr, time.Local)
  740. if tmpErr != nil {
  741. err = errors.New("获取上期日期转换失败:" + tmpErr.Error())
  742. }
  743. for _, year := range yearsList {
  744. moveDay := moveDayMap[year] //需要移动的天数
  745. var tmpHistoryCurrentVal, tmpHistoryLastVal float64
  746. var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
  747. //前几年当日的日期
  748. tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, -moveDay)
  749. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  750. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
  751. if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
  752. tmpHistoryCurrentVal = val
  753. isFindHistoryCurrent = true
  754. break
  755. } else {
  756. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
  757. if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
  758. tmpHistoryCurrentVal = val
  759. isFindHistoryCurrent = true
  760. break
  761. }
  762. }
  763. }
  764. //前几年上一期的日期
  765. tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, -moveDay)
  766. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  767. tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
  768. if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
  769. tmpHistoryLastVal = val
  770. isFindHistoryLast = true
  771. break
  772. } else {
  773. tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
  774. if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
  775. tmpHistoryLastVal = val
  776. isFindHistoryLast = true
  777. break
  778. }
  779. }
  780. }
  781. // 如果两个日期对应的数据都找到了,那么计算两期的差值
  782. if isFindHistoryCurrent && isFindHistoryLast {
  783. af := decimal.NewFromFloat(tmpHistoryCurrentVal)
  784. bf := decimal.NewFromFloat(tmpHistoryLastVal)
  785. tmpHistoryVal = tmpHistoryVal.Add(af.Sub(bf))
  786. tmpHistoryValNum++
  787. }
  788. }
  789. //计算的差值与选择的年份数量不一致,那么当前日期不计算
  790. if tmpHistoryValNum != len(yearsList) {
  791. continue
  792. }
  793. lastDayValDec := decimal.NewFromFloat(lastDayVal)
  794. val, _ := tmpHistoryVal.Div(decimal.NewFromInt(int64(tmpHistoryValNum))).Add(lastDayValDec).RoundCeil(4).Float64()
  795. currentDateStr := currentDate.Format(utils.FormatDate)
  796. tmpData := &models.EdbDataList{
  797. EdbDataId: edbInfoId + 10000000000 + index + k,
  798. EdbInfoId: edbInfoId,
  799. DataTime: currentDateStr,
  800. Value: val,
  801. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  802. }
  803. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  804. allDataList = append(allDataList, tmpData)
  805. existMap[currentDateStr] = val
  806. // 继续使用插值法补充新预测日期的数据之间的值
  807. err = HandleDataByLinearRegression([]*models.EdbDataList{
  808. lastDayData, tmpData,
  809. }, handleDataMap)
  810. if err != nil {
  811. return
  812. }
  813. // 最大最小值
  814. if val < minValue {
  815. minValue = val
  816. }
  817. if val > maxValue {
  818. maxValue = val
  819. }
  820. }
  821. return
  822. }
  823. // MoveAverageConf 移动平均同比规则的配置
  824. type MoveAverageConf struct {
  825. Year int `description:"指定年份"`
  826. NValue int `description:"N期的数据"`
  827. }
  828. // GetChartPredictEdbInfoDataListByRuleMoveAverageTb 根据 移动平均同比 规则获取预测数据
  829. //
  830. // ETA预测规则:季节性
  831. // 2、选择指定N年,N=3
  832. // 指定N年为2012、2015、2018
  833. // 指标A日期 实际值 指标A日期 实际值
  834. // 2012/12/5 150 2012/12/6 130
  835. // 2015/12/5 180 2015/12/6 150
  836. // 2018/12/5 210 2018/12/6 260
  837. // 2012/12/31 200 2013/1/1 200
  838. // 2015/12/31 210 2016/1/1 250
  839. // 2018/12/31 250 2019/1/1 270
  840. // 计算12.7预测值,求过去N年环差均值=[(130-150)+(150-180)+(290-210)]/3=10
  841. // 则12.7预测值=12.6值+过去N年环差均值=200+10=210
  842. // 以此类推...
  843. // 计算2023.1.2预测值,求过去N年环差均值=[(200-200)+(250-210)+(270-250)]/3=16.67
  844. // 则2023.1.2预测值=2023.1.1值+过去N年环差均值
  845. 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) {
  846. allDataList := make([]*models.EdbDataList, 0)
  847. allDataList = append(allDataList, realPredictEdbInfoData...)
  848. allDataList = append(allDataList, predictEdbInfoData...)
  849. newPredictEdbInfoData = predictEdbInfoData
  850. lenAllData := len(allDataList)
  851. if lenAllData < nValue || lenAllData <= 0 {
  852. return
  853. }
  854. if nValue <= 0 {
  855. return
  856. }
  857. // 分母
  858. decimalN := decimal.NewFromInt(int64(nValue))
  859. //获取后面的预测数据
  860. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  861. if len(dayList) <= 0 {
  862. return
  863. }
  864. // 需要减去的年份
  865. subYear := year - dayList[0].Year()
  866. for k, currentDate := range dayList {
  867. tmpLenAllDataList := len(allDataList)
  868. tmpIndex := tmpLenAllDataList - 1 //上1期数据的下标
  869. averageDateList := make([]string, 0) //计算平均数的日期
  870. // 数据集合中的最后一个数据
  871. tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
  872. averageDateList = append(averageDateList, allDataList[tmpIndex].DataTime)
  873. for tmpK := 1; tmpK < nValue; tmpK++ {
  874. tmpIndex2 := tmpIndex - tmpK //上N期的值
  875. tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
  876. tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
  877. averageDateList = append(averageDateList, allDataList[tmpIndex2].DataTime)
  878. }
  879. // 最近的N期平均值
  880. tmpAverageVal := tmpDecimalVal.Div(decimalN)
  881. var tmpHistoryCurrentVal float64 // 前几年当日的数据值
  882. var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
  883. tmpHistoryDecimalVal := decimal.NewFromFloat(0) //前几年N期数据总值
  884. {
  885. // 前几年N期汇总期数
  886. tmpHistoryValNum := 0
  887. {
  888. //前几年当日的日期
  889. tmpHistoryCurrentDate := currentDate.AddDate(subYear, 0, 0)
  890. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  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. } else {
  897. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
  898. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  899. tmpHistoryCurrentVal = val
  900. isFindHistoryCurrent = true
  901. break
  902. }
  903. }
  904. }
  905. }
  906. for _, averageDate := range averageDateList {
  907. lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, averageDate, time.Local)
  908. if tmpErr != nil {
  909. err = tmpErr
  910. return
  911. }
  912. //前几年上一期的日期
  913. tmpHistoryLastDate := lastDay.AddDate(subYear, 0, 0)
  914. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  915. tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
  916. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  917. tmpDecimalVal2 := decimal.NewFromFloat(val)
  918. tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
  919. tmpHistoryValNum++
  920. break
  921. } else {
  922. tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
  923. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  924. tmpDecimalVal2 := decimal.NewFromFloat(val)
  925. tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
  926. tmpHistoryValNum++
  927. break
  928. }
  929. }
  930. }
  931. }
  932. // 汇总期数与配置的N期数量一致
  933. if tmpHistoryValNum == nValue {
  934. isFindHistoryLast = true
  935. }
  936. }
  937. // 如果没有找到前几年的汇总数据,或者没有找到前几年当日的数据,那么退出当前循环,进入下一循环
  938. if !isFindHistoryLast || !isFindHistoryCurrent {
  939. continue
  940. }
  941. // 计算最近N期平均值
  942. tmpHistoryAverageVal := tmpHistoryDecimalVal.Div(decimalN)
  943. // 计算最近N期同比值
  944. tbVal := tmpAverageVal.Div(tmpHistoryAverageVal)
  945. // 预测值结果 = 同比年份同期值(tmpHistoryCurrentVal的值)* 同比值(tbVal的值)
  946. val, _ := decimal.NewFromFloat(tmpHistoryCurrentVal).Mul(tbVal).RoundCeil(4).Float64()
  947. currentDateStr := currentDate.Format(utils.FormatDate)
  948. tmpData := &models.EdbDataList{
  949. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  950. EdbInfoId: edbInfoId,
  951. DataTime: currentDateStr,
  952. Value: val,
  953. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  954. }
  955. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  956. allDataList = append(allDataList, tmpData)
  957. existMap[currentDateStr] = val
  958. // 最大最小值
  959. if val < minValue {
  960. minValue = val
  961. }
  962. if val > maxValue {
  963. maxValue = val
  964. }
  965. }
  966. return
  967. }
  968. // GetChartPredictEdbInfoDataListByRuleTbzscz 根据 同比增速差值 规则获取预测数据
  969. // 同比增速差值计算方式:
  970. // 1、首先计算出所选指标实际最新日期值的同比增速:(本期数值-同期数值)÷同期数值*100%
  971. // 2、根据预测截止日期的同比增速终值、最新日期值的同比增速、与最新日期距离截止日期的期数,计算出到截止日期为止的每一期的同比增速。(等差规则计算每一期的同比增速,结合去年同期值,计算出每一期的同比预测值)。公差=(末项-首项)÷(n-1),an=a1+(n-1)d,(n为正整数,n大于等于2)
  972. // 3、根据去年同期值和未来每一期的同比增速值,求出同比预测值,同比预测值=同期值*(1+同比增速)
  973. // 同比增速差值:计算最新数据的同比增速((本期数值-同期数值)÷同期数值*100%),结合同比增速终值与期数,计算每一期同比增速,进而求出同比预测值。
  974. //
  975. // 例:如上图所示指标,(1)最新日期值2022-12-31 141175 ,结合同期值,计算同比增速;
  976. // (2)同比增速终值,若为50%, 预测日期为2023-03-31,则根据(1)中的同比增速值与同比增速终值,计算出中间两期的同比增速;
  977. // (3)求出每一期的预测同比值,预测同比值=同期值*(1+同比增速)
  978. func GetChartPredictEdbInfoDataListByRuleTbzscz(edbInfoId int, tbEndValue float64, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  979. allDataList := make([]*models.EdbDataList, 0)
  980. allDataList = append(allDataList, realPredictEdbInfoData...)
  981. allDataList = append(allDataList, predictEdbInfoData...)
  982. newPredictEdbInfoData = predictEdbInfoData
  983. index := len(allDataList)
  984. // 获取近期数据的同比值
  985. if index <= 0 {
  986. return
  987. }
  988. lastData := allDataList[index-1]
  989. lastDayTime, _ := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  990. var lastTb decimal.Decimal // 计算最新数据与上一期的数据同比值
  991. {
  992. //上一年的日期
  993. preDate := lastDayTime.AddDate(-1, 0, 0)
  994. preDateStr := preDate.Format(utils.FormatDate)
  995. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  996. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  997. } else {
  998. switch frequency {
  999. case "月度":
  1000. //向上和向下,各找一个月
  1001. nextDateDay := preDate
  1002. preDateDay := preDate
  1003. for i := 0; i <= 35; i++ {
  1004. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  1005. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  1006. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  1007. break
  1008. } else {
  1009. preDateDayStr := preDateDay.Format(utils.FormatDate)
  1010. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  1011. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  1012. break
  1013. }
  1014. }
  1015. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  1016. preDateDay = preDateDay.AddDate(0, 0, -1)
  1017. }
  1018. case "季度", "年度":
  1019. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  1020. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  1021. break
  1022. }
  1023. default:
  1024. nextDateDay := preDate
  1025. preDateDay := preDate
  1026. for i := 0; i < 35; i++ {
  1027. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  1028. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  1029. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  1030. break
  1031. } else {
  1032. preDateDayStr := preDateDay.Format(utils.FormatDate)
  1033. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  1034. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  1035. break
  1036. } else {
  1037. //fmt.Println("pre not find:", preDateStr, "i:", i)
  1038. }
  1039. }
  1040. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  1041. preDateDay = preDateDay.AddDate(0, 0, -1)
  1042. }
  1043. }
  1044. }
  1045. }
  1046. //获取后面的预测数据
  1047. lenDay := len(dayList)
  1048. tbEndValueDecimal := decimal.NewFromFloat(tbEndValue)
  1049. avgTbVal := tbEndValueDecimal.Sub(lastTb).Div(decimal.NewFromInt(int64(lenDay)))
  1050. predictEdbInfoData = make([]*models.EdbDataList, 0)
  1051. for k, currentDate := range dayList {
  1052. var tbValue decimal.Decimal
  1053. if k == lenDay-1 { // 如果是最后的日期了,那么就用终值去计算
  1054. tbValue = tbEndValueDecimal.Add(decimal.NewFromInt(1))
  1055. } else { // 最近数据的同比值 + (平均增值乘以当前期数)
  1056. tbValue = lastTb.Add(avgTbVal.Mul(decimal.NewFromInt(int64(k + 1)))).Add(decimal.NewFromInt(1))
  1057. }
  1058. tmpData := &models.EdbDataList{
  1059. EdbDataId: edbInfoId + 100000 + index + k,
  1060. EdbInfoId: edbInfoId,
  1061. DataTime: currentDate.Format(utils.FormatDate),
  1062. //Value: dataValue,
  1063. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  1064. }
  1065. var val float64
  1066. var calculateStatus bool //计算结果
  1067. //currentItem := existMap[av]
  1068. //上一年的日期
  1069. preDate := currentDate.AddDate(-1, 0, 0)
  1070. preDateStr := preDate.Format(utils.FormatDate)
  1071. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  1072. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
  1073. calculateStatus = true
  1074. } else {
  1075. switch frequency {
  1076. case "月度":
  1077. //向上和向下,各找一个月
  1078. nextDateDay := preDate
  1079. preDateDay := preDate
  1080. for i := 0; i <= 35; i++ {
  1081. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  1082. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  1083. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
  1084. calculateStatus = true
  1085. break
  1086. } else {
  1087. preDateDayStr := preDateDay.Format(utils.FormatDate)
  1088. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  1089. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
  1090. calculateStatus = true
  1091. break
  1092. }
  1093. }
  1094. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  1095. preDateDay = preDateDay.AddDate(0, 0, -1)
  1096. }
  1097. case "季度", "年度":
  1098. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  1099. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
  1100. calculateStatus = true
  1101. break
  1102. }
  1103. default:
  1104. nextDateDay := preDate
  1105. preDateDay := preDate
  1106. for i := 0; i < 35; i++ {
  1107. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  1108. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  1109. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
  1110. calculateStatus = true
  1111. break
  1112. } else {
  1113. preDateDayStr := preDateDay.Format(utils.FormatDate)
  1114. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  1115. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
  1116. calculateStatus = true
  1117. break
  1118. } else {
  1119. //fmt.Println("pre not find:", preDateStr, "i:", i)
  1120. }
  1121. }
  1122. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  1123. preDateDay = preDateDay.AddDate(0, 0, -1)
  1124. }
  1125. }
  1126. }
  1127. if calculateStatus {
  1128. tmpData.Value = val
  1129. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  1130. allDataList = append(allDataList, tmpData)
  1131. existMap[tmpData.DataTime] = val
  1132. // 最大最小值
  1133. if val < minValue {
  1134. minValue = val
  1135. }
  1136. if val > maxValue {
  1137. maxValue = val
  1138. }
  1139. }
  1140. }
  1141. return
  1142. }
  1143. // RuleLineNhConf 一元线性拟合规则的配置
  1144. type RuleLineNhConf struct {
  1145. StartDate string `description:"开始日期"`
  1146. EndDate string `description:"结束日期"`
  1147. MoveDay int `description:"移动天数"`
  1148. EdbInfoId int `description:"指标id"`
  1149. }
  1150. // GetChartPredictEdbInfoDataListByRuleLineNh 根据 一元线性拟合 的计算规则获取预测数据
  1151. //
  1152. // 选择被预测的指标B(作为自变量,非预测指标),选择指标A(作为因变量,可以是基础指标和预测指标)
  1153. // 2、选择拟合时间段,起始日期至今或指定时间段,选择至今,在计算时截止到指标B的最新日期
  1154. // 3、设定A领先B时间(天),正整数、负整数、0
  1155. // 4、调用拟合残差的数据预处理和算法,给出拟合方程Y=aX+b的系数a,b
  1156. // 5、指标A代入拟合方程得到拟合预测指标B',拟合预测指标使用指标B的频度,在指标B的实际值后面连接拟合预测指标B'对应日期的预测值
  1157. //
  1158. // 注:选择预测截止日期,若所选日期 ≤ 指标A设置领先后的日期序列,则预测指标日期最新日期有值(在指标B'的有值范围内);若所选日期 > 指标A设置领先后的日期序列,则预测指标只到指标A领先后的日期序列(超出指标B'的有值范围,最多到指标B'的最新值);指标A、B更新后,更新预测指标
  1159. func GetChartPredictEdbInfoDataListByRuleLineNh(edbInfoId int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, newNhccDataMap, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64, err error) {
  1160. allDataList := make([]*models.EdbDataList, 0)
  1161. allDataList = append(allDataList, realPredictEdbInfoData...)
  1162. allDataList = append(allDataList, predictEdbInfoData...)
  1163. newPredictEdbInfoData = predictEdbInfoData
  1164. lenAllData := len(allDataList)
  1165. if lenAllData <= 0 {
  1166. return
  1167. }
  1168. for k, currentDate := range dayList {
  1169. // 动态拟合残差值数据
  1170. currentDateStr := currentDate.Format(utils.FormatDate)
  1171. val, ok := newNhccDataMap[currentDateStr]
  1172. if !ok {
  1173. continue
  1174. }
  1175. tmpData := &models.EdbDataList{
  1176. EdbDataId: edbInfoId + 100000 + lenAllData + k,
  1177. EdbInfoId: edbInfoId,
  1178. DataTime: currentDateStr,
  1179. Value: val,
  1180. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  1181. }
  1182. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  1183. allDataList = append(allDataList, tmpData)
  1184. existMap[currentDateStr] = val
  1185. // 最大最小值
  1186. if val < minValue {
  1187. minValue = val
  1188. }
  1189. if val > maxValue {
  1190. maxValue = val
  1191. }
  1192. }
  1193. return
  1194. }
  1195. // getCalculateNhccData 获取计算出来的 拟合残差 数据
  1196. func getCalculateNhccData(secondDataList []*models.EdbDataList, ruleConf RuleLineNhConf) (newBDataMap map[string]float64, err error) {
  1197. firstEdbInfoId := ruleConf.EdbInfoId
  1198. moveDay := ruleConf.MoveDay
  1199. startDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.StartDate, time.Local)
  1200. endDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.EndDate, time.Local)
  1201. //查询当前指标现有的数据
  1202. edbInfo, err := data_manage.GetEdbInfoById(firstEdbInfoId)
  1203. if err != nil {
  1204. return
  1205. }
  1206. //第一个指标
  1207. aDataList := make([]models.EdbDataList, 0)
  1208. aDataMap := make(map[string]float64)
  1209. {
  1210. //第一个指标的数据列表
  1211. var firstDataList []*models.EdbDataList
  1212. switch edbInfo.EdbInfoType {
  1213. case 0:
  1214. firstDataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
  1215. case 1:
  1216. _, firstDataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  1217. default:
  1218. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  1219. }
  1220. if err != nil {
  1221. return
  1222. }
  1223. aDataList, aDataMap = handleNhccData(firstDataList, moveDay)
  1224. }
  1225. //第二个指标
  1226. bDataList := make([]models.EdbDataList, 0)
  1227. bDataMap := make(map[string]float64)
  1228. {
  1229. bDataList, bDataMap = handleNhccData(secondDataList, 0)
  1230. }
  1231. if len(aDataList) <= 0 {
  1232. err = errors.New("指标A没有数据")
  1233. return
  1234. }
  1235. if len(bDataList) <= 0 {
  1236. err = errors.New("指标B没有数据")
  1237. return
  1238. }
  1239. // 拟合残差计算的结束日期判断
  1240. {
  1241. endAData := aDataList[len(aDataList)-1]
  1242. tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, endAData.DataTime, time.Local)
  1243. if tmpErr != nil {
  1244. err = tmpErr
  1245. return
  1246. }
  1247. // 如果A指标的最新数据日期早于拟合残差的结束日期,那么就用A指标的最新数据日期
  1248. if tmpEndDate.Before(endDate) {
  1249. endDate = tmpEndDate
  1250. }
  1251. endBData := bDataList[len(bDataList)-1]
  1252. tmpEndDate, tmpErr = time.ParseInLocation(utils.FormatDate, endBData.DataTime, time.Local)
  1253. if tmpErr != nil {
  1254. err = tmpErr
  1255. return
  1256. }
  1257. // 如果B指标的最新数据日期早于拟合残差的结束日期,那么就用A指标的最新数据日期
  1258. if tmpEndDate.Before(endDate) {
  1259. endDate = tmpEndDate
  1260. }
  1261. }
  1262. // 计算线性方程公式
  1263. var a, b float64
  1264. {
  1265. coordinateData := make([]utils.Coordinate, 0)
  1266. for i := startDate; i.Before(endDate) || i.Equal(endDate); i = i.AddDate(0, 0, 1) {
  1267. dateStr := i.Format(utils.FormatDate)
  1268. xValue, ok := aDataMap[dateStr]
  1269. if !ok {
  1270. err = errors.New("指标A日期:" + dateStr + "数据异常,导致计算线性方程公式失败")
  1271. return
  1272. }
  1273. yValue, ok := bDataMap[dateStr]
  1274. if !ok {
  1275. err = errors.New("指标B日期:" + dateStr + "数据异常,导致计算线性方程公式失败")
  1276. return
  1277. }
  1278. tmpCoordinate := utils.Coordinate{
  1279. X: xValue,
  1280. Y: yValue,
  1281. }
  1282. coordinateData = append(coordinateData, tmpCoordinate)
  1283. }
  1284. a, b = utils.GetLinearResult(coordinateData)
  1285. }
  1286. if math.IsNaN(a) || math.IsNaN(b) {
  1287. err = errors.New("线性方程公式生成失败")
  1288. return
  1289. }
  1290. //fmt.Println("a:", a, ";======b:", b)
  1291. //计算B’
  1292. newBDataMap = make(map[string]float64)
  1293. {
  1294. //B’=aA+b
  1295. aDecimal := decimal.NewFromFloat(a)
  1296. bDecimal := decimal.NewFromFloat(b)
  1297. for _, aData := range aDataList {
  1298. xDecimal := decimal.NewFromFloat(aData.Value)
  1299. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
  1300. newBDataMap[aData.DataTime] = val
  1301. }
  1302. }
  1303. return
  1304. }
  1305. // handleNhccData 处理拟合残差需要的数据
  1306. func handleNhccData(dataList []*models.EdbDataList, moveDay int) (newDataList []models.EdbDataList, dateDataMap map[string]float64) {
  1307. dateMap := make(map[time.Time]float64)
  1308. var minDate, maxDate time.Time
  1309. dateDataMap = make(map[string]float64)
  1310. for _, v := range dataList {
  1311. currDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1312. if minDate.IsZero() || currDate.Before(minDate) {
  1313. minDate = currDate
  1314. }
  1315. if maxDate.IsZero() || currDate.After(maxDate) {
  1316. maxDate = currDate
  1317. }
  1318. dateMap[currDate] = v.Value
  1319. }
  1320. // 处理领先、滞后数据
  1321. newDateMap := make(map[time.Time]float64)
  1322. for currDate, value := range dateMap {
  1323. newDate := currDate.AddDate(0, 0, moveDay)
  1324. newDateMap[newDate] = value
  1325. }
  1326. minDate = minDate.AddDate(0, 0, moveDay)
  1327. maxDate = maxDate.AddDate(0, 0, moveDay)
  1328. // 开始平移天数
  1329. dayNum := utils.GetTimeSubDay(minDate, maxDate)
  1330. for i := 0; i <= dayNum; i++ {
  1331. currDate := minDate.AddDate(0, 0, i)
  1332. tmpValue, ok := newDateMap[currDate]
  1333. if !ok {
  1334. // 万一没有数据,那么就过滤当次循环
  1335. if len(newDataList) <= 0 {
  1336. continue
  1337. }
  1338. //找不到数据,那么就用前面的数据吧
  1339. tmpValue = newDataList[len(newDataList)-1].Value
  1340. }
  1341. tmpData := models.EdbDataList{
  1342. //EdbDataId: 0,
  1343. DataTime: currDate.Format(utils.FormatDate),
  1344. Value: tmpValue,
  1345. }
  1346. dateDataMap[tmpData.DataTime] = tmpData.Value
  1347. newDataList = append(newDataList, tmpData)
  1348. }
  1349. return
  1350. }