package controllers import ( "encoding/json" "eta/eta_index_lib/logic" "eta/eta_index_lib/models" excelModel "eta/eta_index_lib/models/excel" 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 } // 获取持仓数据 companyTradeData, e := tradeAnalysisService.GetWarehouseTradeData(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.Ret = 200 br.Success = true 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 = "操作成功" } // TableRefresh // @Title 表格刷新 // @Description 表格刷新 // @Success 200 {object} tradeAnalysisModel.RefreshTableReq // @router /table/refresh [post] func (this *BaseFromTradeAnalysisController) TableRefresh() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req tradeAnalysisModel.RefreshTableReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e) return } if req.ExcelInfoId <= 0 { br.Msg = "请输入表格ID" br.ErrMsg = "请输入表格ID" return } cacheKey := fmt.Sprintf("%s_%d", utils.CACHE_EXCEL_REFRESH, req.ExcelInfoId) if utils.Rc.IsExist(cacheKey) { br.Ret = 200 br.Success = true br.Msg = "系统处理中,请稍后重试" return } utils.Rc.SetNX(cacheKey, 1, 2*time.Minute) defer func() { _ = utils.Rc.Delete(cacheKey) }() // 获取表格信息 excelOb := new(excelModel.ExcelInfo) item, e := excelOb.GetItemById(req.ExcelInfoId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "表格不存在" br.ErrMsg = fmt.Sprintf("表格不存在, ExcelId: %d", req.ExcelInfoId) return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e) return } if item.IsDelete == 1 { br.Msg = "表格已被删除" br.ErrMsg = fmt.Sprintf("表格已被删除, ExcelId: %d", req.ExcelInfoId) return } // 获取表格数据 switch item.Source { case utils.TRADE_ANALYSIS_TABLE: // 多空分析 var tableConfig tradeAnalysisModel.TableExtraConfig if item.ExtraConfig == "" { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("表格配置为空, ExcelId: %d", item.ExcelInfoId) return } if e = json.Unmarshal([]byte(item.ExtraConfig), &tableConfig); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("表格配置解析失败, ExcelId: %d, Err: %v", item.ExcelInfoId, e) return } tableData, e := tradeAnalysisService.GetTableRowsDataByConfig(tableConfig) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取表格行数据失败, %v", e) return } content, e := json.Marshal(tableData) if e != nil { br.Msg = "表格数据JSON格式化失败" br.ErrMsg = fmt.Sprintf("表格数据JSON格式化失败, %v", e) return } item.Content = string(content) case utils.TRADE_ANALYSIS_CORRELATION_TABLE: // 相关性表格 var tableConfig tradeAnalysisModel.CorrelationTableExtraConfig if item.ExtraConfig == "" { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("表格配置为空, ExcelId: %d", item.ExcelInfoId) return } if e = json.Unmarshal([]byte(item.ExtraConfig), &tableConfig); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("表格配置解析失败, ExcelId: %d, Err: %v", item.ExcelInfoId, e) return } tableData, e := tradeAnalysisService.GetCorrelationTableRowsDataByConfig(tableConfig) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取表格数据失败, %v", e) return } content, e := json.Marshal(tableData) if e != nil { br.Msg = "表格数据JSON格式化失败" br.ErrMsg = fmt.Sprintf("表格数据JSON格式化失败, %v", e) return } item.Content = string(content) default: br.Msg = "表格来源有误" br.ErrMsg = fmt.Sprintf("表格来源有误, ExcelId: %d, Source: %d", item.ExcelInfoId, item.Source) return } // 更新内容 updateCols := []string{"Content", "ModifyTime"} if e = item.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新表格数据失败, %v", e) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" }