updateDeptLogic.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package dept
  2. import (
  3. "context"
  4. "time"
  5. "perms-system-server/internal/consts"
  6. authHelper "perms-system-server/internal/logic/auth"
  7. "perms-system-server/internal/response"
  8. "perms-system-server/internal/svc"
  9. "perms-system-server/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type UpdateDeptLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewUpdateDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateDeptLogic {
  18. return &UpdateDeptLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. func (l *UpdateDeptLogic) UpdateDept(req *types.UpdateDeptReq) error {
  25. if err := authHelper.RequireSuperAdmin(l.ctx); err != nil {
  26. return err
  27. }
  28. if len(req.Name) > 64 {
  29. return response.ErrBadRequest("部门名称长度不能超过64个字符")
  30. }
  31. if len(req.Remark) > 255 {
  32. return response.ErrBadRequest("备注长度不能超过255个字符")
  33. }
  34. dept, err := l.svcCtx.SysDeptModel.FindOne(l.ctx, req.Id)
  35. if err != nil {
  36. return response.ErrNotFound("部门不存在")
  37. }
  38. dept.Name = req.Name
  39. dept.Sort = req.Sort
  40. dept.Remark = req.Remark
  41. if req.DeptType != "" {
  42. if req.DeptType != consts.DeptTypeNormal && req.DeptType != consts.DeptTypeDev {
  43. return response.ErrBadRequest("部门类型无效,仅支持 NORMAL 和 DEV")
  44. }
  45. dept.DeptType = req.DeptType
  46. }
  47. if req.Status != 0 {
  48. if req.Status != consts.StatusEnabled && req.Status != consts.StatusDisabled {
  49. return response.ErrBadRequest("状态值无效,仅支持 1(启用) 和 2(禁用)")
  50. }
  51. dept.Status = req.Status
  52. }
  53. dept.UpdateTime = time.Now().Unix()
  54. if err := l.svcCtx.SysDeptModel.Update(l.ctx, dept); err != nil {
  55. return err
  56. }
  57. userIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, req.Id)
  58. affectedCount := len(userIds)
  59. for _, uid := range userIds {
  60. l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid)
  61. }
  62. // 仅当 deptType 变更时才级联清理子部门用户缓存。
  63. // 禁用父部门 status 时不需要清理子部门:loadPerms 只检查用户自身部门的 deptType/status,
  64. // 子部门的权限逻辑不受父部门状态影响。
  65. if req.DeptType == consts.DeptTypeNormal || req.DeptType == consts.DeptTypeDev {
  66. childDepts, _ := l.svcCtx.SysDeptModel.FindByPathPrefix(l.ctx, dept.Path)
  67. for _, cd := range childDepts {
  68. if cd.Id == req.Id {
  69. continue
  70. }
  71. childUserIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, cd.Id)
  72. affectedCount += len(childUserIds)
  73. for _, uid := range childUserIds {
  74. l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid)
  75. }
  76. }
  77. }
  78. if affectedCount > 0 {
  79. l.Infof("UpdateDept id=%d deptType=%s affectedUsers=%d", req.Id, req.DeptType, affectedCount)
  80. }
  81. return nil
  82. }