deleteDeptLogic.go 950 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package dept
  2. import (
  3. "context"
  4. authHelper "perms-system-server/internal/logic/auth"
  5. "perms-system-server/internal/response"
  6. "perms-system-server/internal/svc"
  7. "perms-system-server/internal/types"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type DeleteDeptLogic struct {
  11. logx.Logger
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. }
  15. func NewDeleteDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteDeptLogic {
  16. return &DeleteDeptLogic{
  17. Logger: logx.WithContext(ctx),
  18. ctx: ctx,
  19. svcCtx: svcCtx,
  20. }
  21. }
  22. func (l *DeleteDeptLogic) DeleteDept(req *types.DeleteDeptReq) error {
  23. if err := authHelper.RequireSuperAdmin(l.ctx); err != nil {
  24. return err
  25. }
  26. children, err := l.svcCtx.SysDeptModel.FindByParentId(l.ctx, req.Id)
  27. if err != nil {
  28. return err
  29. }
  30. if len(children) > 0 {
  31. return response.ErrBadRequest("该部门下存在子部门,无法删除")
  32. }
  33. return l.svcCtx.SysDeptModel.Delete(l.ctx, req.Id)
  34. }