sys_role.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package sys_role
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "time"
  6. )
  7. type SysRoleAddReq struct {
  8. RoleName string `description:"角色名称"`
  9. RoleType string `description:"角色类型"`
  10. }
  11. type SysRole struct {
  12. RoleId int `orm:"column(role_id);pk" description:"角色ID"`
  13. RoleName string `description:"角色名称"`
  14. RoleType string `description:"角色类型"`
  15. RoleTypeCode string `description:"角色类型编码"`
  16. CreateTime time.Time `description:"创建时间"`
  17. ModifyTime time.Time
  18. }
  19. type SysRoleItem struct {
  20. RoleId int `orm:"column(role_id);pk" description:"角色ID"`
  21. RoleName string `description:"角色名称"`
  22. RoleType string `description:"角色类型"`
  23. RoleTypeCode string `description:"角色类型编码"`
  24. CreateTime time.Time `description:"创建时间"`
  25. RoleLevel int `description:"角色等级:0-表示一级角色,每个账号只能绑定一个一级角色 ,1-表示二级角色,每个账号可以绑定多个二级 角色"`
  26. }
  27. func GetSysRoleCount(roleName string) (count int, err error) {
  28. sql := `SELECT COUNT(1) AS count FROM sys_role WHERE role_name=? `
  29. o := orm.NewOrm()
  30. err = o.Raw(sql, roleName).QueryRow(&count)
  31. return
  32. }
  33. func AddSysRole(item *SysRole) (lastId int64, err error) {
  34. o := orm.NewOrm()
  35. lastId, err = o.Insert(item)
  36. return
  37. }
  38. type SysRoleEditReq struct {
  39. RoleId int `description:"角色ID"`
  40. RoleName string `description:"角色名称"`
  41. RoleType string `description:"角色类型"`
  42. }
  43. func GetSysRoleById(roleId int) (item *SysRoleItem, err error) {
  44. sql := `SELECT * FROM sys_role WHERE role_id=? `
  45. o := orm.NewOrm()
  46. err = o.Raw(sql, roleId).QueryRow(&item)
  47. return
  48. }
  49. func GetSysRoleByName(roleName string) (item *SysRoleItem, err error) {
  50. sql := `SELECT * FROM sys_role WHERE role_name=? `
  51. o := orm.NewOrm()
  52. err = o.Raw(sql, roleName).QueryRow(&item)
  53. return
  54. }
  55. // GetSysRoleByRoleTypeCode role_type_code
  56. func GetSysRoleByRoleTypeCode(roleTypeCode string) (item *SysRoleItem, err error) {
  57. sql := `SELECT * FROM sys_role WHERE role_type_code=? `
  58. o := orm.NewOrm()
  59. err = o.Raw(sql, roleTypeCode).QueryRow(&item)
  60. return
  61. }
  62. func ModifySysRole(roleName, roleType, roleTypeCode string, groupId int) (err error) {
  63. sql := `UPDATE sys_role SET role_name=?,role_type=?,role_type_code=?,modify_time=NOW() WHERE role_id=? `
  64. o := orm.NewOrm()
  65. _, err = o.Raw(sql, roleName, roleType, roleTypeCode, groupId).Exec()
  66. return
  67. }
  68. type SysRoleDeleteReq struct {
  69. RoleId int `description:"角色ID"`
  70. }
  71. func DeleteSysRole(roleId int) (err error) {
  72. sql := `DELETE FROM sys_role WHERE role_id=? `
  73. o := orm.NewOrm()
  74. _, err = o.Raw(sql, roleId).Exec()
  75. return
  76. }
  77. func GetSysRoleListCount(condition string, pars []interface{}) (count int, err error) {
  78. o := orm.NewOrm()
  79. sql := `SELECT COUNT(1) AS count FROM sys_role WHERE 1=1 `
  80. if condition != "" {
  81. sql += condition
  82. }
  83. err = o.Raw(sql, pars).QueryRow(&count)
  84. return
  85. }
  86. func GetSysRoleList(condition string, pars []interface{}, startSize, pageSize int) (items []*SysRoleItem, err error) {
  87. o := orm.NewOrm()
  88. sql := `SELECT * FROM sys_role WHERE 1=1 `
  89. if condition != "" {
  90. sql += condition
  91. }
  92. sql += `ORDER BY modify_time DESC LIMIT ?,?`
  93. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  94. return
  95. }
  96. type SysRoleListResp struct {
  97. List []*SysRoleItem
  98. Paging *paging.PagingItem `description:"分页数据"`
  99. }
  100. type RoleMenu struct {
  101. MenuId int `description:"导航唯一标识"`
  102. Name string `description:"导航名称"`
  103. }
  104. type RoleMenuList struct {
  105. MenuId int `description:"导航唯一标识"`
  106. Name string `description:"导航名称"`
  107. Child []*RoleMenu
  108. CheckList []int
  109. }
  110. type RoleMenuListResp struct {
  111. List []*RoleMenuList
  112. }
  113. func GetRoleMenuList() (items []*RoleMenuList, err error) {
  114. sql := `SELECT * FROM sys_menu WHERE parent_id=0 ORDER BY sort ASC `
  115. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  116. return
  117. }
  118. func GetRoleMenuByParentId(parentId int) (items []*RoleMenu, err error) {
  119. sql := `SELECT * FROM sys_menu WHERE parent_id=? ORDER BY sort ASC `
  120. _, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
  121. return
  122. }
  123. func GetCheckListRoleMenu(roleId, parentId int) (items []*RoleMenuList, err error) {
  124. sql := ` SELECT b.menu_id FROM sys_role_menu AS a
  125. INNER JOIN sys_menu AS b ON a.menu_id=b.menu_id
  126. WHERE a.role_id=?
  127. AND b.parent_id=? `
  128. _, err = orm.NewOrm().Raw(sql, roleId, parentId).QueryRows(&items)
  129. return
  130. }
  131. type RoleMenusAddReq struct {
  132. RoleId int `description:"角色ID"`
  133. MenuIdStr string `description:"MenuId,多个用英文逗号隔开"`
  134. MenuButtonIdStr string `description:"menu_button_id,多个用英文逗号隔开"`
  135. }
  136. func DeleteRoleMenuByRoleId(roleId int) (err error) {
  137. sql := `DELETE FROM sys_role_menu WHERE role_id=? `
  138. _, err = orm.NewOrm().Raw(sql, roleId).Exec()
  139. return
  140. }
  141. type SysRoleMenu struct {
  142. RoleMenuId int `orm:"column(role_menu_id);pk" description:"关联id"`
  143. RoleId int
  144. MenuId int
  145. }
  146. func AddSysRoleMenu(item *SysRoleMenu) (lastId int64, err error) {
  147. o := orm.NewOrm()
  148. lastId, err = o.Insert(item)
  149. return
  150. }
  151. func GetSysRoleAll(levelFlag bool) (items []*SysRoleItem, err error) {
  152. o := orm.NewOrm()
  153. sql := `SELECT * FROM sys_role WHERE 1=1`
  154. if levelFlag {
  155. sql += ` and role_level = 0`
  156. }
  157. sql += ` ORDER BY create_time DESC `
  158. _, err = o.Raw(sql).QueryRows(&items)
  159. return
  160. }
  161. type SysRoleAllResp struct {
  162. List []*SysRoleItem
  163. }