sys_role.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package system
  2. import (
  3. "eta/eta_api/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "time"
  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. }
  105. type RoleMenuList struct {
  106. MenuId int `description:"导航唯一标识"`
  107. Name string `description:"导航名称"`
  108. Child []*RoleMenu
  109. CheckList []int
  110. }
  111. type RoleMenuListResp struct {
  112. List []*RoleMenuList
  113. }
  114. func GetRoleMenuList() (items []*RoleMenuList, err error) {
  115. sql := `SELECT * FROM sys_menu WHERE parent_id=0 ORDER BY sort ASC `
  116. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  117. return
  118. }
  119. func GetRoleMenuByParentId(parentId int) (items []*RoleMenu, err error) {
  120. sql := `SELECT * FROM sys_menu WHERE parent_id=? ORDER BY sort ASC `
  121. _, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
  122. return
  123. }
  124. func GetCheckListRoleMenu(roleId, parentId int) (items []*RoleMenuList, err error) {
  125. sql := ` SELECT b.menu_id FROM sys_role_menu AS a
  126. INNER JOIN sys_menu AS b ON a.menu_id=b.menu_id
  127. WHERE a.role_id=? AND a.type = 0
  128. AND b.parent_id=? `
  129. _, err = orm.NewOrm().Raw(sql, roleId, parentId).QueryRows(&items)
  130. return
  131. }
  132. type RoleMenusAddReq struct {
  133. RoleId int `description:"角色ID"`
  134. MenuIdStr string `description:"MenuId,多个用英文逗号隔开"`
  135. MenuButtonIdStr string `description:"menu_button_id,多个用英文逗号隔开"`
  136. }
  137. func DeleteRoleMenuByRoleId(roleId int) (err error) {
  138. sql := `DELETE FROM sys_role_menu WHERE role_id=? `
  139. _, err = orm.NewOrm().Raw(sql, roleId).Exec()
  140. return
  141. }
  142. type SysRoleMenu struct {
  143. RoleMenuId int `orm:"column(role_menu_id);pk" description:"关联id"`
  144. RoleId int
  145. MenuId int
  146. Type int `description:"类型:0-全选;1-半选(此字段仅用于前端的回显)"`
  147. }
  148. func AddSysRoleMenu(item *SysRoleMenu) (lastId int64, err error) {
  149. o := orm.NewOrm()
  150. lastId, err = o.Insert(item)
  151. return
  152. }
  153. // CreateMultiSysRoleMenu 删除并新增角色权限
  154. func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenu) (err error) {
  155. if roleId == 0 {
  156. return
  157. }
  158. o := orm.NewOrm()
  159. tx, err := o.Begin()
  160. if err != nil {
  161. return
  162. }
  163. defer func() {
  164. if err != nil {
  165. _ = tx.Rollback()
  166. return
  167. }
  168. _ = tx.Commit()
  169. }()
  170. sql := `DELETE FROM sys_role_menu WHERE role_id = ?`
  171. _, err = tx.Raw(sql, roleId).Exec()
  172. if err != nil {
  173. return
  174. }
  175. if len(items) > 0 {
  176. _, err = tx.InsertMulti(len(items), items)
  177. }
  178. return
  179. }
  180. func GetSysRoleAll(levelFlag bool) (items []*SysRoleItem, err error) {
  181. o := orm.NewOrm()
  182. sql := `SELECT * FROM sys_role WHERE 1=1`
  183. if levelFlag {
  184. sql += ` and role_level = 0`
  185. }
  186. sql += ` ORDER BY create_time DESC `
  187. _, err = o.Raw(sql).QueryRows(&items)
  188. return
  189. }
  190. type SysRoleAllResp struct {
  191. List []*SysRoleItem
  192. }
  193. // GetEnglishAuthRoleIds 获取英文权限角色IDs
  194. func GetEnglishAuthRoleIds(roleCodes []string) (roleIds []int, err error) {
  195. if len(roleCodes) == 0 {
  196. return
  197. }
  198. o := orm.NewOrm()
  199. sql := `SELECT role_id FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  200. _, err = o.Raw(sql, roleCodes).QueryRows(&roleIds)
  201. return
  202. }
  203. // GetSysRoleMenuByRoleId 获取角色关联菜单
  204. func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenu, err error) {
  205. sql := `SELECT
  206. *
  207. FROM
  208. sys_role_menu
  209. WHERE
  210. role_id = ?`
  211. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  212. return
  213. }
  214. // GetEnglishAuthRoleItems 获取英文权限角色
  215. func GetEnglishAuthRoleItems(roleCodes []string) (items []*SysRole, err error) {
  216. if len(roleCodes) == 0 {
  217. return
  218. }
  219. o := orm.NewOrm()
  220. sql := `SELECT * FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  221. _, err = o.Raw(sql, roleCodes).QueryRows(&items)
  222. return
  223. }