| | |
| | | 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; |
| | |
| | | private static final int MAX_LEVEL = 5; |
| | | |
| | | private final SysDeptMapper sysDeptMapper; |
| | | private final MpUserMapper mpUserMapper; |
| | | |
| | | public List<DeptTreeNodeVo> tree() { |
| | | List<SysDept> all = |
| | |
| | | @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>() |
| | |
| | | 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; |