更新字典区域模块,新增path和pathName字段,优化树形结构构建逻辑以支持路径信息的初始化和设置。
This commit is contained in:
@ -9,6 +9,8 @@
|
||||
<result column="name" jdbcType="VARCHAR" property="name"/>
|
||||
<result column="level" jdbcType="VARCHAR" property="level"/>
|
||||
<result column="ab" jdbcType="VARCHAR" property="ab"/>
|
||||
<result column="path" jdbcType="VARCHAR" property="path"/>
|
||||
<result column="path_name" jdbcType="VARCHAR" property="pathName"/>
|
||||
</resultMap>
|
||||
|
||||
<!--逻辑删除方法 此方法为代码生成器生成 不允许修改 如有特殊需求 请自行新建SQL语句-->
|
||||
@ -23,14 +25,14 @@
|
||||
<select id="getAllChildrenIncludingSelf" resultMap="BaseResultMap">
|
||||
WITH RECURSIVE region_tree AS (
|
||||
-- 基础查询:查询当前节点
|
||||
SELECT id, p_id, name, level, ab
|
||||
SELECT id, p_id, name, level, ab, path, path_name
|
||||
FROM dict_region
|
||||
WHERE id = #{id}
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- 递归查询:查询所有子节点
|
||||
SELECT dr.id, dr.p_id, dr.name, dr.level, dr.ab
|
||||
SELECT dr.id, dr.p_id, dr.name, dr.level, dr.ab, dr.path, dr.path_name
|
||||
FROM dict_region dr
|
||||
INNER JOIN region_tree rt ON dr.p_id = rt.id
|
||||
)
|
||||
|
@ -51,6 +51,18 @@ public class DictRegionTreeVO implements Serializable {
|
||||
@ApiModelProperty(value = "缩写")
|
||||
private String ab;
|
||||
|
||||
/**
|
||||
* ID路径,逗号分隔
|
||||
*/
|
||||
@ApiModelProperty(value = "ID路径,逗号分隔")
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 名称路径,逗号分隔
|
||||
*/
|
||||
@ApiModelProperty(value = "名称路径,逗号分隔")
|
||||
private String pathName;
|
||||
|
||||
/**
|
||||
* 子节点列表
|
||||
*/
|
||||
|
@ -70,6 +70,9 @@ public class DictRegionServiceImpl extends BaseServiceImpl<DictRegionMapper, Dic
|
||||
return null;
|
||||
}
|
||||
|
||||
// 初始化所有节点的path和pathName字段
|
||||
initPathAndPathName(allChildren, current);
|
||||
|
||||
// 构建父ID到子节点的映射,提高查找效率
|
||||
Map<String, List<DictRegion>> parentToChildrenMap = new HashMap<>();
|
||||
for (DictRegion node : allChildren) {
|
||||
@ -80,8 +83,47 @@ public class DictRegionServiceImpl extends BaseServiceImpl<DictRegionMapper, Dic
|
||||
return buildTreeFromMap(current, parentToChildrenMap, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化所有节点的path和pathName字段
|
||||
*
|
||||
* @param allChildren 所有子节点列表
|
||||
* @param root 根节点
|
||||
*/
|
||||
private void initPathAndPathName(List<DictRegion> allChildren, DictRegion root) {
|
||||
// 创建ID到节点的映射
|
||||
Map<String, DictRegion> idToNodeMap = new HashMap<>();
|
||||
for (DictRegion node : allChildren) {
|
||||
idToNodeMap.put(node.getId(), node);
|
||||
}
|
||||
|
||||
// 初始化根节点的path和pathName
|
||||
root.setPath(root.getId());
|
||||
root.setPathName(root.getName());
|
||||
|
||||
// BFS遍历设置所有子节点的path和pathName
|
||||
List<DictRegion> queue = new ArrayList<>();
|
||||
queue.add(root);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
DictRegion currentNode = queue.remove(0);
|
||||
String currentPath = currentNode.getPath();
|
||||
String currentPathName = currentNode.getPathName();
|
||||
|
||||
// 查找当前节点的所有直接子节点
|
||||
for (DictRegion node : allChildren) {
|
||||
if (currentNode.getId().equals(node.getPId())) {
|
||||
// 设置子节点的path和pathName
|
||||
node.setPath(currentPath + "," + node.getId());
|
||||
node.setPathName(currentPathName + "," + node.getName());
|
||||
queue.add(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Java版本递归查询所有下级数据(包括自己)
|
||||
*
|
||||
* @param id 当前节点ID
|
||||
* @return 所有下级数据列表
|
||||
*/
|
||||
@ -101,6 +143,7 @@ public class DictRegionServiceImpl extends BaseServiceImpl<DictRegionMapper, Dic
|
||||
|
||||
/**
|
||||
* 递归查询所有子节点
|
||||
*
|
||||
* @param parentId 父节点ID
|
||||
* @return 所有子节点列表
|
||||
*/
|
||||
@ -124,9 +167,10 @@ public class DictRegionServiceImpl extends BaseServiceImpl<DictRegionMapper, Dic
|
||||
|
||||
/**
|
||||
* 从Map中构建树形结构(内存中处理,使用Map提高效率)
|
||||
* @param rootNode 根节点
|
||||
*
|
||||
* @param rootNode 根节点
|
||||
* @param parentToChildrenMap 父ID到子节点的映射
|
||||
* @param depth 当前深度
|
||||
* @param depth 当前深度
|
||||
* @return 树形结构节点
|
||||
*/
|
||||
private DictRegionTreeVO buildTreeFromMap(DictRegion rootNode, Map<String, List<DictRegion>> parentToChildrenMap, int depth) {
|
||||
@ -136,6 +180,8 @@ public class DictRegionServiceImpl extends BaseServiceImpl<DictRegionMapper, Dic
|
||||
.setName(rootNode.getName())
|
||||
.setLevel(rootNode.getLevel())
|
||||
.setAb(rootNode.getAb())
|
||||
.setPath(rootNode.getPath())
|
||||
.setPathName(rootNode.getPathName())
|
||||
.setDepth(depth);
|
||||
|
||||
// 从Map中获取当前节点的直接子节点
|
||||
|
@ -57,6 +57,8 @@ public class TreeUtil {
|
||||
.setName(child.getName())
|
||||
.setLevel(child.getLevel())
|
||||
.setAb(child.getAb())
|
||||
.setPath(child.getPath())
|
||||
.setPathName(child.getPathName())
|
||||
.setDepth(depth);
|
||||
|
||||
// 递归构建子节点
|
||||
|
Reference in New Issue
Block a user