sys_role.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package system
  2. import (
  3. "eta/eta_forum_admin/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. )
  8. type SysRoleAddReq struct {
  9. RoleName string `description:"角色名称"`
  10. RoleType string `description:"角色类型"`
  11. }
  12. type SysRole struct {
  13. RoleId int `orm:"column(role_id);pk" description:"角色ID"`
  14. RoleName string `description:"角色名称"`
  15. RoleType string `description:"角色类型"`
  16. RoleTypeCode string `description:"角色类型编码"`
  17. CreateTime time.Time `description:"创建时间"`
  18. ModifyTime time.Time
  19. }
  20. type SysRoleItem struct {
  21. RoleId int `orm:"column(role_id);pk" description:"角色ID"`
  22. RoleName string `description:"角色名称"`
  23. RoleType string `description:"角色类型"`
  24. RoleTypeCode string `description:"角色类型编码"`
  25. CreateTime time.Time `description:"创建时间"`
  26. RoleLevel int `description:"角色等级:0-表示一级角色,每个账号只能绑定一个一级角色 ,1-表示二级角色,每个账号可以绑定多个二级 角色"`
  27. }
  28. func GetSysRoleCount(roleName string) (count int, err error) {
  29. sql := `SELECT COUNT(1) AS count FROM sys_role WHERE role_name=? `
  30. o := orm.NewOrm()
  31. err = o.Raw(sql, roleName).QueryRow(&count)
  32. return
  33. }
  34. func AddSysRole(item *SysRole) (lastId int64, err error) {
  35. o := orm.NewOrm()
  36. lastId, err = o.Insert(item)
  37. return
  38. }
  39. type SysRoleEditReq struct {
  40. RoleId int `description:"角色ID"`
  41. RoleName string `description:"角色名称"`
  42. RoleType string `description:"角色类型"`
  43. }
  44. func GetSysRoleById(roleId int) (item *SysRoleItem, err error) {
  45. sql := `SELECT * FROM sys_role WHERE role_id=? `
  46. o := orm.NewOrm()
  47. err = o.Raw(sql, roleId).QueryRow(&item)
  48. return
  49. }
  50. func GetSysRoleByName(roleName string) (item *SysRoleItem, err error) {
  51. sql := `SELECT * FROM sys_role WHERE role_name=? `
  52. o := orm.NewOrm()
  53. err = o.Raw(sql, roleName).QueryRow(&item)
  54. return
  55. }
  56. // GetSysRoleByRoleTypeCode role_type_code
  57. func GetSysRoleByRoleTypeCode(roleTypeCode string) (item *SysRoleItem, err error) {
  58. sql := `SELECT * FROM sys_role WHERE role_type_code=? `
  59. o := orm.NewOrm()
  60. err = o.Raw(sql, roleTypeCode).QueryRow(&item)
  61. return
  62. }
  63. func ModifySysRole(roleName, roleType, roleTypeCode string, groupId int) (err error) {
  64. sql := `UPDATE sys_role SET role_name=?,role_type=?,role_type_code=?,modify_time=NOW() WHERE role_id=? `
  65. o := orm.NewOrm()
  66. _, err = o.Raw(sql, roleName, roleType, roleTypeCode, groupId).Exec()
  67. return
  68. }
  69. type SysRoleDeleteReq struct {
  70. RoleId int `description:"角色ID"`
  71. }
  72. func DeleteSysRole(roleId int) (err error) {
  73. sql := `DELETE FROM sys_role WHERE role_id=? `
  74. o := orm.NewOrm()
  75. _, err = o.Raw(sql, roleId).Exec()
  76. return
  77. }
  78. func GetSysRoleListCount(condition string, pars []interface{}) (count int, err error) {
  79. o := orm.NewOrm()
  80. sql := `SELECT COUNT(1) AS count FROM sys_role WHERE 1=1 `
  81. if condition != "" {
  82. sql += condition
  83. }
  84. err = o.Raw(sql, pars).QueryRow(&count)
  85. return
  86. }
  87. func GetSysRoleList(condition string, pars []interface{}, startSize, pageSize int) (items []*SysRoleItem, err error) {
  88. o := orm.NewOrm()
  89. sql := `SELECT * FROM sys_role WHERE 1=1 `
  90. if condition != "" {
  91. sql += condition
  92. }
  93. sql += `ORDER BY modify_time DESC LIMIT ?,?`
  94. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  95. return
  96. }
  97. type SysRoleListResp struct {
  98. List []*SysRoleItem
  99. Paging *paging.PagingItem `description:"分页数据"`
  100. }
  101. type RoleMenu struct {
  102. MenuId int `description:"导航唯一标识"`
  103. Name string `description:"导航名称"`
  104. NameEn string `description:"导航名称(英文)"`
  105. }
  106. type RoleMenuList struct {
  107. MenuId int `description:"导航唯一标识"`
  108. Name string `description:"导航名称"`
  109. NameEn string `description:"导航名称(英文)"`
  110. Child []*RoleMenu
  111. CheckList []int
  112. }
  113. type RoleMenuListResp struct {
  114. List []*RoleMenuList
  115. }
  116. func GetRoleMenuList() (items []*RoleMenuList, err error) {
  117. sql := `SELECT * FROM sys_menu WHERE parent_id=0 ORDER BY sort ASC `
  118. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  119. return
  120. }
  121. func GetRoleMenuByParentId(parentId int) (items []*RoleMenu, err error) {
  122. sql := `SELECT * FROM sys_menu WHERE parent_id=? ORDER BY sort ASC `
  123. _, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
  124. return
  125. }
  126. func GetCheckListRoleMenu(roleId, parentId int) (items []*RoleMenuList, err error) {
  127. sql := ` SELECT b.menu_id FROM sys_role_menu AS a
  128. INNER JOIN sys_menu AS b ON a.menu_id=b.menu_id
  129. WHERE a.role_id=?
  130. AND b.parent_id=? `
  131. _, err = orm.NewOrm().Raw(sql, roleId, parentId).QueryRows(&items)
  132. return
  133. }
  134. type RoleMenusAddReq struct {
  135. RoleId int `description:"角色ID"`
  136. MenuIdStr string `description:"MenuId,多个用英文逗号隔开"`
  137. MenuButtonIdStr string `description:"menu_button_id,多个用英文逗号隔开"`
  138. }
  139. func DeleteRoleMenuByRoleId(roleId int) (err error) {
  140. sql := `DELETE FROM sys_role_menu WHERE role_id=? `
  141. _, err = orm.NewOrm().Raw(sql, roleId).Exec()
  142. return
  143. }
  144. type SysRoleMenu struct {
  145. RoleMenuId int `orm:"column(role_menu_id);pk" description:"关联id"`
  146. RoleId int
  147. MenuId int
  148. }
  149. func AddSysRoleMenu(item *SysRoleMenu) (lastId int64, err error) {
  150. o := orm.NewOrm()
  151. lastId, err = o.Insert(item)
  152. return
  153. }
  154. // CreateMultiSysRoleMenu 删除并新增角色权限
  155. func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenu) (err error) {
  156. if roleId == 0 {
  157. return
  158. }
  159. o := orm.NewOrm()
  160. tx, err := o.Begin()
  161. if err != nil {
  162. return
  163. }
  164. defer func() {
  165. if err != nil {
  166. _ = tx.Rollback()
  167. return
  168. }
  169. _ = tx.Commit()
  170. }()
  171. sql := `DELETE FROM sys_role_menu WHERE role_id = ?`
  172. _, err = tx.Raw(sql, roleId).Exec()
  173. if err != nil {
  174. return
  175. }
  176. if len(items) > 0 {
  177. _, err = tx.InsertMulti(len(items), items)
  178. }
  179. return
  180. }
  181. func GetSysRoleAll(levelFlag bool) (items []*SysRoleItem, err error) {
  182. o := orm.NewOrm()
  183. sql := `SELECT * FROM sys_role WHERE 1=1`
  184. if levelFlag {
  185. sql += ` and role_level = 0`
  186. }
  187. sql += ` ORDER BY create_time DESC `
  188. _, err = o.Raw(sql).QueryRows(&items)
  189. return
  190. }
  191. type SysRoleAllResp struct {
  192. List []*SysRoleItem
  193. }
  194. // GetEnglishAuthRoleIds 获取英文权限角色IDs
  195. func GetEnglishAuthRoleIds(roleCodes []string) (roleIds []int, err error) {
  196. if len(roleCodes) == 0 {
  197. return
  198. }
  199. o := orm.NewOrm()
  200. sql := `SELECT role_id FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  201. _, err = o.Raw(sql, roleCodes).QueryRows(&roleIds)
  202. return
  203. }
  204. // GetSysRoleMenuByRoleId 获取角色关联菜单
  205. func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenu, err error) {
  206. sql := `SELECT
  207. *
  208. FROM
  209. sys_role_menu
  210. WHERE
  211. role_id = ?`
  212. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  213. return
  214. }
  215. // GetEnglishAuthRoleItems 获取英文权限角色
  216. func GetEnglishAuthRoleItems(roleCodes []string) (items []*SysRole, err error) {
  217. if len(roleCodes) == 0 {
  218. return
  219. }
  220. o := orm.NewOrm()
  221. sql := `SELECT * FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  222. _, err = o.Raw(sql, roleCodes).QueryRows(&items)
  223. return
  224. }
  225. func GetSysRoleByIdList(id []int) (items []*SysRole, err error) {
  226. if len(id) == 0 {
  227. return
  228. }
  229. o := orm.NewOrm()
  230. sql := `SELECT * FROM sys_role WHERE role_id IN (` + utils.GetOrmInReplace(len(id)) + `)`
  231. _, err = o.Raw(sql, id).QueryRows(&items)
  232. return
  233. }