roleService.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "iot_manager_service/app/system/dao"
  4. "iot_manager_service/app/system/model"
  5. "iot_manager_service/util"
  6. )
  7. // 角色管理服务
  8. var RoleService = new(roleService)
  9. type roleService struct{}
  10. func (s *roleService) Get(id int64) (*dao.Role, error) {
  11. role := &dao.Role{
  12. ID: id,
  13. }
  14. err := role.Get()
  15. if err != nil {
  16. return nil, err
  17. }
  18. return role, nil
  19. }
  20. func (s *roleService) List(roleName, tenantId, roleAlias string, current, size int) ([]dao.Role, error) {
  21. role := &dao.Role{}
  22. offset := (current - 1) * size
  23. limit := size
  24. if roleName != "" {
  25. role.RoleName = roleName
  26. }
  27. if tenantId != "" {
  28. role.TenantId = tenantId
  29. }
  30. if roleAlias != "" {
  31. role.RoleAlias = roleAlias
  32. }
  33. roles, err := role.GetRoles(offset, limit)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return roles, nil
  38. }
  39. func (s *roleService) Tree() ([]dao.Role, error) {
  40. role := &dao.Role{}
  41. roles, err := role.GetAll()
  42. if err != nil {
  43. return nil, err
  44. }
  45. rsp := model.RspRoleTree{
  46. HasChildren: false,
  47. ID: ,
  48. ParentId: "",
  49. Title: "",
  50. Key: "",
  51. Value: "",
  52. Children: nil,
  53. }
  54. return rsp, nil
  55. }
  56. func (s *roleService) Submit(req dao.Role) *util.Errors {
  57. role := &req
  58. if role.ID == 0 {
  59. err := role.Save()
  60. if err != nil {
  61. return util.FailResponse(err.Error(), nil)
  62. }
  63. return util.SuccessResponse(util.Succeeded, nil)
  64. }
  65. err := role.Update()
  66. if err != nil {
  67. return util.FailResponse(err.Error(), nil)
  68. }
  69. return util.SuccessResponse(util.Succeeded, nil)
  70. }
  71. func (s *roleService) GetList() ([]dao.Role, error) {
  72. role := &dao.Role{}
  73. roles, err := role.GetAll()
  74. if err != nil {
  75. return nil, err
  76. }
  77. return roles, nil
  78. }
  79. func (s *roleService) Remove(id int64) *util.Errors {
  80. role := &dao.Role{
  81. ID: id,
  82. IsDeleted: 1,
  83. }
  84. queryRole := &dao.Role{
  85. ParentId: id,
  86. IsDeleted: 0,
  87. }
  88. if queryRole.IsExistChild() {
  89. return util.FailResponse(model.ExistChild, nil)
  90. }
  91. err := role.Remove()
  92. if err != nil {
  93. return util.FailResponse(err.Error(), nil)
  94. }
  95. return nil
  96. }
  97. func (s *roleService) Grant(req *model.ReqRoleGrant) error {
  98. roleIds := util.StringToIntArray(req.RoleIds)
  99. //todo 数据权限 接口权限
  100. menuIds := util.StringToIntArray(req.MenuIds)
  101. menuErr := RoleMenuService.UpdateRoleMenus(roleIds, menuIds)
  102. return menuErr
  103. }