table.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package common
  2. import (
  3. "fmt"
  4. "mtp2_if/db"
  5. "mtp2_if/global/app"
  6. "mtp2_if/global/e"
  7. "mtp2_if/global/utils"
  8. "mtp2_if/logger"
  9. "mtp2_if/models"
  10. "net/http"
  11. "strconv"
  12. "strings"
  13. "github.com/gin-gonic/gin"
  14. )
  15. // QueryTraderMenuReq 查询交易端菜单请求参数
  16. type QueryTraderMenuReq struct {
  17. LoginID int `form:"loginid" binding:"required"`
  18. }
  19. // QuotePrimaryMenu 报价牌一级分类菜单
  20. type QuotePrimaryMenu struct {
  21. Index int `json:"index"` // 序号
  22. Key string `json:"key"` // 键名
  23. Name string `json:"name"` // 菜单名称
  24. SubTitleType int `json:"titletype"` // 子菜单标题模式:0-市场名称;1-外部交易所名称
  25. TradeModes string `json:"trademodes"` // 包含市场交易类型
  26. SubMenus []QuoteSecondaryMenu `json:"submenus"` // 子菜单
  27. }
  28. // QuoteSecondaryMenu 报价牌二级分类菜单
  29. type QuoteSecondaryMenu struct {
  30. Index int `json:"index"` // 序号
  31. MarketID int `json:"marketid"` // 市场ID
  32. TradeMode int `json:"trademode"` // 交易模式
  33. MenuTitle string `json:"menutitle" xorm:"'ExExchangeName'"` // 菜单标题(市场名称或外部交易所名称)
  34. GoodsGroupIDs []int `json:"goodsgroupids"` // 商品组ID列表
  35. ExExchangeID int `json:"exexchangeid" xorm:"'ExExchangeID'"` // 外部交易所ID
  36. ExExchangeCode string `json:"exexchangecode" xorm:"'ExExchangeCode'"` // 外部交易所代码
  37. }
  38. // QueryTraderMenu 查询交易端菜单
  39. // @Summary 查询交易端菜单
  40. // @Produce json
  41. // @Security ApiKeyAuth
  42. // @Param loginid query int true "登录账号"
  43. // @Success 200 {object} QuotePrimaryMenu
  44. // @Failure 500 {object} app.Response
  45. // @Router /Common/QueryTraderMenu [get]
  46. // @Tags 通用
  47. func QueryTraderMenu(c *gin.Context) {
  48. appG := app.Gin{C: c}
  49. // 获取请求参数
  50. var req QueryTraderMenuReq
  51. if err := appG.C.ShouldBindQuery(&req); err != nil {
  52. logger.GetLogger().Errorf("QueryPreasleApply failed: %s", err.Error())
  53. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  54. return
  55. }
  56. datas, _ := getMenu(req.LoginID)
  57. menuMap := make(map[string]interface{})
  58. menuMap["menu"] = datas
  59. // 查询成功
  60. logger.GetLogger().Infof("QueryPreasleApply successed: %v", menuMap)
  61. appG.Response(http.StatusOK, e.SUCCESS, menuMap)
  62. }
  63. func getMenu(loginID int) ([]QuotePrimaryMenu, error) {
  64. engine := db.GetEngine()
  65. rst := make([]QuotePrimaryMenu, 0)
  66. // ********************* 准备数据 *********************
  67. var taAccounts []string // 账户下所有资金账户
  68. var marketIDs []int // 资金账户下有权限的市场ID列表
  69. // 先要获取当前登录账户对应的资金账户
  70. if err := engine.SQL(fmt.Sprintf(`select to_char(AccountID) from LoginTAAccount where LoginID = %d`, loginID)).Find(&taAccounts); err != nil {
  71. return nil, err
  72. }
  73. // 如果一条记录都没有则直接通过资金账户表获取
  74. if len(taAccounts) == 0 {
  75. sql := fmt.Sprintf(`select to_char(ta.accountid) from taaccount ta
  76. inner join loginaccount l on ta.userid = l.userid
  77. where l.loginid = %d`, loginID)
  78. if err := engine.SQL(sql).Find(&taAccounts); err != nil {
  79. return nil, err
  80. }
  81. }
  82. // 获取资金账户对应的市场权限(有权限的市场ID)
  83. if len(taAccounts) > 0 {
  84. taAccountStr := strings.Join(taAccounts, ",")
  85. if err := engine.SQL(fmt.Sprintf(`select MarketID from TAAccountMarket where AccountID in (%s)`, taAccountStr)).Find(&marketIDs); err != nil {
  86. return nil, err
  87. }
  88. }
  89. // ********************* 构建行情报价牌菜单 *********************
  90. // 获取一级菜单列表
  91. datas := make([]models.Tablecolumnconfig, 0)
  92. if err := engine.Where("TableKey = 'trader_master_menu' and IsShow = 1").Find(&datas); err != nil {
  93. return nil, err
  94. }
  95. // 构建一级菜单对象
  96. for i, v := range datas {
  97. quotePrimaryMenu := QuotePrimaryMenu{
  98. Index: i,
  99. Key: v.Columnfield,
  100. Name: v.Columntitle,
  101. }
  102. // 跳过自选
  103. if v.Columnfield == "optional" {
  104. rst = append(rst, quotePrimaryMenu)
  105. continue
  106. }
  107. // FormatterString:模式,0-使用市场名称,1-使用外部交易所名称;Remark:包含市场交易模式
  108. quotePrimaryMenu.SubTitleType, _ = strconv.Atoi(v.Formatterstring)
  109. quotePrimaryMenu.TradeModes = v.Remark
  110. // 构建二级子菜单对象
  111. marketIDsStr := utils.JoinItoString(marketIDs, ",")
  112. if quotePrimaryMenu.SubTitleType == 0 {
  113. // 获取目标交易模式的市场信息
  114. markets := make([]models.Market, 0)
  115. if err := engine.Where(fmt.Sprintf(`TradeMode in (%s) and MarketID in (%s)`, quotePrimaryMenu.TradeModes, marketIDsStr)).Find(&markets); err != nil {
  116. return nil, err
  117. }
  118. // 使用市场名称
  119. for mi, mv := range markets {
  120. quoteSecondaryMenu := QuoteSecondaryMenu{
  121. Index: mi,
  122. MarketID: int(mv.Marketid),
  123. TradeMode: int(mv.Trademode),
  124. MenuTitle: mv.Marketname,
  125. }
  126. quotePrimaryMenu.SubMenus = append(quotePrimaryMenu.SubMenus, quoteSecondaryMenu)
  127. }
  128. } else {
  129. // 使用外部交易所名称
  130. quoteSecondaryMenus := make([]QuoteSecondaryMenu, 0)
  131. sql := fmt.Sprintf(`select distinct
  132. e.autoid ExExchangeID,
  133. e.ExExchangeName,
  134. e.ExExchangeCode
  135. from ExternalExchange e
  136. inner join goodsgroup g on g.exexchangeid = e.autoid
  137. inner join Market m on g.marketid = m.marketid
  138. where m.trademode in (%s) and m.marketid in (%s)`, quotePrimaryMenu.TradeModes, marketIDsStr)
  139. if err := engine.SQL(sql).Find(&quoteSecondaryMenus); err != nil {
  140. return nil, err
  141. }
  142. // 获取外部交易所对应的商品组信息
  143. for ei, ev := range quoteSecondaryMenus {
  144. q := &quoteSecondaryMenus[ei]
  145. q.Index = ei
  146. // 商品组列表
  147. goodsgroups := make([]models.Goodsgroup, 0)
  148. if err := engine.Where("Exexchangeid = ?", ev.ExExchangeID).Find(&goodsgroups); err != nil {
  149. return nil, err
  150. }
  151. marketID := 0
  152. var goodsGroupIDs []int
  153. for _, gv := range goodsgroups {
  154. marketID = int(gv.Marketid)
  155. goodsGroupIDs = append(goodsGroupIDs, int(gv.Goodsgroupid))
  156. }
  157. q.MarketID = marketID
  158. q.GoodsGroupIDs = goodsGroupIDs
  159. }
  160. quotePrimaryMenu.SubMenus = quoteSecondaryMenus
  161. }
  162. rst = append(rst, quotePrimaryMenu)
  163. }
  164. return rst, nil
  165. }