predict_edb_info.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package edb_info
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. "hongze/hongze_yb/models/request"
  6. edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
  7. "hongze/hongze_yb/services/chart"
  8. "hongze/hongze_yb/utils"
  9. "strconv"
  10. "time"
  11. )
  12. // PredictEdbData 预测指标数据
  13. func PredictEdbData(c *gin.Context) {
  14. var req request.PredictEdbInfoDataReq
  15. if err := c.Bind(&req); err != nil {
  16. response.Fail("参数有误", c)
  17. return
  18. }
  19. if req.EdbInfoId <= 0 {
  20. response.Fail("参数错误", c)
  21. return
  22. }
  23. if req.DateType <= 0 {
  24. req.DateType = 3
  25. }
  26. if req.EdbInfoType <= 0 {
  27. req.EdbInfoType = 1
  28. }
  29. if req.ChartType <= 0 {
  30. req.ChartType = 1
  31. }
  32. if req.Calendar == "" {
  33. req.Calendar = "公历"
  34. }
  35. isTimeBetween := false //是否是时间区间
  36. if req.ChartType == 2 {
  37. req.DateType = 0
  38. isTimeBetween = true
  39. }
  40. if req.DateType == 5 {
  41. isTimeBetween = true
  42. }
  43. edbInfo, e := edbInfoModel.GetEdbInfoById(req.EdbInfoId)
  44. if e != nil {
  45. if e == utils.ErrNoRow {
  46. response.FailMsg("获取失败", "指标不存在,EdbInfoId:"+strconv.Itoa(req.EdbInfoId), c)
  47. return
  48. }
  49. response.FailMsg("获取失败", "获取指标信息失败,Err:"+e.Error(), c)
  50. return
  51. }
  52. maxYear := 0
  53. if !edbInfo.LatestDate.IsZero() {
  54. maxYear = edbInfo.LatestDate.Year()
  55. }
  56. startDate, endDate := utils.GetDateByDateTypeV2(req.DateType, req.StartDate, req.EndDate, req.StartYear, maxYear)
  57. if endDate == "" {
  58. endDate = time.Now().Format(utils.FormatDate)
  59. }
  60. if req.DateType == 10 {
  61. startDate = ""
  62. endDate = ""
  63. }
  64. dataList, _, _, e, _ := chart.GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  65. if e != nil {
  66. response.FailMsg("获取失败", "获取预测指标数据失败, Err: "+e.Error(), c)
  67. return
  68. }
  69. // 季节性图、公历、农历处理
  70. resultDataList, err := chart.GetChartDataList(dataList, req.ChartType, req.Calendar, edbInfo.LatestDate.Format(utils.FormatDate), startDate)
  71. if err != nil {
  72. response.FailMsg("获取失败", "季节性图、公历、农历处理失败, Err: "+e.Error(), c)
  73. return
  74. }
  75. type PredictEdbInfoDataResp struct {
  76. EdbInfo *edbInfoModel.EdbInfo
  77. DataList interface{}
  78. }
  79. resp := new(PredictEdbInfoDataResp)
  80. resp.EdbInfo = edbInfo
  81. resp.DataList = resultDataList
  82. response.OkData("获取成功", resp, c)
  83. }