package trade_analysis import ( "eta/eta_api/controllers" "eta/eta_api/models" tradeAnalysisModel "eta/eta_api/models/data_manage/trade_analysis" "eta/eta_api/services/data/trade_analysis" "eta/eta_api/utils" "fmt" "strings" ) // TradeAnalysisController 供应分析 type TradeAnalysisController struct { controllers.BaseAuthController } // GetClassifyName // @Title 获取交易所合约列表 // @Description 获取交易所合约列表 // @Param request body saModel.SaCompareSaveReq true "type json string" // @Success 200 {object} trade_analysis.TradeClassifyNameListSort // @router /classify [get] func (c *TradeAnalysisController) GetClassifyName() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() //userinfo := userService.GetInfoByClaims(c) list, err, errMsg := trade_analysis.GetClassifyName(c.Lang) if err != nil { br.Msg = errMsg br.ErrMsg = "获取失败, ErrMsg: " + err.Error() return } br.Data = list br.Ret = 200 br.Success = true br.Msg = "获取成功" } // GetPositionTop // @Title 获取合约榜单详情 // @Description 获取合约榜单详情 // @Param Exchange query string false "交易所" // @Param ClassifyName query string false "分类名称" // @Param ClassifyType query string false "具体合约名称" // @Param DataTime query string false "请求日期,如果为空,则返回最新的榜单日期" // @Success 200 {object} trade_analysis.GetPositionTopResp // @Success 200 Ret=200 操作成功 // @router /top [get] func (c *TradeAnalysisController) GetPositionTop() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() req := tradeAnalysisModel.GetPositionTopReq{ Exchange: c.GetString("Exchange"), ClassifyName: c.GetString("ClassifyName"), ClassifyType: c.GetString("ClassifyType"), DataTime: c.GetString("DataTime"), } if req.ClassifyName == "" { br.Msg = "请输入分类名称" br.IsSendEmail = false return } if req.ClassifyType == "" { br.Msg = "请输入合约名称" br.IsSendEmail = false return } list, err, errMsg := trade_analysis.GetPositionTopDetail(req) if err != nil { br.Msg = errMsg br.ErrMsg = err.Error() return } br.Data = list br.Ret = 200 br.Success = true return } // GetTradeExchangeList // @Title 获取交易所列表 // @Description 获取交易所列表 // @Param IsTotal query bool false "是否显示全部" // @Success 200 {object} data_manage.BaseFromTradeExchangeItem // @router /exchange_list [get] func (this *TradeAnalysisController) GetTradeExchangeList() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } isTotal, _ := this.GetBool("IsTotal", false) var cond string var pars []interface{} exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange) if !isTotal { cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().AnalysisState) pars = append(pars, 1) } list, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e) return } resp := make([]*tradeAnalysisModel.BaseFromTradeExchangeItem, 0) for _, v := range list { resp = append(resp, v.Format2Item()) } br.Data = resp br.Msg = "获取成功" br.Ret = 200 br.Success = true } // GetTradeClassifyList // @Title 获取品种列表 // @Description 获取品种列表 // @Param Exchange query string false "交易所标识" // @Param ClassifyName query string false "品种名称" // @Success 200 {object} tradeAnalysisModel.BaseFromTradeClassifyItem // @router /classify_list [get] func (this *TradeAnalysisController) GetTradeClassifyList() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } exchange := this.GetString("Exchange") exchange = strings.TrimSpace(exchange) classifyName := this.GetString("ClassifyName") classifyName = strings.TrimSpace(classifyName) // 交易所名称 exchangeName := make(map[string]string) { var cond string var pars []interface{} exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange) if exchange != "" { cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().Exchange) pars = append(pars, exchange) item, e := exchangeOb.GetItemByCondition(cond, pars, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取交易所信息失败, %v", e) return } exchangeName[item.Exchange] = item.ExchangeName } else { list, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e) return } for _, v := range list { exchangeName[v.Exchange] = v.ExchangeName } } } // 搜索品种 var cond string var pars []interface{} classifyOb := new(tradeAnalysisModel.BaseFromTradeClassify) if exchange != "" { cond += fmt.Sprintf(` AND %s = ?`, classifyOb.Cols().Exchange) pars = append(pars, exchange) } if classifyName != "" { cond += fmt.Sprintf(` AND %s LIKE ?`, classifyOb.Cols().ClassifyName) pars = append(pars, fmt.Sprint("%", classifyName, "%")) } fields := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().Exchange} list, e := classifyOb.GetClassifyItemsByCondition(cond, pars, fields, "id ASC") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取品种列表失败, %v", e) return } resp := make([]*tradeAnalysisModel.BaseFromTradeClassifyItem, 0) classifyExist := make(map[string]bool) for _, v := range list { // 郑商所 if v.Exchange == tradeAnalysisModel.TradeExchangeZhengzhou { name := trade_analysis.GetZhengzhouClassifyName(v.ClassifyName) if classifyExist[name] { continue } classifyExist[name] = true resp = append(resp, &tradeAnalysisModel.BaseFromTradeClassifyItem{ ClassifyName: name, Exchange: v.Exchange, ExchangeName: exchangeName[v.Exchange], }) continue } resp = append(resp, &tradeAnalysisModel.BaseFromTradeClassifyItem{ ClassifyName: v.ClassifyName, Exchange: v.Exchange, ExchangeName: exchangeName[v.Exchange], }) } br.Data = resp br.Msg = "获取成功" br.Ret = 200 br.Success = true } // GetTradeContractList // @Title 获取合约列表 // @Description 获取合约列表 // @Param Exchange query string false "交易所标识" // @Param ClassifyName query string false "品种名称" // @Param ContractName query string false "合约名称" // @Param ExceptTop query bool false "排除TOP20: true-不包含; false-包含(默认)" // @Success 200 {object} tradeAnalysisModel.BaseFromTradeContractItem // @router /contract_list [get] func (this *TradeAnalysisController) GetTradeContractList() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } exchange := this.GetString("Exchange") exchange = strings.TrimSpace(exchange) classifyName := this.GetString("ClassifyName") classifyName = strings.TrimSpace(classifyName) contractName := this.GetString("ContractName") contractName = strings.TrimSpace(contractName) exceptTop, _ := this.GetBool("ExceptTop") // 交易所名称 exchangeName := make(map[string]string) { var cond string var pars []interface{} exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange) if exchange != "" { cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().Exchange) pars = append(pars, exchange) item, e := exchangeOb.GetItemByCondition(cond, pars, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取交易所信息失败, %v", e) return } exchangeName[item.Exchange] = item.ExchangeName } else { list, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e) return } for _, v := range list { exchangeName[v.Exchange] = v.ExchangeName } } } // 搜索合约 var cond string var pars []interface{} classifyOb := new(tradeAnalysisModel.BaseFromTradeClassify) if exchange != "" { cond += fmt.Sprintf(` AND %s = ?`, classifyOb.Cols().Exchange) pars = append(pars, exchange) } // 郑商所品种名称在列表中过滤 if classifyName != "" { cond += fmt.Sprintf(` AND ((%s = ? AND %s <> ?) OR %s = ?)`, classifyOb.Cols().ClassifyName, classifyOb.Cols().Exchange, classifyOb.Cols().Exchange) pars = append(pars, classifyName, tradeAnalysisModel.TradeExchangeZhengzhou, tradeAnalysisModel.TradeExchangeZhengzhou) } if contractName != "" { cond += fmt.Sprintf(` AND %s LIKE ?`, classifyOb.Cols().ClassifyType) pars = append(pars, fmt.Sprint("%", contractName, "%")) } list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s DESC", classifyOb.Cols().Exchange, classifyOb.Cols().LatestDate)) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取合约列表失败, %v", e) return } resp := make([]*tradeAnalysisModel.BaseFromTradeContractItem, 0) if !exceptTop && contractName == "" { resp = append(resp, &tradeAnalysisModel.BaseFromTradeContractItem{ ClassifyType: tradeAnalysisModel.TradeFuturesCompanyTop20, }) } for _, v := range list { // 郑商所 classifyType := v.ClassifyType if classifyName != "" && v.Exchange == tradeAnalysisModel.TradeExchangeZhengzhou { name := trade_analysis.GetZhengzhouClassifyName(v.ClassifyName) if classifyName != name { continue } classifyType = v.ClassifyName } resp = append(resp, &tradeAnalysisModel.BaseFromTradeContractItem{ ClassifyName: v.ClassifyName, ClassifyType: classifyType, Exchange: v.Exchange, ExchangeName: exchangeName[v.Exchange], LatestDate: v.LatestDate.Format(utils.FormatDate), CreateTime: v.CreateTime.Format(utils.FormatDateTime), }) } br.Data = resp br.Msg = "获取成功" br.Ret = 200 br.Success = true } // GetTradeFuturesCompanyList // @Title 获取期货公司列表 // @Description 获取期货公司列表 // @Param CompanyName query string false "公司名称" // @Success 200 {object} tradeAnalysisModel.BaseFromTradeContractItem // @router /company_list [get] func (this *TradeAnalysisController) GetTradeFuturesCompanyList() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } companyName := this.GetString("CompanyName") companyName = strings.TrimSpace(companyName) var cond string var pars []interface{} companyOb := new(tradeAnalysisModel.TradeFuturesCompany) if companyName != "" { cond += fmt.Sprintf(` AND %s LIKE ?`, companyOb.Cols().CompanyName) pars = append(pars, fmt.Sprint("%", companyName, "%")) } list, e := companyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", companyOb.Cols().Sort)) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取期货公司列表失败, %v", e) return } resp := make([]*tradeAnalysisModel.TradeFuturesCompanyItem, 0) for _, v := range list { resp = append(resp, &tradeAnalysisModel.TradeFuturesCompanyItem{ CompanyId: v.TradeFuturesCompanyId, CompanyName: v.CompanyName, Sort: v.Sort, }) } br.Data = resp br.Msg = "获取成功" br.Ret = 200 br.Success = true } // GetTradeExchangeClassifyTree // @Title 获取交易所-品种树 // @Description 获取交易所-品种树 // @Param IsTotal query bool false "是否显示全部交易所" // @Success 200 {object} data_manage.BaseFromTradeExchangeItem // @router /exchange_classify/tree [get] func (this *TradeAnalysisController) GetTradeExchangeClassifyTree() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } isTotal, _ := this.GetBool("IsTotal", false) // 交易所 var cond string var pars []interface{} exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange) if !isTotal { cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().AnalysisState) pars = append(pars, 1) } exchanges, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e) return } // 品种 classifyOb := new(tradeAnalysisModel.BaseFromTradeClassify) fields := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().Exchange} classifies, e := classifyOb.GetClassifyItemsByCondition(``, make([]interface{}, 0), fields, "id ASC") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取品种列表失败, %v", e) return } exchangeClassify := make(map[string][]*tradeAnalysisModel.BaseFromTradeClassify, 0) for _, v := range classifies { if exchangeClassify[v.Exchange] == nil { exchangeClassify[v.Exchange] = make([]*tradeAnalysisModel.BaseFromTradeClassify, 0) } exchangeClassify[v.Exchange] = append(exchangeClassify[v.Exchange], v) } // 构建树 resp := make([]*tradeAnalysisModel.TradeExchangeClassifyNode, 0) classifyExist := make(map[string]bool) for _, v := range exchanges { t := new(tradeAnalysisModel.TradeExchangeClassifyNode) t.UniqueFlag = v.Exchange t.NodeType = 1 t.NodeName = v.ExchangeName t.NodeValue = v.Exchange t.Children = make([]*tradeAnalysisModel.TradeExchangeClassifyNode, 0) for _, c := range exchangeClassify[v.Exchange] { // 郑商所 classifyName := c.ClassifyName if v.Exchange == tradeAnalysisModel.TradeExchangeZhengzhou { classifyName = trade_analysis.GetZhengzhouClassifyName(c.ClassifyName) if classifyExist[classifyName] { continue } classifyExist[classifyName] = true } tc := new(tradeAnalysisModel.TradeExchangeClassifyNode) tc.UniqueFlag = fmt.Sprintf("%s-%s", v.Exchange, classifyName) tc.ParentFlag = v.Exchange tc.NodeType = 2 tc.NodeName = classifyName tc.NodeValue = classifyName t.Children = append(t.Children, tc) } resp = append(resp, t) } br.Data = resp br.Msg = "获取成功" br.Ret = 200 br.Success = true }