| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- /**
- * @Author: zou.yingbin
- * @Create : 2021/3/30 13:38
- * @Modify : 2021/3/30 13:38
- * @note : 账户管理
- **/
- package models
- import (
- "fmt"
- "mtp2_if/db"
- "mtp2_if/mtpcache"
- "mtp2_if/utils"
- "strings"
- )
- // ErmcpLoginUserEx 登录账号(按角色)
- type ErmcpLoginUserEx struct {
- ROLENAME string `json:"rolename"` // 角色名称
- USERTYPE int32 `json:"usertype"` // 用户类型 2-机构 7-企业成员(云平台)
- UserList []ErmcpLoginUser `json:"userlist"` // 用户列表
- RoleId int32 `json:"-"` // 角色id
- }
- // ErmcpLoginUser 查询登录账户
- type ErmcpLoginUser struct {
- USERID int64 `json:"userid" xorm:"'USERID'"` // 用户id
- MEMBERUSERID int64 `json:"memberuserid" xorm:"'MEMBERUSERID'"` // 所属会员id
- ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 用户名称
- USERTYPE int32 `json:"usertype" xorm:"'USERTYPE'"` // 用户类型 - 1:交易所 2:机构 3:会员子机构 4:经纪人 5:投资者 6:客户 7:企业成员(云平台)
- LOGINSTATUS int32 `json:"loginstatus" xorm:"'LOGINSTATUS'"` // 登录账户状态 - 1:正常 2:冻结 3:无效
- CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
- MODIFYTIME string `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间
- ROLETYPE string `json:"roletype" xorm:"'ROLETYPE'"` // 角色类型(逗号隔开,如22,23), 22:业务员 23:跟单员 24:交易员
- CLIENTROLEID int32 `json:"clientroleid" xorm:"'CLIENTROLEID'"` // 角色id(usertype=2)
- ROLENAME string `json:"rolename" xorm:"'ROLENAME'"` // 角色名称
- ROLESTATUS int32 `json:"rolestatus" xorm:"'ROLESTATUS'"` // 角色状态 1-启用 2-停用
- }
- func (r *ErmcpLoginUser) calc() {
- }
- func (r *ErmcpLoginUser) buildSql() string {
- var sqlId utils.SQLVal = "with tmp as" +
- " (select userid, wm_concat(roletype) roletype from arearole group by userid)" +
- "select t.userid," +
- " t.memberuserid," +
- " t.usertype," +
- " t.accountname," +
- " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
- " to_char(t.modifytime, 'yyyy-mm-dd hh24:mi:ss') modifytime," +
- " r.roletype," +
- " 1 as rolestatus," +
- " l.logincode," +
- " l.loginid," +
- " l.clientroleid," +
- " 'name(22,23,24)' rolename," +
- " l.loginstatus" +
- " from useraccount t" +
- " left join loginaccount l" +
- " on t.userid = l.userid" +
- " left join tmp r" +
- " on t.userid = r.userid" +
- " where 1=1" +
- " and t.memberuserid = %v" +
- " and t.usertype = 7" +
- " union all " +
- "select t.userid," +
- " t.memberuserid," +
- " t.usertype," +
- " t.accountname," +
- " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
- " to_char(t.modifytime, 'yyyy-mm-dd hh24:mi:ss') modifytime," +
- " to_char(r.roletype) roletype," +
- " r.rolestatus," +
- " l.logincode," +
- " l.loginid," +
- " l.clientroleid," +
- " Nvl(r.rolename, l.clientroleid) rolename," +
- " l.loginstatus" +
- " from useraccount t" +
- " left join loginaccount l" +
- " on t.userid = l.userid" +
- " left join systemmanagerrole r on l.clientroleid=r.autoid and r.areauserid=l.userid" +
- " where 1=1" +
- " and t.userid = %v" +
- " and t.usertype = 2"
- sqlId.FormatParam(r.USERID, r.USERID)
- return sqlId.String()
- }
- // hasRoletype 是否拥用某种角色
- func (r *ErmcpLoginUser) hasRoletype(nType int32) bool {
- sType := strings.Split(r.ROLETYPE, ",")
- strType := fmt.Sprintf("%v", nType)
- for i := range sType {
- if sType[i] == strType {
- return true
- }
- }
- return false
- }
- // GetDataEx 查询登录用户
- func (r *ErmcpLoginUser) GetDataEx() (interface{}, error) {
- sData := make([]ErmcpLoginUser, 0)
- err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
- sDataEx := make([]ErmcpLoginUserEx, 0)
- // 初始化固定三种企业成员角色
- sDataEx = append(sDataEx, ErmcpLoginUserEx{ROLENAME: "业务员", USERTYPE: 7, RoleId: 22, UserList: make([]ErmcpLoginUser, 0)})
- sDataEx = append(sDataEx, ErmcpLoginUserEx{ROLENAME: "跟单员", USERTYPE: 7, RoleId: 23, UserList: make([]ErmcpLoginUser, 0)})
- sDataEx = append(sDataEx, ErmcpLoginUserEx{ROLENAME: "交易员", USERTYPE: 7, RoleId: 24, UserList: make([]ErmcpLoginUser, 0)})
- for _, v := range sData {
- if v.USERTYPE == 7 {
- if len(sDataEx) >= 3 {
- if v.hasRoletype(22) {
- sDataEx[0].UserList = append(sDataEx[0].UserList, v)
- } else if v.hasRoletype(23) {
- sDataEx[1].UserList = append(sDataEx[1].UserList, v)
- } else if v.hasRoletype(24) {
- sDataEx[2].UserList = append(sDataEx[2].UserList, v)
- }
- }
- } else if v.USERTYPE == 2 {
- var bAdd bool = false
- for i := range sDataEx {
- if sDataEx[i].USERTYPE == 2 && sDataEx[i].RoleId == v.CLIENTROLEID {
- sDataEx[i].UserList = append(sDataEx[i].UserList, v)
- bAdd = true
- break
- }
- }
- // 不存在的新类别
- if !bAdd {
- val := ErmcpLoginUserEx{ROLENAME: v.ROLENAME, USERTYPE: 2, RoleId: v.CLIENTROLEID, UserList: make([]ErmcpLoginUser, 0)}
- val.UserList = append(val.UserList, v)
- sDataEx = append(sDataEx, val)
- }
- }
- }
- return sDataEx, err
- }
- // ErmcpTaAccount 期货账户(分组)
- type ErmcpTaAccountEx struct {
- MainAcc ErmcpTaAccount `json:"mainAcc"` // 主账号
- SubAccList []ErmcpTaAccount `json:"subacclist"` // 子账号列表
- }
- // ErmcpTaAccount 期货账户
- type ErmcpTaAccount struct {
- ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 账户id
- CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
- TRADESTATUS int32 `json:"tradestatus" xorm:"'TRADESTATUS'"` // 交易状态 - 1:正常 2:受限 3:冻结 4:禁止建仓(人工受限) 5:禁止交易(人工冻结) 6:待激活 7:已注销
- ISMAIN int32 `json:"ismain" xorm:"'ISMAIN'"` // 是否主账户 0-否 1-是
- RELATEDUSERID int64 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联userid
- ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 账户名称
- PARENTACCOUNTID int64 `json:"parentaccountid" xorm:"'PARENTACCOUNTID'"` // 父账户id
- }
- func (r *ErmcpTaAccount) calc() {
- }
- func (r *ErmcpTaAccount) buildSql() string {
- var sqlId utils.SQLVal = "select t.accountid," +
- " t.currencyid," +
- " t.tradestatus," +
- " t.ismain," +
- " t.relateduserid," +
- " t.accountname," +
- " t.parentaccountid" +
- " from taaccount t" +
- " where 1 = 1"
- sqlId.And("t.relateduserid", r.RELATEDUSERID)
- return sqlId.String()
- }
- // GetDataEx 查询企业期货账户
- func (r *ErmcpTaAccount) GetDataEx() (interface{}, error) {
- sData := make([]ErmcpTaAccount, 0)
- err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
- sDataEx := make([]ErmcpTaAccountEx, 0)
- // 主账号
- for i := range sData {
- if sData[i].ISMAIN == 1 {
- val := ErmcpTaAccountEx{MainAcc: sData[i], SubAccList: make([]ErmcpTaAccount, 0)}
- sDataEx = append(sDataEx, val)
- }
- }
- // 子账号
- for i := range sData {
- if sData[i].ISMAIN == 0 {
- for k := range sDataEx {
- if sDataEx[k].MainAcc.ACCOUNTID == sData[i].PARENTACCOUNTID {
- sDataEx[k].SubAccList = append(sDataEx[k].SubAccList, sData[i])
- }
- }
- }
- }
- return sDataEx, err
- }
- // MainAccountInfo 主账号详情
- type MainAccountInfo struct {
- ACCOUNTID string `json:"accountid" xorm:"'ACCOUNTID'"` // 账号id
- HEDGEACCOUNTCODE string `json:"hedgeaccountcode" xorm:"'HEDGEACCOUNTCODE'"` // 外部账号代码
- HEDGEACCOUNTPWD string `json:"hedgeaccountpwd" xorm:"'HEDGEACCOUNTPWD'"` // 密码
- AUTHCODE string `json:"authcode" xorm:"'AUTHCODE'"` // 授权码
- APPID string `json:"appid" xorm:"'APPID'"` // AppID
- BROKERID string `json:"brokerid" xorm:"'BROKERID'"` // 经纪商id
- FCID int32 `json:"fcid" xorm:"'FCID'"` // 期货公司id
- FCNAME string `json:"fcname" xorm:"'FCNAME'"` // 期货公司代码
- ISMAIN int32 `json:"ismain" xorm:"'ISMAIN'"` // 是否主账号
- RELATEDUSERID int64 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联用户id
- }
- func (r *MainAccountInfo) calc() {
- r.HEDGEACCOUNTPWD = DecryptField(r.HEDGEACCOUNTPWD)
- }
- func (r *MainAccountInfo) buildSql() string {
- var sqlId utils.SQLVal = "select t.accountid," +
- " t.hedgeaccountcode," +
- " c.hedgeaccountpwd," +
- " c.authcode," +
- " c.appid," +
- " c.brokerid," +
- " c.fcid," +
- " f.fcname," +
- " ta.ismain," +
- " ta.relateduserid" +
- " from hedge_outtaaccount t" +
- " left join hedge_outmainconfig c" +
- " on t.accountid = c.accountid" +
- " left join ERMCP_FuturesCompany f" +
- " on c.fcid = f.fcid" +
- " left join taaccount ta on t.accountid=ta.accountid" +
- " where 1 = 1"
- if r.RELATEDUSERID > 0 {
- sqlId.And("ta.relateduserid", r.RELATEDUSERID)
- }
- if len(r.ACCOUNTID) > 0 {
- sqlId.And("t.ACCOUNTID", r.ACCOUNTID)
- }
- return sqlId.String()
- }
- // GetDataEx 查询主账号信息
- func (r *MainAccountInfo) GetDataEx() (interface{}, error) {
- sData := make([]MainAccountInfo, 0)
- err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
- for i := range sData {
- sData[i].calc()
- }
- return sData, err
- }
- // ErmcpRole 角色
- type ErmcpRole struct {
- AUTOID int32 `json:"autoid" xorm:"'AUTOID'"` // 角色ID(自增ID)
- ROLENAME string `json:"rolename" xorm:"'ROLENAME'"` // 角色名称
- AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
- ROLESTATUS int32 `json:"rolestatus" xorm:"'ROLESTATUS'"` // 角色状态 - 1:启用 2:停用
- ROLETYPE int32 `json:"roletype" xorm:"'ROLETYPE'"` // 角色类型 - 1- 管理端 2- 交易端
- MODIFYTIME string `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间(创建时间)
- MODIFIERID int64 `json:"modifierid" xorm:"'MODIFIERID'"` // 修改人(创建人)
- MODIFIERNAME string `json:"modifiername"` // 修改人名称
- }
- func (r *ErmcpRole) calc() {
- if r.MODIFIERID > 0 {
- r.MODIFIERNAME = mtpcache.GetSystemmangerLoginCode(r.MODIFIERID)
- // 目前看表里修改人id存的是管理员id,为防止以后终端操作存的是登录id,多做一层判断
- // 如果按管理员id找不到,则按登录id试试
- if r.MODIFIERID > 1e8 && len(r.MODIFIERNAME) == 0 {
- r.MODIFIERNAME = mtpcache.GetLoginCodeByLoginId(r.MODIFIERID)
- }
- }
- }
- func (r *ErmcpRole) buildSql() string {
- var sqlId utils.SQLVal = "SELECT t.AUTOID," +
- " t.ROLENAME," +
- " t.AREAUSERID," +
- " t.ROLESTATUS," +
- " t.ROLETYPE," +
- " t.SENSITIVEFIELDS," +
- " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
- " to_char(t.MODIFIERID) MODIFIERID" +
- " FROM SYSTEMMANAGERROLE t" +
- " WHERE 1 = 1"
- if r.AREAUSERID > 0 {
- sqlId.And("t.AREAUSERID", r.AREAUSERID)
- }
- return sqlId.String()
- }
- // GetDataEx 查询角色
- func (r *ErmcpRole) GetDataEx() (interface{}, error) {
- sData := make([]ErmcpRole, 0)
- err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
- for i := range sData {
- sData[i].calc()
- }
- sDataEx := make([]ErmcpRole, 0)
- // 添加固定角色22,23,24
- sDataEx = append(sDataEx, ErmcpRole{AREAUSERID: r.AREAUSERID, ROLENAME: "业务员", AUTOID: 22,
- ROLESTATUS: 1, ROLETYPE: 2, MODIFYTIME: "-", MODIFIERID: 1, MODIFIERNAME: "平台"})
- sDataEx = append(sDataEx, ErmcpRole{AREAUSERID: r.AREAUSERID, ROLENAME: "跟单员", AUTOID: 23,
- ROLESTATUS: 1, ROLETYPE: 2, MODIFYTIME: "-", MODIFIERID: 1, MODIFIERNAME: "平台"})
- sDataEx = append(sDataEx, ErmcpRole{AREAUSERID: r.AREAUSERID, ROLENAME: "交易员", AUTOID: 24,
- ROLESTATUS: 1, ROLETYPE: 2, MODIFYTIME: "-", MODIFIERID: 1, MODIFIERNAME: "平台"})
- sDataEx = append(sDataEx, sData...)
- return sDataEx, err
- }
- // ErmcpRoleMenuEx 角色菜单(分层级)
- type ErmcpRoleMenuEx struct {
- Menu ErmcpRoleMenu // 父级菜单
- SubMenu []ErmcpRoleMenu // 子级菜单
- }
- // add 添加子级菜单
- func (r *ErmcpRoleMenuEx) add(menu ErmcpRoleMenu) {
- if menu.PARENTCODE == r.Menu.RESOURCECODE {
- r.SubMenu = append(r.SubMenu, menu)
- }
- }
- // ErmcpRoleMenu 角色菜单
- type ErmcpRoleMenu struct {
- USERID int64 `json:"userid" xorm:"'USERID'"` // 用户id
- ROLEID int32 `json:"roleid" xorm:"'ROLEID'"` // 角色id
- RESOURCECODE string `json:"resourcecode" xorm:"'RESOURCECODE'"` // 菜单代码
- RESOURCENAME string `json:"resourcename" xorm:"'RESOURCENAME'"` // 菜单名称
- RESOURCELEVEL int32 `json:"resourcelevel" xorm:"'RESOURCELEVEL'"` // 级别
- MENUTYPE int32 `json:"menutype" xorm:"'MENUTYPE'"` // 菜单类型 1:管理端 2:交易端 3:终端(企业云平台)
- PARENTCODE string `json:"parentcode" xorm:"'PARENTCODE'"` // 上级资源代码
- URL string `json:"url" xorm:"'URL'"` // URL
- SORT int32 `json:"sort" xorm:"'SORT'"` // 排序
- ICONAME string `json:"iconame" xorm:"'ICONAME'"` // 菜单图标
- REMARK string `json:"remark" xorm:"'REMARK'"` // 菜单备注
- FilterRoleId string `json:"-"` // 查询过滤条件, 角色 - 逗号隔开
- }
- func (r *ErmcpRoleMenu) calc() {
- }
- func (r *ErmcpRoleMenu) buildSql() string {
- var sqlId utils.SQLVal = "select distinct t.userid," +
- " t.roleid," +
- " t.resourcecode," +
- " f.resourcename," +
- " f.resourcelevel," +
- " f.menutype," +
- " f.parentcode," +
- " f.url," +
- " f.sort," +
- " f.iconame," +
- " f.remark" +
- " from ermcp_memberfuncmenu t" +
- " inner join funcmenulist f" +
- " on t.resourcecode = f.resourcecode" +
- " where t.isvalid = 1"
- if r.USERID > 0 {
- sqlId.And("t.userid", r.USERID)
- }
- if len(r.FilterRoleId) > 0 {
- sqlId.Join(fmt.Sprintf(" and t.roleid in(%v)", r.FilterRoleId))
- }
- sqlId.Join(" order by f.resourcelevel, f.sort")
- return sqlId.String()
- }
- // GetDataEx 查询角色菜单
- func (r *ErmcpRoleMenu) GetDataEx() (interface{}, error) {
- sData := make([]ErmcpRoleMenu, 0)
- err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
- for i := range sData {
- sData[i].calc()
- }
- // 构建层级树
- sDataEx := make([]ErmcpRoleMenuEx, 0)
- // 最多构建4层
- for level := int32(1); level <= 4; level++ {
- for _, v := range sData {
- if v.RESOURCELEVEL == level {
- val := ErmcpRoleMenuEx{Menu: v, SubMenu: make([]ErmcpRoleMenu, 0)}
- for _, v2 := range sData {
- // 获取子菜单
- if v2.ROLEID == v.ROLEID && v2.PARENTCODE == v.RESOURCECODE {
- val.SubMenu = append(val.SubMenu, v2)
- }
- }
- // 第一层固定要, 不管有没有子菜单
- if val.Menu.RESOURCELEVEL == 1 {
- sDataEx = append(sDataEx, val)
- } else if val.Menu.RESOURCELEVEL > 1 && len(val.SubMenu) > 0 {
- // 第二层开始,有子菜单内容的才要
- sDataEx = append(sDataEx, val)
- }
- }
- }
- }
- return sDataEx, err
- }
|