sys_role_admin.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package sys_role_admin
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type SysRoleAdmin struct {
  7. Id int `orm:"column(id);pk" description:"自增ID"`
  8. AdminId int `orm:"column(admin_id);" description:"系统用户id"`
  9. RoleId int `orm:"column(role_id);" description:"角色ID"`
  10. CreateTime time.Time `orm:"column(create_time);" description:"创建时间"`
  11. }
  12. type RoleAdminItem struct {
  13. SysRoleAdminId int `description:"管理员账号和角色映射表id"`
  14. AdminId int `description:"系统用户id"`
  15. AdminName string `description:"系统用户名称"`
  16. RealName string `description:"系统用户姓名"`
  17. Enabled int `description:"1:有效,0:禁用"`
  18. Mobile string `description:"手机号"`
  19. RoleId int `description:"角色Id"`
  20. RoleTypeCode string `description:"角色编码"`
  21. DepartmentId int `description:"部门id"`
  22. DepartmentName string `description:"部门名称"`
  23. DepartmentGroup string `description:"部门分组"`
  24. TeamId int `description:"小组id"`
  25. GroupId int `description:"分组id"`
  26. GroupName string `description:"分组名称"`
  27. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  28. LabelVal int `description:"标签:1:超级管理员,2:管理员,3:部门经理,4:组长,5:ficc销售主管"`
  29. }
  30. func (sa *SysRoleAdmin) TableName() string {
  31. return "sys_role_admin"
  32. }
  33. // GetRoleIdsByAdminId 根据管理员账号查询角色ID
  34. func GetRoleIdsByAdminId(adminId int) (items []*SysRoleAdmin, err error) {
  35. sql := `SELECT * from sys_role_admin where admin_id = ?`
  36. _, err = orm.NewOrm().Raw(sql, adminId).QueryRows(&items)
  37. return
  38. }
  39. // GetRoleIdsByAdminIdRoleId 根据管理员账号和角色查询
  40. func GetRoleIdsByAdminIdRoleId(adminId int, roleId int) (item *SysRoleAdmin, err error) {
  41. sql := `SELECT * from sys_role_admin where admin_id = ? and role_id=?`
  42. err = orm.NewOrm().Raw(sql, adminId, roleId).QueryRow(&item)
  43. return
  44. }
  45. // GetRoleAdminList 查询对应角色绑定的管理员列表
  46. func GetRoleAdminList(condition string, pars []interface{}, startSize, pageSize int) (list []*RoleAdminItem, err error) {
  47. sql := `SELECT
  48. s.id as sys_role_admin_id,
  49. a.admin_id,
  50. a.admin_name,
  51. a.real_name,
  52. a.mobile,
  53. a.department_id,
  54. a.department_name,
  55. a.enabled,
  56. a.role_id,
  57. a.role_type_code ,
  58. a.group_id,
  59. a.authority,
  60. a.group_name
  61. FROM
  62. sys_role_admin s
  63. INNER JOIN admin a ON s.admin_id = a.admin_id
  64. where 1=1`
  65. if condition != "" {
  66. sql += condition
  67. }
  68. sql += ` ORDER BY s.id DESC limit ?, ?`
  69. _, err = orm.NewOrm().Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  70. return
  71. }
  72. // GetRoleAdminListTotal 查询对应角色绑定的管理员列表总数
  73. func GetRoleAdminListTotal(condition string, pars []interface{}) (total int64, err error) {
  74. sql := `SELECT
  75. count(*)
  76. FROM
  77. sys_role_admin s
  78. INNER JOIN admin a ON s.admin_id = a.admin_id
  79. where 1=1`
  80. if condition != "" {
  81. sql += condition
  82. }
  83. err = orm.NewOrm().Raw(sql, pars).QueryRow(&total)
  84. return
  85. }
  86. // CheckRoleAdminByAdminIds 判断是否已经绑定过该角色
  87. func CheckRoleAdminByAdminIds(adminIds string, roleId int) (list []*SysRoleAdmin, err error) {
  88. sql := `SELECT
  89. s.*
  90. FROM
  91. sys_role_admin s
  92. LEFT JOIN admin a ON s.admin_id = a.admin_id
  93. where s.role_id=? and s.admin_id in (` + adminIds + `)`
  94. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&list)
  95. return
  96. }
  97. // AddRoleAdminBatch 批量新增留言版管理员
  98. func AddRoleAdminBatch(admins []*SysRoleAdmin) (err error) {
  99. o := orm.NewOrm()
  100. _, err = o.InsertMulti(len(admins), admins)
  101. return
  102. }
  103. // DeleteRoleAdmin 删除管理员和角色的绑定
  104. func DeleteRoleAdmin(SysRoleAdminId int) (err error) {
  105. sql := `DELETE FROM sys_role_admin WHERE id=? `
  106. o := orm.NewOrm()
  107. _, err = o.Raw(sql, SysRoleAdminId).Exec()
  108. return
  109. }
  110. // GetRoleAdminById 根据ID查询绑定记录
  111. func GetRoleAdminById(SysRoleAdminId int) (item *SysRoleAdmin, err error) {
  112. sql := `SELECT
  113. *
  114. FROM
  115. sys_role_admin
  116. where id=?`
  117. err = orm.NewOrm().Raw(sql, SysRoleAdminId).QueryRow(&item)
  118. return
  119. }