sys_role.go 7.7 KB

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