predict_edb_info.go 26 KB

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