predict_edb_info_rule.go 55 KB

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