roleService.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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() error {
  40. return nil
  41. }
  42. func (s *roleService) Submit(req dao.Role) *util.Errors {
  43. role := &req
  44. if role.ID == 0 {
  45. err := role.Save()
  46. if err != nil {
  47. return util.FailResponse(err.Error(), nil)
  48. }
  49. return util.SuccessResponse(util.Succeeded, nil)
  50. }
  51. err := role.Update()
  52. if err != nil {
  53. return util.FailResponse(err.Error(), nil)
  54. }
  55. return util.SuccessResponse(util.Succeeded, nil)
  56. }
  57. func (s *roleService) GetList() ([]dao.Role, error) {
  58. role := &dao.Role{}
  59. roles, err := role.GetAll()
  60. if err != nil {
  61. return nil, err
  62. }
  63. return roles, nil
  64. }
  65. func (s *roleService) Remove(id int64) *util.Errors {
  66. role := &dao.Role{
  67. ID: id,
  68. IsDeleted: 1,
  69. }
  70. queryRole := &dao.Role{
  71. ParentId: id,
  72. IsDeleted: 0,
  73. }
  74. if queryRole.IsExistChild() {
  75. return util.FailResponse(model.ExistChild, nil)
  76. }
  77. err := role.Remove()
  78. if err != nil {
  79. return util.FailResponse(err.Error(), nil)
  80. }
  81. return nil
  82. }
  83. func (s *roleService) Grant(req *model.ReqRoleGrant) error {
  84. roleIds := util.StringToIntArray(req.RoleIds)
  85. //todo 数据权限 接口权限
  86. menuIds := util.StringToIntArray(req.MenuIds)
  87. menuErr := RoleMenuService.UpdateRoleMenus(roleIds, menuIds)
  88. return menuErr
  89. }