package controllers import ( "encoding/json" "eta/eta_index_lib/logic" "eta/eta_index_lib/models" "eta/eta_index_lib/utils" "fmt" "strconv" "strings" "time" ) // PredictController 预测指标 type PredictController struct { BaseAuthController } // Save // @Title 新增/编辑预测指标运算接口 // @Description 新增预测指标运算接口 // @Success 200 {object} models.EdbInfoCalculateSaveReq // @router /save [post] func (this *PredictController) Save() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.AddPredictEdbInfoReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.EdbInfoId <= 0 { addPredict(br, req) } else { editPredict(br, req) } } // AddPredictCalculate 添加预测指标运算 func addPredict(br *models.BaseResponse, req models.AddPredictEdbInfoReq) { //加入缓存机制,避免创建同一个名称的指标 start redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT, ":", req.EdbName) isExist := utils.Rc.IsExist(redisKey) if isExist { br.Msg = "指标正在处理,请勿重复提交" return } else { //设置3分钟缓存 utils.Rc.SetNX(redisKey, 1, time.Second*300) defer func() { _ = utils.Rc.Delete(redisKey) }() } // 添加指标 edbInfo, err, errMsg := logic.AddPredictEdbInfo(req.SourceEdbInfoId, req.ClassifyId, req.EdbName, req.DataDateType, req.RuleList, req.MinValue, req.MaxValue, req.AdminId, req.AdminName) if err != nil { br.Msg = "保存失败" if errMsg != `` { br.Msg = errMsg } br.ErrMsg = err.Error() return } resp := models.AddEdbInfoResp{ EdbInfoId: edbInfo.EdbInfoId, UniqueCode: edbInfo.UniqueCode, } br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = resp br.IsAddLog = true } // editPredictCalculate 编辑预测指标运算 func editPredict(br *models.BaseResponse, req models.AddPredictEdbInfoReq) { req.EdbName = strings.Trim(req.EdbName, " ") //加入缓存机制,避免创建同一个名称的指标 start redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT, ":", req.EdbName) isExist := utils.Rc.IsExist(redisKey) if isExist { br.Msg = "指标正在处理,请勿重复提交" return } else { //设置3分钟缓存 utils.Rc.SetNX(redisKey, 1, time.Second*300) defer func() { _ = utils.Rc.Delete(redisKey) }() } // 编辑指标 edbInfo, err, errMsg := logic.EditPredictEdbInfo(req.EdbInfoId, req.ClassifyId, req.EdbName, req.DataDateType, req.RuleList, req.MinValue, req.MaxValue) if err != nil { br.Msg = "保存失败" if errMsg != `` { br.Msg = errMsg } br.ErrMsg = err.Error() return } resp := models.AddEdbInfoResp{ EdbInfoId: edbInfo.EdbInfoId, UniqueCode: edbInfo.UniqueCode, } br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = resp br.IsAddLog = true } // Refresh // @Title 刷新预测指标接口(有些预测指标里面含有动态数据) // @Description 刷新预测指标接口(有些预测指标里面含有动态数据) // @Success 200 {object} models.RefreshEdbInfoReq // @router /refresh [post] func (this *PredictController) Refresh() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.RefreshEdbInfoReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.EdbCode == "" { br.Msg = "请输入指标编码!" br.ErrMsg = "请输入指标编码,指标编码为空" return } if req.EdbInfoId <= 0 { br.Msg = "请输入指标ID!" br.ErrMsg = "请输入指标ID" return } edbInfo, err := models.GetEdbInfoById(req.EdbInfoId) if err != nil { br.Msg = "指标不存在!" br.ErrMsg = "指标不存在" return } cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode if utils.Rc.IsExist(cacheKey) { br.Ret = 501 br.Success = true br.Msg = "系统处理中,请稍后重试" br.IsSendEmail = false return } utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) defer func() { _ = utils.Rc.Delete(cacheKey) }() var errMsg string _, err, errMsg = logic.RefreshPredictEdbInfo(req.EdbInfoId) if err != nil { br.Msg = "刷新指标失败!" if errMsg != `` { br.ErrMsg = errMsg } br.ErrMsg = "刷新指标失败,err:" + err.Error() return } // 更新ES go logic.UpdateEs(edbInfo.EdbInfoId) br.Ret = 200 br.Success = true br.Msg = "获取成功" }