sys_role.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package system
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  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 `gorm:"primaryKey"`
  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. err = global.DEFAULT_DmSQL.Raw(sql, roleName).First(&count).Error
  31. return
  32. }
  33. func AddSysRole(item *SysRole) (lastId int64, err error) {
  34. err = global.DEFAULT_DmSQL.Create(item).Error
  35. return
  36. }
  37. type SysRoleEditReq struct {
  38. RoleId int `description:"角色ID"`
  39. RoleName string `description:"角色名称"`
  40. RoleType string `description:"角色类型"`
  41. }
  42. func GetSysRoleById(roleId int) (item *SysRoleItem, err error) {
  43. sql := `SELECT * FROM sys_role WHERE role_id=? `
  44. err = global.DEFAULT_DmSQL.Raw(sql, roleId).First(&item).Error
  45. return
  46. }
  47. func GetSysRoleByName(roleName string) (item *SysRoleItem, err error) {
  48. sql := `SELECT * FROM sys_role WHERE role_name=? `
  49. err = global.DEFAULT_DmSQL.Raw(sql, roleName).First(&item).Error
  50. return
  51. }
  52. // GetSysRoleByRoleTypeCode role_type_code
  53. func GetSysRoleByRoleTypeCode(roleTypeCode string) (item *SysRoleItem, err error) {
  54. sql := `SELECT * FROM sys_role WHERE role_type_code=? `
  55. err = global.DEFAULT_DmSQL.Raw(sql, roleTypeCode).First(&item).Error
  56. return
  57. }
  58. func ModifySysRole(roleName, roleType, roleTypeCode string, groupId int) (err error) {
  59. sql := `UPDATE sys_role SET role_name=?,role_type=?,role_type_code=?,modify_time=NOW() WHERE role_id=? `
  60. err = global.DEFAULT_DmSQL.Exec(sql, roleName, roleType, roleTypeCode, groupId).Error
  61. return
  62. }
  63. type SysRoleDeleteReq struct {
  64. RoleId int `description:"角色ID"`
  65. }
  66. func DeleteSysRole(roleId int) (err error) {
  67. sql := `DELETE FROM sys_role WHERE role_id=? `
  68. err = global.DEFAULT_DmSQL.Exec(sql, roleId).Error
  69. return
  70. }
  71. func GetSysRoleListCount(condition string, pars []interface{}) (count int, err error) {
  72. sql := `SELECT COUNT(1) AS count FROM sys_role WHERE 1=1 `
  73. if condition != "" {
  74. sql += condition
  75. }
  76. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  77. return
  78. }
  79. func GetSysRoleList(condition string, pars []interface{}, startSize, pageSize int) (items []*SysRoleItem, err error) {
  80. sql := `SELECT * FROM sys_role WHERE 1=1 `
  81. if condition != "" {
  82. sql += condition
  83. }
  84. sql += `ORDER BY modify_time DESC LIMIT ?,?`
  85. pars = append(pars, startSize)
  86. pars = append(pars, pageSize)
  87. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  88. return
  89. }
  90. type SysRoleListResp struct {
  91. List []*SysRoleItem
  92. Paging *paging.PagingItem `description:"分页数据"`
  93. }
  94. type RoleMenu struct {
  95. MenuId int `description:"导航唯一标识"`
  96. Name string `description:"导航名称"`
  97. NameEn string `description:"导航名称(英文)"`
  98. }
  99. type RoleMenuList struct {
  100. MenuId int `description:"导航唯一标识"`
  101. Name string `description:"导航名称"`
  102. NameEn string `description:"导航名称(英文)"`
  103. Child []*RoleMenu `gorm:"-"`
  104. CheckList []int
  105. }
  106. type RoleMenuListResp struct {
  107. List []*RoleMenuList
  108. }
  109. func GetRoleMenuList() (items []*RoleMenuList, err error) {
  110. sql := `SELECT * FROM sys_menu WHERE parent_id=0 ORDER BY sort ASC `
  111. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  112. return
  113. }
  114. func GetRoleMenuByParentId(parentId int) (items []*RoleMenu, err error) {
  115. sql := `SELECT * FROM sys_menu WHERE parent_id=? ORDER BY sort ASC `
  116. err = global.DEFAULT_DmSQL.Raw(sql, parentId).Find(&items).Error
  117. return
  118. }
  119. func GetCheckListRoleMenu(roleId, parentId int) (items []*RoleMenuList, err error) {
  120. sql := ` SELECT b.menu_id FROM sys_role_menu AS a
  121. INNER JOIN sys_menu AS b ON a.menu_id=b.menu_id
  122. WHERE a.role_id=? AND a.type = 0
  123. AND b.parent_id=? `
  124. err = global.DEFAULT_DmSQL.Raw(sql, roleId, parentId).Find(&items).Error
  125. return
  126. }
  127. type RoleMenusAddReq struct {
  128. RoleId int `description:"角色ID"`
  129. MenuIdStr string `description:"MenuId,多个用英文逗号隔开"`
  130. MenuButtonIdStr string `description:"menu_button_id,多个用英文逗号隔开"`
  131. }
  132. func DeleteRoleMenuByRoleId(roleId int) (err error) {
  133. sql := `DELETE FROM sys_role_menu WHERE role_id=? `
  134. err = global.DEFAULT_DmSQL.Exec(sql, roleId).Error
  135. return
  136. }
  137. type SysRoleMenu struct {
  138. RoleMenuId int `gorm:"primaryKey"`
  139. RoleId int
  140. MenuId int
  141. Type int `description:"类型:0-全选;1-半选(此字段仅用于前端的回显)"`
  142. }
  143. func AddSysRoleMenu(item *SysRoleMenu) (lastId int64, err error) {
  144. err = global.DEFAULT_DmSQL.Create(item).Error
  145. return
  146. }
  147. // CreateMultiSysRoleMenu 删除并新增角色权限
  148. func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenu) (err error) {
  149. if roleId == 0 {
  150. return
  151. }
  152. tx := global.DEFAULT_DmSQL.Begin()
  153. defer func() {
  154. if err != nil {
  155. _ = tx.Rollback()
  156. return
  157. }
  158. _ = tx.Commit()
  159. }()
  160. sql := `DELETE FROM sys_role_menu WHERE role_id = ?`
  161. err = tx.Exec(sql, roleId).Error
  162. if err != nil {
  163. return
  164. }
  165. if len(items) > 0 {
  166. err = tx.CreateInBatches(items, utils.MultiAddNum).Error
  167. }
  168. return
  169. }
  170. func GetSysRoleAll(levelFlag bool) (items []*SysRoleItem, err error) {
  171. sql := `SELECT * FROM sys_role WHERE 1=1`
  172. if levelFlag {
  173. sql += ` and role_level = 0`
  174. }
  175. sql += ` ORDER BY create_time DESC `
  176. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  177. return
  178. }
  179. type SysRoleAllResp struct {
  180. List []*SysRoleItem
  181. }
  182. // GetEnglishAuthRoleIds 获取英文权限角色IDs
  183. func GetEnglishAuthRoleIds(roleCodes []string) (roleIds []int, err error) {
  184. if len(roleCodes) == 0 {
  185. return
  186. }
  187. sql := `SELECT role_id FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  188. err = global.DEFAULT_DmSQL.Raw(sql, roleCodes).Scan(&roleIds).Error
  189. return
  190. }
  191. // GetSysRoleMenuByRoleId 获取角色关联菜单
  192. func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenu, err error) {
  193. sql := `SELECT
  194. *
  195. FROM
  196. sys_role_menu
  197. WHERE
  198. role_id = ?`
  199. err = global.DEFAULT_DmSQL.Raw(sql, roleId).Find(&items).Error
  200. return
  201. }
  202. // GetEnglishAuthRoleItems 获取英文权限角色
  203. func GetEnglishAuthRoleItems(roleCodes []string) (items []*SysRole, err error) {
  204. if len(roleCodes) == 0 {
  205. return
  206. }
  207. sql := `SELECT * FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  208. err = global.DEFAULT_DmSQL.Raw(sql, roleCodes).Find(&items).Error
  209. return
  210. }