| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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"`
- }
- // QuotePrimaryMenu 报价牌一级分类菜单
- type QuotePrimaryMenu struct {
- Index int `json:"index"` // 序号
- Key string `json:"key"` // 键名
- Name string `json:"name"` // 菜单名称
- SubTitleType int `json:"titletype"` // 子菜单标题模式: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'"` // 外部交易所代码
- }
- // QueryTraderMenu 查询交易端菜单
- // @Summary 查询交易端菜单
- // @Produce json
- // @Security ApiKeyAuth
- // @Param loginid query int true "登录账号"
- // @Success 200 {object} QuotePrimaryMenu
- // @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("QueryPreasleApply failed: %s", err.Error())
- appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
- return
- }
- datas, _ := getMenu(req.LoginID)
- menuMap := make(map[string]interface{})
- menuMap["menu"] = datas
- // 查询成功
- logger.GetLogger().Infof("QueryPreasleApply successed: %v", menuMap)
- appG.Response(http.StatusOK, e.SUCCESS, menuMap)
- }
- func getMenu(loginID int) ([]QuotePrimaryMenu, error) {
- engine := db.GetEngine()
- rst := make([]QuotePrimaryMenu, 0)
- // ********************* 准备数据 *********************
- var taAccounts []string // 账户下所有资金账户
- var marketIDs []int // 资金账户下有权限的市场ID列表
- // 先要获取当前登录账户对应的资金账户
- if err := engine.SQL(fmt.Sprintf(`select to_char(AccountID) from LoginTAAccount where LoginID = %d`, loginID)).Find(&taAccounts); err != nil {
- return nil, err
- }
- // 如果一条记录都没有则直接通过资金账户表获取
- if len(taAccounts) == 0 {
- sql := fmt.Sprintf(`select to_char(ta.accountid) from taaccount ta
- inner join loginaccount l on ta.userid = l.userid
- where l.loginid = %d`, loginID)
- if err := engine.SQL(sql).Find(&taAccounts); err != nil {
- return nil, err
- }
- }
- // 获取资金账户对应的市场权限(有权限的市场ID)
- if len(taAccounts) > 0 {
- taAccountStr := strings.Join(taAccounts, ",")
- if err := engine.SQL(fmt.Sprintf(`select MarketID from TAAccountMarket where AccountID in (%s)`, taAccountStr)).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
- }
- // 构建一级菜单对象
- for i, v := range datas {
- quotePrimaryMenu := QuotePrimaryMenu{
- Index: i,
- Key: v.Columnfield,
- Name: v.Columntitle,
- }
- // 跳过自选
- if v.Columnfield == "optional" {
- rst = append(rst, quotePrimaryMenu)
- continue
- }
- // FormatterString:模式,0-使用市场名称,1-使用外部交易所名称;Remark:包含市场交易模式
- quotePrimaryMenu.SubTitleType, _ = strconv.Atoi(v.Formatterstring)
- quotePrimaryMenu.TradeModes = v.Remark
- // 构建二级子菜单对象
- 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,
- }
- 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
- }
|