updateDeptLogic.go 2.7 KB

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