predict_edb_info_rule.go 56 KB

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