package common import ( "fmt" "mtp2_if/db" "mtp2_if/global/app" "mtp2_if/global/e" "mtp2_if/global/utils" "mtp2_if/logger" "mtp2_if/models" "net/http" "strconv" "strings" "github.com/gin-gonic/gin" ) // QueryTraderMenuReq 查询交易端菜单请求参数 type QueryTraderMenuReq struct { LoginID int `form:"loginid" binding:"required"` } // QueryTraderMenuRsp 交易端菜单 type QueryTraderMenuRsp struct { QuoteMenu []QuotePrimaryMenu `json:"QuoteMenu"` // 报价牌分类菜单 OperationMenu []OperationPrimaryMenu `json:"OperationMenu"` // 功能菜单 } // QuotePrimaryMenu 报价牌一级分类菜单 type QuotePrimaryMenu struct { Index int `json:"Index"` // 序号 Key string `json:"Key"` // 键名 Name string `json:"Name"` // 菜单名称 SubTitleType int `json:"SubTitleType"` // 子菜单标题模式:0-市场名称;1-外部交易所名称 TradeModes string `json:"TradeModes"` // 包含市场交易类型 SubMenus []QuoteSecondaryMenu `json:"SubMenus"` // 子菜单 } // QuoteSecondaryMenu 报价牌二级分类菜单 type QuoteSecondaryMenu struct { Index int `json:"Index"` // 序号 MarketID int `json:"MarketID"` // 市场ID TradeMode int `json:"TradeMode"` // 交易模式 MenuTitle string `json:"MenuTitle" xorm:"'ExExchangeName'"` // 菜单标题(市场名称或外部交易所名称) GoodsGroupIDs []int `json:"GoodsGroupIDs"` // 商品组ID列表 ExExchangeID int `json:"ExExchangeID" xorm:"'ExExchangeID'"` // 外部交易所ID ExExchangeCode string `json:"ExExchangeCode" xorm:"'ExExchangeCode'"` // 外部交易所代码 } // OperationPrimaryMenu 一级功能菜单 type OperationPrimaryMenu struct { Key string `json:"Key"` // 菜单KEY Label string `json:"Label"` // 菜单标题 Children []OperationSecondaryMenu `json:"Children"` // 二级功能菜单 } // OperationSecondaryMenu 二级功能菜单 type OperationSecondaryMenu struct { Key string `json:"Key"` // 菜单KEY Label string `json:"Label"` // 菜单标题 TabList []OperationTabMenu `json:"TabList"` // 三级功能菜单 } // OperationTabMenu 三级功能菜单 type OperationTabMenu struct { Key string `json:"Key"` // 菜单KEY Label string `json:"Label"` // 菜单标题 } // QueryTraderMenu 查询交易端菜单 // @Summary 查询交易端菜单 // @Produce json // @Param loginid query int true "登录账号" // @Success 200 {object} QueryTraderMenuRsp // @Failure 500 {object} app.Response // @Router /Common/QueryTraderMenu [get] // @Tags 通用服务 func QueryTraderMenu(c *gin.Context) { appG := app.Gin{C: c} // 获取请求参数 var req QueryTraderMenuReq if err := appG.C.ShouldBindQuery(&req); err != nil { logger.GetLogger().Errorf("QueryTraderMenu failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) return } var queryTraderMenuRsp QueryTraderMenuRsp // 获取行情报价牌分类菜单 if datas, err := getQuoteMenu(req.LoginID); err == nil { queryTraderMenuRsp.QuoteMenu = datas } else { logger.GetLogger().Errorf("QueryTraderMenu failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.ERROR_QUERY_QUOTEMENU_FAIL, nil) return } // 获取功能菜单 if datas, err := getOperationMenu(); err == nil { queryTraderMenuRsp.OperationMenu = datas } else { logger.GetLogger().Errorf("QueryTraderMenu failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.ERROR_QUERY_OPERATIONMENU_FAIL, nil) return } // 查询成功 logger.GetLogger().Infof("QueryTraderMenu successed: %v", queryTraderMenuRsp) appG.Response(http.StatusOK, e.SUCCESS, queryTraderMenuRsp) } // getQuoteMenu 获取行情报价牌分类菜单 func getQuoteMenu(loginID int) ([]QuotePrimaryMenu, error) { engine := db.GetEngine() rst := make([]QuotePrimaryMenu, 0) // 账户下有权限的市场ID列表 var marketIDs []int // 获取账户类型 - 1:交易所 2:机构 3:会员子机构 4:经纪人 5:投资者 6:客户 (目前可能登录交易端的账号类型为 2 5) userAccount := new(models.Useraccount) has, err := engine.Join("INNER", "LOGINACCOUNT", "USERACCOUNT.UserID = LOGINACCOUNT.UserID").Where("LOGINACCOUNT.LoginID = ?", loginID).Get(userAccount) if err != nil || !has { return nil, err } if userAccount.Usertype == 5 { // 如果账户类型为5(投资者),则需要通过其所属经济会员来获取市场权限(表:AreaRoleMarket, 条件:对应市场状态为正常;角色类型:经济会员) if err := engine.Table("AREAROLEMARKET"). Cols("AREAROLEMARKET.MARKETID"). Join("INNER", "MARKET", "MARKET.MARKETID = AREAROLEMARKET.MARKETID"). Where("MARKET.MARKETSTATUS = 2 and AREAROLEMARKET.ROLETYPE = 7 and AREAROLEMARKET.AREAUSERID = ?", userAccount.Memberuserid).Find(&marketIDs); err != nil { return nil, err } } else { // 非投资者账号直接通过资金账号获取市场权限(表:TAAccountMarket) var taAccounts []string // 账户下所有资金账户 // 先要获取当前登录账户对应的资金账户;如果为外部子资金账号(TaAccount.TaAccountType = 1),则使用TaAccount.FromAccountID来获取市场权限 type taAccount struct { AccountID int `xorm:"ACCOUNTID"` TaAccountType int `xorm:"TAACCOUNTTYPE"` FromAccountID int `xorm:"FROMACCOUNTID"` } datas := make([]taAccount, 0) if err := engine.Table("LOGINTAACCOUNT"). Join("INNER", "TAACCOUNT", "LOGINTAACCOUNT.ACCOUNTID = TAACCOUNT.ACCOUNTID"). Cols("TAACCOUNT.ACCOUNTID", "TAACCOUNT.TAACCOUNTTYPE", "TAACCOUNT.FROMACCOUNTID"). Where("LOGINTAACCOUNT.LOGINID = ?", loginID).Find(&datas); err != nil { return nil, err } // 如果一条记录都没有(未配置自营会员资金账户)则直接通过资金账户表获取 if len(datas) == 0 { // 这里要注意,TaAccount表与LoginAccount表关联时,要使用TaAccount.RelatedUserID if err := engine.Table("TAACCOUNT"). Join("INNER", "LOGINACCOUNT", "TAACCOUNT.RELATEDUSERID = LOGINACCOUNT.USERID"). Cols("TAACCOUNT.ACCOUNTID", "TAACCOUNT.TAACCOUNTTYPE", "TAACCOUNT.FROMACCOUNTID"). Where("LOGINACCOUNT.LOGINID = ?", loginID).Find(&datas); err != nil { return nil, err } } for _, v := range datas { if v.TaAccountType == 1 { // 外部资金账户使用TaAccount.FromAccountID来获取市场权限 taAccounts = append(taAccounts, strconv.Itoa(v.FromAccountID)) } else { taAccounts = append(taAccounts, strconv.Itoa(v.AccountID)) } } // 获取资金账户对应的市场权限(有权限的市场ID) if len(taAccounts) > 0 { taAccountStr := strings.Join(taAccounts, ",") if err := engine.Table("TAACCOUNTMARKET"). Join("INNER", "MARKET", "MARKET.MARKETID = TAACCOUNTMARKET.MARKETID"). Cols("TAACCOUNTMARKET.MARKETID"). In("TAACCOUNTMARKET.ACCOUNTID", taAccountStr). And("MARKET.MARKETSTATUS = 2").Find(&marketIDs); err != nil { return nil, err } } } // ********************* 构建行情报价牌菜单 ********************* // 获取一级菜单列表 // datas := make([]models.Tablecolumnconfig, 0) // if err := engine.Where("TableKey = 'trader_master_menu' and IsShow = 1").Find(&datas); err != nil { // return nil, err // } datas := make([]models.Funcmenulist, 0) if err := engine.Join("INNER", "ROLEFUNCMENU", "FUNCMENULIST.RESOURCECODE = ROLEFUNCMENU.RESOURCECODE"). Where("FUNCMENULIST.PARENTCODE = 'trader_master_menu'"). Asc("SORT"). Find(&datas); err != nil { return nil, err } // 构建一级菜单对象 for i, v := range datas { quotePrimaryMenu := QuotePrimaryMenu{ Index: i, Key: v.Resourcecode, Name: v.Resourcename, SubMenus: make([]QuoteSecondaryMenu, 0), } // 跳过自选 if v.Resourcecode == "optional" { rst = append(rst, quotePrimaryMenu) continue } // URL:模式,0-使用市场名称,1-使用外部交易所名称;Remark:包含市场交易模式 quotePrimaryMenu.SubTitleType, _ = strconv.Atoi(v.URL) quotePrimaryMenu.TradeModes = v.Remark // 如果传入的LoginID获取不到有权限的市场ID列表(有可能LoginID是错误的),则不构建二级子菜单 if len(marketIDs) == 0 { rst = append(rst, quotePrimaryMenu) continue } // 构建二级子菜单对象 marketIDsStr := utils.JoinItoString(marketIDs, ",") if quotePrimaryMenu.SubTitleType == 0 { // 获取目标交易模式的市场信息 markets := make([]models.Market, 0) if err := engine.Where(fmt.Sprintf(`TradeMode in (%s) and MarketID in (%s)`, quotePrimaryMenu.TradeModes, marketIDsStr)).Find(&markets); err != nil { return nil, err } // 使用市场名称 for mi, mv := range markets { quoteSecondaryMenu := QuoteSecondaryMenu{ Index: mi, MarketID: int(mv.Marketid), TradeMode: int(mv.Trademode), MenuTitle: mv.Marketname, GoodsGroupIDs: make([]int, 0), } quotePrimaryMenu.SubMenus = append(quotePrimaryMenu.SubMenus, quoteSecondaryMenu) } } else { // 使用外部交易所名称 quoteSecondaryMenus := make([]QuoteSecondaryMenu, 0) sql := fmt.Sprintf(`select distinct e.autoid ExExchangeID, e.ExExchangeName, e.ExExchangeCode from ExternalExchange e inner join goodsgroup g on g.exexchangeid = e.autoid inner join Market m on g.marketid = m.marketid where m.trademode in (%s) and m.marketid in (%s)`, quotePrimaryMenu.TradeModes, marketIDsStr) if err := engine.SQL(sql).Find("eSecondaryMenus); err != nil { return nil, err } // 获取外部交易所对应的商品组信息 for ei, ev := range quoteSecondaryMenus { q := "eSecondaryMenus[ei] q.Index = ei // 商品组列表 goodsgroups := make([]models.Goodsgroup, 0) if err := engine.Where("Exexchangeid = ?", ev.ExExchangeID).Find(&goodsgroups); err != nil { return nil, err } marketID := 0 var goodsGroupIDs []int for _, gv := range goodsgroups { marketID = int(gv.Marketid) goodsGroupIDs = append(goodsGroupIDs, int(gv.Goodsgroupid)) } q.MarketID = marketID q.GoodsGroupIDs = goodsGroupIDs } quotePrimaryMenu.SubMenus = quoteSecondaryMenus } rst = append(rst, quotePrimaryMenu) } return rst, nil } // getOperationMenu 获取功能菜单 func getOperationMenu() ([]OperationPrimaryMenu, error) { engine := db.GetEngine() rst := make([]OperationPrimaryMenu, 0) // 获取一级功能菜单 opm := make([]models.Funcmenulist, 0) if err := engine.Join("INNER", "ROLEFUNCMENU", "FUNCMENULIST.RESOURCECODE = ROLEFUNCMENU.RESOURCECODE"). Where("FUNCMENULIST.PARENTCODE = 'trader_operation_master_menu'"). Asc("SORT"). Find(&opm); err != nil { return nil, err } for _, pv := range opm { var operationPrimaryMenu = OperationPrimaryMenu{ Key: pv.Resourcecode, Label: pv.Resourcename, Children: make([]OperationSecondaryMenu, 0), } // 获取二级功能菜单 osm := make([]models.Funcmenulist, 0) if err := engine.Join("INNER", "ROLEFUNCMENU", "FUNCMENULIST.RESOURCECODE = ROLEFUNCMENU.RESOURCECODE"). Where("FUNCMENULIST.PARENTCODE = ?", operationPrimaryMenu.Key). Asc("SORT"). Find(&osm); err != nil { return nil, err } for _, sv := range osm { var operationSecondaryMenu = OperationSecondaryMenu{ Key: sv.Resourcecode, Label: sv.Resourcename, TabList: make([]OperationTabMenu, 0), } // 获取三级功能菜单 otm := make([]models.Funcmenulist, 0) if err := engine.Join("INNER", "ROLEFUNCMENU", "FUNCMENULIST.RESOURCECODE = ROLEFUNCMENU.RESOURCECODE"). Where("FUNCMENULIST.PARENTCODE = ?", operationSecondaryMenu.Key). Asc("SORT"). Find(&otm); err != nil { return nil, err } for _, tv := range otm { var operationTabMenu = OperationTabMenu{ Key: tv.Resourcecode, Label: tv.Resourcename, } operationSecondaryMenu.TabList = append(operationSecondaryMenu.TabList, operationTabMenu) } operationPrimaryMenu.Children = append(operationPrimaryMenu.Children, operationSecondaryMenu) } rst = append(rst, operationPrimaryMenu) } return rst, nil } // QueryTableDefineReq 查询交易端列表头信息请求参数 type QueryTableDefineReq struct { TableKey string `json:"TableKey"` // 表key } // QueryTableDefineRsp 查询交易端列表头信息返回模型 type QueryTableDefineRsp struct { models.Tabledefine `xorm:"extends"` Columns []models.Tablecolumnconfig `json:"columns" xorm:"-"` // 列头信息数组 } // QueryTableDefine 查询交易端列表头信息 // @Summary 查询交易端列表头信息 // @Produce json // @Param TableKey query string false "表key" // @Success 200 {object} models.Tablecolumnconfig // @Failure 500 {object} app.Response // @Router /Common/QueryTableDefine [get] // @Tags 通用服务 func QueryTableDefine(c *gin.Context) { appG := app.Gin{C: c} // 获取请求参数 var req QueryTableDefineReq if err := appG.C.ShouldBindQuery(&req); err != nil { logger.GetLogger().Errorf("QueryTableDefine failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) return } engine := db.GetEngine() tableDefines := make([]QueryTableDefineRsp, 0) // 获取表定义 s := engine.Where("TABLEDEFINE.TableType = 2") if len(req.TableKey) > 0 { s = s.And("TABLEDEFINE.TableKey = ?", req.TableKey) } if err := s.Find(&tableDefines); err != nil { logger.GetLogger().Errorf("QueryTableDefine failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) return } for i, v := range tableDefines { tableDefine := &tableDefines[i] // 获取列表数据 tablecolumnconfigs := make([]models.Tablecolumnconfig, 0) // 这里的表名必须要大写 (Oracle的表名都是大写) if err := engine.Join("INNER", "TABLEDEFINE", "TABLEDEFINE.TableKey = TABLECOLUMNCONFIG.TableKey"). Where("TABLEDEFINE.TableType = 2 and TABLEDEFINE.TableKey = ?", v.Tablekey).Find(&tablecolumnconfigs); err != nil { logger.GetLogger().Errorf("QueryTableDefine failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) } tableDefine.Columns = tablecolumnconfigs } // 查询成功 logger.GetLogger().Infof("QueryTableDefine successed: %v", tableDefines) appG.Response(http.StatusOK, e.SUCCESS, tableDefines) }