package controllers import ( "encoding/json" "eta/eta_index_lib/logic" "eta/eta_index_lib/models" tradeAnalysisModel "eta/eta_index_lib/models/trade_analysis" tradeAnalysisService "eta/eta_index_lib/services/trade_analysis" "eta/eta_index_lib/utils" "fmt" "time" ) // BaseFromTradeAnalysisController 持仓分析指标 type BaseFromTradeAnalysisController struct { BaseAuthController } // EdbRefresh // @Title 指标库刷新 // @Description 指标库刷新 // @Success 200 {object} models.RefreshEdbInfoReq // @router /edb/refresh [post] func (this *BaseFromTradeAnalysisController) EdbRefresh() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } 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 } edbOb := new(models.EdbTradeAnalysis) source := edbOb.GetSource() cacheKey := fmt.Sprintf("%s_%d_%s", utils.CACHE_EDB_DATA_REFRESH, source, req.EdbCode) if utils.Rc.IsExist(cacheKey) { br.Ret = 501 br.Success = true br.Msg = "系统处理中,请稍后重试" return } utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) defer func() { _ = utils.Rc.Delete(cacheKey) }() // 获取指标详情 edbInfo, e := models.GetEdbInfoByEdbCode(source, req.EdbCode) if e != nil { br.Msg = "指标不存在" br.ErrMsg = fmt.Sprintf("指标不存在, %v", e) return } formula := edbInfo.CalculateFormula // 校验计算公式 var extraConfig tradeAnalysisModel.WarehouseExtraConfig if formula == `` { br.Msg = "指标计算公式有误" br.ErrMsg = fmt.Sprintf("指标计算公式有误, conf: %s", formula) return } if e := json.Unmarshal([]byte(formula), &extraConfig); e != nil { br.Msg = "指标计算公式有误" br.ErrMsg = fmt.Sprintf("指标计算公式解析失败, err: %v; conf: %s", e, formula) return } if extraConfig.Exchange == "" { br.Msg = "指标计算公式有误" br.ErrMsg = fmt.Sprintf("指标计算公式交易所异常, conf: %s", formula) return } if extraConfig.ClassifyName == "" { br.Msg = "指标计算公式有误" br.ErrMsg = fmt.Sprintf("指标计算公式品种异常, conf: %s", formula) return } if len(extraConfig.Contracts) == 0 { br.Msg = "指标计算公式有误" br.ErrMsg = fmt.Sprintf("指标计算公式合约异常, conf: %s", formula) return } if len(extraConfig.Companies) != 1 { br.Msg = "指标计算公式有误" br.ErrMsg = fmt.Sprintf("指标计算公式期货公司异常, conf: %s", formula) return } if extraConfig.PredictRatio < 0 || extraConfig.PredictRatio > 1 { br.Msg = "指标计算公式有误" br.ErrMsg = fmt.Sprintf("指标计算公式估计参数异常, conf: %s", formula) return } // 获取指标数据, 该图表未用实际指标, 为了统一数据格式用ChartEdbInfoMapping companyTradeData, e := tradeAnalysisService.GetOriginTradeData(extraConfig.Exchange, extraConfig.ClassifyName, extraConfig.Contracts, extraConfig.Companies, extraConfig.PredictRatio) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取期货公司持仓加总数据失败, %v", e) return } if len(companyTradeData) == 0 { br.Msg = "期货数据为空" return } // 转换持仓数据为指标数据 convertData, e := tradeAnalysisService.FormatCompanyTradeData2EdbData(companyTradeData[0], extraConfig.WarehouseChartType) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("持仓数据转为指标数据失败, %v", e) return } // 刷新指标 if e = edbOb.Refresh(edbInfo, convertData); e != nil { br.Msg = "刷新指标失败" br.ErrMsg = fmt.Sprintf("刷新指标失败, %v", e) return } // 更新指标最值 if e = edbOb.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo); e != nil { br.Msg = "刷新指标失败" br.ErrMsg = fmt.Sprintf("更新指标最值失败, %v", e) return } // 更新ES go logic.UpdateEs(edbInfo.EdbInfoId) br.Ret = 200 br.Success = true br.Msg = "操作成功" }