sys_role.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package system
  2. import (
  3. "errors"
  4. "hongze/fms_api/models/system"
  5. "hongze/fms_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 EditRole(req *system.SysRoleEditReq) (err error, errMsg string) {
  35. role := new(system.SysRole)
  36. // 判断角色名称是否已存在
  37. exist, err := role.GetRoleByRoleName(req.RoleName)
  38. if err != nil && err != utils.ErrNoRow {
  39. errMsg = "查询角色名称出错"
  40. err = errors.New(errMsg + "Err:" + err.Error())
  41. return
  42. }
  43. if exist != nil && exist.RoleId > 0 {
  44. if exist.RoleId != req.RoleId {
  45. errMsg = "角色名称已存在, 请重新输入"
  46. err = errors.New(errMsg)
  47. return
  48. }
  49. }
  50. err = nil
  51. // 编辑角色
  52. item, e := role.GetRoleByRoleId(req.RoleId)
  53. if e != nil {
  54. errMsg = "查询角色出错"
  55. err = errors.New(errMsg + "Err:" + e.Error())
  56. return
  57. }
  58. updateCols := []string{"RoleName", "ModifyTime"}
  59. item.RoleName = req.RoleName
  60. item.ModifyTime = time.Now().Local()
  61. if e = item.Update(updateCols); e != nil {
  62. errMsg = "更新角色出错"
  63. err = errors.New(errMsg + "Err:" + e.Error())
  64. return
  65. }
  66. return
  67. }
  68. func EditRoleMenu(req *system.SysRoleMenuEditReq) (err error, errMsg string) {
  69. role := new(system.SysRole)
  70. // 判断角色名称是否已存在
  71. _, err = role.GetRoleByRoleId(req.RoleId)
  72. if err != nil {
  73. if err == utils.ErrNoRow {
  74. errMsg = "角色不存在或者已经被删除"
  75. err = errors.New(errMsg + "Err:" + err.Error())
  76. return
  77. }
  78. errMsg = "查询角色出错"
  79. err = errors.New(errMsg + "Err:" + err.Error())
  80. return
  81. }
  82. //删除绑定的所有菜单
  83. roleMenu := new(system.SysRoleMenu)
  84. cond := "role_id = ?"
  85. pars := make([]interface{}, 0)
  86. pars = append(pars, req.RoleId)
  87. err = roleMenu.DeleteByCondition(cond, pars)
  88. if err != nil {
  89. errMsg = "删除角色原先绑定的菜单出错"
  90. err = errors.New(errMsg + "Err:" + err.Error())
  91. return
  92. }
  93. //给角色添加菜单权限
  94. list := make([]*system.SysRoleMenu, 0)
  95. if len(req.MenuIdList) > 0 {
  96. for _, v := range req.MenuIdList {
  97. item := new(system.SysRoleMenu)
  98. item.RoleId = req.RoleId
  99. item.MenuId = v
  100. item.CreateTime = time.Now()
  101. item.ModifyTime = time.Now()
  102. list = append(list, item)
  103. }
  104. if err = roleMenu.AddMulti(list); err != nil {
  105. errMsg = "给角色添加菜单权限出错"
  106. err = errors.New(errMsg + "Err:" + err.Error())
  107. return
  108. }
  109. }
  110. return
  111. }
  112. func RoleMenuList(req *system.SysRoleMenuListReq) (ret system.SysRoleMenuResp, err error, errMsg string) {
  113. //查询角色绑定的菜单
  114. roleMenu := new(system.SysRoleMenu)
  115. cond := " rm.role_id = ?"
  116. pars := make([]interface{}, 0)
  117. pars = append(pars, req.RoleId)
  118. roleHasMenuList, err := roleMenu.GetMenuListByCondition(cond, pars)
  119. if err != nil {
  120. errMsg = "查询角色绑定的菜单出错"
  121. err = errors.New(errMsg + "Err:" + err.Error())
  122. return
  123. }
  124. RoleMenuMap := make(map[int64]struct{}, 0)
  125. HasMenuIds := make([]int64, 0)
  126. for _, v := range roleHasMenuList {
  127. RoleMenuMap[v.MenuId] = struct{}{}
  128. HasMenuIds = append(HasMenuIds, v.MenuId)
  129. }
  130. //查询所有菜单列表
  131. menu := new(system.SysMenu)
  132. orderStr := "sort asc, create_time, menu_id desc"
  133. childList, err := menu.GetMenuListByCondition("", []interface{}{}, orderStr)
  134. if err != nil {
  135. return
  136. }
  137. list := make([]*system.MenuListItemResp, 0)
  138. for _, v := range childList {
  139. tmp := &system.MenuListItemResp{
  140. MenuId: v.MenuId,
  141. ParentId: v.ParentId,
  142. Name: v.Name,
  143. Sort: v.Sort,
  144. Path: v.Path,
  145. PathName: v.PathName,
  146. IconPath: v.IconPath,
  147. Component: v.Component,
  148. Hidden: v.Hidden,
  149. HiddenLayout: v.HiddenLayout,
  150. Level: v.Level,
  151. MenuType: v.MenuType,
  152. ButtonCode: v.ButtonCode,
  153. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  154. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  155. Children: nil,
  156. }
  157. if _, ok := RoleMenuMap[tmp.MenuId]; ok {
  158. tmp.HasBind = true
  159. }
  160. list = append(list, tmp)
  161. }
  162. //组装返回值
  163. list = getMenuTreeRecursive(list, 0)
  164. ret.List = list
  165. ret.ChoiceList = HasMenuIds
  166. return
  167. }
  168. func DeleteRole(req *system.SysRoleDelReq) (err error, errMsg string) {
  169. role := new(system.SysRole)
  170. // 判断角色名称是否已存在
  171. _, err = role.GetRoleByRoleId(req.RoleId)
  172. if err != nil {
  173. if err == utils.ErrNoRow {
  174. errMsg = "角色不存在或者已经被删除"
  175. err = errors.New(errMsg + "Err:" + err.Error())
  176. return
  177. }
  178. errMsg = "查询角色出错"
  179. err = errors.New(errMsg + "Err:" + err.Error())
  180. return
  181. }
  182. //删除绑定的所有菜单
  183. roleMenu := new(system.SysRoleMenu)
  184. cond := "role_id = ?"
  185. pars := make([]interface{}, 0)
  186. pars = append(pars, req.RoleId)
  187. err = roleMenu.DeleteByCondition(cond, pars)
  188. if err != nil {
  189. errMsg = "删除角色原先绑定的菜单出错"
  190. err = errors.New(errMsg + "Err:" + err.Error())
  191. return
  192. }
  193. //查询与角色绑定的所有账号
  194. admin := new(system.SysAdmin)
  195. aCond := "role_id = ?"
  196. aPars := make([]interface{}, 0)
  197. aPars = append(pars, req.RoleId)
  198. admins, err := admin.GetAdminListByCondition(aCond, aPars)
  199. if err != nil {
  200. errMsg = "查询角色对应的用户出错"
  201. err = errors.New(errMsg + "Err:" + err.Error())
  202. return
  203. }
  204. if len(admins) > 0 {
  205. errMsg = "有账号绑定该角色,不可删除"
  206. err = errors.New(errMsg)
  207. return
  208. }
  209. // 删除角色
  210. role.RoleId = req.RoleId
  211. err = role.Delete()
  212. if err != nil {
  213. errMsg = "删除角色出错"
  214. err = errors.New(errMsg + "Err:" + err.Error())
  215. return
  216. }
  217. return
  218. }