updateDeptLogic.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 == consts.DeptTypeNormal || req.DeptType == consts.DeptTypeDev {
  42. dept.DeptType = req.DeptType
  43. }
  44. if req.Status == consts.StatusEnabled || req.Status == consts.StatusDisabled {
  45. dept.Status = req.Status
  46. }
  47. dept.UpdateTime = time.Now().Unix()
  48. if err := l.svcCtx.SysDeptModel.Update(l.ctx, dept); err != nil {
  49. return err
  50. }
  51. userIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, req.Id)
  52. affectedCount := len(userIds)
  53. for _, uid := range userIds {
  54. l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid)
  55. }
  56. if req.DeptType == consts.DeptTypeNormal || req.DeptType == consts.DeptTypeDev {
  57. childDepts, _ := l.svcCtx.SysDeptModel.FindByPathPrefix(l.ctx, dept.Path)
  58. for _, cd := range childDepts {
  59. if cd.Id == req.Id {
  60. continue
  61. }
  62. childUserIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, cd.Id)
  63. affectedCount += len(childUserIds)
  64. for _, uid := range childUserIds {
  65. l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid)
  66. }
  67. }
  68. }
  69. if affectedCount > 0 {
  70. l.Infof("UpdateDept id=%d deptType=%s affectedUsers=%d", req.Id, req.DeptType, affectedCount)
  71. }
  72. return nil
  73. }