predict_edb_info.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/system"
  7. "eta_gn/eta_api/services/data/data_manage_permission"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "github.com/shopspring/decimal"
  11. "strconv"
  12. "time"
  13. )
  14. // RefreshPredictEdbInfo 刷新预测指标
  15. func RefreshPredictEdbInfo(edbInfoId int, refreshAll bool) (edbInfo *data_manage.EdbInfo, isAsync bool, err error, errMsg string) {
  16. // 指标信息校验
  17. {
  18. edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
  19. if err != nil && !utils.IsErrNoRow(err) {
  20. errMsg = "刷新失败"
  21. err = errors.New("获取预测指标失败,Err:" + err.Error())
  22. return
  23. }
  24. if edbInfo == nil {
  25. errMsg = "找不到该预测指标"
  26. err = nil
  27. return
  28. }
  29. //必须是预测的指标
  30. if edbInfo.EdbInfoType != 1 {
  31. errMsg = "指标异常,不是预测指标"
  32. return
  33. }
  34. }
  35. err, isAsync = EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, refreshAll, false)
  36. return
  37. }
  38. // MovePredictEdbInfo 移动预测指标
  39. func MovePredictEdbInfo(edbInfoId, classifyId, prevEdbInfoId, nextEdbInfoId int, sysUser *system.Admin, requestBody, requestUrl string) (err error, errMsg string) {
  40. //判断分类是否存在
  41. count, _ := data_manage.GetEdbClassifyCountById(classifyId)
  42. if count <= 0 {
  43. errMsg = "分类已被删除,不可移动,请刷新页面"
  44. return
  45. }
  46. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  47. if err != nil {
  48. if err != nil && !utils.IsErrNoRow(err) {
  49. errMsg = "移动失败"
  50. err = errors.New("获取预测指标失败,Err:" + err.Error())
  51. return
  52. }
  53. if edbInfo == nil {
  54. errMsg = "找不到该预测指标"
  55. err = nil
  56. return
  57. }
  58. return
  59. }
  60. var haveOperaAuth bool
  61. // 权限校验
  62. {
  63. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, sysUser.AdminId)
  64. if err != nil {
  65. errMsg = "移动失败"
  66. err = errors.New("校验指标权限失败,Err:" + err.Error())
  67. return
  68. }
  69. }
  70. // 移动权限校验
  71. button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  72. if !button.MoveButton {
  73. errMsg = "无权限操作"
  74. err = nil
  75. return
  76. }
  77. //如果改变了分类,那么移动该指标数据
  78. if edbInfo.ClassifyId != classifyId {
  79. err = data_manage.MoveEdbInfo(edbInfoId, classifyId)
  80. if err != nil {
  81. errMsg = "移动失败"
  82. err = errors.New("移动预测指标失败,Err:" + err.Error())
  83. return
  84. }
  85. }
  86. updateCol := make([]string, 0)
  87. //如果有传入 上一个兄弟节点分类id
  88. if prevEdbInfoId > 0 {
  89. prevEdbInfo, tmpErr := data_manage.GetEdbInfoById(prevEdbInfoId)
  90. if tmpErr != nil {
  91. errMsg = "移动失败"
  92. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
  93. return
  94. }
  95. //如果是移动在两个兄弟节点之间
  96. if nextEdbInfoId > 0 {
  97. //下一个兄弟节点
  98. nextEdbInfo, tmpErr := data_manage.GetEdbInfoById(nextEdbInfoId)
  99. if tmpErr != nil {
  100. errMsg = "移动失败"
  101. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
  102. return
  103. }
  104. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  105. if prevEdbInfo.Sort == nextEdbInfo.Sort || prevEdbInfo.Sort == edbInfo.Sort {
  106. //变更兄弟节点的排序
  107. updateSortStr := `sort + 2`
  108. _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
  109. } else {
  110. //如果下一个兄弟的排序权重正好是上个兄弟节点 的下一层,那么需要再加一层了
  111. if nextEdbInfo.Sort-prevEdbInfo.Sort == 1 {
  112. //变更兄弟节点的排序
  113. updateSortStr := `sort + 1`
  114. _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
  115. }
  116. }
  117. }
  118. edbInfo.Sort = prevEdbInfo.Sort + 1
  119. edbInfo.ModifyTime = time.Now()
  120. updateCol = append(updateCol, "Sort", "ModifyTime")
  121. } else {
  122. firstClassify, tmpErr := data_manage.GetFirstEdbInfoByClassifyId(classifyId)
  123. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  124. errMsg = "移动失败"
  125. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error())
  126. return
  127. }
  128. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  129. if firstClassify != nil && firstClassify.ClassifyId > 0 && firstClassify.Sort == 0 {
  130. updateSortStr := ` sort + 1 `
  131. _ = data_manage.UpdateEdbInfoSortByClassifyId(firstClassify.ClassifyId, 0, firstClassify.EdbInfoId-1, updateSortStr)
  132. }
  133. edbInfo.Sort = 0 //那就是排在第一位
  134. edbInfo.ModifyTime = time.Now()
  135. updateCol = append(updateCol, "Sort", "ModifyTime")
  136. }
  137. //更新
  138. if len(updateCol) > 0 {
  139. err = edbInfo.Update(updateCol)
  140. }
  141. if err != nil {
  142. errMsg = "移动失败"
  143. err = errors.New("修改失败,Err:" + err.Error())
  144. return
  145. }
  146. //新增操作日志
  147. {
  148. edbLog := new(data_manage.EdbInfoLog)
  149. edbLog.EdbInfoId = edbInfo.EdbInfoId
  150. edbLog.SourceName = edbInfo.SourceName
  151. edbLog.Source = edbInfo.Source
  152. edbLog.EdbCode = edbInfo.EdbCode
  153. edbLog.EdbName = edbInfo.EdbName
  154. edbLog.ClassifyId = edbInfo.ClassifyId
  155. edbLog.SysUserId = sysUser.AdminId
  156. edbLog.SysUserRealName = sysUser.RealName
  157. edbLog.CreateTime = time.Now()
  158. edbLog.Content = requestBody
  159. edbLog.Status = "移动指标"
  160. edbLog.Method = requestUrl
  161. go data_manage.AddEdbInfoLog(edbLog)
  162. }
  163. return
  164. }
  165. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  166. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []data_manage.PredictEdbConfAndData, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*data_manage.EdbDataList) (predictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error, errMsg string) {
  167. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  168. if err != nil {
  169. return
  170. }
  171. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  172. if err != nil {
  173. return
  174. }
  175. // 开始预测数据的时间
  176. startDate := latestDate
  177. // 如果有筛选时间的话
  178. if filtrateStartDateStr != `` {
  179. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  180. if tmpErr != nil {
  181. err = tmpErr
  182. return
  183. }
  184. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  185. if filtrateStartDate.After(latestDate) {
  186. startDate = filtrateStartDate.AddDate(0, 0, -1)
  187. }
  188. }
  189. //var dateArr []string
  190. // 对应日期的值
  191. existMap := make(map[string]float64)
  192. for _, v := range realPredictEdbInfoData {
  193. //dateArr = append(dateArr, v.DataTime)
  194. existMap[v.DataTime] = v.Value
  195. }
  196. predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
  197. //dataValue := lastDataValue
  198. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  199. for _, predictEdbConf := range predictEdbConfList {
  200. dataEndTime := endDate
  201. if predictEdbConf.EndDate.Before(dataEndTime) {
  202. dataEndTime = predictEdbConf.EndDate
  203. }
  204. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  205. dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
  206. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  207. continue
  208. }
  209. switch predictEdbConf.RuleType {
  210. case 1: //1:最新
  211. var lastDataValue float64 //最新值
  212. tmpAllData := make([]*data_manage.EdbDataList, 0)
  213. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  214. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  215. lenTmpAllData := len(tmpAllData)
  216. if lenTmpAllData > 0 {
  217. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  218. }
  219. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  220. tmpMaxValue = lastDataValue
  221. tmpMinValue = lastDataValue
  222. case 2: //2:固定值
  223. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  224. if tmpErr != nil {
  225. err = tmpErr
  226. return
  227. }
  228. dataValue, _ := tmpValDecimal.Float64()
  229. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  230. tmpMaxValue = dataValue
  231. tmpMinValue = dataValue
  232. case 3: //3:同比
  233. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  234. if tmpErr != nil {
  235. err = tmpErr
  236. return
  237. }
  238. tbValue, _ := tmpValDecimal.Float64()
  239. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  240. case 4: //4:同差
  241. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  242. if tmpErr != nil {
  243. err = tmpErr
  244. return
  245. }
  246. tcValue, _ := tmpValDecimal.Float64()
  247. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  248. case 5: //5:环比
  249. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  250. if tmpErr != nil {
  251. err = tmpErr
  252. return
  253. }
  254. hbValue, _ := tmpValDecimal.Float64()
  255. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  256. case 6: //6:环差
  257. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  258. if tmpErr != nil {
  259. err = tmpErr
  260. return
  261. }
  262. hcValue, _ := tmpValDecimal.Float64()
  263. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  264. case 7: //7:N期移动均值
  265. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  266. if tmpErr != nil {
  267. err = tmpErr
  268. return
  269. }
  270. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  271. case 8: //8:N期段线性外推值
  272. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  273. if tmpErr != nil {
  274. err = tmpErr
  275. return
  276. }
  277. if nValue <= 1 {
  278. errMsg = `N期段线性外推值的N值必须大于1`
  279. err = errors.New(errMsg)
  280. return
  281. }
  282. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  283. if err != nil {
  284. return
  285. }
  286. case 9: //9:动态环差”预测规则;
  287. //规则计算的环差值map
  288. hcDataMap := make(map[string]float64)
  289. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  290. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  291. if tmpErr != nil {
  292. err = tmpErr
  293. return
  294. }
  295. for _, v := range tmpPredictEdbRuleDataList {
  296. hcDataMap[v.DataTime] = v.Value
  297. }
  298. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  299. if len(predictEdbConf.DataList) <= 0 {
  300. return
  301. }
  302. for _, v := range predictEdbConf.DataList {
  303. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  304. if tmpErr != nil {
  305. continue
  306. }
  307. // 只处理时间段内的数据
  308. if currentDate.Before(startDate) || currentDate.After(endDate) {
  309. continue
  310. }
  311. hcDataMap[v.DataTime] = v.Value
  312. }
  313. }
  314. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, hcDataMap, existMap)
  315. case 10: //10:根据 给定终值后插值 规则获取预测数据
  316. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  317. if tmpErr != nil {
  318. err = tmpErr
  319. return
  320. }
  321. finalValue, _ := tmpValDecimal.Float64()
  322. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  323. case 11: //11:根据 季节性 规则获取预测数据
  324. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  325. if err != nil {
  326. return
  327. }
  328. case 12: //12:根据 移动平均同比 规则获取预测数据
  329. var moveAverageConf MoveAverageConf
  330. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  331. if tmpErr != nil {
  332. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  333. return
  334. }
  335. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  336. if err != nil {
  337. return
  338. }
  339. case 13: //13:根据 同比增速差值 规则获取预测数据
  340. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  341. if tmpErr != nil {
  342. err = tmpErr
  343. return
  344. }
  345. tbEndValue, _ := tmpValDecimal.Float64()
  346. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  347. case 14: //14:根据 一元线性拟合 规则获取预测数据
  348. var ruleConf RuleLineNhConf
  349. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  350. if err != nil {
  351. err = errors.New("一元线性拟合配置信息异常:" + err.Error())
  352. return
  353. }
  354. // 规则计算的拟合残差值map
  355. newNhccDataMap := make(map[string]float64)
  356. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  357. tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, "", "")
  358. if tmpErr != nil {
  359. err = tmpErr
  360. return
  361. }
  362. for _, v := range tmpPredictEdbRuleDataList {
  363. newNhccDataMap[v.DataTime] = v.Value
  364. }
  365. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  366. newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  367. if err != nil {
  368. return
  369. }
  370. }
  371. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  372. if err != nil {
  373. return
  374. }
  375. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  376. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  377. if err != nil {
  378. return
  379. }
  380. case 16: //16:年度值倒推
  381. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  382. if err != nil {
  383. return
  384. }
  385. }
  386. // 下一个规则的开始日期
  387. {
  388. lenPredictEdbInfoData := len(predictEdbInfoData)
  389. if lenPredictEdbInfoData > 0 {
  390. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  391. if startDate.Before(tmpDataEndTime) {
  392. startDate = tmpDataEndTime
  393. }
  394. }
  395. }
  396. if tmpMinValue < minValue {
  397. minValue = tmpMinValue
  398. }
  399. if tmpMaxValue > maxValue {
  400. maxValue = tmpMaxValue
  401. }
  402. }
  403. return
  404. }
  405. // GetPredictEdbDayList 获取预测指标日期列表
  406. func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
  407. //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
  408. if dataDateType == `` {
  409. dataDateType = `交易日`
  410. }
  411. switch frequency {
  412. case "日度":
  413. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  414. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  415. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  416. continue
  417. }
  418. dayList = append(dayList, currDate)
  419. }
  420. case "周度":
  421. //nextDate := startDate.AddDate(0, 0, 7)
  422. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  423. dayList = append(dayList, currDate)
  424. }
  425. case "旬度":
  426. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  427. nextDate := currDate.AddDate(0, 0, 1)
  428. //每个月的10号、20号、最后一天,那么就写入
  429. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  430. dayList = append(dayList, currDate)
  431. }
  432. currDate = nextDate
  433. }
  434. case "月度":
  435. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  436. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  437. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  438. dayList = append(dayList, currDate)
  439. }
  440. currDate = currDate.AddDate(0, 0, 1)
  441. }
  442. case "季度":
  443. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  444. // 每月的最后一天
  445. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  446. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  447. // 季度日期就写入,否则不写入
  448. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  449. dayList = append(dayList, currDate)
  450. }
  451. }
  452. currDate = currDate.AddDate(0, 0, 1)
  453. }
  454. case "半年度":
  455. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  456. // 每月的最后一天
  457. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  458. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  459. // 半年度日期就写入,否则不写入
  460. if currDate.Month() == 6 || currDate.Month() == 12 {
  461. dayList = append(dayList, currDate)
  462. }
  463. }
  464. currDate = currDate.AddDate(0, 0, 1)
  465. }
  466. case "年度":
  467. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  468. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  469. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  470. dayList = append(dayList, currDate)
  471. }
  472. }
  473. }
  474. return
  475. }
  476. // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据(日期正序返回)
  477. func GetPredictDataListByPredictEdbInfoId(edbInfoId int, startDate, endDate string, isTimeBetween bool) (edbInfo *data_manage.EdbInfo, dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  478. edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
  479. if err != nil {
  480. errMsg = `获取预测指标信息失败`
  481. return
  482. }
  483. dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  484. return
  485. }
  486. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
  487. func GetPredictDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  488. if !isTimeBetween { //如果不是区间数据,那么就结束日期为空
  489. endDate = ``
  490. }
  491. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  492. // 非计算指标,直接从表里获取数据
  493. if edbInfo.EdbType != 1 {
  494. if !isTimeBetween { //如果不是区间数据,那么就结束日期为空
  495. endDate = ``
  496. }
  497. return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
  498. }
  499. // 查找该预测指标配置
  500. predictEdbConfList, err := data_manage.GetPredictEdbConfListById(edbInfo.EdbInfoId)
  501. if err != nil && !utils.IsErrNoRow(err) {
  502. errMsg = "获取预测指标配置信息失败"
  503. return
  504. }
  505. if len(predictEdbConfList) == 0 {
  506. errMsg = "获取预测指标配置信息失败"
  507. err = errors.New(errMsg)
  508. return
  509. }
  510. predictEdbConf = predictEdbConfList[0]
  511. // 来源指标
  512. sourceEdbInfoItem, err = data_manage.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  513. if err != nil {
  514. if utils.IsErrNoRow(err) {
  515. errMsg = "找不到来源指标信息"
  516. err = errors.New(errMsg)
  517. }
  518. return
  519. }
  520. allDataList := make([]*data_manage.EdbDataList, 0)
  521. //获取指标数据(实际已生成)
  522. dataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  523. if err != nil {
  524. return
  525. }
  526. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  527. if startDate != `` {
  528. allDataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, "", "")
  529. if err != nil {
  530. return
  531. }
  532. } else {
  533. allDataList = dataList
  534. }
  535. // 获取预测指标未来的数据
  536. predictDataList := make([]*data_manage.EdbDataList, 0)
  537. endDateStr := utils.TimeToFormatDate(edbInfo.EndDate) //预测指标的结束日期
  538. if isTimeBetween && endDate != `` { //如果是时间区间,同时截止日期不为空的情况,那么
  539. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  540. endDateTime := edbInfo.EndDate
  541. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  542. if reqEndDateTime.Before(endDateTime) {
  543. endDateStr = endDate
  544. }
  545. }
  546. //predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate, sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
  547. predictEdbConfDataList := make([]data_manage.PredictEdbConfAndData, 0)
  548. for _, v := range predictEdbConfList {
  549. predictEdbConfDataList = append(predictEdbConfDataList, data_manage.PredictEdbConfAndData{
  550. ConfigId: v.ConfigId,
  551. PredictEdbInfoId: v.PredictEdbInfoId,
  552. SourceEdbInfoId: v.SourceEdbInfoId,
  553. RuleType: v.RuleType,
  554. FixedValue: v.FixedValue,
  555. Value: v.Value,
  556. EndDate: v.EndDate,
  557. ModifyTime: v.ModifyTime,
  558. CreateTime: v.CreateTime,
  559. DataList: make([]*data_manage.EdbDataList, 0),
  560. })
  561. }
  562. //var predictMinValue, predictMaxValue float64
  563. predictDataList, _, _, err, _ = GetChartPredictEdbInfoDataListByConfList(predictEdbConfDataList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, edbInfo.Frequency, edbInfo.DataDateType, allDataList)
  564. if err != nil {
  565. return
  566. }
  567. dataList = append(dataList, predictDataList...)
  568. //if len(predictDataList) > 0 {
  569. // // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  570. // if edbInfo.MinValue > predictMinValue {
  571. // edbInfo.MinValue = predictMinValue
  572. // }
  573. //
  574. // // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  575. // if edbInfo.MaxValue < predictMaxValue {
  576. // edbInfo.MaxValue = predictMaxValue
  577. // }
  578. //}
  579. return
  580. }
  581. // GetChartDataList 通过完整的预测数据 进行 季节性图、公历、农历处理
  582. func GetChartDataList(dataList []*data_manage.EdbDataList, chartType int, calendar, latestDateStr, startDate string) (resultDataList interface{}, err error) {
  583. startDateReal := startDate
  584. calendarPreYear := 0
  585. if calendar == "农历" {
  586. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  587. if err != nil {
  588. fmt.Println("time.Parse:" + err.Error())
  589. }
  590. calendarPreYear = newStartDateReal.Year() - 1
  591. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  592. startDateReal = newStartDateReal.Format(utils.FormatDate)
  593. }
  594. //实际数据的截止日期
  595. latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr)
  596. if tmpErr != nil {
  597. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr))
  598. return
  599. }
  600. latestDateYear := latestDate.Year() //实际数据截止年份
  601. // 曲线图
  602. if chartType == 1 {
  603. resultDataList = dataList
  604. return
  605. }
  606. if calendar == "农历" {
  607. if len(dataList) <= 0 {
  608. resultDataList = data_manage.EdbDataResult{}
  609. } else {
  610. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  611. if tmpErr != nil {
  612. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  613. return
  614. }
  615. // 处理季节图的截止日期
  616. for k, edbDataItems := range result.List {
  617. var cuttingDataTimestamp int64
  618. // 切割的日期时间字符串
  619. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  620. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  621. if edbDataItems.Year >= latestDateYear {
  622. for _, tmpData := range edbDataItems.Items {
  623. if tmpData.DataTime == cuttingDataTimeStr {
  624. cuttingDataTimestamp = tmpData.DataTimestamp
  625. break
  626. }
  627. }
  628. }
  629. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  630. result.List[k] = edbDataItems
  631. }
  632. if result.List[0].Year != calendarPreYear {
  633. itemList := make([]*data_manage.EdbDataList, 0)
  634. items := new(data_manage.EdbDataItems)
  635. //items.Year = calendarPreYear
  636. items.Items = itemList
  637. newResult := new(data_manage.EdbDataResult)
  638. newResult.List = append(newResult.List, items)
  639. newResult.List = append(newResult.List, result.List...)
  640. resultDataList = newResult
  641. } else {
  642. resultDataList = result
  643. }
  644. }
  645. } else {
  646. currentYear := time.Now().Year()
  647. quarterDataList := make([]*data_manage.QuarterData, 0)
  648. quarterMap := make(map[int][]*data_manage.EdbDataList)
  649. var quarterArr []int
  650. for _, v := range dataList {
  651. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  652. if tmpErr != nil {
  653. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  654. return
  655. }
  656. year := itemDate.Year()
  657. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  658. timestamp := newItemDate.UnixNano() / 1e6
  659. v.DataTimestamp = timestamp
  660. if findVal, ok := quarterMap[year]; !ok {
  661. quarterArr = append(quarterArr, year)
  662. findVal = append(findVal, v)
  663. quarterMap[year] = findVal
  664. } else {
  665. findVal = append(findVal, v)
  666. quarterMap[year] = findVal
  667. }
  668. }
  669. for _, v := range quarterArr {
  670. itemList := quarterMap[v]
  671. quarterItem := new(data_manage.QuarterData)
  672. quarterItem.Year = v
  673. quarterItem.DataList = itemList
  674. //如果等于最后的实际日期,那么将切割时间戳记录
  675. if v == latestDateYear {
  676. var cuttingDataTimestamp int64
  677. for _, tmpData := range itemList {
  678. if tmpData.DataTime == latestDateStr {
  679. cuttingDataTimestamp = tmpData.DataTimestamp
  680. break
  681. }
  682. }
  683. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  684. } else if v > latestDateYear {
  685. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  686. if len(itemList) > 0 {
  687. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  688. }
  689. }
  690. quarterDataList = append(quarterDataList, quarterItem)
  691. }
  692. resultDataList = quarterDataList
  693. }
  694. return
  695. }
  696. // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
  697. func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string) (dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
  698. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
  699. return
  700. }
  701. // ModifyPredictEdbBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的基础信息
  702. func ModifyPredictEdbBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, frequency, unit string) {
  703. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  704. if err != nil {
  705. return
  706. }
  707. for _, v := range list {
  708. v.Frequency = frequency
  709. v.Unit = unit
  710. v.Update([]string{"Frequency", "Unit"})
  711. AddOrEditEdbInfoToEs(v.EdbInfoId)
  712. }
  713. }
  714. // ModifyPredictEdbEnBaseInfoBySourceEdb 根据来源ETA指标修改预测指标的英文基础信息
  715. func ModifyPredictEdbEnBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, unitEn string) {
  716. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
  717. if err != nil {
  718. return
  719. }
  720. for _, v := range list {
  721. v.UnitEn = unitEn
  722. v.Update([]string{"UnitEn"})
  723. AddOrEditEdbInfoToEs(v.EdbInfoId)
  724. }
  725. }
  726. // ModifyPredictEdbUnitBySourceEdbInfoId
  727. // @Description: 根据来源ETA指标修改预测指标的频度和单位基础信息
  728. // @author: Roc
  729. // @datetime 2024-01-05 11:07:39
  730. // @param sourceEdbInfoId int
  731. // @param frequency string
  732. // @param unit string
  733. // @return err error
  734. func ModifyPredictEdbUnitBySourceEdbInfoId(sourceEdbInfoId int, frequency, unit string) (err error) {
  735. list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEdbInfoId)
  736. if err != nil {
  737. return
  738. }
  739. for _, v := range list {
  740. v.Frequency = frequency
  741. v.Unit = unit
  742. v.Update([]string{"Frequency", "Unit"})
  743. AddOrEditEdbInfoToEs(v.EdbInfoId)
  744. }
  745. return
  746. }