sys_role.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package system
  2. import (
  3. "errors"
  4. "hongze/hrms_api/models/system"
  5. "hongze/hrms_api/utils"
  6. "time"
  7. )
  8. func AddRole(req *system.SysRoleAddReq) (err error, errMsg string) {
  9. role := new(system.SysRole)
  10. // 判断角色名称是否已存在
  11. _, err = role.GetRoleByRoleName(req.RoleName)
  12. if err == nil {
  13. errMsg = "角色名称已存在, 请重新输入"
  14. err = errors.New(errMsg)
  15. return
  16. }else{
  17. if err != utils.ErrNoRow {
  18. errMsg = "查询角色名称出错"
  19. err = errors.New(errMsg+"Err:" + err.Error())
  20. return
  21. }
  22. err = nil
  23. }
  24. // 添加角色
  25. role.RoleName = req.RoleName
  26. err = role.Add()
  27. if err != nil {
  28. errMsg = "添加角色出错"
  29. err = errors.New(errMsg+"Err:" + err.Error())
  30. return
  31. }
  32. return
  33. }
  34. func EditRoleMenu(req *system.SysRoleMenuEditReq) (err error, errMsg string) {
  35. role := new(system.SysRole)
  36. // 判断角色名称是否已存在
  37. _, err = role.GetRoleByRoleId(req.RoleId)
  38. if err != nil {
  39. if err == utils.ErrNoRow {
  40. errMsg = "角色不存在或者已经被删除"
  41. err = errors.New(errMsg+"Err:" + err.Error())
  42. return
  43. }
  44. errMsg = "查询角色出错"
  45. err = errors.New(errMsg+"Err:" + err.Error())
  46. return
  47. }
  48. //删除绑定的所有菜单
  49. roleMenu := new(system.SysRoleMenu)
  50. cond := "role_id = ?"
  51. pars := make([]interface{}, 0)
  52. pars = append(pars, req.RoleId)
  53. err = roleMenu.DeleteByCondition(cond, pars)
  54. if err != nil {
  55. errMsg = "删除角色原先绑定的菜单出错"
  56. err = errors.New(errMsg+"Err:" + err.Error())
  57. return
  58. }
  59. //给角色添加菜单权限
  60. list := make([]*system.SysRoleMenu, 0)
  61. if len(req.MenuIdList) > 0 {
  62. for _, v := range req.MenuIdList {
  63. item := new(system.SysRoleMenu)
  64. item.RoleId = req.RoleId
  65. item.MenuId = v
  66. item.CreateTime = time.Now()
  67. item.ModifyTime = time.Now()
  68. list = append(list, item)
  69. }
  70. if err = roleMenu.AddMulti(list); err != nil {
  71. errMsg = "给角色添加菜单权限出错"
  72. err = errors.New(errMsg+"Err:" + err.Error())
  73. return
  74. }
  75. }
  76. return
  77. }
  78. func RoleMenuList(req *system.SysRoleMenuListReq) (ret system.SysRoleMenuResp, err error, errMsg string) {
  79. //查询角色绑定的菜单
  80. roleMenu := new(system.SysRoleMenu)
  81. cond := " rm.role_id = ?"
  82. pars := make([]interface{}, 0)
  83. pars = append(pars, req.RoleId)
  84. roleHasMenuList, err := roleMenu.GetMenuListByCondition(cond, pars)
  85. if err != nil {
  86. errMsg = "查询角色绑定的菜单出错"
  87. err = errors.New(errMsg+"Err:" + err.Error())
  88. return
  89. }
  90. RoleMenuMap := make(map[int64]struct{}, 0)
  91. HasMenuIds := make([]int64, 0)
  92. for _, v := range roleHasMenuList {
  93. RoleMenuMap[v.MenuId] = struct{}{}
  94. HasMenuIds = append(HasMenuIds, v.MenuId)
  95. }
  96. //查询所有菜单列表
  97. menu := new(system.SysMenu)
  98. orderStr := "sort asc, create_time, menu_id desc"
  99. childList, err := menu.GetMenuListByCondition("", []interface{}{},orderStr)
  100. if err != nil {
  101. return
  102. }
  103. list := make([]*system.MenuListItemResp, 0)
  104. for _, v:= range childList {
  105. tmp := &system.MenuListItemResp{
  106. MenuId: v.MenuId,
  107. ParentId: v.ParentId,
  108. Name: v.Name,
  109. Sort: v.Sort,
  110. Path: v.Path,
  111. PathName: v.PathName,
  112. IconPath: v.IconPath,
  113. Component: v.Component,
  114. Hidden: v.Hidden,
  115. HiddenLayout: v.HiddenLayout,
  116. Level: v.Level,
  117. MenuType: v.MenuType,
  118. ButtonCode: v.ButtonCode,
  119. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  120. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  121. Children: nil,
  122. }
  123. if _, ok := RoleMenuMap[tmp.MenuId]; ok {
  124. tmp.HasBind = true
  125. }
  126. list = append(list, tmp)
  127. }
  128. //组装返回值
  129. list = getMenuTreeRecursive(list, 0)
  130. ret.List = list
  131. ret.ChoiceList = HasMenuIds
  132. return
  133. }
  134. func DeleteRole(req *system.SysRoleDelReq) (err error, errMsg string) {
  135. role := new(system.SysRole)
  136. // 判断角色名称是否已存在
  137. _, err = role.GetRoleByRoleId(req.RoleId)
  138. if err != nil {
  139. if err == utils.ErrNoRow {
  140. errMsg = "角色不存在或者已经被删除"
  141. err = errors.New(errMsg+"Err:" + err.Error())
  142. return
  143. }
  144. errMsg = "查询角色出错"
  145. err = errors.New(errMsg+"Err:" + err.Error())
  146. return
  147. }
  148. //删除绑定的所有菜单
  149. roleMenu := new(system.SysRoleMenu)
  150. cond := "role_id = ?"
  151. pars := make([]interface{}, 0)
  152. pars = append(pars, req.RoleId)
  153. err = roleMenu.DeleteByCondition(cond, pars)
  154. if err != nil {
  155. errMsg = "删除角色原先绑定的菜单出错"
  156. err = errors.New(errMsg+"Err:" + err.Error())
  157. return
  158. }
  159. //查询与角色绑定的所有账号
  160. admin := new(system.SysAdmin)
  161. aCond := "role_id = ?"
  162. aPars := make([]interface{}, 0)
  163. aPars = append(pars, req.RoleId)
  164. admins, err := admin.GetAdminListByCondition(aCond, aPars)
  165. if err != nil {
  166. errMsg = "查询角色对应的用户出错"
  167. err = errors.New(errMsg+"Err:" + err.Error())
  168. return
  169. }
  170. if len(admins) > 0 {
  171. errMsg = "有账号绑定该角色,不可删除"
  172. err = errors.New(errMsg)
  173. return
  174. }
  175. // 删除角色
  176. role.RoleId = req.RoleId
  177. err = role.Delete()
  178. if err != nil {
  179. errMsg = "删除角色出错"
  180. err = errors.New(errMsg+"Err:" + err.Error())
  181. return
  182. }
  183. return
  184. }