chart_info.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_admin/models"
  5. "eta/eta_forum_admin/services"
  6. "eta/eta_forum_admin/utils"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. type ChartInfoController struct {
  12. BaseAuthController
  13. }
  14. // ChartInfoDetail
  15. // @Title 获取图表详情
  16. // @Description 获取图表详情接口
  17. // @Param ChartInfoId query int true "图表id"
  18. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  19. // @Param StartDate query string true "自定义开始日期"
  20. // @Param EndDate query string true "自定义结束日期"
  21. // @Param Calendar query string true "公历/农历"
  22. // @Param SeasonStartDate query string true "季节性图开始日期"
  23. // @Param SeasonEndDate query string true "季节性图结束日期"
  24. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  25. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  26. // @Success 200 {object} models.ChartInfoDetailResp
  27. // @router /detail [get]
  28. func (this *ChartInfoController) ChartInfoDetail() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. sysUser := this.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. chartInfoId, _ := this.GetInt("ChartInfoId")
  42. dateType, _ := this.GetInt("DateType")
  43. fmt.Println("dateType:", dateType)
  44. if dateType <= 0 {
  45. dateType = 3
  46. }
  47. startDate := this.GetString("StartDate")
  48. endDate := this.GetString("EndDate")
  49. startYear, _ := this.GetInt("StartYear")
  50. edbInfoId := this.GetString("EdbInfoId")
  51. chartType, _ := this.GetInt("ChartType")
  52. calendar := this.GetString("Calendar")
  53. if calendar == "" {
  54. calendar = "公历"
  55. }
  56. var err error
  57. chartInfo := new(models.ChartInfoView)
  58. chartInfo.HaveOperaAuth = true
  59. if chartInfoId > 0 {
  60. chartInfo, err = models.GetChartInfoViewById(chartInfoId)
  61. if err != nil {
  62. if err.Error() == utils.ErrNoRow() {
  63. br.Msg = "该图表已删除,自动查看下一图表"
  64. br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
  65. br.Ret = 406
  66. return
  67. }
  68. br.Msg = "获取失败"
  69. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  70. return
  71. }
  72. chartType = chartInfo.ChartType
  73. // 获取主题样式
  74. chartTheme, err := services.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  75. if err != nil {
  76. br.Msg = "获取失败"
  77. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  78. return
  79. }
  80. chartInfo.ChartThemeStyle = chartTheme.Config
  81. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  82. }
  83. resp := new(models.ChartInfoDetailResp)
  84. mappingList := make([]*models.ChartEdbInfoMapping, 0)
  85. if chartInfoId > 0 {
  86. mappingList, err = models.GetChartEdbMappingList(chartInfoId)
  87. if err != nil {
  88. br.Msg = "获取失败"
  89. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  90. return
  91. }
  92. } else {
  93. if edbInfoId != "" {
  94. edbInfoIds := strings.Split(edbInfoId, ",")
  95. mappingList, err = models.GetChartEdbMappingListByEdbInfoId(edbInfoIds)
  96. if err != nil {
  97. br.Msg = "获取失败"
  98. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  99. return
  100. }
  101. }
  102. }
  103. // 图表额外数据参数
  104. extraConfigStr := chartInfo.ExtraConfig
  105. // 柱方图的一些配置
  106. var barConfig models.BarChartInfoReq
  107. if chartInfo != nil && chartInfo.ChartType == 7 {
  108. if chartInfo.BarConfig == `` {
  109. br.Msg = "柱方图未配置"
  110. br.ErrMsg = "柱方图未配置"
  111. return
  112. }
  113. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  114. if err != nil {
  115. br.Msg = "柱方图配置异常"
  116. br.ErrMsg = "柱方图配置异常"
  117. return
  118. }
  119. extraConfigStr = chartInfo.BarConfig
  120. }
  121. yearMax := 0
  122. if dateType == utils.DateTypeNYears {
  123. for _, v := range mappingList {
  124. if v.LatestDate != "" {
  125. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  126. if tErr != nil {
  127. br.Msg = "获取失败"
  128. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  129. return
  130. }
  131. if lastDateT.Year() > yearMax {
  132. yearMax = lastDateT.Year()
  133. }
  134. }
  135. }
  136. }
  137. // 开始/结束日期
  138. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  139. // 获取图表中的指标数据
  140. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := services.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  141. if err != nil {
  142. br.Msg = "获取失败"
  143. if errMsg != `` {
  144. br.Msg = errMsg
  145. }
  146. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  147. return
  148. }
  149. // 单位
  150. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  151. chartInfo.Unit = yDataList[0].Unit
  152. chartInfo.UnitEn = yDataList[0].UnitEn
  153. }
  154. warnEdbList := make([]string, 0)
  155. for _, v := range edbList {
  156. if v.IsNullData {
  157. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  158. }
  159. }
  160. if len(warnEdbList) > 0 {
  161. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  162. }
  163. if chartInfoId > 0 && chartInfo != nil {
  164. if chartInfo.ChartType == 2 {
  165. if chartInfo.SeasonStartDate != "" {
  166. chartInfo.StartDate = chartInfo.SeasonStartDate
  167. chartInfo.EndDate = chartInfo.SeasonEndDate
  168. if chartInfo.DateType == 3 {
  169. chartInfo.DateType = 5
  170. }
  171. }
  172. }
  173. }
  174. // 图表的指标来源
  175. sourceNameList, sourceNameEnList := services.GetEdbSourceByEdbInfoIdList(edbList)
  176. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  177. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  178. //判断是否需要展示英文标识
  179. chartInfo.IsEnChart = services.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  180. resp.EdbInfoList = edbList
  181. resp.XEdbIdValue = xEdbIdValue
  182. resp.YDataList = yDataList
  183. resp.DataResp = dataResp
  184. chartInfo.Button = models.ChartViewButton{
  185. IsEdit: chartInfo.IsEdit,
  186. IsEnChart: chartInfo.IsEnChart,
  187. IsAdd: chartInfo.IsAdd,
  188. IsCopy: true,
  189. IsSetName: chartInfo.IsSetName,
  190. }
  191. resp.ChartInfo = chartInfo
  192. resp.BarChartInfo = barConfig
  193. br.Ret = 200
  194. br.Success = true
  195. br.Msg = "获取成功"
  196. br.Data = resp
  197. }