updateDeptLogic.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. dept, err := l.svcCtx.SysDeptModel.FindOne(l.ctx, req.Id)
  29. if err != nil {
  30. return response.ErrNotFound("部门不存在")
  31. }
  32. dept.Name = req.Name
  33. dept.Sort = req.Sort
  34. dept.Remark = req.Remark
  35. if req.DeptType == consts.DeptTypeNormal || req.DeptType == consts.DeptTypeDev {
  36. dept.DeptType = req.DeptType
  37. }
  38. if req.Status == consts.StatusEnabled || req.Status == consts.StatusDisabled {
  39. dept.Status = req.Status
  40. }
  41. dept.UpdateTime = time.Now().Unix()
  42. if err := l.svcCtx.SysDeptModel.Update(l.ctx, dept); err != nil {
  43. return err
  44. }
  45. userIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, req.Id)
  46. for _, uid := range userIds {
  47. l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid)
  48. }
  49. return nil
  50. }