predict_edb_info_rule.go 56 KB

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