mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-09 04:49:02 +08:00
♻️ Refactoring code. #4836 WITH RECURSIVE 递归查询语法兼容性问题
This commit is contained in:
1 parent
31f85c57eb
commit
43e2bc986a
3 files changed
+29
-24
No files matched your search
-7
@@ -36,11 +36,4 @@ import java.util.List;
|
||||
@Mapper
|
||||
public interface SysDeptMapper extends PigxBaseMapper<SysDept> {
|
||||
|
||||
/**
|
||||
* 获取部门的所有后代部门列表
|
||||
* @param deptId 部门ID
|
||||
* @return 后代部门列表
|
||||
*/
|
||||
List<SysDept> listDescendant(Long deptId);
|
||||
|
||||
}
|
||||
+29
-5
@@ -65,7 +65,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean removeDeptById(Long id) {
|
||||
// 级联删除部门
|
||||
List<Long> idList = baseMapper.listDescendant(id).stream().map(SysDept::getDeptId).collect(Collectors.toList());
|
||||
List<Long> idList = this.listDescendant(id).stream().map(SysDept::getDeptId).collect(Collectors.toList());
|
||||
|
||||
Optional.ofNullable(idList).filter(CollUtil::isNotEmpty).ifPresent(this::removeByIds);
|
||||
|
||||
@@ -174,13 +174,37 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取部门的所有后代部门列表
|
||||
* @param deptId 部门ID
|
||||
* @return 后代部门列表
|
||||
* 查询所有子节点 (包含当前节点)
|
||||
* @param deptId 部门ID 目标部门ID
|
||||
* @return ID
|
||||
*/
|
||||
@Override
|
||||
public List<SysDept> listDescendant(Long deptId) {
|
||||
return baseMapper.listDescendant(deptId);
|
||||
// 查询全部部门
|
||||
List<SysDept> allDeptList = baseMapper.selectList(Wrappers.emptyWrapper());
|
||||
|
||||
// 递归查询所有子节点
|
||||
List<SysDept> resDeptList = new ArrayList<>();
|
||||
recursiveDept(allDeptList, deptId, resDeptList);
|
||||
|
||||
// 添加当前节点
|
||||
resDeptList.addAll(allDeptList.stream().filter(sysDept -> deptId.equals(sysDept.getDeptId()))
|
||||
.collect(Collectors.toList()));
|
||||
return resDeptList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归查询所有子节点。
|
||||
* @param allDeptList 所有部门列表
|
||||
* @param parentId 父部门ID
|
||||
* @param resDeptList 结果集合
|
||||
*/
|
||||
private void recursiveDept(List<SysDept> allDeptList, Long parentId, List<SysDept> resDeptList) {
|
||||
// 使用 Stream API 进行筛选和遍历
|
||||
allDeptList.stream().filter(sysDept -> sysDept.getParentId().equals(parentId)).forEach(sysDept -> {
|
||||
resDeptList.add(sysDept);
|
||||
recursiveDept(allDeptList, sysDept.getDeptId(), resDeptList);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,16 +20,4 @@
|
||||
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.pig4cloud.pigx.admin.mapper.SysDeptMapper">
|
||||
<!-- 获取部门的所有后代部门列表 -->
|
||||
<select id="listDescendant" resultType="com.pig4cloud.pigx.admin.api.entity.SysDept">
|
||||
WITH RECURSIVE cte_dept_tree (dept_id, parent_id, name) AS (
|
||||
SELECT dept_id, parent_id, name FROM sys_dept WHERE dept_id = #{deptId}
|
||||
UNION ALL
|
||||
SELECT d.dept_id, d.parent_id, d.name
|
||||
FROM cte_dept_tree c
|
||||
JOIN sys_dept d ON c.dept_id = d.parent_id
|
||||
)
|
||||
SELECT dept_id, parent_id, name
|
||||
FROM cte_dept_tree;
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in new issue
Block a user