package com.ruoyi.system.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.basic.PageInfo; import com.ruoyi.common.core.domain.entity.TDept; import com.ruoyi.system.mapper.TDeptMapper; import com.ruoyi.system.service.TDeptService; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** *

* 点位管理 服务实现类 *

* * @author xiaochen * @since 2025-05-28 */ @Service public class TDeptServiceImpl extends ServiceImpl implements TDeptService { /** * 获取部门树结构 * @return 部门树列表 */ public List selectDeptTreeList() { // 查询所有部门 List depts = this.list(); // 构建树结构 return buildDeptTree(depts); } @Override public List getAllSubDeptIds(String deptId) { List allSubIds = new ArrayList<>(); getSubDeptIdsRecursive(Integer.valueOf(deptId), allSubIds); return allSubIds.stream().distinct().collect(Collectors.toList()); } private void getSubDeptIdsRecursive(Integer parentId, List allSubIds) { // 查询直接下级 List directSubIds = this.baseMapper.selectList(new LambdaQueryWrapper().eq(TDept::getParentId, parentId)).stream() .map(TDept::getId).collect(Collectors.toList()); for (Integer subId : directSubIds) { allSubIds.add(subId); // 递归查询下级的下级 getSubDeptIdsRecursive(subId, allSubIds); } allSubIds.add(Integer.valueOf(parentId)); } /** * 构建部门树结构 * @param depts 部门列表 * @return 树结构的部门列表 */ private List buildDeptTree(List depts) { return depts.stream() // 筛选出顶级部门(parentId为null或0的部门) .filter(dept -> dept.getParentId() == null || dept.getParentId() == 0) // 为每个顶级部门构建子树 .peek(dept -> dept.setChildren(getChildren(dept, depts))) .collect(Collectors.toList()); } /** * 递归获取部门的子部门 * @param root 当前部门 * @param depts 所有部门列表 * @return 子部门列表 */ private List getChildren(TDept root, List depts) { return depts.stream() // 筛选出当前部门的直接子部门 .filter(dept -> dept.getParentId() != null && dept.getParentId().equals(root.getId())) // 为每个子部门递归构建其子树 .peek(dept -> dept.setChildren(getChildren(dept, depts))) .collect(Collectors.toList()); } }