predict_edb_info.go 30 KB

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