ermcpAccMgr.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/3/30 13:38
  4. * @Modify : 2021/3/30 13:38
  5. * @note : 账户管理
  6. **/
  7. package models
  8. import (
  9. "fmt"
  10. "mtp2_if/db"
  11. "mtp2_if/mtpcache"
  12. "mtp2_if/utils"
  13. "strings"
  14. )
  15. // ErmcpLoginUserEx 登录账号(按角色)
  16. type ErmcpLoginUserEx struct {
  17. ROLENAME string `json:"rolename"` // 角色名称
  18. USERTYPE int32 `json:"usertype"` // 用户类型 2-机构 7-企业成员(云平台)
  19. UserList []ErmcpLoginUser `json:"userlist"` // 用户列表
  20. RoleId int32 `json:"-"` // 角色id
  21. }
  22. // ErmcpLoginUser 查询登录账户
  23. type ErmcpLoginUser struct {
  24. USERID int64 `json:"userid" xorm:"'USERID'"` // 用户id
  25. MEMBERUSERID int64 `json:"memberuserid" xorm:"'MEMBERUSERID'"` // 所属会员id
  26. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 用户名称
  27. USERTYPE int32 `json:"usertype" xorm:"'USERTYPE'"` // 用户类型 - 1:交易所 2:机构 3:会员子机构 4:经纪人 5:投资者 6:客户 7:企业成员(云平台)
  28. LOGINSTATUS int32 `json:"loginstatus" xorm:"'LOGINSTATUS'"` // 登录账户状态 - 1:正常 2:冻结 3:无效
  29. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  30. MODIFYTIME string `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间
  31. ROLETYPE string `json:"roletype" xorm:"'ROLETYPE'"` // 角色类型(逗号隔开,如22,23), 22:业务员 23:跟单员 24:交易员
  32. CLIENTROLEID int32 `json:"clientroleid" xorm:"'CLIENTROLEID'"` // 角色id(usertype=2)
  33. ROLENAME string `json:"rolename" xorm:"'ROLENAME'"` // 角色名称
  34. ROLESTATUS int32 `json:"rolestatus" xorm:"'ROLESTATUS'"` // 角色状态 1-启用 2-停用
  35. }
  36. func (r *ErmcpLoginUser) calc() {
  37. }
  38. func (r *ErmcpLoginUser) buildSql() string {
  39. var sqlId utils.SQLVal = "with tmp as" +
  40. " (select userid, wm_concat(roletype) roletype from arearole group by userid)" +
  41. "select t.userid," +
  42. " t.memberuserid," +
  43. " t.usertype," +
  44. " t.accountname," +
  45. " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  46. " to_char(t.modifytime, 'yyyy-mm-dd hh24:mi:ss') modifytime," +
  47. " r.roletype," +
  48. " 1 as rolestatus," +
  49. " l.logincode," +
  50. " l.loginid," +
  51. " l.clientroleid," +
  52. " 'name(22,23,24)' rolename," +
  53. " l.loginstatus" +
  54. " from useraccount t" +
  55. " left join loginaccount l" +
  56. " on t.userid = l.userid" +
  57. " left join tmp r" +
  58. " on t.userid = r.userid" +
  59. " where 1=1" +
  60. " and t.memberuserid = %v" +
  61. " and t.usertype = 7" +
  62. " union all " +
  63. "select t.userid," +
  64. " t.memberuserid," +
  65. " t.usertype," +
  66. " t.accountname," +
  67. " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  68. " to_char(t.modifytime, 'yyyy-mm-dd hh24:mi:ss') modifytime," +
  69. " to_char(r.roletype) roletype," +
  70. " r.rolestatus," +
  71. " l.logincode," +
  72. " l.loginid," +
  73. " l.clientroleid," +
  74. " Nvl(r.rolename, l.clientroleid) rolename," +
  75. " l.loginstatus" +
  76. " from useraccount t" +
  77. " left join loginaccount l" +
  78. " on t.userid = l.userid" +
  79. " left join systemmanagerrole r on l.clientroleid=r.autoid and r.areauserid=l.userid" +
  80. " where 1=1" +
  81. " and t.userid = %v" +
  82. " and t.usertype = 2"
  83. sqlId.FormatParam(r.USERID, r.USERID)
  84. return sqlId.String()
  85. }
  86. // hasRoletype 是否拥用某种角色
  87. func (r *ErmcpLoginUser) hasRoletype(nType int32) bool {
  88. sType := strings.Split(r.ROLETYPE, ",")
  89. strType := fmt.Sprintf("%v", nType)
  90. for i := range sType {
  91. if sType[i] == strType {
  92. return true
  93. }
  94. }
  95. return false
  96. }
  97. // GetDataEx 查询登录用户
  98. func (r *ErmcpLoginUser) GetDataEx() (interface{}, error) {
  99. sData := make([]ErmcpLoginUser, 0)
  100. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  101. sDataEx := make([]ErmcpLoginUserEx, 0)
  102. // 初始化固定三种企业成员角色
  103. sDataEx = append(sDataEx, ErmcpLoginUserEx{ROLENAME: "业务员", USERTYPE: 7, RoleId: 22, UserList: make([]ErmcpLoginUser, 0)})
  104. sDataEx = append(sDataEx, ErmcpLoginUserEx{ROLENAME: "跟单员", USERTYPE: 7, RoleId: 23, UserList: make([]ErmcpLoginUser, 0)})
  105. sDataEx = append(sDataEx, ErmcpLoginUserEx{ROLENAME: "交易员", USERTYPE: 7, RoleId: 24, UserList: make([]ErmcpLoginUser, 0)})
  106. for _, v := range sData {
  107. if v.USERTYPE == 7 {
  108. if len(sDataEx) >= 3 {
  109. if v.hasRoletype(22) {
  110. sDataEx[0].UserList = append(sDataEx[0].UserList, v)
  111. } else if v.hasRoletype(23) {
  112. sDataEx[1].UserList = append(sDataEx[1].UserList, v)
  113. } else if v.hasRoletype(24) {
  114. sDataEx[2].UserList = append(sDataEx[2].UserList, v)
  115. }
  116. }
  117. } else if v.USERTYPE == 2 {
  118. var bAdd bool = false
  119. for i := range sDataEx {
  120. if sDataEx[i].USERTYPE == 2 && sDataEx[i].RoleId == v.CLIENTROLEID {
  121. sDataEx[i].UserList = append(sDataEx[i].UserList, v)
  122. bAdd = true
  123. break
  124. }
  125. }
  126. // 不存在的新类别
  127. if !bAdd {
  128. val := ErmcpLoginUserEx{ROLENAME: v.ROLENAME, USERTYPE: 2, RoleId: v.CLIENTROLEID, UserList: make([]ErmcpLoginUser, 0)}
  129. val.UserList = append(val.UserList, v)
  130. sDataEx = append(sDataEx, val)
  131. }
  132. }
  133. }
  134. return sDataEx, err
  135. }
  136. // ErmcpTaAccount 期货账户(分组)
  137. type ErmcpTaAccountEx struct {
  138. MainAcc ErmcpTaAccount `json:"mainAcc"` // 主账号
  139. SubAccList []ErmcpTaAccount `json:"subacclist"` // 子账号列表
  140. }
  141. // ErmcpTaAccount 期货账户
  142. type ErmcpTaAccount struct {
  143. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 账户id
  144. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  145. TRADESTATUS int32 `json:"tradestatus" xorm:"'TRADESTATUS'"` // 交易状态 - 1:正常 2:受限 3:冻结 4:禁止建仓(人工受限) 5:禁止交易(人工冻结) 6:待激活 7:已注销
  146. ISMAIN int32 `json:"ismain" xorm:"'ISMAIN'"` // 是否主账户 0-否 1-是
  147. RELATEDUSERID int64 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联userid
  148. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 账户名称
  149. PARENTACCOUNTID int64 `json:"parentaccountid" xorm:"'PARENTACCOUNTID'"` // 父账户id
  150. }
  151. func (r *ErmcpTaAccount) calc() {
  152. }
  153. func (r *ErmcpTaAccount) buildSql() string {
  154. var sqlId utils.SQLVal = "select t.accountid," +
  155. " t.currencyid," +
  156. " t.tradestatus," +
  157. " t.ismain," +
  158. " t.relateduserid," +
  159. " t.accountname," +
  160. " t.parentaccountid" +
  161. " from taaccount t" +
  162. " where 1 = 1"
  163. sqlId.And("t.relateduserid", r.RELATEDUSERID)
  164. return sqlId.String()
  165. }
  166. // GetDataEx 查询企业期货账户
  167. func (r *ErmcpTaAccount) GetDataEx() (interface{}, error) {
  168. sData := make([]ErmcpTaAccount, 0)
  169. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  170. sDataEx := make([]ErmcpTaAccountEx, 0)
  171. // 主账号
  172. for i := range sData {
  173. if sData[i].ISMAIN == 1 {
  174. val := ErmcpTaAccountEx{MainAcc: sData[i], SubAccList: make([]ErmcpTaAccount, 0)}
  175. sDataEx = append(sDataEx, val)
  176. }
  177. }
  178. // 子账号
  179. for i := range sData {
  180. if sData[i].ISMAIN == 0 {
  181. for k := range sDataEx {
  182. if sDataEx[k].MainAcc.ACCOUNTID == sData[i].PARENTACCOUNTID {
  183. sDataEx[k].SubAccList = append(sDataEx[k].SubAccList, sData[i])
  184. }
  185. }
  186. }
  187. }
  188. return sDataEx, err
  189. }
  190. // MainAccountInfo 主账号详情
  191. type MainAccountInfo struct {
  192. ACCOUNTID string `json:"accountid" xorm:"'ACCOUNTID'"` // 账号id
  193. HEDGEACCOUNTCODE string `json:"hedgeaccountcode" xorm:"'HEDGEACCOUNTCODE'"` // 外部账号代码
  194. HEDGEACCOUNTPWD string `json:"hedgeaccountpwd" xorm:"'HEDGEACCOUNTPWD'"` // 密码
  195. AUTHCODE string `json:"authcode" xorm:"'AUTHCODE'"` // 授权码
  196. APPID string `json:"appid" xorm:"'APPID'"` // AppID
  197. BROKERID string `json:"brokerid" xorm:"'BROKERID'"` // 经纪商id
  198. FCID int32 `json:"fcid" xorm:"'FCID'"` // 期货公司id
  199. FCNAME string `json:"fcname" xorm:"'FCNAME'"` // 期货公司代码
  200. ISMAIN int32 `json:"ismain" xorm:"'ISMAIN'"` // 是否主账号
  201. RELATEDUSERID int64 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联用户id
  202. }
  203. func (r *MainAccountInfo) calc() {
  204. r.HEDGEACCOUNTPWD = DecryptField(r.HEDGEACCOUNTPWD)
  205. }
  206. func (r *MainAccountInfo) buildSql() string {
  207. var sqlId utils.SQLVal = "select t.accountid," +
  208. " t.hedgeaccountcode," +
  209. " c.hedgeaccountpwd," +
  210. " c.authcode," +
  211. " c.appid," +
  212. " c.brokerid," +
  213. " c.fcid," +
  214. " f.fcname," +
  215. " ta.ismain," +
  216. " ta.relateduserid" +
  217. " from hedge_outtaaccount t" +
  218. " left join hedge_outmainconfig c" +
  219. " on t.accountid = c.accountid" +
  220. " left join ERMCP_FuturesCompany f" +
  221. " on c.fcid = f.fcid" +
  222. " left join taaccount ta on t.accountid=ta.accountid" +
  223. " where 1 = 1"
  224. if r.RELATEDUSERID > 0 {
  225. sqlId.And("ta.relateduserid", r.RELATEDUSERID)
  226. }
  227. if len(r.ACCOUNTID) > 0 {
  228. sqlId.And("t.ACCOUNTID", r.ACCOUNTID)
  229. }
  230. return sqlId.String()
  231. }
  232. // GetDataEx 查询主账号信息
  233. func (r *MainAccountInfo) GetDataEx() (interface{}, error) {
  234. sData := make([]MainAccountInfo, 0)
  235. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  236. for i := range sData {
  237. sData[i].calc()
  238. }
  239. return sData, err
  240. }
  241. // ErmcpRole 角色
  242. type ErmcpRole struct {
  243. AUTOID int32 `json:"autoid" xorm:"'AUTOID'"` // 角色ID(自增ID)
  244. ROLENAME string `json:"rolename" xorm:"'ROLENAME'"` // 角色名称
  245. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  246. ROLESTATUS int32 `json:"rolestatus" xorm:"'ROLESTATUS'"` // 角色状态 - 1:启用 2:停用
  247. ROLETYPE int32 `json:"roletype" xorm:"'ROLETYPE'"` // 角色类型 - 1- 管理端 2- 交易端
  248. MODIFYTIME string `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间(创建时间)
  249. MODIFIERID int64 `json:"modifierid" xorm:"'MODIFIERID'"` // 修改人(创建人)
  250. MODIFIERNAME string `json:"modifiername"` // 修改人名称
  251. }
  252. func (r *ErmcpRole) calc() {
  253. if r.MODIFIERID > 0 {
  254. r.MODIFIERNAME = mtpcache.GetSystemmangerLoginCode(r.MODIFIERID)
  255. // 目前看表里修改人id存的是管理员id,为防止以后终端操作存的是登录id,多做一层判断
  256. // 如果按管理员id找不到,则按登录id试试
  257. if r.MODIFIERID > 1e8 && len(r.MODIFIERNAME) == 0 {
  258. r.MODIFIERNAME = mtpcache.GetLoginCodeByLoginId(r.MODIFIERID)
  259. }
  260. }
  261. }
  262. func (r *ErmcpRole) buildSql() string {
  263. var sqlId utils.SQLVal = "SELECT t.AUTOID," +
  264. " t.ROLENAME," +
  265. " t.AREAUSERID," +
  266. " t.ROLESTATUS," +
  267. " t.ROLETYPE," +
  268. " t.SENSITIVEFIELDS," +
  269. " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
  270. " to_char(t.MODIFIERID) MODIFIERID" +
  271. " FROM SYSTEMMANAGERROLE t" +
  272. " WHERE 1 = 1"
  273. if r.AREAUSERID > 0 {
  274. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  275. }
  276. return sqlId.String()
  277. }
  278. // GetDataEx 查询角色
  279. func (r *ErmcpRole) GetDataEx() (interface{}, error) {
  280. sData := make([]ErmcpRole, 0)
  281. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  282. for i := range sData {
  283. sData[i].calc()
  284. }
  285. sDataEx := make([]ErmcpRole, 0)
  286. // 添加固定角色22,23,24
  287. sDataEx = append(sDataEx, ErmcpRole{AREAUSERID: r.AREAUSERID, ROLENAME: "业务员", AUTOID: 22,
  288. ROLESTATUS: 1, ROLETYPE: 2, MODIFYTIME: "-", MODIFIERID: 1, MODIFIERNAME: "平台"})
  289. sDataEx = append(sDataEx, ErmcpRole{AREAUSERID: r.AREAUSERID, ROLENAME: "跟单员", AUTOID: 23,
  290. ROLESTATUS: 1, ROLETYPE: 2, MODIFYTIME: "-", MODIFIERID: 1, MODIFIERNAME: "平台"})
  291. sDataEx = append(sDataEx, ErmcpRole{AREAUSERID: r.AREAUSERID, ROLENAME: "交易员", AUTOID: 24,
  292. ROLESTATUS: 1, ROLETYPE: 2, MODIFYTIME: "-", MODIFIERID: 1, MODIFIERNAME: "平台"})
  293. sDataEx = append(sDataEx, sData...)
  294. return sDataEx, err
  295. }
  296. // ErmcpRoleMenuEx 角色菜单(分层级)
  297. type ErmcpRoleMenuEx struct {
  298. Menu ErmcpRoleMenu // 父级菜单
  299. SubMenu []ErmcpRoleMenu // 子级菜单
  300. }
  301. // add 添加子级菜单
  302. func (r *ErmcpRoleMenuEx) add(menu ErmcpRoleMenu) {
  303. if menu.PARENTCODE == r.Menu.RESOURCECODE {
  304. r.SubMenu = append(r.SubMenu, menu)
  305. }
  306. }
  307. // ErmcpRoleMenu 角色菜单
  308. type ErmcpRoleMenu struct {
  309. USERID int64 `json:"userid" xorm:"'USERID'"` // 用户id
  310. ROLEID int32 `json:"roleid" xorm:"'ROLEID'"` // 角色id
  311. RESOURCECODE string `json:"resourcecode" xorm:"'RESOURCECODE'"` // 菜单代码
  312. RESOURCENAME string `json:"resourcename" xorm:"'RESOURCENAME'"` // 菜单名称
  313. RESOURCELEVEL int32 `json:"resourcelevel" xorm:"'RESOURCELEVEL'"` // 级别
  314. MENUTYPE int32 `json:"menutype" xorm:"'MENUTYPE'"` // 菜单类型 1:管理端 2:交易端 3:终端(企业云平台)
  315. PARENTCODE string `json:"parentcode" xorm:"'PARENTCODE'"` // 上级资源代码
  316. URL string `json:"url" xorm:"'URL'"` // URL
  317. SORT int32 `json:"sort" xorm:"'SORT'"` // 排序
  318. ICONAME string `json:"iconame" xorm:"'ICONAME'"` // 菜单图标
  319. REMARK string `json:"remark" xorm:"'REMARK'"` // 菜单备注
  320. FilterRoleId string `json:"-"` // 查询过滤条件, 角色 - 逗号隔开
  321. }
  322. func (r *ErmcpRoleMenu) calc() {
  323. }
  324. func (r *ErmcpRoleMenu) buildSql() string {
  325. var sqlId utils.SQLVal = "select distinct t.userid," +
  326. " t.roleid," +
  327. " t.resourcecode," +
  328. " f.resourcename," +
  329. " f.resourcelevel," +
  330. " f.menutype," +
  331. " f.parentcode," +
  332. " f.url," +
  333. " f.sort," +
  334. " f.iconame," +
  335. " f.remark" +
  336. " from ermcp_memberfuncmenu t" +
  337. " inner join funcmenulist f" +
  338. " on t.resourcecode = f.resourcecode" +
  339. " where t.isvalid = 1"
  340. if r.USERID > 0 {
  341. sqlId.And("t.userid", r.USERID)
  342. }
  343. if len(r.FilterRoleId) > 0 {
  344. sqlId.Join(fmt.Sprintf(" and t.roleid in(%v)", r.FilterRoleId))
  345. }
  346. sqlId.Join(" order by f.resourcelevel, f.sort")
  347. return sqlId.String()
  348. }
  349. // GetDataEx 查询角色菜单
  350. func (r *ErmcpRoleMenu) GetDataEx() (interface{}, error) {
  351. sData := make([]ErmcpRoleMenu, 0)
  352. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  353. for i := range sData {
  354. sData[i].calc()
  355. }
  356. // 构建层级树
  357. sDataEx := make([]ErmcpRoleMenuEx, 0)
  358. // 最多构建4层
  359. for level := int32(1); level <= 4; level++ {
  360. for _, v := range sData {
  361. if v.RESOURCELEVEL == level {
  362. val := ErmcpRoleMenuEx{Menu: v, SubMenu: make([]ErmcpRoleMenu, 0)}
  363. for _, v2 := range sData {
  364. // 获取子菜单
  365. if v2.ROLEID == v.ROLEID && v2.PARENTCODE == v.RESOURCECODE {
  366. val.SubMenu = append(val.SubMenu, v2)
  367. }
  368. }
  369. // 第一层固定要, 不管有没有子菜单
  370. if val.Menu.RESOURCELEVEL == 1 {
  371. sDataEx = append(sDataEx, val)
  372. } else if val.Menu.RESOURCELEVEL > 1 && len(val.SubMenu) > 0 {
  373. // 第二层开始,有子菜单内容的才要
  374. sDataEx = append(sDataEx, val)
  375. }
  376. }
  377. }
  378. }
  379. return sDataEx, err
  380. }