predict_edb_info_rule.go 55 KB

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