predict_edb_info_rule.go 55 KB

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