sys_role_admin.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package system
  2. import (
  3. "eta_gn/eta_api/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type SysRoleAdmin struct {
  8. Id int `orm:"column(id);pk" description:"自增ID"`
  9. AdminId int `orm:"column(admin_id);" description:"系统用户id"`
  10. RoleId int `orm:"column(role_id);" description:"角色ID"`
  11. CreateTime time.Time `orm:"column(create_time);" description:"创建时间"`
  12. }
  13. type RoleAdminItem struct {
  14. SysRoleAdminId int `description:"管理员账号和角色映射表id"`
  15. AdminId int `description:"系统用户id"`
  16. AdminName string `description:"系统用户名称"`
  17. RealName string `description:"系统用户姓名"`
  18. Enabled int `description:"1:有效,0:禁用"`
  19. Mobile string `description:"手机号"`
  20. RoleId int `description:"角色Id"`
  21. RoleTypeCode string `description:"角色编码"`
  22. DepartmentId int `description:"部门id"`
  23. DepartmentName string `description:"部门名称"`
  24. DepartmentGroup string `description:"部门分组"`
  25. TeamId int `description:"小组id"`
  26. GroupId int `description:"分组id"`
  27. GroupName string `description:"分组名称"`
  28. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  29. LabelVal int `description:"标签:1:超级管理员,2:管理员,3:部门经理,4:组长,5:ficc销售主管"`
  30. }
  31. func (sa *SysRoleAdmin) TableName() string {
  32. return "sys_role_admin"
  33. }
  34. // GetRoleIdsByAdminId 根据管理员账号查询角色ID
  35. func GetRoleIdsByAdminId(adminId int) (items []*SysRoleAdmin, err error) {
  36. sql := `SELECT * from sys_role_admin where admin_id = ?`
  37. _, err = orm.NewOrm().Raw(sql, adminId).QueryRows(&items)
  38. return
  39. }
  40. // GetRoleIdsByAdminIdRoleId 根据管理员账号和角色查询
  41. func GetRoleIdsByAdminIdRoleId(adminId int, roleId int) (item *SysRoleAdmin, err error) {
  42. sql := `SELECT * from sys_role_admin where admin_id = ? and role_id=?`
  43. err = orm.NewOrm().Raw(sql, adminId, roleId).QueryRow(&item)
  44. return
  45. }
  46. // GetRoleAdminList 查询对应角色绑定的管理员列表
  47. func GetRoleAdminList(condition string, pars []interface{}, startSize, pageSize int) (list []*RoleAdminItem, err error) {
  48. sql := `SELECT
  49. s.id as sys_role_admin_id,
  50. a.admin_id,
  51. a.admin_name,
  52. a.real_name,
  53. a.mobile,
  54. a.department_id,
  55. a.department_name,
  56. a.enabled,
  57. a.role_id,
  58. a.role_type_code ,
  59. a.group_id,
  60. a.authority,
  61. a.group_name
  62. FROM
  63. sys_role_admin s
  64. INNER JOIN admin a ON s.admin_id = a.admin_id
  65. where 1=1`
  66. if condition != "" {
  67. sql += condition
  68. }
  69. sql += ` ORDER BY s.id DESC limit ?, ?`
  70. _, err = orm.NewOrm().Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  71. return
  72. }
  73. // GetRoleAdminListTotal 查询对应角色绑定的管理员列表总数
  74. func GetRoleAdminListTotal(condition string, pars []interface{}) (total int64, err error) {
  75. sql := `SELECT
  76. count(*)
  77. FROM
  78. sys_role_admin s
  79. INNER JOIN admin a ON s.admin_id = a.admin_id
  80. where 1=1`
  81. if condition != "" {
  82. sql += condition
  83. }
  84. err = orm.NewOrm().Raw(sql, pars).QueryRow(&total)
  85. return
  86. }
  87. // CheckRoleAdminByAdminIds 判断是否已经绑定过该角色
  88. func CheckRoleAdminByAdminIds(adminIds string, roleId int) (list []*SysRoleAdmin, err error) {
  89. sql := `SELECT
  90. s.*
  91. FROM
  92. sys_role_admin s
  93. LEFT JOIN admin a ON s.admin_id = a.admin_id
  94. where s.role_id=? and s.admin_id in (` + adminIds + `)`
  95. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&list)
  96. return
  97. }
  98. // AddRoleAdminBatch 批量新增留言版管理员
  99. func AddRoleAdminBatch(admins []*SysRoleAdmin) (err error) {
  100. o := orm.NewOrm()
  101. _, err = o.InsertMulti(len(admins), admins)
  102. return
  103. }
  104. // DeleteRoleAdmin 删除管理员和角色的绑定
  105. func DeleteRoleAdmin(SysRoleAdminId int) (err error) {
  106. sql := `DELETE FROM sys_role_admin WHERE id=? `
  107. o := orm.NewOrm()
  108. _, err = o.Raw(sql, SysRoleAdminId).Exec()
  109. return
  110. }
  111. // GetRoleAdminById 根据ID查询绑定记录
  112. func GetRoleAdminById(SysRoleAdminId int) (item *SysRoleAdmin, err error) {
  113. sql := `SELECT
  114. *
  115. FROM
  116. sys_role_admin
  117. where id=?`
  118. err = orm.NewOrm().Raw(sql, SysRoleAdminId).QueryRow(&item)
  119. return
  120. }
  121. // GetEnglishAuthRoleAdminList 查询英文权限管理员列表
  122. func GetEnglishAuthRoleAdminList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*RoleAdminItem, err error) {
  123. o := orm.NewOrm()
  124. sql := `SELECT
  125. s.id as sys_role_admin_id,
  126. a.admin_id,
  127. a.admin_name,
  128. a.real_name,
  129. a.mobile,
  130. a.department_id,
  131. a.department_name,
  132. a.enabled,
  133. a.role_id,
  134. a.role_type_code ,
  135. a.group_id,
  136. a.authority,
  137. a.group_name
  138. FROM
  139. sys_role_admin AS s
  140. JOIN sys_role AS r ON s.role_id = r.role_id
  141. JOIN admin AS a ON s.admin_id = a.admin_id
  142. WHERE
  143. 1 = 1 `
  144. sql += condition
  145. sql += ` GROUP BY s.admin_id `
  146. totalSQl := `SELECT COUNT(1) total FROM (` + sql + `) z`
  147. if err = o.Raw(totalSQl, pars).QueryRow(&total); err != nil {
  148. return
  149. }
  150. sql += ` ORDER BY s.create_time DESC,s.admin_id ASC LIMIT ?,? `
  151. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  152. return
  153. }
  154. // DeleteRoleAdminByAdminIdAndRoleIds 通过管理员ID及角色IDs删除绑定
  155. func DeleteRoleAdminByAdminIdAndRoleIds(adminId int, roleIds []int) (err error) {
  156. if len(roleIds) == 0 {
  157. return
  158. }
  159. o := orm.NewOrm()
  160. sql := `DELETE FROM sys_role_admin WHERE admin_id = ? AND role_id IN (` + utils.GetOrmInReplace(len(roleIds)) + `) `
  161. _, err = o.Raw(sql, adminId, roleIds).Exec()
  162. return
  163. }
  164. // CheckEnglishAuthRoleAdminByAdminIds 通过管理员IDs判断是否已经绑定过英文权限角色
  165. func CheckEnglishAuthRoleAdminByAdminIds(adminIds []int, roleCodes []string) (list []*SysRoleAdmin, err error) {
  166. if len(adminIds) == 0 || len(roleCodes) == 0 {
  167. return
  168. }
  169. o := orm.NewOrm()
  170. sql := `SELECT
  171. s.*
  172. FROM
  173. sys_role_admin AS s
  174. JOIN sys_role AS r ON s.role_id = r.role_id
  175. JOIN admin AS a ON s.admin_id = a.admin_id
  176. WHERE
  177. s.admin_id IN (` + utils.GetOrmInReplace(len(adminIds)) + `) AND r.role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)
  178. GROUP BY
  179. s.admin_id`
  180. _, err = o.Raw(sql, adminIds, roleCodes).QueryRows(&list)
  181. return
  182. }
  183. // EnglishAuthRoleDetail 英文权限角色详情
  184. type EnglishAuthRoleDetail struct {
  185. AdminId int `description:"用户ID"`
  186. RoleTypeCode string `description:"角色类型编码"`
  187. }
  188. // GetAdminEnglishAuthRoleDetail 获取用户英文权限详情
  189. func GetAdminEnglishAuthRoleDetail(adminId int, roleCodes []string) (list []*EnglishAuthRoleDetail, err error) {
  190. if len(roleCodes) == 0 {
  191. return
  192. }
  193. o := orm.NewOrm()
  194. sql := `SELECT
  195. s.admin_id, r.role_type_code
  196. FROM
  197. sys_role_admin AS s
  198. JOIN sys_role AS r ON s.role_id = r.role_id
  199. JOIN admin AS a ON s.admin_id = a.admin_id
  200. WHERE
  201. s.admin_id = ? AND r.role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  202. _, err = o.Raw(sql, adminId, roleCodes).QueryRows(&list)
  203. return
  204. }
  205. // EditEnglishAuthRoleAdmin 编辑管理员英文权限
  206. func EditEnglishAuthRoleAdmin(adminId int, englishRoleIds []int, roleAdmins []*SysRoleAdmin) (err error) {
  207. o := orm.NewOrm()
  208. tx, e := o.Begin()
  209. if e != nil {
  210. return
  211. }
  212. defer func() {
  213. if err != nil {
  214. _ = tx.Rollback()
  215. } else {
  216. _ = tx.Commit()
  217. }
  218. }()
  219. // 删除原绑定关系
  220. sql := `DELETE FROM sys_role_admin WHERE admin_id = ? AND role_id IN (` + utils.GetOrmInReplace(len(englishRoleIds)) + `)`
  221. _, err = tx.Raw(sql, adminId, englishRoleIds).Exec()
  222. if err != nil {
  223. return
  224. }
  225. // 新增绑定关系
  226. _, err = tx.InsertMulti(len(roleAdmins), roleAdmins)
  227. return
  228. }
  229. // SyncAdminEnRole 同步用户英文角色
  230. type SyncAdminEnRole struct {
  231. AdminName string `description:"用户名"`
  232. RoleCodes []string `description:"角色Codes"`
  233. }