package controllers import ( "encoding/json" "eta/eta_forum_hub/models" "eta/eta_forum_hub/services" "eta/eta_forum_hub/utils" "strings" "time" ) type ChartInfoController struct { BaseAuthController } // Update // @Title 图表-更新 // @Description 图表-更新 // @Param request body data_manage.AddChartReq true "type json string" // @Success 200 {object} data_manage.AddChartInfoResp // @router /update [post] func (this *ChartInfoController) Update() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() /*deleteCache := true cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId) defer func() { if deleteCache { utils.Rc.Delete(cacheKey) } }() if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) { deleteCache = false br.Msg = "系统处理中,请稍后重试!" br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody) return }*/ var req models.AddChartReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ChartInfo == nil { br.Msg = "请选择图表" return } if req.ChartInfo.ChartInfoId <= 0 { br.Msg = "请选择图表" return } //校验图表名称是否重复 req.ChartInfo.ChartName = strings.Trim(req.ChartInfo.ChartName, " ") if req.ChartInfo.ChartName == "" { br.Msg = "请填写图表名称!" return } if len(req.EdbInfoList) <= 0 { br.Msg = "指标信息不能为空" return } for _, edbInfo := range req.EdbInfoList { if edbInfo.EdbName == "" { br.Msg = "指标名称不能为空" return } } if len(req.ChartEdbMapping) <= 0 { br.Msg = "图表指标映射不能为空" return } chartInfo, err, errMsg, _ := services.UpdateChartInfoAndEdbInfo(&req, 0, "") if err != nil { br.Msg = errMsg br.ErrMsg = err.Error() return } br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = models.AddChartInfoResp{ ChartInfoId: chartInfo.ChartInfoId, UniqueCode: chartInfo.UniqueCode, ChartType: chartInfo.ChartType, } return } // Save // @Title 图表-保存 // @Description 图表-保存 // @Param request body data_manage.AddChartReq true "type json string" // @Success 200 {object} data_manage.AddChartInfoResp // @router /save [post] func (this *ChartInfoController) Save() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.AddChartReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ChartInfo == nil { br.Msg = "请选择图表" return } //校验图表名称是否重复 req.ChartInfo.ChartName = strings.Trim(req.ChartInfo.ChartName, " ") if req.ChartInfo.ChartName == "" { br.Msg = "请填写图表名称!" return } if len(req.EdbInfoList) <= 0 { br.Msg = "指标信息不能为空" return } for _, edbInfo := range req.EdbInfoList { if edbInfo.EdbName == "" { br.Msg = "指标名称不能为空" return } } if len(req.ChartEdbMapping) <= 0 { br.Msg = "图表指标映射不能为空" return } deleteCache := true cacheKey := "eta_forum_hub:CACHE_CHART_INFO_ADD:" + req.ChartInfo.ChartName defer func() { if deleteCache { utils.Rc.Delete(cacheKey) } }() if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) { deleteCache = false br.Msg = "系统处理中,请稍后重试!" br.ErrMsg = "系统处理中,请稍后重试!;data:" + string(this.Ctx.Input.RequestBody) return } chartInfo, err, errMsg, _ := services.AddChartInfo(&req, 0, "") if err != nil { br.Msg = errMsg br.ErrMsg = err.Error() return } br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = models.AddChartInfoResp{ ChartInfoId: chartInfo.ChartInfoId, UniqueCode: chartInfo.UniqueCode, ChartType: chartInfo.ChartType, } return } // Delete // @Title 图表-删除 // @Description 图表-删除 // @Param request body data_manage.DeleteChartReq true "type json string" // @Success 200 {object} // @router /delete [post] func (this *ChartInfoController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.DeleteChartReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ChartInfoId <= 0 { br.Msg = "请选择图表" return } err, errMsg := services.DeleteChart(req.ChartInfoId) if err != nil { br.Msg = errMsg br.ErrMsg = err.Error() return } br.Ret = 200 br.Success = true br.Msg = "删除成功" }