1.0
longz
2026-06-24 e45e74e5ae207f7642be9a88ed99b593de89fb3b
backend/src/main/java/com/sinata/appointment/service/DeptManageApplicationService.java
@@ -5,8 +5,10 @@
import com.sinata.appointment.dto.DeptCreateRequest;
import com.sinata.appointment.dto.DeptTreeNodeVo;
import com.sinata.appointment.dto.DeptUpdateRequest;
import com.sinata.appointment.entity.MpUser;
import com.sinata.appointment.entity.SysDept;
import com.sinata.appointment.exception.BizException;
import com.sinata.appointment.mapper.MpUserMapper;
import com.sinata.appointment.mapper.SysDeptMapper;
import com.sinata.appointment.util.UuidAssert;
import java.util.ArrayList;
@@ -29,6 +31,7 @@
    private static final int MAX_LEVEL = 5;
    private final SysDeptMapper sysDeptMapper;
    private final MpUserMapper mpUserMapper;
    public List<DeptTreeNodeVo> tree() {
        List<SysDept> all =
@@ -119,6 +122,16 @@
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id, String uuid) {
        SysDept d = loadActiveWithUuid(id, uuid);
        Set<Long> deptScope = collectSubtreeDeptIds(id);
        long staffCnt =
                mpUserMapper.selectCount(
                        new LambdaQueryWrapper<MpUser>()
                                .eq(MpUser::getDelFlag, 0)
                                .eq(MpUser::getUserType, "EMPLOYEE")
                                .in(MpUser::getDeptId, deptScope));
        if (staffCnt > 0) {
            throw new BizException(BizErrorCode.DEPT_HAS_BOUND_STAFF, "该部门下有关联员工,无法删除");
        }
        long childCnt =
                sysDeptMapper.selectCount(
                        new LambdaQueryWrapper<SysDept>()
@@ -196,6 +209,34 @@
        return m;
    }
    private Set<Long> collectSubtreeDeptIds(Long rootDeptId) {
        Set<Long> deptIds = new HashSet<>();
        if (rootDeptId == null || rootDeptId <= 0) {
            return deptIds;
        }
        List<SysDept> all =
                sysDeptMapper.selectList(
                        new LambdaQueryWrapper<SysDept>()
                                .eq(SysDept::getDelFlag, 0)
                                .select(SysDept::getId, SysDept::getParentId));
        Map<Long, List<Long>> children = new HashMap<>();
        for (SysDept dept : all) {
            Long parentId = dept.getParentId() == null ? 0L : dept.getParentId();
            children.computeIfAbsent(parentId, k -> new ArrayList<>()).add(dept.getId());
        }
        List<Long> stack = new ArrayList<>();
        stack.add(rootDeptId);
        while (!stack.isEmpty()) {
            Long current = stack.remove(stack.size() - 1);
            if (!deptIds.add(current)) {
                continue;
            }
            List<Long> next = children.getOrDefault(current, List.of());
            stack.addAll(next);
        }
        return deptIds;
    }
    /** 节点到根的距离:根为 1 */
    private int depthOf(long id) {
        int d = 1;