table.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. // QueryTraderMenuRsp 交易端菜单
  20. type QueryTraderMenuRsp struct {
  21. QuoteMenu []QuotePrimaryMenu `json:"QuoteMenu"` // 报价牌分类菜单
  22. OperationMenu []OperationPrimaryMenu `json:"OperationMenu"` // 功能菜单
  23. }
  24. // QuotePrimaryMenu 报价牌一级分类菜单
  25. type QuotePrimaryMenu struct {
  26. Index int `json:"Index"` // 序号
  27. Key string `json:"Key"` // 键名
  28. Name string `json:"Name"` // 菜单名称
  29. SubTitleType int `json:"SubTitleType"` // 子菜单标题模式:0-市场名称;1-外部交易所名称
  30. TradeModes string `json:"TradeModes"` // 包含市场交易类型
  31. SubMenus []QuoteSecondaryMenu `json:"SubMenus"` // 子菜单
  32. }
  33. // QuoteSecondaryMenu 报价牌二级分类菜单
  34. type QuoteSecondaryMenu struct {
  35. Index int `json:"Index"` // 序号
  36. MarketID int `json:"MarketID"` // 市场ID
  37. TradeMode int `json:"TradeMode"` // 交易模式
  38. MenuTitle string `json:"MenuTitle" xorm:"'ExExchangeName'"` // 菜单标题(市场名称或外部交易所名称)
  39. GoodsGroupIDs []int `json:"GoodsGroupIDs"` // 商品组ID列表
  40. ExExchangeID int `json:"ExExchangeID" xorm:"'ExExchangeID'"` // 外部交易所ID
  41. ExExchangeCode string `json:"ExExchangeCode" xorm:"'ExExchangeCode'"` // 外部交易所代码
  42. }
  43. // OperationPrimaryMenu 一级功能菜单
  44. type OperationPrimaryMenu struct {
  45. Key string `json:"Key"` // 菜单KEY
  46. Label string `json:"Label"` // 菜单标题
  47. Children []OperationSecondaryMenu `json:"Children"` // 二级功能菜单
  48. }
  49. // OperationSecondaryMenu 二级功能菜单
  50. type OperationSecondaryMenu struct {
  51. Key string `json:"Key"` // 菜单KEY
  52. Label string `json:"Label"` // 菜单标题
  53. TabList []OperationTabMenu `json:"TabList"` // 三级功能菜单
  54. }
  55. // OperationTabMenu 三级功能菜单
  56. type OperationTabMenu struct {
  57. Key string `json:"Key"` // 菜单KEY
  58. Label string `json:"Label"` // 菜单标题
  59. }
  60. // QueryTraderMenu 查询交易端菜单
  61. // @Summary 查询交易端菜单
  62. // @Produce json
  63. // @Security ApiKeyAuth
  64. // @Param loginid query int true "登录账号"
  65. // @Success 200 {object} QueryTraderMenuRsp
  66. // @Failure 500 {object} app.Response
  67. // @Router /Common/QueryTraderMenu [get]
  68. // @Tags 通用
  69. func QueryTraderMenu(c *gin.Context) {
  70. appG := app.Gin{C: c}
  71. // 获取请求参数
  72. var req QueryTraderMenuReq
  73. if err := appG.C.ShouldBindQuery(&req); err != nil {
  74. logger.GetLogger().Errorf("QueryPreasleApply failed: %s", err.Error())
  75. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  76. return
  77. }
  78. var queryTraderMenuRsp QueryTraderMenuRsp
  79. // 获取行情报价牌分类菜单
  80. if datas, err := getQuoteMenu(req.LoginID); err == nil {
  81. queryTraderMenuRsp.QuoteMenu = datas
  82. }
  83. // 获取功能菜单
  84. if datas, err := getOperationMenu(); err == nil {
  85. queryTraderMenuRsp.OperationMenu = datas
  86. }
  87. // 查询成功
  88. logger.GetLogger().Infof("QueryPreasleApply successed: %v", queryTraderMenuRsp)
  89. appG.Response(http.StatusOK, e.SUCCESS, queryTraderMenuRsp)
  90. }
  91. // getQuoteMenu 获取行情报价牌分类菜单
  92. func getQuoteMenu(loginID int) ([]QuotePrimaryMenu, error) {
  93. engine := db.GetEngine()
  94. rst := make([]QuotePrimaryMenu, 0)
  95. // ********************* 准备数据 *********************
  96. var taAccounts []string // 账户下所有资金账户
  97. var marketIDs []int // 资金账户下有权限的市场ID列表
  98. // 先要获取当前登录账户对应的资金账户
  99. if err := engine.SQL(fmt.Sprintf(`select to_char(AccountID) from LoginTAAccount where LoginID = %d`, loginID)).Find(&taAccounts); err != nil {
  100. return nil, err
  101. }
  102. // 如果一条记录都没有则直接通过资金账户表获取
  103. if len(taAccounts) == 0 {
  104. sql := fmt.Sprintf(`select to_char(ta.accountid) from taaccount ta
  105. inner join loginaccount l on ta.userid = l.userid
  106. where l.loginid = %d`, loginID)
  107. if err := engine.SQL(sql).Find(&taAccounts); err != nil {
  108. return nil, err
  109. }
  110. }
  111. // 获取资金账户对应的市场权限(有权限的市场ID)
  112. if len(taAccounts) > 0 {
  113. taAccountStr := strings.Join(taAccounts, ",")
  114. if err := engine.SQL(fmt.Sprintf(`select MarketID from TAAccountMarket where AccountID in (%s)`, taAccountStr)).Find(&marketIDs); err != nil {
  115. return nil, err
  116. }
  117. }
  118. // ********************* 构建行情报价牌菜单 *********************
  119. // 获取一级菜单列表
  120. datas := make([]models.Tablecolumnconfig, 0)
  121. if err := engine.Where("TableKey = 'trader_master_menu' and IsShow = 1").Find(&datas); err != nil {
  122. return nil, err
  123. }
  124. // 构建一级菜单对象
  125. for i, v := range datas {
  126. quotePrimaryMenu := QuotePrimaryMenu{
  127. Index: i,
  128. Key: v.Columnfield,
  129. Name: v.Columntitle,
  130. }
  131. // 跳过自选
  132. if v.Columnfield == "optional" {
  133. rst = append(rst, quotePrimaryMenu)
  134. continue
  135. }
  136. // FormatterString:模式,0-使用市场名称,1-使用外部交易所名称;Remark:包含市场交易模式
  137. quotePrimaryMenu.SubTitleType, _ = strconv.Atoi(v.Formatterstring)
  138. quotePrimaryMenu.TradeModes = v.Remark
  139. // 构建二级子菜单对象
  140. marketIDsStr := utils.JoinItoString(marketIDs, ",")
  141. if quotePrimaryMenu.SubTitleType == 0 {
  142. // 获取目标交易模式的市场信息
  143. markets := make([]models.Market, 0)
  144. if err := engine.Where(fmt.Sprintf(`TradeMode in (%s) and MarketID in (%s)`, quotePrimaryMenu.TradeModes, marketIDsStr)).Find(&markets); err != nil {
  145. return nil, err
  146. }
  147. // 使用市场名称
  148. for mi, mv := range markets {
  149. quoteSecondaryMenu := QuoteSecondaryMenu{
  150. Index: mi,
  151. MarketID: int(mv.Marketid),
  152. TradeMode: int(mv.Trademode),
  153. MenuTitle: mv.Marketname,
  154. }
  155. quotePrimaryMenu.SubMenus = append(quotePrimaryMenu.SubMenus, quoteSecondaryMenu)
  156. }
  157. } else {
  158. // 使用外部交易所名称
  159. quoteSecondaryMenus := make([]QuoteSecondaryMenu, 0)
  160. sql := fmt.Sprintf(`select distinct
  161. e.autoid ExExchangeID,
  162. e.ExExchangeName,
  163. e.ExExchangeCode
  164. from ExternalExchange e
  165. inner join goodsgroup g on g.exexchangeid = e.autoid
  166. inner join Market m on g.marketid = m.marketid
  167. where m.trademode in (%s) and m.marketid in (%s)`, quotePrimaryMenu.TradeModes, marketIDsStr)
  168. if err := engine.SQL(sql).Find(&quoteSecondaryMenus); err != nil {
  169. return nil, err
  170. }
  171. // 获取外部交易所对应的商品组信息
  172. for ei, ev := range quoteSecondaryMenus {
  173. q := &quoteSecondaryMenus[ei]
  174. q.Index = ei
  175. // 商品组列表
  176. goodsgroups := make([]models.Goodsgroup, 0)
  177. if err := engine.Where("Exexchangeid = ?", ev.ExExchangeID).Find(&goodsgroups); err != nil {
  178. return nil, err
  179. }
  180. marketID := 0
  181. var goodsGroupIDs []int
  182. for _, gv := range goodsgroups {
  183. marketID = int(gv.Marketid)
  184. goodsGroupIDs = append(goodsGroupIDs, int(gv.Goodsgroupid))
  185. }
  186. q.MarketID = marketID
  187. q.GoodsGroupIDs = goodsGroupIDs
  188. }
  189. quotePrimaryMenu.SubMenus = quoteSecondaryMenus
  190. }
  191. rst = append(rst, quotePrimaryMenu)
  192. }
  193. return rst, nil
  194. }
  195. // getOperationMenu 获取功能菜单
  196. func getOperationMenu() ([]OperationPrimaryMenu, error) {
  197. engine := db.GetEngine()
  198. rst := make([]OperationPrimaryMenu, 0)
  199. // 获取一级功能菜单
  200. opm := make([]models.Tablecolumnconfig, 0)
  201. if err := engine.Where("IsShow = 1 and TableKey = ?", "trader_operation_master_menu").Asc("ORDERINDEX").Find(&opm); err != nil {
  202. return nil, err
  203. }
  204. for _, pv := range opm {
  205. var operationPrimaryMenu = OperationPrimaryMenu{
  206. Key: pv.Columnfield,
  207. Label: pv.Columntitle,
  208. Children: make([]OperationSecondaryMenu, 0),
  209. }
  210. // 获取二级功能菜单
  211. osm := make([]models.Tablecolumnconfig, 0)
  212. if err := engine.Where("IsShow = 1 and Remark = ?", operationPrimaryMenu.Key).Asc("ORDERINDEX").Find(&osm); err != nil {
  213. return nil, err
  214. }
  215. for _, sv := range osm {
  216. var operationSecondaryMenu = OperationSecondaryMenu{
  217. Key: sv.Columnfield,
  218. Label: sv.Columntitle,
  219. TabList: make([]OperationTabMenu, 0),
  220. }
  221. // 获取三级功能菜单
  222. otm := make([]models.Tablecolumnconfig, 0)
  223. if err := engine.Where("IsShow = 1 and Remark = ?", operationSecondaryMenu.Key).Asc("ORDERINDEX").Find(&otm); err != nil {
  224. return nil, err
  225. }
  226. for _, tv := range otm {
  227. var operationTabMenu = OperationTabMenu{
  228. Key: tv.Columnfield,
  229. Label: tv.Columntitle,
  230. }
  231. operationSecondaryMenu.TabList = append(operationSecondaryMenu.TabList, operationTabMenu)
  232. }
  233. operationPrimaryMenu.Children = append(operationPrimaryMenu.Children, operationSecondaryMenu)
  234. }
  235. rst = append(rst, operationPrimaryMenu)
  236. }
  237. return rst, nil
  238. }