predict_edb_info_rule.go 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/nosixtools/solarlunar"
  9. "github.com/shopspring/decimal"
  10. "math"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type RuleParams struct {
  16. EdbInfoId int
  17. DayList []time.Time
  18. PredictEdbInfoData []*data_manage.EdbDataList
  19. RealPredictEdbInfoData []*data_manage.EdbDataList
  20. ExistMap map[string]float64
  21. Value string
  22. }
  23. type RuleCalculate interface {
  24. Calculate(params RuleParams) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error)
  25. }
  26. // GetChartPredictEdbInfoDataListByRule1 根据规则1获取预测数据
  27. func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, dayList []time.Time, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList) {
  28. newPredictEdbInfoData = predictEdbInfoData
  29. //获取后面的预测数据
  30. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  31. for k, v := range dayList {
  32. newPredictEdbInfoData = append(newPredictEdbInfoData, &data_manage.EdbDataList{
  33. EdbDataId: edbInfoId + 100000 + k,
  34. EdbInfoId: edbInfoId,
  35. DataTime: v.Format(utils.FormatDate),
  36. Value: dataValue,
  37. DataTimestamp: v.UnixNano() / 1e6,
  38. })
  39. existMap[v.Format(utils.FormatDate)] = dataValue
  40. }
  41. return
  42. }
  43. // GetChartPredictEdbInfoDataListByRuleTb 根据同比值规则获取预测数据
  44. // 2.1 同比: 在未来某一个时间段内,给定一个固定的同比增速a,用去年同期值X乘以同比增速(1+a),得到预测值Y=X(1+a)
  45. // 例: 今年1-3月值,100,100,120。给定同比增速a=0.1,则明年1-3月预测值为: 100*1.1=110,100*1.1=110,120*1.1=132。
  46. func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
  47. allDataList := make([]*data_manage.EdbDataList, 0)
  48. allDataList = append(allDataList, realPredictEdbInfoData...)
  49. allDataList = append(allDataList, predictEdbInfoData...)
  50. newPredictEdbInfoData = predictEdbInfoData
  51. index := len(allDataList)
  52. //获取后面的预测数据
  53. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  54. for k, currentDate := range dayList {
  55. tmpData := &data_manage.EdbDataList{
  56. EdbDataId: edbInfoId + 100000 + index + k,
  57. EdbInfoId: edbInfoId,
  58. DataTime: currentDate.Format(utils.FormatDate),
  59. //Value: dataValue,
  60. DataTimestamp: currentDate.UnixNano() / 1e6,
  61. }
  62. var val float64
  63. var calculateStatus bool //计算结果
  64. //currentItem := existMap[av]
  65. //上一年的日期
  66. preDate := currentDate.AddDate(-1, 0, 0)
  67. preDateStr := preDate.Format(utils.FormatDate)
  68. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  69. val = TbzDivMul(preValue, tbValue)
  70. calculateStatus = true
  71. } else {
  72. switch frequency {
  73. case "月度":
  74. //向上和向下,各找一个月
  75. nextDateDay := preDate
  76. preDateDay := preDate
  77. for i := 0; i <= 35; i++ {
  78. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  79. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  80. val = TbzDivMul(preValue, tbValue)
  81. calculateStatus = true
  82. break
  83. } else {
  84. preDateDayStr := preDateDay.Format(utils.FormatDate)
  85. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  86. val = TbzDivMul(preValue, tbValue)
  87. calculateStatus = true
  88. break
  89. }
  90. }
  91. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  92. preDateDay = preDateDay.AddDate(0, 0, -1)
  93. }
  94. case "季度", "年度":
  95. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  96. val = TbzDivMul(preValue, tbValue)
  97. calculateStatus = true
  98. break
  99. }
  100. default:
  101. nextDateDay := preDate
  102. preDateDay := preDate
  103. for i := 0; i < 35; i++ {
  104. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  105. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  106. val = TbzDivMul(preValue, tbValue)
  107. calculateStatus = true
  108. break
  109. } else {
  110. preDateDayStr := preDateDay.Format(utils.FormatDate)
  111. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  112. val = TbzDivMul(preValue, tbValue)
  113. calculateStatus = true
  114. break
  115. } else {
  116. //fmt.Println("pre not find:", preDateStr, "i:", i)
  117. }
  118. }
  119. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  120. preDateDay = preDateDay.AddDate(0, 0, -1)
  121. }
  122. }
  123. }
  124. if calculateStatus {
  125. tmpData.Value = val
  126. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  127. allDataList = append(allDataList, tmpData)
  128. existMap[tmpData.DataTime] = val
  129. // 最大最小值
  130. if val < minValue {
  131. minValue = val
  132. }
  133. if val > maxValue {
  134. maxValue = val
  135. }
  136. }
  137. }
  138. return
  139. }
  140. // TbzDivMul 同比值计算
  141. // @params a float64 去年同期值
  142. // @params b float64 固定同比增速
  143. func TbzDivMul(a, b float64) (result float64) {
  144. // 去年同期值
  145. af := decimal.NewFromFloat(a)
  146. // 同比增速
  147. bf := decimal.NewFromFloat(b)
  148. // 默认1
  149. cf := decimal.NewFromFloat(1)
  150. // 总增速
  151. val := bf.Add(cf)
  152. // 计算
  153. result, _ = val.Mul(af).Round(4).Float64()
  154. return
  155. }
  156. // GetChartPredictEdbInfoDataListByRuleTc 根据同差值规则获取预测数据
  157. // 2.2 同差: 在未来某一个时间段内,给定一个固定的同比增加值a,用去年同期值X加上同比增加值A,得到预测值Y=X+a
  158. // 例: 今年1-3月值,100,100,120。给定同比增加值a=10,则明年1-3月预测值为: 100+10=110,100+10=110,120+10=130
  159. func GetChartPredictEdbInfoDataListByRuleTc(edbInfoId int, tcValue float64, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
  160. allDataList := make([]*data_manage.EdbDataList, 0)
  161. allDataList = append(allDataList, realPredictEdbInfoData...)
  162. allDataList = append(allDataList, predictEdbInfoData...)
  163. newPredictEdbInfoData = predictEdbInfoData
  164. index := len(allDataList)
  165. //获取后面的预测数据
  166. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  167. for k, currentDate := range dayList {
  168. tmpData := &data_manage.EdbDataList{
  169. EdbDataId: edbInfoId + 100000 + index + k,
  170. EdbInfoId: edbInfoId,
  171. DataTime: currentDate.Format(utils.FormatDate),
  172. //Value: dataValue,
  173. DataTimestamp: currentDate.UnixNano() / 1e6,
  174. }
  175. var val float64
  176. var calculateStatus bool //计算结果
  177. //currentItem := existMap[av]
  178. //上一年的日期
  179. preDate := currentDate.AddDate(-1, 0, 0)
  180. preDateStr := preDate.Format(utils.FormatDate)
  181. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  182. val = TczDiv(preValue, tcValue)
  183. calculateStatus = true
  184. } else {
  185. switch frequency {
  186. case "月度":
  187. //向上和向下,各找一个月
  188. nextDateDay := preDate
  189. preDateDay := preDate
  190. for i := 0; i <= 35; i++ {
  191. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  192. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  193. val = TczDiv(preValue, tcValue)
  194. calculateStatus = true
  195. break
  196. } else {
  197. preDateDayStr := preDateDay.Format(utils.FormatDate)
  198. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  199. val = TczDiv(preValue, tcValue)
  200. calculateStatus = true
  201. break
  202. }
  203. }
  204. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  205. preDateDay = preDateDay.AddDate(0, 0, -1)
  206. }
  207. case "季度", "年度":
  208. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  209. val = TczDiv(preValue, tcValue)
  210. calculateStatus = true
  211. break
  212. }
  213. default:
  214. nextDateDay := preDate
  215. preDateDay := preDate
  216. for i := 0; i < 35; i++ {
  217. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  218. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  219. val = TczDiv(preValue, tcValue)
  220. calculateStatus = true
  221. break
  222. } else {
  223. preDateDayStr := preDateDay.Format(utils.FormatDate)
  224. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  225. val = TczDiv(preValue, tcValue)
  226. calculateStatus = true
  227. break
  228. } else {
  229. //fmt.Println("pre not find:", preDateStr, "i:", i)
  230. }
  231. }
  232. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  233. preDateDay = preDateDay.AddDate(0, 0, -1)
  234. }
  235. }
  236. }
  237. if calculateStatus {
  238. tmpData.Value = val
  239. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  240. allDataList = append(allDataList, tmpData)
  241. existMap[tmpData.DataTime] = val
  242. // 最大最小值
  243. if val < minValue {
  244. minValue = val
  245. }
  246. if val > maxValue {
  247. maxValue = val
  248. }
  249. }
  250. }
  251. return
  252. }
  253. // TczDiv 环差值计算
  254. // @params a float64 上一期值
  255. // @params b float64 固定的环比增加值
  256. func TczDiv(a, b float64) (result float64) {
  257. if b != 0 {
  258. // 上一期值
  259. af := decimal.NewFromFloat(a)
  260. // 固定的环比增加值
  261. bf := decimal.NewFromFloat(b)
  262. // 计算
  263. result, _ = af.Add(bf).Round(4).Float64()
  264. } else {
  265. result = 0
  266. }
  267. return
  268. }
  269. // GetChartPredictEdbInfoDataListByRuleHb 根据环比值规则获取预测数据
  270. // 环比:在未来某一个时间段内,给定一个固定的环比增速a,用上一期值X乘以环比增速(1+a),得到预测值Y=X(1+a)
  271. // 例: 最近1期值为100,给定环比增速a=0.2,则未来3期预测值为: 100*1.2=120,120*1.2=144,144*1.2=172.8
  272. func GetChartPredictEdbInfoDataListByRuleHb(edbInfoId int, hbValue float64, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
  273. allDataList := make([]*data_manage.EdbDataList, 0)
  274. allDataList = append(allDataList, realPredictEdbInfoData...)
  275. allDataList = append(allDataList, predictEdbInfoData...)
  276. newPredictEdbInfoData = predictEdbInfoData
  277. index := len(allDataList)
  278. //获取后面的预测数据
  279. for k, currentDate := range dayList {
  280. tmpK := index + k - 1 //上1期的值
  281. // 环比值计算
  282. val := HbzDiv(allDataList[tmpK].Value, hbValue)
  283. currentDateStr := currentDate.Format(utils.FormatDate)
  284. tmpData := &data_manage.EdbDataList{
  285. EdbDataId: edbInfoId + 100000 + index + k,
  286. EdbInfoId: edbInfoId,
  287. DataTime: currentDateStr,
  288. Value: val,
  289. DataTimestamp: currentDate.UnixNano() / 1e6,
  290. }
  291. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  292. allDataList = append(allDataList, tmpData)
  293. existMap[currentDateStr] = val
  294. // 最大最小值
  295. if val < minValue {
  296. minValue = val
  297. }
  298. if val > maxValue {
  299. maxValue = val
  300. }
  301. }
  302. return
  303. }
  304. // HbzDiv 环比值计算
  305. // @params a float64 上一期值
  306. // @params b float64 固定的环比增速
  307. func HbzDiv(a, b float64) (result float64) {
  308. if b != 0 {
  309. // 上一期值
  310. af := decimal.NewFromFloat(a)
  311. // 固定的环比增速
  312. bf := decimal.NewFromFloat(b)
  313. // 默认1
  314. cf := decimal.NewFromFloat(1)
  315. // 总增速
  316. val := bf.Add(cf)
  317. // 计算
  318. result, _ = val.Mul(af).Round(4).Float64()
  319. } else {
  320. result = 0
  321. }
  322. return
  323. }
  324. // GetChartPredictEdbInfoDataListByRuleHc 根据环差值规则获取预测数据
  325. // 2.4 环差:在未来某一个时间段内,给定一个固定的环比增加值a,用上一期值X加上环比增加值a,得到预测值Y=X+a
  326. // 例: 最近1期值为100,给定环比增加值a=10,则未来3期预测值为: 100+10=110,110+10=120,120+10=130
  327. func GetChartPredictEdbInfoDataListByRuleHc(edbInfoId int, hcValue float64, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
  328. allDataList := make([]*data_manage.EdbDataList, 0)
  329. allDataList = append(allDataList, realPredictEdbInfoData...)
  330. allDataList = append(allDataList, predictEdbInfoData...)
  331. newPredictEdbInfoData = predictEdbInfoData
  332. index := len(allDataList)
  333. //获取后面的预测数据
  334. for k, currentDate := range dayList {
  335. tmpK := index + k - 1 //上1期的值
  336. // 环差别值计算
  337. val := HczDiv(allDataList[tmpK].Value, hcValue)
  338. currentDateStr := currentDate.Format(utils.FormatDate)
  339. tmpData := &data_manage.EdbDataList{
  340. EdbDataId: edbInfoId + 100000 + index + k,
  341. EdbInfoId: edbInfoId,
  342. DataTime: currentDateStr,
  343. Value: val,
  344. DataTimestamp: currentDate.UnixNano() / 1e6,
  345. }
  346. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  347. allDataList = append(allDataList, tmpData)
  348. existMap[currentDateStr] = val
  349. // 最大最小值
  350. if val < minValue {
  351. minValue = val
  352. }
  353. if val > maxValue {
  354. maxValue = val
  355. }
  356. }
  357. return
  358. }
  359. // HczDiv 环差值计算
  360. // @params a float64 上一期值
  361. // @params b float64 固定的环比增加值
  362. func HczDiv(a, b float64) (result float64) {
  363. if b != 0 {
  364. // 上一期值
  365. af := decimal.NewFromFloat(a)
  366. // 固定的环比增加值
  367. bf := decimal.NewFromFloat(b)
  368. // 计算
  369. result, _ = af.Add(bf).Round(4).Float64()
  370. } else {
  371. result = 0
  372. }
  373. return
  374. }
  375. // GetChartPredictEdbInfoDataListByRuleNMoveMeanValue 根据N期移动均值规则获取预测数据
  376. // 2.5 N期移动均值:在未来某一个时间段内,下一期值等于过去N期值得平均值。
  377. // 例:最近3期值(N=3),为95,98,105则未来第1期值为 1/3*(95+98+105)=99.33, 未来第2期值为 1/3*(98+105+99.33)=100.78依次类推。
  378. func GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(edbInfoId int, nValue int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
  379. allDataList := make([]*data_manage.EdbDataList, 0)
  380. allDataList = append(allDataList, realPredictEdbInfoData...)
  381. allDataList = append(allDataList, predictEdbInfoData...)
  382. newPredictEdbInfoData = predictEdbInfoData
  383. lenAllData := len(allDataList)
  384. if lenAllData < nValue || lenAllData <= 0 {
  385. return
  386. }
  387. if nValue <= 0 {
  388. return
  389. }
  390. // 分母
  391. decimalN := decimal.NewFromInt(int64(nValue))
  392. //获取后面的预测数据
  393. for k, currentDate := range dayList {
  394. tmpIndex := lenAllData + k - 1 //上1期的值
  395. // 数据集合中的最后一个数据
  396. tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
  397. for tmpK := 2; tmpK <= nValue; tmpK++ {
  398. tmpIndex2 := tmpIndex - tmpK //上N期的值
  399. tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
  400. tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
  401. }
  402. // N期移动均值计算
  403. val, _ := tmpDecimalVal.Div(decimalN).Round(4).Float64()
  404. currentDateStr := currentDate.Format(utils.FormatDate)
  405. tmpData := &data_manage.EdbDataList{
  406. EdbDataId: edbInfoId + 100000 + lenAllData + k,
  407. EdbInfoId: edbInfoId,
  408. DataTime: currentDateStr,
  409. Value: val,
  410. DataTimestamp: currentDate.UnixNano() / 1e6,
  411. }
  412. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  413. allDataList = append(allDataList, tmpData)
  414. existMap[currentDateStr] = val
  415. // 最大最小值
  416. if val < minValue {
  417. minValue = val
  418. }
  419. if val > maxValue {
  420. maxValue = val
  421. }
  422. }
  423. return
  424. }
  425. // GetChartPredictEdbInfoDataListByRuleNLinearRegression 根据N期移动均值规则获取预测数据
  426. // 2.6N期段线性外推值:给出过去N期值所确定的线性回归方程(Y=aX+b)在未来一段时间内的推算值。回归方程虽然比较复杂,但各种编程语言应该都有现成的模块或函数,应该无需自己编写。
  427. // 例1:过去5期值(N=5)分别为:3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:13,15,17。
  428. //
  429. // 例2:过去6期值(N=6)分别为:3,3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:12.33,14.05,15.76。例1和例2的区别在于,多加了一期数据,导致回归方程发生改变,从而预测值不同。
  430. func GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  431. allDataList := make([]*data_manage.EdbDataList, 0)
  432. allDataList = append(allDataList, realPredictEdbInfoData...)
  433. allDataList = append(allDataList, predictEdbInfoData...)
  434. newPredictEdbInfoData = predictEdbInfoData
  435. lenAllData := len(allDataList)
  436. if lenAllData < nValue || lenAllData <= 0 {
  437. return
  438. }
  439. if nValue <= 1 {
  440. return
  441. }
  442. //获取后面的预测数据
  443. // 获取线性方程公式的a、b的值
  444. coordinateData := make([]utils.Coordinate, 0)
  445. for tmpK := nValue; tmpK > 0; tmpK-- {
  446. tmpIndex2 := lenAllData - tmpK //上N期的值
  447. tmpCoordinate := utils.Coordinate{
  448. X: float64(nValue - tmpK + 1),
  449. Y: allDataList[tmpIndex2].Value,
  450. }
  451. coordinateData = append(coordinateData, tmpCoordinate)
  452. }
  453. a, b := utils.GetLinearResult(coordinateData)
  454. if math.IsNaN(a) || math.IsNaN(b) {
  455. err = errors.New("线性方程公式生成失败")
  456. return
  457. }
  458. //fmt.Println("a:", a, ";======b:", b)
  459. for k, currentDate := range dayList {
  460. tmpK := nValue + k + 1
  461. aDecimal := decimal.NewFromFloat(a)
  462. xDecimal := decimal.NewFromInt(int64(tmpK))
  463. bDecimal := decimal.NewFromFloat(b)
  464. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  465. currentDateStr := currentDate.Format(utils.FormatDate)
  466. tmpData := &data_manage.EdbDataList{
  467. EdbDataId: edbInfoId + 100000 + lenAllData + k,
  468. EdbInfoId: edbInfoId,
  469. DataTime: currentDateStr,
  470. Value: val,
  471. DataTimestamp: currentDate.UnixNano() / 1e6,
  472. }
  473. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  474. allDataList = append(allDataList, tmpData)
  475. existMap[currentDateStr] = val
  476. // 最大最小值
  477. if val < minValue {
  478. minValue = val
  479. }
  480. if val > maxValue {
  481. maxValue = val
  482. }
  483. }
  484. return
  485. }
  486. // GetChartPredictEdbInfoDataListByRuleTrendsHC 根据动态环比增加值的计算规则获取预测数据
  487. //
  488. // 研究员有对预测指标进行动态环差计算的需求,即预测指标使用环差规则进行预测时,环比增加值不是固定值,而是由几个预测指标计算得出的动态变化的值;
  489. // 需求说明:
  490. // 1、增加“动态环差”预测规则;
  491. // 2、环比增加值在弹窗设置;
  492. // 3、动态环差预测举例:
  493. // 指标A实际最新数据为2022-10-27(100);
  494. // 预测指标B预测数据为2022-10-28(240)、2022-10-29(300);
  495. // 预测指标C预测数据为2022-10-28(260)、2022-10-29(310);
  496. // 计算公式为B-C;
  497. // 则指标A至2022-10-29的预测值为2022-10-28(100+(240-260)=80)、2022-10-29(80+(300-310)=90);
  498. // 注:动态环比增加值的计算遵从计算指标的计算规则,即用于计算的指标若有部分指标缺少部分日期数据,则这部分日期数据不做计算,为空;若动态环比增加值某一天为空,则往前追溯最近一期有值的环比增加值作为该天的数值参与计算;
  499. func GetChartPredictEdbInfoDataListByRuleTrendsHC(edbInfoId int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, hcDataMap, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
  500. allDataList := make([]*data_manage.EdbDataList, 0)
  501. allDataList = append(allDataList, realPredictEdbInfoData...)
  502. allDataList = append(allDataList, predictEdbInfoData...)
  503. newPredictEdbInfoData = predictEdbInfoData
  504. lenAllData := len(allDataList)
  505. if lenAllData <= 0 {
  506. return
  507. }
  508. for k, currentDate := range dayList {
  509. // 最近一条数据
  510. tmpLenAllDataList := len(allDataList)
  511. lastValue := allDataList[tmpLenAllDataList-1].Value
  512. // 动态环差值数据
  513. currentDateStr := currentDate.Format(utils.FormatDate)
  514. hcVal, ok := hcDataMap[currentDateStr]
  515. if !ok {
  516. continue
  517. }
  518. lastValueDecimal := decimal.NewFromFloat(lastValue)
  519. hcValDecimal := decimal.NewFromFloat(hcVal)
  520. val, _ := lastValueDecimal.Add(hcValDecimal).Round(4).Float64()
  521. tmpData := &data_manage.EdbDataList{
  522. EdbDataId: edbInfoId + 100000 + lenAllData + k,
  523. EdbInfoId: edbInfoId,
  524. DataTime: currentDateStr,
  525. Value: val,
  526. DataTimestamp: currentDate.UnixNano() / 1e6,
  527. }
  528. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  529. allDataList = append(allDataList, tmpData)
  530. existMap[currentDateStr] = val
  531. // 最大最小值
  532. if val < minValue {
  533. minValue = val
  534. }
  535. if val > maxValue {
  536. maxValue = val
  537. }
  538. }
  539. return
  540. }
  541. // GetChartPredictEdbInfoDataListByRuleFinalValueHc 根据 给定终值后插值 规则获取预测数据
  542. //
  543. // 项目背景:
  544. // 假设螺纹产量在2023年1月1号的预测值是255万吨,从当下到2023年1月1号,螺纹产量将会线性变化,那么每一期的螺纹产量是多少?
  545. // 算法:从当下(2022/10/28)到2023/1/1号,一共65天,从当前值(305.02)到255,差值-50.02,
  546. // 则每日环差为-50.02/65=-0.7695。因为数据点是周度频率,每周环差为,-0.3849*7=-5.3868。
  547. // 从以上计算过程可看出,“给定终值后差值”的算法,是在“环差”算法的基础上,做的一个改动。即这个”环差值”=【(终值-最新值)/终值与最新值得日期差】*数据频率
  548. // 需求说明:
  549. // 1、增加一个预测规则,名为“给定终值后插值”,给定预测截止日期和预测终值,计算最新数据日期至预测截止日期的时间差T,计算最新数据和预测终值的数据差S,数据频率与指标频度有关,日度=1,周度=7,旬度=10,月度=30,季度=90,年度=365,环差值=S/T*频率,预测数值=前一天数值+环差值;
  550. // 2、最新数据值和日期改动后,需重新计算环差值和预测数值;
  551. func GetChartPredictEdbInfoDataListByRuleFinalValueHc(edbInfoId int, finalValue float64, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
  552. allDataList := make([]*data_manage.EdbDataList, 0)
  553. allDataList = append(allDataList, realPredictEdbInfoData...)
  554. allDataList = append(allDataList, predictEdbInfoData...)
  555. newPredictEdbInfoData = predictEdbInfoData
  556. index := len(allDataList)
  557. //获取后面的预测日期
  558. lenDay := len(dayList)
  559. if lenDay <= 0 {
  560. return
  561. }
  562. var hcValue float64
  563. lastValueDeciamal := decimal.NewFromFloat(allDataList[index-1].Value) // 实际数据的最后一个值
  564. finalValueDeciamal := decimal.NewFromFloat(finalValue) // 给定的终止数据
  565. dayDecimal := decimal.NewFromInt(int64(lenDay)) // 需要作为分母的期数
  566. hcValue, _ = finalValueDeciamal.Sub(lastValueDeciamal).Div(dayDecimal).Float64() // 计算出来的环差值
  567. //获取后面的预测数据
  568. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  569. lastK := lenDay - 1 // 最后的日期
  570. for k, currentDate := range dayList {
  571. tmpK := index + k - 1 //上1期的值
  572. var val float64
  573. // 环差别值计算
  574. if k == lastK { //如果是最后一天,那么就用最终值,否则就计算
  575. val = finalValue
  576. } else {
  577. val = HczDiv(allDataList[tmpK].Value, hcValue)
  578. }
  579. currentDateStr := currentDate.Format(utils.FormatDate)
  580. tmpData := &data_manage.EdbDataList{
  581. EdbDataId: edbInfoId + 100000 + index + k,
  582. EdbInfoId: edbInfoId,
  583. DataTime: currentDateStr,
  584. Value: val,
  585. DataTimestamp: currentDate.UnixNano() / 1e6,
  586. }
  587. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  588. allDataList = append(allDataList, tmpData)
  589. existMap[currentDateStr] = val
  590. // 最大最小值
  591. if val < minValue {
  592. minValue = val
  593. }
  594. if val > maxValue {
  595. maxValue = val
  596. }
  597. }
  598. return
  599. }
  600. // SeasonConf 季节性规则的配置
  601. type SeasonConf struct {
  602. Calendar string `description:"公历、农历"`
  603. YearType int `description:"选择方式,1:连续N年;2:指定年份"`
  604. NValue int `description:"连续N年"`
  605. YearList []int `description:"指定年份列表"`
  606. }
  607. // GetChartPredictEdbInfoDataListByRuleSeason 根据 季节性 规则获取预测数据
  608. //
  609. // ETA预测规则:季节性
  610. // 已知选定指标A最近更新日期: 2022-12-6 200
  611. // 设置预测截止日期2023-01-06
  612. // 1、选择过去N年,N=3
  613. // 则过去N年为2021、2020、2019
  614. // 指标A日期 实际值 指标A日期
  615. // 2019/12/5 150 2019/12/6
  616. // 2020/12/5 180 2020/12/6
  617. // 2021/12/5 210 2021/12/6
  618. // 2019/12/31 200 2020/1/1
  619. // 2020/12/31 210 2021/1/1
  620. // 2021/12/31 250 2022/1/1
  621. //
  622. // 计算12.7预测值,求过去N年环差均值=[(100-150)+(160-180)+(250-210)]/3=-10
  623. // 则12.7预测值=12.6值+过去N年环差均值=200-10=190
  624. // 以此类推...
  625. //
  626. // 计算2023.1.2预测值,求过去N年环差均值=[(300-200)+(220-210)+(260-250)]/3=40
  627. // 则2023.1.2预测值=2023.1.1值+过去N年环差均值
  628. func GetChartPredictEdbInfoDataListByRuleSeason(edbInfoId int, configValue string, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  629. // 获取配置的年份列表
  630. yearList, seasonConf, err := getYearListBySeasonConf(configValue)
  631. if err != nil {
  632. return
  633. }
  634. calendar := seasonConf.Calendar
  635. allDataList := make([]*data_manage.EdbDataList, 0)
  636. allDataList = append(allDataList, realPredictEdbInfoData...)
  637. allDataList = append(allDataList, predictEdbInfoData...)
  638. newPredictEdbInfoData = predictEdbInfoData
  639. // 插值法数据处理
  640. handleDataMap := make(map[string]float64)
  641. err = handleDataByLinearRegression(allDataList, handleDataMap)
  642. if err != nil {
  643. return
  644. }
  645. // 获取每个年份的日期数据需要平移的天数
  646. moveDayMap := make(map[int]int, 0) // 每个年份的春节公历
  647. {
  648. if calendar == "公历" {
  649. for _, year := range yearList {
  650. moveDayMap[year] = 0 //公历就不平移了
  651. }
  652. } else {
  653. currentDay := time.Now()
  654. if currentDay.Month() >= 11 { //如果大于等于11月份,那么用的是下一年的春节
  655. currentDay = currentDay.AddDate(1, 0, 0)
  656. }
  657. currentYear := currentDay.Year()
  658. currentYearCjnl := fmt.Sprintf("%d-01-01", currentYear) //当年的春节农历
  659. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当年的春节公历
  660. currentYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, currentYearCjgl, time.Local)
  661. if tmpErr != nil {
  662. err = errors.New("当前春节公历日期转换失败:" + tmpErr.Error())
  663. return
  664. }
  665. // 指定的年份
  666. for _, year := range yearList {
  667. tmpYearCjnl := fmt.Sprintf("%d-01-01", year) //指定年的春节农历
  668. tmpYearCjgl := solarlunar.LunarToSolar(tmpYearCjnl, false) //指定年的春节公历
  669. //moveDayList = append(moveDayList, 0) //公历就不平移了
  670. tmpYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpYearCjgl, time.Local)
  671. if tmpErr != nil {
  672. err = errors.New(fmt.Sprintf("%d公历日期转换失败:%s", year, tmpErr.Error()))
  673. return
  674. }
  675. tmpCurrentYearCjglTime := currentYearCjglTime.AddDate(year-currentYear, 0, 0)
  676. moveDay := utils.GetTimeSubDay(tmpYearCjglTime, tmpCurrentYearCjglTime)
  677. moveDayMap[year] = moveDay //公历平移
  678. }
  679. }
  680. }
  681. index := len(allDataList)
  682. //获取后面的预测数据
  683. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  684. for k, currentDate := range dayList {
  685. // 如果遇到闰二月,如2.29,去掉该天数据
  686. if strings.Contains(currentDate.Format(utils.FormatDate), "02-29") {
  687. continue
  688. }
  689. tmpHistoryVal := decimal.NewFromFloat(0) //往期的差值总和
  690. tmpHistoryValNum := 0 // 往期差值计算的数量
  691. tmpLenAllDataList := len(allDataList)
  692. tmpK := tmpLenAllDataList - 1 //上1期数据的下标
  693. lastDayData := allDataList[tmpK] // 上1期的数据
  694. //lastDayStr := lastDayData.DataTime
  695. lastDayVal := lastDayData.Value
  696. //lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, lastDayStr, time.Local)
  697. //if tmpErr != nil {
  698. // err = errors.New("获取上期日期转换失败:" + tmpErr.Error())
  699. //}
  700. for _, year := range yearList {
  701. moveDay := moveDayMap[year] //需要移动的天数
  702. var tmpHistoryCurrentVal, tmpHistoryLastVal float64
  703. var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
  704. //前几年当日的日期
  705. tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, -(moveDay + 1))
  706. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  707. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
  708. if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
  709. tmpHistoryCurrentVal = val
  710. isFindHistoryCurrent = true
  711. break
  712. } else {
  713. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
  714. if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
  715. tmpHistoryCurrentVal = val
  716. isFindHistoryCurrent = true
  717. break
  718. }
  719. }
  720. }
  721. //前几年上一期的日期
  722. tmpHistoryLastDate := tmpHistoryCurrentDate.AddDate(0, 0, -1)
  723. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  724. tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
  725. if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
  726. tmpHistoryLastVal = val
  727. isFindHistoryLast = true
  728. break
  729. } else {
  730. tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
  731. if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
  732. tmpHistoryLastVal = val
  733. isFindHistoryLast = true
  734. break
  735. }
  736. }
  737. }
  738. // 如果两个日期对应的数据都找到了,那么计算两期的差值
  739. if isFindHistoryCurrent && isFindHistoryLast {
  740. af := decimal.NewFromFloat(tmpHistoryCurrentVal)
  741. bf := decimal.NewFromFloat(tmpHistoryLastVal)
  742. tmpHistoryVal = tmpHistoryVal.Add(af.Sub(bf))
  743. tmpHistoryValNum++
  744. }
  745. }
  746. //计算的差值与选择的年份数量不一致,那么当前日期不计算
  747. if tmpHistoryValNum != len(yearList) {
  748. continue
  749. }
  750. lastDayValDec := decimal.NewFromFloat(lastDayVal)
  751. val, _ := tmpHistoryVal.Div(decimal.NewFromInt(int64(tmpHistoryValNum))).Add(lastDayValDec).Round(4).Float64()
  752. currentDateStr := currentDate.Format(utils.FormatDate)
  753. tmpData := &data_manage.EdbDataList{
  754. EdbDataId: edbInfoId + 100000 + index + k,
  755. EdbInfoId: edbInfoId,
  756. DataTime: currentDateStr,
  757. Value: val,
  758. DataTimestamp: currentDate.UnixNano() / 1e6,
  759. }
  760. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  761. allDataList = append(allDataList, tmpData)
  762. existMap[currentDateStr] = val
  763. // 继续使用插值法补充新预测日期的数据之间的值
  764. err = handleDataByLinearRegression([]*data_manage.EdbDataList{
  765. lastDayData, tmpData,
  766. }, handleDataMap)
  767. if err != nil {
  768. return
  769. }
  770. // 最大最小值
  771. if val < minValue {
  772. minValue = val
  773. }
  774. if val > maxValue {
  775. maxValue = val
  776. }
  777. }
  778. return
  779. }
  780. // MoveAverageConf 移动平均同比规则的配置
  781. type MoveAverageConf struct {
  782. Year int `description:"指定年份"`
  783. NValue int `description:"N期的数据"`
  784. }
  785. // GetChartPredictEdbInfoDataListByRuleMoveAverageTb 根据 移动平均同比 规则获取预测数据
  786. //
  787. // ETA预测规则:季节性
  788. // 2、选择指定N年,N=3
  789. // 指定N年为2012、2015、2018
  790. // 指标A日期 实际值 指标A日期 实际值
  791. // 2012/12/5 150 2012/12/6 130
  792. // 2015/12/5 180 2015/12/6 150
  793. // 2018/12/5 210 2018/12/6 260
  794. // 2012/12/31 200 2013/1/1 200
  795. // 2015/12/31 210 2016/1/1 250
  796. // 2018/12/31 250 2019/1/1 270
  797. // 计算12.7预测值,求过去N年环差均值=[(130-150)+(150-180)+(290-210)]/3=10
  798. // 则12.7预测值=12.6值+过去N年环差均值=200+10=210
  799. // 以此类推...
  800. // 计算2023.1.2预测值,求过去N年环差均值=[(200-200)+(250-210)+(270-250)]/3=16.67
  801. // 则2023.1.2预测值=2023.1.1值+过去N年环差均值
  802. func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, year int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  803. allDataList := make([]*data_manage.EdbDataList, 0)
  804. allDataList = append(allDataList, realPredictEdbInfoData...)
  805. allDataList = append(allDataList, predictEdbInfoData...)
  806. newPredictEdbInfoData = predictEdbInfoData
  807. lenAllData := len(allDataList)
  808. if lenAllData < nValue || lenAllData <= 0 {
  809. return
  810. }
  811. if nValue <= 0 {
  812. return
  813. }
  814. // 分母
  815. decimalN := decimal.NewFromInt(int64(nValue))
  816. // 需要减去的年份
  817. subYear := year - dayList[0].Year()
  818. //获取后面的预测数据
  819. for k, currentDate := range dayList {
  820. tmpLenAllDataList := len(allDataList)
  821. tmpIndex := tmpLenAllDataList - 1 //上1期数据的下标
  822. averageDateList := make([]string, 0) //计算平均数的日期
  823. // 数据集合中的最后一个数据
  824. tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
  825. averageDateList = append(averageDateList, allDataList[tmpIndex].DataTime)
  826. for tmpK := 1; tmpK < nValue; tmpK++ {
  827. tmpIndex2 := tmpIndex - tmpK //上N期的值
  828. tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
  829. tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
  830. averageDateList = append(averageDateList, allDataList[tmpIndex2].DataTime)
  831. }
  832. // 最近的N期平均值
  833. tmpAverageVal := tmpDecimalVal.Div(decimalN)
  834. var tmpHistoryCurrentVal float64 // 前几年当日的数据值
  835. var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
  836. tmpHistoryDecimalVal := decimal.NewFromFloat(0) //前几年N期数据总值
  837. {
  838. // 前几年N期汇总期数
  839. tmpHistoryValNum := 0
  840. {
  841. //前几年当日的日期
  842. tmpHistoryCurrentDate := currentDate.AddDate(subYear, 0, 0)
  843. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  844. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
  845. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  846. tmpHistoryCurrentVal = val
  847. isFindHistoryCurrent = true
  848. break
  849. } else {
  850. tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
  851. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  852. tmpHistoryCurrentVal = val
  853. isFindHistoryCurrent = true
  854. break
  855. }
  856. }
  857. }
  858. }
  859. for _, averageDate := range averageDateList {
  860. lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, averageDate, time.Local)
  861. if tmpErr != nil {
  862. err = tmpErr
  863. return
  864. }
  865. //前几年上一期的日期
  866. tmpHistoryLastDate := lastDay.AddDate(subYear, 0, 0)
  867. for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
  868. tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
  869. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  870. tmpDecimalVal2 := decimal.NewFromFloat(val)
  871. tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
  872. tmpHistoryValNum++
  873. break
  874. } else {
  875. tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
  876. if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
  877. tmpDecimalVal2 := decimal.NewFromFloat(val)
  878. tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
  879. tmpHistoryValNum++
  880. break
  881. }
  882. }
  883. }
  884. }
  885. // 汇总期数与配置的N期数量一致
  886. if tmpHistoryValNum == nValue {
  887. isFindHistoryLast = true
  888. }
  889. }
  890. // 如果没有找到前几年的汇总数据,或者没有找到前几年当日的数据,那么退出当前循环,进入下一循环
  891. if !isFindHistoryLast || !isFindHistoryCurrent {
  892. continue
  893. }
  894. // 计算最近N期平均值
  895. tmpHistoryAverageVal := tmpHistoryDecimalVal.Div(decimalN)
  896. // 计算最近N期同比值
  897. tbVal := tmpAverageVal.Div(tmpHistoryAverageVal)
  898. // 预测值结果 = 同比年份同期值(tmpHistoryCurrentVal的值)* 同比值(tbVal的值)
  899. val, _ := decimal.NewFromFloat(tmpHistoryCurrentVal).Mul(tbVal).Round(4).Float64()
  900. currentDateStr := currentDate.Format(utils.FormatDate)
  901. tmpData := &data_manage.EdbDataList{
  902. EdbDataId: edbInfoId + 100000 + lenAllData + k,
  903. EdbInfoId: edbInfoId,
  904. DataTime: currentDateStr,
  905. Value: val,
  906. DataTimestamp: currentDate.UnixNano() / 1e6,
  907. }
  908. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  909. allDataList = append(allDataList, tmpData)
  910. existMap[currentDateStr] = val
  911. // 最大最小值
  912. if val < minValue {
  913. minValue = val
  914. }
  915. if val > maxValue {
  916. maxValue = val
  917. }
  918. }
  919. return
  920. }
  921. // GetChartPredictEdbInfoDataListByRuleTbzscz 根据 同比增速差值 规则获取预测数据
  922. // 同比增速差值计算方式:
  923. // 1、首先计算出所选指标实际最新日期值的同比增速:(本期数值-同期数值)÷同期数值*100%
  924. // 2、根据预测截止日期的同比增速终值、最新日期值的同比增速、与最新日期距离截止日期的期数,计算出到截止日期为止的每一期的同比增速。(等差规则计算每一期的同比增速,结合去年同期值,计算出每一期的同比预测值)。公差=(末项-首项)÷(n-1),an=a1+(n-1)d,(n为正整数,n大于等于2)
  925. // 3、根据去年同期值和未来每一期的同比增速值,求出同比预测值,同比预测值=同期值*(1+同比增速)
  926. // 同比增速差值:计算最新数据的同比增速((本期数值-同期数值)÷同期数值*100%),结合同比增速终值与期数,计算每一期同比增速,进而求出同比预测值。
  927. //
  928. // 例:如上图所示指标,(1)最新日期值2022-12-31 141175 ,结合同期值,计算同比增速;
  929. // (2)同比增速终值,若为50%, 预测日期为2023-03-31,则根据(1)中的同比增速值与同比增速终值,计算出中间两期的同比增速;
  930. // (3)求出每一期的预测同比值,预测同比值=同期值*(1+同比增速)
  931. func GetChartPredictEdbInfoDataListByRuleTbzscz(edbInfoId int, tbEndValue float64, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
  932. allDataList := make([]*data_manage.EdbDataList, 0)
  933. allDataList = append(allDataList, realPredictEdbInfoData...)
  934. allDataList = append(allDataList, predictEdbInfoData...)
  935. newPredictEdbInfoData = predictEdbInfoData
  936. index := len(allDataList)
  937. // 获取近期数据的同比值
  938. if index <= 0 {
  939. return
  940. }
  941. lastData := allDataList[index-1]
  942. lastDayTime, _ := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  943. var lastTb decimal.Decimal // 计算最新数据与上一期的数据同比值
  944. {
  945. //上一年的日期
  946. preDate := lastDayTime.AddDate(-1, 0, 0)
  947. preDateStr := preDate.Format(utils.FormatDate)
  948. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  949. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  950. } else {
  951. switch frequency {
  952. case "月度":
  953. //向上和向下,各找一个月
  954. nextDateDay := preDate
  955. preDateDay := preDate
  956. for i := 0; i <= 35; i++ {
  957. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  958. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  959. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  960. break
  961. } else {
  962. preDateDayStr := preDateDay.Format(utils.FormatDate)
  963. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  964. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  965. break
  966. }
  967. }
  968. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  969. preDateDay = preDateDay.AddDate(0, 0, -1)
  970. }
  971. case "季度", "年度":
  972. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  973. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  974. break
  975. }
  976. default:
  977. nextDateDay := preDate
  978. preDateDay := preDate
  979. for i := 0; i < 35; i++ {
  980. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  981. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  982. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  983. break
  984. } else {
  985. preDateDayStr := preDateDay.Format(utils.FormatDate)
  986. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  987. lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
  988. break
  989. } else {
  990. //fmt.Println("pre not find:", preDateStr, "i:", i)
  991. }
  992. }
  993. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  994. preDateDay = preDateDay.AddDate(0, 0, -1)
  995. }
  996. }
  997. }
  998. }
  999. //获取后面的预测数据
  1000. lenDay := len(dayList)
  1001. tbEndValueDecimal := decimal.NewFromFloat(tbEndValue)
  1002. avgTbVal := tbEndValueDecimal.Sub(lastTb).Div(decimal.NewFromInt(int64(lenDay)))
  1003. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  1004. for k, currentDate := range dayList {
  1005. var tbValue decimal.Decimal
  1006. if k == lenDay-1 { // 如果是最后的日期了,那么就用终值去计算
  1007. tbValue = tbEndValueDecimal.Add(decimal.NewFromInt(1))
  1008. } else { // 最近数据的同比值 + (平均增值乘以当前期数)
  1009. tbValue = lastTb.Add(avgTbVal.Mul(decimal.NewFromInt(int64(k + 1)))).Add(decimal.NewFromInt(1))
  1010. }
  1011. tmpData := &data_manage.EdbDataList{
  1012. EdbDataId: edbInfoId + 100000 + index + k,
  1013. EdbInfoId: edbInfoId,
  1014. DataTime: currentDate.Format(utils.FormatDate),
  1015. //Value: dataValue,
  1016. DataTimestamp: currentDate.UnixNano() / 1e6,
  1017. }
  1018. var val float64
  1019. var calculateStatus bool //计算结果
  1020. //currentItem := existMap[av]
  1021. //上一年的日期
  1022. preDate := currentDate.AddDate(-1, 0, 0)
  1023. preDateStr := preDate.Format(utils.FormatDate)
  1024. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  1025. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).Round(4).Float64()
  1026. calculateStatus = true
  1027. } else {
  1028. switch frequency {
  1029. case "月度":
  1030. //向上和向下,各找一个月
  1031. nextDateDay := preDate
  1032. preDateDay := preDate
  1033. for i := 0; i <= 35; i++ {
  1034. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  1035. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  1036. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).Round(4).Float64()
  1037. calculateStatus = true
  1038. break
  1039. } else {
  1040. preDateDayStr := preDateDay.Format(utils.FormatDate)
  1041. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  1042. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).Round(4).Float64()
  1043. calculateStatus = true
  1044. break
  1045. }
  1046. }
  1047. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  1048. preDateDay = preDateDay.AddDate(0, 0, -1)
  1049. }
  1050. case "季度", "年度":
  1051. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  1052. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).Round(4).Float64()
  1053. calculateStatus = true
  1054. break
  1055. }
  1056. default:
  1057. nextDateDay := preDate
  1058. preDateDay := preDate
  1059. for i := 0; i < 35; i++ {
  1060. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  1061. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  1062. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).Round(4).Float64()
  1063. calculateStatus = true
  1064. break
  1065. } else {
  1066. preDateDayStr := preDateDay.Format(utils.FormatDate)
  1067. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  1068. val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).Round(4).Float64()
  1069. calculateStatus = true
  1070. break
  1071. } else {
  1072. //fmt.Println("pre not find:", preDateStr, "i:", i)
  1073. }
  1074. }
  1075. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  1076. preDateDay = preDateDay.AddDate(0, 0, -1)
  1077. }
  1078. }
  1079. }
  1080. if calculateStatus {
  1081. tmpData.Value = val
  1082. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  1083. allDataList = append(allDataList, tmpData)
  1084. existMap[tmpData.DataTime] = val
  1085. // 最大最小值
  1086. if val < minValue {
  1087. minValue = val
  1088. }
  1089. if val > maxValue {
  1090. maxValue = val
  1091. }
  1092. }
  1093. }
  1094. return
  1095. }
  1096. // RuleLineNhConf 一元线性拟合规则的配置
  1097. type RuleLineNhConf struct {
  1098. StartDate string `description:"开始日期"`
  1099. EndDate string `description:"结束日期"`
  1100. MoveDay int `description:"移动天数"`
  1101. EdbInfoId int `description:"指标id"`
  1102. DateType int `description:"时间类型:0:开始日期至截止日期,1开始日期-至今"`
  1103. }
  1104. // GetChartPredictEdbInfoDataListByRuleLineNh 根据 一元线性拟合 的计算规则获取预测数据
  1105. //
  1106. // 选择被预测的指标B(作为自变量,非预测指标),选择指标A(作为因变量,可以是基础指标和预测指标)
  1107. // 2、选择拟合时间段,起始日期至今或指定时间段,选择至今,在计算时截止到指标B的最新日期
  1108. // 3、设定A领先B时间(天),正整数、负整数、0
  1109. // 4、调用拟合残差的数据预处理和算法,给出拟合方程Y=aX+b的系数a,b
  1110. // 5、指标A代入拟合方程得到拟合预测指标B',拟合预测指标使用指标B的频度,在指标B的实际值后面连接拟合预测指标B'对应日期的预测值
  1111. //
  1112. // 注:选择预测截止日期,若所选日期 ≤ 指标A设置领先后的日期序列,则预测指标日期最新日期有值(在指标B'的有值范围内);若所选日期 > 指标A设置领先后的日期序列,则预测指标只到指标A领先后的日期序列(超出指标B'的有值范围,最多到指标B'的最新值);指标A、B更新后,更新预测指标
  1113. func GetChartPredictEdbInfoDataListByRuleLineNh(edbInfoId int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, newNhccDataMap, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  1114. allDataList := make([]*data_manage.EdbDataList, 0)
  1115. allDataList = append(allDataList, realPredictEdbInfoData...)
  1116. allDataList = append(allDataList, predictEdbInfoData...)
  1117. newPredictEdbInfoData = predictEdbInfoData
  1118. lenAllData := len(allDataList)
  1119. if lenAllData <= 0 {
  1120. return
  1121. }
  1122. for k, currentDate := range dayList {
  1123. // 动态拟合残差值数据
  1124. currentDateStr := currentDate.Format(utils.FormatDate)
  1125. val, ok := newNhccDataMap[currentDateStr]
  1126. if !ok {
  1127. continue
  1128. }
  1129. tmpData := &data_manage.EdbDataList{
  1130. EdbDataId: edbInfoId + 100000 + lenAllData + k,
  1131. EdbInfoId: edbInfoId,
  1132. DataTime: currentDateStr,
  1133. Value: val,
  1134. DataTimestamp: currentDate.UnixNano() / 1e6,
  1135. }
  1136. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  1137. allDataList = append(allDataList, tmpData)
  1138. existMap[currentDateStr] = val
  1139. // 最大最小值
  1140. if val < minValue {
  1141. minValue = val
  1142. }
  1143. if val > maxValue {
  1144. maxValue = val
  1145. }
  1146. }
  1147. return
  1148. }
  1149. // getCalculateNhccData 获取计算出来的 拟合残差 数据
  1150. func getCalculateNhccData(secondDataList []*data_manage.EdbDataList, ruleConf RuleLineNhConf) (newBDataMap map[string]float64, err error) {
  1151. firstEdbInfoId := ruleConf.EdbInfoId
  1152. moveDay := ruleConf.MoveDay
  1153. startDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.StartDate, time.Local)
  1154. var endDate time.Time
  1155. if ruleConf.DateType == 0 {
  1156. endDate, _ = time.ParseInLocation(utils.FormatDate, ruleConf.EndDate, time.Local)
  1157. } else {
  1158. endDate, _ = time.ParseInLocation(utils.FormatDate, time.Now().Format(utils.FormatDate), time.Local)
  1159. }
  1160. //查询当前指标现有的数据
  1161. edbInfo, err := data_manage.GetEdbInfoById(firstEdbInfoId)
  1162. if err != nil {
  1163. return
  1164. }
  1165. //第一个指标
  1166. aDataList := make([]data_manage.EdbDataList, 0)
  1167. aDataMap := make(map[string]float64)
  1168. {
  1169. //第一个指标的数据列表
  1170. var firstDataList []*data_manage.EdbDataList
  1171. switch edbInfo.EdbInfoType {
  1172. case 0:
  1173. firstDataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  1174. case 1:
  1175. _, firstDataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  1176. default:
  1177. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  1178. }
  1179. if err != nil {
  1180. return
  1181. }
  1182. aDataList, aDataMap = handleNhccData(firstDataList, moveDay)
  1183. }
  1184. //第二个指标
  1185. bDataList := make([]data_manage.EdbDataList, 0)
  1186. bDataMap := make(map[string]float64)
  1187. {
  1188. bDataList, bDataMap = handleNhccData(secondDataList, 0)
  1189. }
  1190. if len(aDataList) <= 0 {
  1191. err = errors.New("指标A没有数据")
  1192. return
  1193. }
  1194. if len(bDataList) <= 0 {
  1195. err = errors.New("指标B没有数据")
  1196. return
  1197. }
  1198. // 拟合残差计算的结束日期判断
  1199. {
  1200. endAData := aDataList[len(aDataList)-1]
  1201. tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, endAData.DataTime, time.Local)
  1202. if tmpErr != nil {
  1203. err = tmpErr
  1204. return
  1205. }
  1206. // 如果A指标的最新数据日期早于拟合残差的结束日期,那么就用A指标的最新数据日期
  1207. if tmpEndDate.Before(endDate) {
  1208. endDate = tmpEndDate
  1209. }
  1210. endBData := bDataList[len(bDataList)-1]
  1211. tmpEndDate, tmpErr = time.ParseInLocation(utils.FormatDate, endBData.DataTime, time.Local)
  1212. if tmpErr != nil {
  1213. err = tmpErr
  1214. return
  1215. }
  1216. // 如果B指标的最新数据日期早于拟合残差的结束日期,那么就用A指标的最新数据日期
  1217. if tmpEndDate.Before(endDate) {
  1218. endDate = tmpEndDate
  1219. }
  1220. }
  1221. // 计算线性方程公式
  1222. var a, b float64
  1223. {
  1224. coordinateData := make([]utils.Coordinate, 0)
  1225. for i := startDate; i.Before(endDate) || i.Equal(endDate); i = i.AddDate(0, 0, 1) {
  1226. dateStr := i.Format(utils.FormatDate)
  1227. xValue, ok := aDataMap[dateStr]
  1228. if !ok {
  1229. err = errors.New("指标A日期:" + dateStr + "数据异常,导致计算线性方程公式失败")
  1230. return
  1231. }
  1232. yValue, ok := bDataMap[dateStr]
  1233. if !ok {
  1234. err = errors.New("指标B日期:" + dateStr + "数据异常,导致计算线性方程公式失败")
  1235. return
  1236. }
  1237. tmpCoordinate := utils.Coordinate{
  1238. X: xValue,
  1239. Y: yValue,
  1240. }
  1241. coordinateData = append(coordinateData, tmpCoordinate)
  1242. }
  1243. a, b = utils.GetLinearResult(coordinateData)
  1244. }
  1245. if math.IsNaN(a) || math.IsNaN(b) {
  1246. err = errors.New("线性方程公式生成失败")
  1247. return
  1248. }
  1249. //fmt.Println("a:", a, ";======b:", b)
  1250. //计算B’
  1251. newBDataMap = make(map[string]float64)
  1252. {
  1253. //B’=aA+b
  1254. aDecimal := decimal.NewFromFloat(a)
  1255. bDecimal := decimal.NewFromFloat(b)
  1256. for _, aData := range aDataList {
  1257. xDecimal := decimal.NewFromFloat(aData.Value)
  1258. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  1259. newBDataMap[aData.DataTime] = val
  1260. }
  1261. }
  1262. return
  1263. }
  1264. // handleNhccData 处理拟合残差需要的数据
  1265. func handleNhccData(dataList []*data_manage.EdbDataList, moveDay int) (newDataList []data_manage.EdbDataList, dateDataMap map[string]float64) {
  1266. dateMap := make(map[time.Time]float64)
  1267. var minDate, maxDate time.Time
  1268. dateDataMap = make(map[string]float64)
  1269. for _, v := range dataList {
  1270. currDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1271. if minDate.IsZero() || currDate.Before(minDate) {
  1272. minDate = currDate
  1273. }
  1274. if maxDate.IsZero() || currDate.After(maxDate) {
  1275. maxDate = currDate
  1276. }
  1277. dateMap[currDate] = v.Value
  1278. }
  1279. // 处理领先、滞后数据
  1280. newDateMap := make(map[time.Time]float64)
  1281. for currDate, value := range dateMap {
  1282. newDate := currDate.AddDate(0, 0, moveDay)
  1283. newDateMap[newDate] = value
  1284. }
  1285. minDate = minDate.AddDate(0, 0, moveDay)
  1286. maxDate = maxDate.AddDate(0, 0, moveDay)
  1287. // 开始平移天数
  1288. dayNum := utils.GetTimeSubDay(minDate, maxDate)
  1289. for i := 0; i <= dayNum; i++ {
  1290. currDate := minDate.AddDate(0, 0, i)
  1291. tmpValue, ok := newDateMap[currDate]
  1292. if !ok {
  1293. // 万一没有数据,那么就过滤当次循环
  1294. if len(newDataList) <= 0 {
  1295. continue
  1296. }
  1297. //找不到数据,那么就用前面的数据吧
  1298. tmpValue = newDataList[len(newDataList)-1].Value
  1299. }
  1300. tmpData := data_manage.EdbDataList{
  1301. //EdbDataId: 0,
  1302. DataTime: currDate.Format(utils.FormatDate),
  1303. Value: tmpValue,
  1304. }
  1305. dateDataMap[tmpData.DataTime] = tmpData.Value
  1306. newDataList = append(newDataList, tmpData)
  1307. }
  1308. return
  1309. }
  1310. // GetChartPredictEdbInfoDataListByRuleNAnnualAverage 根据 N年均值 规则获取预测数据
  1311. // ETA预测规则:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  1312. func GetChartPredictEdbInfoDataListByRuleNAnnualAverage(edbInfoId int, configValue string, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  1313. // 获取配置的年份列表
  1314. yearList, _, err := getYearListBySeasonConf(configValue)
  1315. if err != nil {
  1316. return
  1317. }
  1318. allDataList := make([]*data_manage.EdbDataList, 0)
  1319. allDataList = append(allDataList, realPredictEdbInfoData...)
  1320. allDataList = append(allDataList, predictEdbInfoData...)
  1321. newPredictEdbInfoData = predictEdbInfoData
  1322. // 插值法数据处理
  1323. handleDataMap := make(map[string]float64)
  1324. err = handleDataByLinearRegression(allDataList, handleDataMap)
  1325. if err != nil {
  1326. return
  1327. }
  1328. index := len(allDataList)
  1329. //获取后面的预测数据
  1330. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  1331. for k, currentDate := range dayList {
  1332. // 如果遇到闰二月,如2.29,去掉该天数据
  1333. if strings.Contains(currentDate.Format(utils.FormatDate), "02-29") {
  1334. continue
  1335. }
  1336. tmpK := len(allDataList) - 1 //上1期数据的下标
  1337. lastDayData := allDataList[tmpK] // 上1期的数据
  1338. tmpHistoryVal := decimal.NewFromFloat(0) //往期的差值总和
  1339. tmpHistoryValNum := 0 // 往期差值计算的数量
  1340. for _, year := range yearList {
  1341. //前几年当日的日期
  1342. tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, 0)
  1343. if val, ok := handleDataMap[tmpHistoryCurrentDate.Format(utils.FormatDate)]; ok {
  1344. tmpHistoryVal = tmpHistoryVal.Add(decimal.NewFromFloat(val))
  1345. tmpHistoryValNum++
  1346. }
  1347. }
  1348. //计算的差值与选择的年份数量不一致,那么当前日期不计算
  1349. if tmpHistoryValNum != len(yearList) {
  1350. continue
  1351. }
  1352. val, _ := tmpHistoryVal.Div(decimal.NewFromInt(int64(tmpHistoryValNum))).Round(4).Float64()
  1353. currentDateStr := currentDate.Format(utils.FormatDate)
  1354. tmpData := &data_manage.EdbDataList{
  1355. EdbDataId: edbInfoId + 100000 + index + k,
  1356. EdbInfoId: edbInfoId,
  1357. DataTime: currentDateStr,
  1358. Value: val,
  1359. DataTimestamp: currentDate.UnixNano() / 1e6,
  1360. }
  1361. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  1362. allDataList = append(allDataList, tmpData)
  1363. existMap[currentDateStr] = val
  1364. // 继续使用插值法补充新预测日期的数据之间的值
  1365. err = handleDataByLinearRegression([]*data_manage.EdbDataList{
  1366. lastDayData, tmpData,
  1367. }, handleDataMap)
  1368. if err != nil {
  1369. return
  1370. }
  1371. // 最大最小值
  1372. if val < minValue {
  1373. minValue = val
  1374. }
  1375. if val > maxValue {
  1376. maxValue = val
  1377. }
  1378. }
  1379. return
  1380. }
  1381. // AnnualValueInversionConf 年度值倒推规则
  1382. type AnnualValueInversionConf struct {
  1383. Value float64 `description:"年度值"`
  1384. Type int `description:"分配方式,1:均值法;2:同比法"`
  1385. Year int `description:"同比年份"`
  1386. YearList []int `description:"指定年份列表"`
  1387. }
  1388. func getReplaceValue(replaceValueMap map[string]float64, days, dayStepForward int, dayStepBack int, currentDate time.Time) (replaceValue decimal.Decimal, success bool) {
  1389. nextDateDay := currentDate
  1390. backDateDay := currentDate
  1391. for i := 0; i <= days; i++ {
  1392. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  1393. if preValue, ok := replaceValueMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  1394. replaceValue = decimal.NewFromFloat(preValue)
  1395. success = true
  1396. return
  1397. }
  1398. backDateDayStr := backDateDay.Format(utils.FormatDate)
  1399. if backValue, ok := replaceValueMap[backDateDayStr]; ok { //上一年同期->下一个月找到
  1400. replaceValue = decimal.NewFromFloat(backValue)
  1401. success = true
  1402. return
  1403. }
  1404. nextDateDay = nextDateDay.AddDate(0, 0, dayStepForward)
  1405. backDateDay = nextDateDay.AddDate(0, 0, dayStepBack)
  1406. }
  1407. return decimal.NewFromInt(0), false
  1408. }
  1409. // GetChartPredictEdbInfoDataListByRuleDynamicYOYComparisonOrDifference 动态同比
  1410. // 2、指标选择范围为预测指标。
  1411. // 3、动态同比计算方法:预测值=去年同期值*(1+同比指标预测值)
  1412. // 4、上述“去年同期”如果没有严格对应的日期,则前后查找最近35天的值。
  1413. // 5、选择的同比指标日期需要与预测指标未来日期对应上,对应不上的不生成预测值。
  1414. func GetChartPredictEdbInfoDataListByRuleDynamicYOYComparisonOrDifference(ruleType, edbInfoId int, configValue string, dayList []time.Time, realPredictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  1415. //预测指标的去年同期数据
  1416. baseDynamicDataList := make(map[string]decimal.Decimal, len(dayList))
  1417. //动态同比同差指标
  1418. DynamicCalculateDataList := make(map[string]decimal.Decimal, len(dayList))
  1419. index := len(realPredictEdbInfoData)
  1420. if index <= 0 {
  1421. return
  1422. }
  1423. dynamicYOYComparisonIndexId, err := strconv.Atoi(configValue)
  1424. if err != nil {
  1425. return
  1426. }
  1427. newPredictEdbInfoData = make([]*data_manage.EdbDataList, 0, len(dayList))
  1428. // 获取同比预测指标的预测数据
  1429. dynamicYOYComparisonIndex, err := data_manage.GetEdbInfoById(dynamicYOYComparisonIndexId)
  1430. if err != nil {
  1431. return
  1432. }
  1433. if dynamicYOYComparisonIndex.EdbInfoType != 1 {
  1434. err = errors.New("选择的指标不是预测指标")
  1435. return
  1436. }
  1437. startDate, endDate := dayList[0].Format(utils.FormatDate), dayList[len(dayList)-1].Format(utils.FormatDate)
  1438. //获取动态同比指标对应预测日期的预测数据
  1439. dynamicYOYComparisonIndexDataList, err := data_manage.GetEdbDataList(dynamicYOYComparisonIndex.Source, dynamicYOYComparisonIndex.SubSource, dynamicYOYComparisonIndex.EdbInfoId, startDate, endDate)
  1440. if err != nil {
  1441. return
  1442. }
  1443. if len(dynamicYOYComparisonIndexDataList) <= 0 {
  1444. return
  1445. } else {
  1446. for _, v := range dynamicYOYComparisonIndexDataList {
  1447. DynamicCalculateDataList[v.DataTime] = decimal.NewFromFloat(v.Value)
  1448. }
  1449. }
  1450. var predictDayList []time.Time
  1451. //获取上一期的同期数据
  1452. for _, date := range dayList {
  1453. preDate := date.AddDate(-1, 0, 0)
  1454. preDateStr := preDate.Format(utils.FormatDate)
  1455. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  1456. baseDynamicDataList[preDateStr] = decimal.NewFromFloat(preValue)
  1457. predictDayList = append(predictDayList, date)
  1458. } else {
  1459. if replaceValue, replaceOk := getReplaceValue(existMap, 35, 1, -1, preDate); !replaceOk {
  1460. continue
  1461. } else {
  1462. baseDynamicDataList[preDateStr] = replaceValue
  1463. predictDayList = append(predictDayList, date)
  1464. }
  1465. }
  1466. }
  1467. //获取后面的预测数据
  1468. for k, currentDate := range predictDayList {
  1469. var calculateValue decimal.Decimal
  1470. var dateStr = currentDate.Format(utils.FormatDate)
  1471. preDate := currentDate.AddDate(-1, 0, 0)
  1472. preDateStr := preDate.Format(utils.FormatDate)
  1473. _, dynamicVal := DynamicCalculateDataList[dateStr]
  1474. _, baseVal := baseDynamicDataList[preDateStr]
  1475. if dynamicVal && baseVal {
  1476. switch ruleType {
  1477. case 17:
  1478. calculateValue = baseDynamicDataList[preDateStr].Mul(DynamicCalculateDataList[dateStr].Add(decimal.NewFromInt(1)))
  1479. case 18:
  1480. calculateValue = baseDynamicDataList[preDateStr].Add(DynamicCalculateDataList[dateStr])
  1481. default:
  1482. err = errors.New("计算规则不存在")
  1483. return
  1484. }
  1485. tmpData := &data_manage.EdbDataList{
  1486. EdbDataId: edbInfoId + 100000 + index + k,
  1487. EdbInfoId: edbInfoId,
  1488. DataTime: currentDate.Format(utils.FormatDate),
  1489. DataTimestamp: currentDate.UnixNano() / 1e6,
  1490. }
  1491. var val = calculateValue.InexactFloat64()
  1492. tmpData.Value = val
  1493. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  1494. existMap[tmpData.DataTime] = val
  1495. if k == 0 {
  1496. minValue = val
  1497. maxValue = val
  1498. } else {
  1499. // 最大最小值
  1500. if val < minValue {
  1501. minValue = val
  1502. }
  1503. if val > maxValue {
  1504. maxValue = val
  1505. }
  1506. }
  1507. }
  1508. }
  1509. return
  1510. }
  1511. // getYearListBySeasonConf 根据配置获取年份列表
  1512. func getYearListBySeasonConf(configValue string) (yearList []int, seasonConf SeasonConf, err error) {
  1513. tmpErr := json.Unmarshal([]byte(configValue), &seasonConf)
  1514. if tmpErr != nil {
  1515. err = errors.New("年份配置信息异常:" + tmpErr.Error())
  1516. return
  1517. }
  1518. //选择方式,1:连续N年;2:指定年份
  1519. if seasonConf.YearType == 1 {
  1520. if seasonConf.NValue < 1 {
  1521. err = errors.New("连续N年不允许小于1")
  1522. return
  1523. }
  1524. currYear := time.Now().Year()
  1525. for i := 0; i < seasonConf.NValue; i++ {
  1526. yearList = append(yearList, currYear-i-1)
  1527. }
  1528. } else {
  1529. yearList = seasonConf.YearList
  1530. }
  1531. return
  1532. }
  1533. // GetChartPredictEdbInfoDataListByRuleAnnualValueInversion 根据 年度值倒推 规则获取预测数据
  1534. // 预测指标-年度值倒推
  1535. // 1、年度值倒推,选择同比法,支持选择多个年份(当前只可选择一个年份)。选择多个年份时,计算多个年份的余额平均,和同期平均。
  1536. // 2、年度值倒推,同比法的算法优化:旬度,月度,季度,半年度的算法,同原先算法。
  1537. // 日度、周度值算法更新(假设指标实际值最新日期月2024/3/1):
  1538. // 1、设定年度值
  1539. // 2、计算余额:年度值-年初至今累计值
  1540. // 3、年初至今累计值计算方法:用后置填充变频成连续自然日日度数据。计算1/1至指标最新日期(2024/3/3/1)的累计值。
  1541. // 4、计算同比年份全年累计值,年初至指标最新值同期(2023/3/1)累计值,两者相减得到同比年份同期余额,再取平均值,作为最终的同期余额
  1542. // 5、用今年余额/去年同期余额得到同比增速。
  1543. // 6、每一期预测值,为同比年份的同期值,乘以(1+同比)。去年同期,用变频后的序列对应。
  1544. // 7、如果选择的同比年份是多个。则计算多个年份的平均余额。今年余额/平均余额=同比增速。同比基数为多个年份的同期平均值
  1545. func GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(edbInfoId int, configValue string, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  1546. if frequency == "年度" {
  1547. err = errors.New("当前指标频度是年度,不允许配置年度值倒推")
  1548. return
  1549. }
  1550. // 获取配置
  1551. var annualValueInversionConf AnnualValueInversionConf
  1552. err = json.Unmarshal([]byte(configValue), &annualValueInversionConf)
  1553. if err != nil {
  1554. err = errors.New("年度值倒推配置信息异常:" + err.Error())
  1555. return
  1556. }
  1557. allDataList := make([]*data_manage.EdbDataList, 0)
  1558. allDataList = append(allDataList, realPredictEdbInfoData...)
  1559. allDataList = append(allDataList, predictEdbInfoData...)
  1560. newPredictEdbInfoData = predictEdbInfoData
  1561. index := len(allDataList)
  1562. // 没有数据,直接返回
  1563. if index <= 0 {
  1564. return
  1565. }
  1566. // 配置的年度值
  1567. yearValueConfig := annualValueInversionConf.Value
  1568. // 最新数据的日期
  1569. currDayTime, err := time.ParseInLocation(utils.FormatDate, allDataList[index-1].DataTime, time.Local)
  1570. if err != nil {
  1571. return
  1572. }
  1573. // 当前年的日期
  1574. lastDayTime := dayList[len(dayList)-1]
  1575. if currDayTime.Year() != lastDayTime.Year() {
  1576. err = errors.New("年度值倒推不支持跨年预测")
  1577. return
  1578. }
  1579. // 均值法
  1580. if annualValueInversionConf.Type == 1 {
  1581. // 当前年的期数
  1582. currYearN := 0
  1583. // 当前已经消耗的额度
  1584. var currYearVal float64
  1585. // 计算当前年的期数以及已经消耗的额度
  1586. {
  1587. if frequency != "周度" {
  1588. for _, v := range allDataList {
  1589. currTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1590. if tmpErr != nil {
  1591. err = tmpErr
  1592. return
  1593. }
  1594. // 只是计算今年的
  1595. if currTime.Year() != currDayTime.Year() {
  1596. continue
  1597. }
  1598. currYearN++
  1599. currYearVal = currYearVal + v.Value
  1600. }
  1601. } else {
  1602. tmpDataList := make([]*data_manage.EdbDataList, 0)
  1603. // 上一期的数据
  1604. var lastData *data_manage.EdbDataList
  1605. // 是否第一条数据
  1606. isFirst := true
  1607. for _, v := range allDataList {
  1608. currTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1609. if tmpErr != nil {
  1610. err = tmpErr
  1611. return
  1612. }
  1613. // 只是计算今年的
  1614. if currTime.Year() != currDayTime.Year() {
  1615. lastData = v
  1616. continue
  1617. }
  1618. if isFirst {
  1619. tmpDataList = append(tmpDataList, lastData)
  1620. }
  1621. isFirst = false
  1622. tmpDataList = append(tmpDataList, v)
  1623. currYearN++
  1624. }
  1625. // 需要插值法处理
  1626. tmpHandleDataMap := make(map[string]float64)
  1627. err = handleDataByLinearRegression(tmpDataList, tmpHandleDataMap)
  1628. if err != nil {
  1629. return
  1630. }
  1631. for tmpDate, val := range tmpHandleDataMap {
  1632. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  1633. if tmpErr != nil {
  1634. err = tmpErr
  1635. return
  1636. }
  1637. if tmpDateTime.Year() != currDayTime.Year() {
  1638. continue
  1639. }
  1640. currYearVal = currYearVal + val
  1641. }
  1642. currYearVal = currYearVal / 7
  1643. }
  1644. }
  1645. var averageVal float64
  1646. switch frequency {
  1647. case "半年度":
  1648. averageVal, _ = (decimal.NewFromFloat(yearValueConfig).Sub(decimal.NewFromFloat(currYearVal))).Div(decimal.NewFromInt(int64(2 - currYearN))).Float64()
  1649. case "季度":
  1650. averageVal, _ = (decimal.NewFromFloat(yearValueConfig).Sub(decimal.NewFromFloat(currYearVal))).Div(decimal.NewFromInt(int64(4 - currYearN))).Float64()
  1651. case "月度":
  1652. averageVal, _ = (decimal.NewFromFloat(yearValueConfig).Sub(decimal.NewFromFloat(currYearVal))).Div(decimal.NewFromInt(int64(12 - currYearN))).Float64()
  1653. case "旬度":
  1654. averageVal, _ = (decimal.NewFromFloat(yearValueConfig).Sub(decimal.NewFromFloat(currYearVal))).Div(decimal.NewFromInt(int64(36 - currYearN))).Float64()
  1655. case "周度", "日度":
  1656. //剩余期数=剩余自然日历天数/今年指标最新日期自然日历天数*今年至今指标数据期数
  1657. // 当前年的第一天
  1658. yearFirstDay := time.Date(currDayTime.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  1659. subDay := utils.GetTimeSubDay(yearFirstDay, currDayTime) + 1
  1660. // 当前年的最后一天
  1661. yearLastDay := time.Date(currDayTime.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  1662. subDay2 := utils.GetTimeSubDay(yearFirstDay, yearLastDay) + 1
  1663. // 剩余期数
  1664. surplusN := decimal.NewFromInt(int64(subDay2 - subDay)).Div(decimal.NewFromInt(int64(subDay))).Mul(decimal.NewFromInt(int64(currYearN)))
  1665. // 剩余余额
  1666. balance := decimal.NewFromFloat(annualValueInversionConf.Value).Sub(decimal.NewFromFloat(currYearVal))
  1667. averageVal, _ = balance.Div(surplusN).Round(4).Float64()
  1668. }
  1669. // 保留四位小数
  1670. averageVal, _ = decimal.NewFromFloat(averageVal).Round(4).Float64()
  1671. for k, currentDate := range dayList {
  1672. currentDateStr := currentDate.Format(utils.FormatDate)
  1673. tmpData := &data_manage.EdbDataList{
  1674. EdbDataId: edbInfoId + 100000 + index + k,
  1675. EdbInfoId: edbInfoId,
  1676. DataTime: currentDateStr,
  1677. Value: averageVal,
  1678. DataTimestamp: currentDate.UnixNano() / 1e6,
  1679. }
  1680. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  1681. allDataList = append(allDataList, tmpData)
  1682. existMap[currentDateStr] = averageVal
  1683. }
  1684. // 最大最小值
  1685. if averageVal < minValue {
  1686. minValue = averageVal
  1687. }
  1688. if averageVal > maxValue {
  1689. maxValue = averageVal
  1690. }
  1691. return
  1692. }
  1693. // 同比法分配
  1694. // 同比法保证每期同比相等(同比增速=余额/同比年份相应日期的余额,预测值等于同比年份同期值*同比增速);
  1695. // 同比法分配:同比增速=900/同比年份5.19的余额
  1696. yearList := annualValueInversionConf.YearList
  1697. if len(yearList) == 0 {
  1698. //兼容历史数据
  1699. yearList = append(yearList, annualValueInversionConf.Year)
  1700. }
  1701. if len(yearList) == 0 {
  1702. err = errors.New("同比年份不能为空")
  1703. return
  1704. }
  1705. // 每年截止到当前日期的累计值
  1706. dateTotalMap := make(map[time.Time]float64)
  1707. //把每一期的期数和日期绑定
  1708. dateIndexMap := make(map[time.Time]int)
  1709. indexDateMap := make(map[int]time.Time)
  1710. // 每年的累计值(计算使用)
  1711. yearTotalMap := make(map[int]float64)
  1712. //数据按找后值填充的方式处理成连续自然日日度数据
  1713. allDataListMap := make(map[string]float64)
  1714. // todo 如果是日度和周度,用后置填充变频成连续自然日日度数据。计算1/1至指标最新日期(2024/3/3/1)的累计值
  1715. switch frequency {
  1716. case "日度", "周度":
  1717. for _, v := range allDataList {
  1718. allDataListMap[v.DataTime] = v.Value
  1719. }
  1720. //找到最早日期的的年份的1月1日,转成time格式
  1721. earliestYear := allDataList[0].DataTime[:4]
  1722. earliestYearFirstDay, _ := time.ParseInLocation(utils.FormatDate, earliestYear+"-01-01", time.Local)
  1723. days := int(currDayTime.Sub(earliestYearFirstDay).Hours() / float64(24))
  1724. //循环累加日期,直到循环到最新日期
  1725. for i := 0; i <= days; i++ {
  1726. currentDate := earliestYearFirstDay.AddDate(0, 0, i)
  1727. currentDateStr := currentDate.Format(utils.FormatDate)
  1728. val, ok := allDataListMap[currentDateStr]
  1729. if !ok { //如果不存在,则填充后值
  1730. //循环向后查找数据,直到找到
  1731. for j := i + 1; j <= days; j++ {
  1732. //循环往后取值
  1733. currentDateTmp := earliestYearFirstDay.AddDate(0, 0, j)
  1734. currentDateTmpStr := currentDateTmp.Format(utils.FormatDate)
  1735. if tmpVal, ok1 := allDataListMap[currentDateTmpStr]; ok1 {
  1736. allDataListMap[currentDateStr] = tmpVal
  1737. val = tmpVal
  1738. break
  1739. }
  1740. }
  1741. }
  1742. //计算每一天的年初至今累计值
  1743. yearVal := yearTotalMap[currentDate.Year()]
  1744. if frequency == "周度" {
  1745. // 每日累计值需要当前值除7
  1746. yearVal = yearVal + val/7
  1747. } else {
  1748. yearVal = yearVal + val
  1749. }
  1750. yearTotalMap[currentDate.Year()] = yearVal
  1751. dateTotalMap[currentDate] = yearVal
  1752. dateIndexMap[currentDate] = i
  1753. indexDateMap[i] = currentDate
  1754. }
  1755. default:
  1756. for k, v := range allDataList {
  1757. currTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1758. if tmpErr != nil {
  1759. err = tmpErr
  1760. return
  1761. }
  1762. allDataListMap[v.DataTime] = v.Value
  1763. yearVal := yearTotalMap[currTime.Year()]
  1764. yearVal = yearVal + v.Value
  1765. yearTotalMap[currTime.Year()] = yearVal
  1766. dateTotalMap[currTime] = yearVal
  1767. dateIndexMap[currTime] = k
  1768. indexDateMap[k] = currTime
  1769. }
  1770. }
  1771. // 当年的余额
  1772. currYearBalance := yearValueConfig - yearTotalMap[currDayTime.Year()]
  1773. //fmt.Printf("当年的余额%.4f=给定额度%.4f-当年累计值%.4f\n", currYearBalance, yearValueConfig, yearTotalMap[currDayTime.Year()])
  1774. // 循环统计同比年份同期余额
  1775. var sum, avg float64
  1776. for _, year := range yearList {
  1777. yearTotal := yearTotalMap[year]
  1778. //fmt.Printf("同比年份的累计值%.4f\n", yearTotal)
  1779. tmpDate := time.Date(year, currDayTime.Month(), currDayTime.Day(), 0, 0, 0, 0, currDayTime.Location())
  1780. //fmt.Printf("同比年份的同期%s\n", tmpDate)
  1781. dateTotal, ok := dateTotalMap[tmpDate]
  1782. //fmt.Printf("同比年份的同期累计值%.4f\n", dateTotal)
  1783. if ok {
  1784. sum = sum + (yearTotal - dateTotal)
  1785. } else {
  1786. // 查找下一期的余额
  1787. tmpIndex, ok1 := dateIndexMap[tmpDate]
  1788. if ok1 {
  1789. for tmpDateTime := indexDateMap[tmpIndex+1]; tmpDateTime.Year() == year; tmpDateTime = indexDateMap[tmpIndex+1] {
  1790. dateTotal, ok = dateTotalMap[tmpDateTime]
  1791. if ok {
  1792. //fmt.Printf("同比年份的同期累计值%.4f\n", dateTotal)
  1793. sum = sum + (yearTotal - dateTotal)
  1794. break
  1795. }
  1796. tmpIndex += 1
  1797. }
  1798. }
  1799. }
  1800. }
  1801. if sum == 0 {
  1802. err = errors.New("同比年份的累计值为0")
  1803. return
  1804. }
  1805. //fmt.Printf("同比年份的余额%.4f\n", sum)
  1806. avg = sum / float64(len(yearList))
  1807. //fmt.Printf("同比年份的余额%.4f\n", avg)
  1808. // 同比增速=当年余额/同比年份上一期日期的余额
  1809. tbVal := decimal.NewFromFloat(currYearBalance).Div(decimal.NewFromFloat(avg))
  1810. /*tbVal11, _ := tbVal.Round(4).Float64()
  1811. fmt.Printf("同比增速%.4f\n", tbVal11)*/
  1812. //(同比增速=余额/同比年份相应日期的余额的平均值,预测值等于同比年份同期值*同比增速);
  1813. for k, currentDate := range dayList {
  1814. // 循环遍历多个同比年份
  1815. var valSum float64
  1816. for _, year := range yearList {
  1817. //多个同比年份的同期值的平均值
  1818. tmpCurrentDate := time.Date(year, currentDate.Month(), currentDate.Day(), 0, 0, 0, 0, currentDate.Location())
  1819. if tmpVal, ok := allDataListMap[tmpCurrentDate.Format(utils.FormatDate)]; ok {
  1820. valSum += tmpVal
  1821. } else {
  1822. // 查找下一期的余额
  1823. tmpIndex, ok1 := dateIndexMap[tmpCurrentDate]
  1824. if ok1 {
  1825. for tmpDateTime := indexDateMap[tmpIndex+1]; tmpDateTime.Year() == year; tmpDateTime = indexDateMap[tmpIndex+1] {
  1826. tmpVal, ok = allDataListMap[tmpDateTime.Format(utils.FormatDate)]
  1827. if ok {
  1828. valSum += tmpVal
  1829. break
  1830. }
  1831. tmpIndex += 1
  1832. }
  1833. }
  1834. }
  1835. }
  1836. lastDateVal := valSum / float64(len(yearList))
  1837. //预测值 = 同比年份同期值*同比增速
  1838. tmpVal, _ := decimal.NewFromFloat(lastDateVal).Mul(tbVal).Round(4).Float64()
  1839. currentDateStr := currentDate.Format(utils.FormatDate)
  1840. tmpData := &data_manage.EdbDataList{
  1841. EdbDataId: edbInfoId + 100000 + index + k,
  1842. EdbInfoId: edbInfoId,
  1843. DataTime: currentDateStr,
  1844. Value: tmpVal,
  1845. DataTimestamp: currentDate.UnixNano() / 1e6,
  1846. }
  1847. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  1848. allDataList = append(allDataList, tmpData)
  1849. existMap[currentDateStr] = tmpVal
  1850. yearVal := yearTotalMap[currentDate.Year()]
  1851. yearVal = yearVal + tmpVal
  1852. yearTotalMap[currentDate.Year()] = yearVal
  1853. dateTotalMap[currentDate] = yearVal
  1854. // 最大最小值
  1855. if tmpVal < minValue {
  1856. minValue = tmpVal
  1857. }
  1858. if tmpVal > maxValue {
  1859. maxValue = tmpVal
  1860. }
  1861. }
  1862. return
  1863. }