package models import ( "errors" "fmt" "mtp2_if/db" "mtp2_if/dbmodels" "mtp2_if/global/utils" "strconv" ) // 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"` // 菜单标题 } // 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(dbmodels.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", taAccounts). And("MARKET.MARKETSTATUS = 2").Find(&marketIDs); err != nil { return nil, err } } } // ********************* 构建行情报价牌菜单 ********************* datas := make([]dbmodels.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([]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([]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([]dbmodels.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([]dbmodels.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([]dbmodels.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 } // GetClientTableDefines 获取终端列表定义信息 // tableKey 列表Key func GetClientTableDefines(tableKey string) ([]dbmodels.Tabledefine, error) { engine := db.GetEngine() tableDefiles := make([]dbmodels.Tabledefine, 0) s := engine.Where("TABLEDEFINE.TableType = 2") // TableType = 2 表示终端专用表定义 if len(tableKey) > 0 { s = s.And("TABLEDEFINE.TableKey = ?", tableKey) } if err := s.Find(&tableDefiles); err != nil { return nil, err } return tableDefiles, nil } // GetClientTableColumns 获取客户端指定表的列头信息 // tableKey 列表Key func GetClientTableColumns(tableKey string) ([]dbmodels.Tablecolumnconfig, error) { engine := db.GetEngine() // 获取列表数据 tablecolumnconfigs := make([]dbmodels.Tablecolumnconfig, 0) // 这里的表名必须要大写 (Oracle的表名都是大写) if err := engine.Join("INNER", "TABLEDEFINE", "TABLEDEFINE.TableKey = TABLECOLUMNCONFIG.TableKey"). Where("TABLEDEFINE.TableType = 2 and TABLEDEFINE.TableKey = ?", tableKey).Find(&tablecolumnconfigs); err != nil { return nil, err } return tablecolumnconfigs, nil } // GetNotices 获取指定账户的通知信息(终端) // @param loginID int 登录账号 // @param msgType int 消息类型 - 1:公告通知 2:系统消息 // @param onlyUnRead bool 是否只获取未读信息 func GetNotices(loginID, msgType int, onlyUnRead bool) ([]dbmodels.Noticemsg, error) { engine := db.GetEngine() rst := make([]dbmodels.Noticemsg, 0) // 获取登录账号所属会员信息 var userAccount dbmodels.Useraccount if has, _ := engine.Join("LEFT", "LOGINACCOUNT", "LOGINACCOUNT.USERID = USERACCOUNT.USERID"). Where("LOGINACCOUNT.LOGINID = ?", loginID).Get(&userAccount); !has { return nil, errors.New("获取登录账号所属会员ID失败") } // 获取登录账号已读公告ID, 这里要使用的是个人的UserID来查询 var msgReceivers []int engine.Table("MSGRECEIVER").Select("AUTOID").Where("RECEIVERTYPE = 2 and MANAGERID = ?", userAccount.Userid).Find(&msgReceivers) // 查询SENDTYPE = 1的数据 datas1 := make([]dbmodels.Noticemsg, 0) s := engine.Where("SENDTYPE = 1 and SYSDATE > SCHEDULETIME and SYSDATE < ENDTIME and SENTSTATUS=1"). And("PUBLISHER = ? or PUBLISHER in (select USERACCOUNT.USERID from USERACCOUNT where USERACCOUNT.USERTYPE = 1)", userAccount.Memberuserid) if msgType > 0 { s = s.And("MSGTYPE = ?", msgType) } if onlyUnRead && len(msgReceivers) > 0 { s = s.NotIn("AUTOID", msgReceivers) } if err := s.Find(&datas1); err != nil { // 查询失败 return nil, err } rst = append(rst, datas1...) // 查询SENDTYPE = 2的数据 datas2 := make([]dbmodels.Noticemsg, 0) s = engine.Where(fmt.Sprintf(`AUTOID in (select MEMBERRECV.MSGID from MEMBERRECV where MEMBERRECV.MEMBERID = %d)`, userAccount.Memberuserid)). And("SENDTYPE = 2 and SYSDATE > SCHEDULETIME and SYSDATE < ENDTIME and SENTSTATUS=1") if msgType > 0 { s = s.And("MSGTYPE = ?", msgType) } if onlyUnRead && len(msgReceivers) > 0 { s = s.NotIn("AUTOID", msgReceivers) } if err := s.Find(&datas2); err != nil { // 查询失败 return nil, err } rst = append(rst, datas2...) // 查询SENDTYPE = 3的数据 datas3 := make([]dbmodels.Noticemsg, 0) s = engine.Where(fmt.Sprintf(`USERID = (SELECT USERID FROM LOGINACCOUNT WHERE LOGINID = %d)`, loginID)). And("SENDTYPE = 3 and SYSDATE > SCHEDULETIME and SYSDATE < ENDTIME and SENTSTATUS=1") if msgType > 0 { s = s.And("MSGTYPE = ?", msgType) } if onlyUnRead && len(msgReceivers) > 0 { s = s.NotIn("AUTOID", msgReceivers) } if err := s.Find(&datas3); err != nil { // 查询失败 return nil, err } rst = append(rst, datas3...) return rst, nil }