updateDeptLogic.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. if req.DeptType == consts.DeptTypeNormal || req.DeptType == consts.DeptTypeDev {
  50. childDepts, _ := l.svcCtx.SysDeptModel.FindByPathPrefix(l.ctx, dept.Path)
  51. for _, cd := range childDepts {
  52. if cd.Id == req.Id {
  53. continue
  54. }
  55. childUserIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, cd.Id)
  56. for _, uid := range childUserIds {
  57. l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid)
  58. }
  59. }
  60. }
  61. return nil
  62. }