predict_edb_info_rule.go 50 KB

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