sys_role.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. 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=? AND a.type = 0
  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. Type int `description:"类型:0-全选;1-半选(此字段仅用于前端的回显)"`
  149. }
  150. func AddSysRoleMenu(item *SysRoleMenu) (lastId int64, err error) {
  151. o := orm.NewOrm()
  152. lastId, err = o.Insert(item)
  153. return
  154. }
  155. // CreateMultiSysRoleMenu 删除并新增角色权限
  156. func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenu) (err error) {
  157. if roleId == 0 {
  158. return
  159. }
  160. o := orm.NewOrm()
  161. tx, err := o.Begin()
  162. if err != nil {
  163. return
  164. }
  165. defer func() {
  166. if err != nil {
  167. _ = tx.Rollback()
  168. return
  169. }
  170. _ = tx.Commit()
  171. }()
  172. sql := `DELETE FROM sys_role_menu WHERE role_id = ?`
  173. _, err = tx.Raw(sql, roleId).Exec()
  174. if err != nil {
  175. return
  176. }
  177. if len(items) > 0 {
  178. _, err = tx.InsertMulti(len(items), items)
  179. }
  180. return
  181. }
  182. func GetSysRoleAll(levelFlag bool) (items []*SysRoleItem, err error) {
  183. o := orm.NewOrm()
  184. sql := `SELECT * FROM sys_role WHERE 1=1`
  185. if levelFlag {
  186. sql += ` and role_level = 0`
  187. }
  188. sql += ` ORDER BY create_time DESC `
  189. _, err = o.Raw(sql).QueryRows(&items)
  190. return
  191. }
  192. type SysRoleAllResp struct {
  193. List []*SysRoleItem
  194. }
  195. // GetEnglishAuthRoleIds 获取英文权限角色IDs
  196. func GetEnglishAuthRoleIds(roleCodes []string) (roleIds []int, err error) {
  197. if len(roleCodes) == 0 {
  198. return
  199. }
  200. o := orm.NewOrm()
  201. sql := `SELECT role_id FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  202. _, err = o.Raw(sql, roleCodes).QueryRows(&roleIds)
  203. return
  204. }
  205. // GetSysRoleMenuByRoleId 获取角色关联菜单
  206. func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenu, err error) {
  207. sql := `SELECT
  208. *
  209. FROM
  210. sys_role_menu
  211. WHERE
  212. role_id = ?`
  213. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  214. return
  215. }
  216. // GetEnglishAuthRoleItems 获取英文权限角色
  217. func GetEnglishAuthRoleItems(roleCodes []string) (items []*SysRole, err error) {
  218. if len(roleCodes) == 0 {
  219. return
  220. }
  221. o := orm.NewOrm()
  222. sql := `SELECT * FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  223. _, err = o.Raw(sql, roleCodes).QueryRows(&items)
  224. return
  225. }