table.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. // @Param loginid query int true "登录账号"
  64. // @Success 200 {object} QueryTraderMenuRsp
  65. // @Failure 500 {object} app.Response
  66. // @Router /Common/QueryTraderMenu [get]
  67. // @Tags 通用服务
  68. func QueryTraderMenu(c *gin.Context) {
  69. appG := app.Gin{C: c}
  70. // 获取请求参数
  71. var req QueryTraderMenuReq
  72. if err := appG.C.ShouldBindQuery(&req); err != nil {
  73. logger.GetLogger().Errorf("QueryTraderMenu failed: %s", err.Error())
  74. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  75. return
  76. }
  77. var queryTraderMenuRsp QueryTraderMenuRsp
  78. // 获取行情报价牌分类菜单
  79. if datas, err := getQuoteMenu(req.LoginID); err == nil {
  80. queryTraderMenuRsp.QuoteMenu = datas
  81. } else {
  82. logger.GetLogger().Errorf("QueryTraderMenu failed: %s", err.Error())
  83. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_QUOTEMENU_FAIL, nil)
  84. return
  85. }
  86. // 获取功能菜单
  87. if datas, err := getOperationMenu(); err == nil {
  88. queryTraderMenuRsp.OperationMenu = datas
  89. } else {
  90. logger.GetLogger().Errorf("QueryTraderMenu failed: %s", err.Error())
  91. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_OPERATIONMENU_FAIL, nil)
  92. return
  93. }
  94. // 查询成功
  95. logger.GetLogger().Infof("QueryTraderMenu successed: %v", queryTraderMenuRsp)
  96. appG.Response(http.StatusOK, e.SUCCESS, queryTraderMenuRsp)
  97. }
  98. // getQuoteMenu 获取行情报价牌分类菜单
  99. func getQuoteMenu(loginID int) ([]QuotePrimaryMenu, error) {
  100. engine := db.GetEngine()
  101. rst := make([]QuotePrimaryMenu, 0)
  102. // 账户下有权限的市场ID列表
  103. var marketIDs []int
  104. // 获取账户类型 - 1:交易所 2:机构 3:会员子机构 4:经纪人 5:投资者 6:客户 (目前可能登录交易端的账号类型为 2 5)
  105. userAccount := new(models.Useraccount)
  106. has, err := engine.Join("INNER", "LOGINACCOUNT", "USERACCOUNT.UserID = LOGINACCOUNT.UserID").Where("LOGINACCOUNT.LoginID = ?", loginID).Get(userAccount)
  107. if err != nil || !has {
  108. return nil, err
  109. }
  110. if userAccount.Usertype == 5 {
  111. // 如果账户类型为5(投资者),则需要通过其所属经济会员来获取市场权限(表:AreaRoleMarket, 条件:对应市场状态为正常;角色类型:经济会员)
  112. 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 {
  113. return nil, err
  114. }
  115. } else {
  116. // 非投资者账号直接通过资金账号获取市场权限(表:TAAccountMarket)
  117. var taAccounts []string // 账户下所有资金账户
  118. // 先要获取当前登录账户对应的资金账户
  119. if err := engine.SQL(fmt.Sprintf(`select to_char(AccountID) from LoginTAAccount where LoginID = %d`, loginID)).Find(&taAccounts); err != nil {
  120. return nil, err
  121. }
  122. // 如果一条记录都没有则直接通过资金账户表获取
  123. if len(taAccounts) == 0 {
  124. sql := fmt.Sprintf(`select to_char(ta.accountid) from taaccount ta
  125. inner join loginaccount l on ta.userid = l.userid
  126. where l.loginid = %d`, loginID)
  127. if err := engine.SQL(sql).Find(&taAccounts); err != nil {
  128. return nil, err
  129. }
  130. }
  131. // 获取资金账户对应的市场权限(有权限的市场ID)
  132. if len(taAccounts) > 0 {
  133. taAccountStr := strings.Join(taAccounts, ",")
  134. if err := engine.SQL(fmt.Sprintf(`select MarketID from TAAccountMarket where AccountID in (%s)`, taAccountStr)).Find(&marketIDs); err != nil {
  135. return nil, err
  136. }
  137. }
  138. }
  139. // ********************* 构建行情报价牌菜单 *********************
  140. // 获取一级菜单列表
  141. datas := make([]models.Tablecolumnconfig, 0)
  142. if err := engine.Where("TableKey = 'trader_master_menu' and IsShow = 1").Find(&datas); err != nil {
  143. return nil, err
  144. }
  145. // 构建一级菜单对象
  146. for i, v := range datas {
  147. quotePrimaryMenu := QuotePrimaryMenu{
  148. Index: i,
  149. Key: v.Columnfield,
  150. Name: v.Columntitle,
  151. SubMenus: make([]QuoteSecondaryMenu, 0),
  152. }
  153. // 跳过自选
  154. if v.Columnfield == "optional" {
  155. rst = append(rst, quotePrimaryMenu)
  156. continue
  157. }
  158. // FormatterString:模式,0-使用市场名称,1-使用外部交易所名称;Remark:包含市场交易模式
  159. quotePrimaryMenu.SubTitleType, _ = strconv.Atoi(v.Formatterstring)
  160. quotePrimaryMenu.TradeModes = v.Remark
  161. // 如果传入的LoginID获取不到有权限的市场ID列表(有可能LoginID是错误的),则不构建二级子菜单
  162. if len(marketIDs) == 0 {
  163. rst = append(rst, quotePrimaryMenu)
  164. continue
  165. }
  166. // 构建二级子菜单对象
  167. marketIDsStr := utils.JoinItoString(marketIDs, ",")
  168. if quotePrimaryMenu.SubTitleType == 0 {
  169. // 获取目标交易模式的市场信息
  170. markets := make([]models.Market, 0)
  171. if err := engine.Where(fmt.Sprintf(`TradeMode in (%s) and MarketID in (%s)`, quotePrimaryMenu.TradeModes, marketIDsStr)).Find(&markets); err != nil {
  172. return nil, err
  173. }
  174. // 使用市场名称
  175. for mi, mv := range markets {
  176. quoteSecondaryMenu := QuoteSecondaryMenu{
  177. Index: mi,
  178. MarketID: int(mv.Marketid),
  179. TradeMode: int(mv.Trademode),
  180. MenuTitle: mv.Marketname,
  181. GoodsGroupIDs: make([]int, 0),
  182. }
  183. quotePrimaryMenu.SubMenus = append(quotePrimaryMenu.SubMenus, quoteSecondaryMenu)
  184. }
  185. } else {
  186. // 使用外部交易所名称
  187. quoteSecondaryMenus := make([]QuoteSecondaryMenu, 0)
  188. sql := fmt.Sprintf(`select distinct
  189. e.autoid ExExchangeID,
  190. e.ExExchangeName,
  191. e.ExExchangeCode
  192. from ExternalExchange e
  193. inner join goodsgroup g on g.exexchangeid = e.autoid
  194. inner join Market m on g.marketid = m.marketid
  195. where m.trademode in (%s) and m.marketid in (%s)`, quotePrimaryMenu.TradeModes, marketIDsStr)
  196. if err := engine.SQL(sql).Find(&quoteSecondaryMenus); err != nil {
  197. return nil, err
  198. }
  199. // 获取外部交易所对应的商品组信息
  200. for ei, ev := range quoteSecondaryMenus {
  201. q := &quoteSecondaryMenus[ei]
  202. q.Index = ei
  203. // 商品组列表
  204. goodsgroups := make([]models.Goodsgroup, 0)
  205. if err := engine.Where("Exexchangeid = ?", ev.ExExchangeID).Find(&goodsgroups); err != nil {
  206. return nil, err
  207. }
  208. marketID := 0
  209. var goodsGroupIDs []int
  210. for _, gv := range goodsgroups {
  211. marketID = int(gv.Marketid)
  212. goodsGroupIDs = append(goodsGroupIDs, int(gv.Goodsgroupid))
  213. }
  214. q.MarketID = marketID
  215. q.GoodsGroupIDs = goodsGroupIDs
  216. }
  217. quotePrimaryMenu.SubMenus = quoteSecondaryMenus
  218. }
  219. rst = append(rst, quotePrimaryMenu)
  220. }
  221. return rst, nil
  222. }
  223. // getOperationMenu 获取功能菜单
  224. func getOperationMenu() ([]OperationPrimaryMenu, error) {
  225. engine := db.GetEngine()
  226. rst := make([]OperationPrimaryMenu, 0)
  227. // 获取一级功能菜单
  228. opm := make([]models.Tablecolumnconfig, 0)
  229. if err := engine.Where("IsShow = 1 and TableKey = ?", "trader_operation_master_menu").Asc("ORDERINDEX").Find(&opm); err != nil {
  230. return nil, err
  231. }
  232. for _, pv := range opm {
  233. var operationPrimaryMenu = OperationPrimaryMenu{
  234. Key: pv.Columnfield,
  235. Label: pv.Columntitle,
  236. Children: make([]OperationSecondaryMenu, 0),
  237. }
  238. // 获取二级功能菜单
  239. osm := make([]models.Tablecolumnconfig, 0)
  240. if err := engine.Where("IsShow = 1 and Remark = ?", operationPrimaryMenu.Key).Asc("ORDERINDEX").Find(&osm); err != nil {
  241. return nil, err
  242. }
  243. for _, sv := range osm {
  244. var operationSecondaryMenu = OperationSecondaryMenu{
  245. Key: sv.Columnfield,
  246. Label: sv.Columntitle,
  247. TabList: make([]OperationTabMenu, 0),
  248. }
  249. // 获取三级功能菜单
  250. otm := make([]models.Tablecolumnconfig, 0)
  251. if err := engine.Where("IsShow = 1 and Remark = ?", operationSecondaryMenu.Key).Asc("ORDERINDEX").Find(&otm); err != nil {
  252. return nil, err
  253. }
  254. for _, tv := range otm {
  255. var operationTabMenu = OperationTabMenu{
  256. Key: tv.Columnfield,
  257. Label: tv.Columntitle,
  258. }
  259. operationSecondaryMenu.TabList = append(operationSecondaryMenu.TabList, operationTabMenu)
  260. }
  261. operationPrimaryMenu.Children = append(operationPrimaryMenu.Children, operationSecondaryMenu)
  262. }
  263. rst = append(rst, operationPrimaryMenu)
  264. }
  265. return rst, nil
  266. }
  267. // QueryTableColumnConfigReq 查询交易端列表头信息请求参数
  268. type QueryTableColumnConfigReq struct {
  269. TableKey string `json:"TableKey"` // 表key
  270. }
  271. // QueryTableColumnConfig 查询交易端列表头信息
  272. // @Summary 查询交易端列表头信息
  273. // @Produce json
  274. // @Param TableKey query string false "表key"
  275. // @Success 200 {object} models.Tablecolumnconfig
  276. // @Failure 500 {object} app.Response
  277. // @Router /Common/QueryTableColumnConfig [get]
  278. // @Tags 通用服务
  279. func QueryTableColumnConfig(c *gin.Context) {
  280. appG := app.Gin{C: c}
  281. // 获取请求参数
  282. var req QueryTableColumnConfigReq
  283. if err := appG.C.ShouldBindQuery(&req); err != nil {
  284. logger.GetLogger().Errorf("QueryTableColumnConfig failed: %s", err.Error())
  285. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  286. return
  287. }
  288. engine := db.GetEngine()
  289. tablecolumnconfigs := make([]models.Tablecolumnconfig, 0)
  290. // 这里的表名必须要大写 (Oracle的表名都是大写)
  291. s := engine.Join("INNER", "TABLEDEFINE", "TABLEDEFINE.TableKey = TABLECOLUMNCONFIG.TableKey")
  292. if len(req.TableKey) > 0 {
  293. s = s.Where("TABLEDEFINE.TableType = 2 and TABLEDEFINE.TableKey = ?", req.TableKey)
  294. } else {
  295. s = s.Where("TABLEDEFINE.TableType = 2")
  296. }
  297. if err := s.Find(&tablecolumnconfigs); err != nil {
  298. logger.GetLogger().Errorf("QueryTableColumnConfig failed: %s", err.Error())
  299. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  300. return
  301. }
  302. // 查询成功
  303. logger.GetLogger().Infof("QueryTableColumnConfig successed: %v", tablecolumnconfigs)
  304. appG.Response(http.StatusOK, e.SUCCESS, tablecolumnconfigs)
  305. }