updateDeptLogic.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if req.DeptType == consts.DeptTypeNormal || req.DeptType == consts.DeptTypeDev {
  63. childDepts, _ := l.svcCtx.SysDeptModel.FindByPathPrefix(l.ctx, dept.Path)
  64. for _, cd := range childDepts {
  65. if cd.Id == req.Id {
  66. continue
  67. }
  68. childUserIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, cd.Id)
  69. affectedCount += len(childUserIds)
  70. for _, uid := range childUserIds {
  71. l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid)
  72. }
  73. }
  74. }
  75. if affectedCount > 0 {
  76. l.Infof("UpdateDept id=%d deptType=%s affectedUsers=%d", req.Id, req.DeptType, affectedCount)
  77. }
  78. return nil
  79. }