package controllers import ( "encoding/json" "eta/eta_forum_admin/models" "eta/eta_forum_admin/services" "eta/eta_forum_admin/utils" "fmt" "strings" "time" ) type ChartInfoController struct { BaseAuthController } // ChartInfoDetail // @Title 获取图表详情 // @Description 获取图表详情接口 // @Param ChartInfoId query int true "图表id" // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间" // @Param StartDate query string true "自定义开始日期" // @Param EndDate query string true "自定义结束日期" // @Param Calendar query string true "公历/农历" // @Param SeasonStartDate query string true "季节性图开始日期" // @Param SeasonEndDate query string true "季节性图结束日期" // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开" // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图" // @Success 200 {object} models.ChartInfoDetailResp // @router /detail [get] func (this *ChartInfoController) ChartInfoDetail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } chartInfoId, _ := this.GetInt("ChartInfoId") dateType, _ := this.GetInt("DateType") fmt.Println("dateType:", dateType) if dateType <= 0 { dateType = 3 } startDate := this.GetString("StartDate") endDate := this.GetString("EndDate") startYear, _ := this.GetInt("StartYear") edbInfoId := this.GetString("EdbInfoId") chartType, _ := this.GetInt("ChartType") calendar := this.GetString("Calendar") if calendar == "" { calendar = "公历" } var err error chartInfo := new(models.ChartInfoView) chartInfo.HaveOperaAuth = true if chartInfoId > 0 { chartInfo, err = models.GetChartInfoViewById(chartInfoId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "该图表已删除,自动查看下一图表" br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error() br.Ret = 406 return } br.Msg = "获取失败" br.ErrMsg = "获取图表信息失败,Err:" + err.Error() return } chartType = chartInfo.ChartType // 获取主题样式 chartTheme, err := services.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取主题信息失败,Err:" + err.Error() return } chartInfo.ChartThemeStyle = chartTheme.Config chartInfo.ChartThemeId = chartTheme.ChartThemeId } resp := new(models.ChartInfoDetailResp) mappingList := make([]*models.ChartEdbInfoMapping, 0) if chartInfoId > 0 { mappingList, err = models.GetChartEdbMappingList(chartInfoId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error() return } } else { if edbInfoId != "" { edbInfoIds := strings.Split(edbInfoId, ",") mappingList, err = models.GetChartEdbMappingListByEdbInfoId(edbInfoIds) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error() return } } } // 图表额外数据参数 extraConfigStr := chartInfo.ExtraConfig // 柱方图的一些配置 var barConfig models.BarChartInfoReq if chartInfo != nil && chartInfo.ChartType == 7 { if chartInfo.BarConfig == `` { br.Msg = "柱方图未配置" br.ErrMsg = "柱方图未配置" return } err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig) if err != nil { br.Msg = "柱方图配置异常" br.ErrMsg = "柱方图配置异常" return } extraConfigStr = chartInfo.BarConfig } yearMax := 0 if dateType == utils.DateTypeNYears { for _, v := range mappingList { if v.LatestDate != "" { lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate) if tErr != nil { br.Msg = "获取失败" br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error() return } if lastDateT.Year() > yearMax { yearMax = lastDateT.Year() } } } } // 开始/结束日期 startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax) // 获取图表中的指标数据 edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := services.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig) if err != nil { br.Msg = "获取失败" if errMsg != `` { br.Msg = errMsg } br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error() return } // 单位 if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 { chartInfo.Unit = yDataList[0].Unit chartInfo.UnitEn = yDataList[0].UnitEn } warnEdbList := make([]string, 0) for _, v := range edbList { if v.IsNullData { warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")") } } if len(warnEdbList) > 0 { chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",") } if chartInfoId > 0 && chartInfo != nil { if chartInfo.ChartType == 2 { if chartInfo.SeasonStartDate != "" { chartInfo.StartDate = chartInfo.SeasonStartDate chartInfo.EndDate = chartInfo.SeasonEndDate if chartInfo.DateType == 3 { chartInfo.DateType = 5 } } } } // 图表的指标来源 sourceNameList, sourceNameEnList := services.GetEdbSourceByEdbInfoIdList(edbList) chartInfo.ChartSource = strings.Join(sourceNameList, ",") chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",") //判断是否需要展示英文标识 chartInfo.IsEnChart = services.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType) resp.EdbInfoList = edbList resp.XEdbIdValue = xEdbIdValue resp.YDataList = yDataList resp.DataResp = dataResp chartInfo.Button = models.ChartViewButton{ IsEdit: chartInfo.IsEdit, IsEnChart: chartInfo.IsEnChart, IsAdd: chartInfo.IsAdd, IsCopy: true, IsSetName: chartInfo.IsSetName, } resp.ChartInfo = chartInfo resp.BarChartInfo = barConfig br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp }