Commit 7df1af2b by 林永健

refactor(financial): 重构标签路径分隔符处理逻辑

- 移除 NbCbAccDetailService 中的 convertPathSplit 方法
- 将 TAG_PATH_SPLIT 常量从 " - " 修改为 "," 并添加 TAG_PATH_NAME_SPLIT 常量 "/"
- 更新标签路径构建逻辑,修改路径拼接方式
- 优化路径匹配查询条件,移除多余的分隔符匹配
- 修改 XML 映射文件中的路径查询逻辑,统一使用前缀匹配方式
1 parent 11aa028f
......@@ -666,8 +666,6 @@ public class NbCbAccDetailService {
for (NbCbAccDetailDTO nbCbAccDetailDTO : list) {
nbCbAccDetailDTO.setCurCode(CurCodeEnum.getDisplayNameByValue(nbCbAccDetailDTO.getCurCode()));
nbCbAccDetailDTO.setJdSync(Objects.isNull(nbCbAccDetailDTO.getJdSync()) ? YesNoEnum.NO.getValue() : nbCbAccDetailDTO.getJdSync());
// 转标签路径分隔符
nbCbAccDetailDTO.setAccDetailTagPathName(convertPathSplit(nbCbAccDetailDTO.getAccDetailTagPathName()));
result.add(nbCbAccDetailDTO);
}
page.clear();
......@@ -681,16 +679,6 @@ public class NbCbAccDetailService {
}
}
/**
* 转换标签路径分隔符
*/
private String convertPathSplit(String path) {
if (StringUtils.isBlank(path)) {
return path;
}
return StringUtils.replaceAll(path, NbCbAccDetailTagSummaryService.TAG_PATH_SPLIT, "/");
}
public NbCbAccDetailDTO getAccDetail(Integer id) {
try {
NbCbAccDetailDTO condition = new NbCbAccDetailDTO();
......
......@@ -47,7 +47,9 @@ public class NbCbAccDetailTagSummaryService {
private static final String MONTH_PATTERN = "yyyy-MM";
public static final String TAG_PATH_SPLIT = " - ";
public static final String TAG_PATH_SPLIT = ",";
private static final String TAG_PATH_NAME_SPLIT = "/";
private static final List<String> FUND_SUMMARY_FIXED_ROWS = Arrays.asList(
"月末余额",
......@@ -211,8 +213,8 @@ public class NbCbAccDetailTagSummaryService {
nbCbAccDetailTagMapper.updateByPrimaryKeySelective(entity);
}
String path = parent == null ? String.valueOf(entity.getId()) : parent.getPath() + TAG_PATH_SPLIT + entity.getId();
String pathName = parent == null ? entity.getName() : parent.getPathName() + TAG_PATH_SPLIT + entity.getName();
String path = parent == null ? TAG_PATH_SPLIT + entity.getId() + TAG_PATH_SPLIT : parent.getPath() + entity.getId() + TAG_PATH_SPLIT;
String pathName = parent == null ? entity.getName() : parent.getPathName() + TAG_PATH_NAME_SPLIT + entity.getName();
NbCbAccDetailTag pathUpdate = new NbCbAccDetailTag();
pathUpdate.setId(entity.getId());
pathUpdate.setPath(path);
......@@ -763,7 +765,7 @@ public class NbCbAccDetailTagSummaryService {
for (NbCbAccDetailTag tag : allTags) {
List<Integer> leafIds = allTags.stream()
.filter(this::isCompleteLeafTag)
.filter(item -> StringUtils.equals(item.getPath(), tag.getPath()) || StringUtils.startsWith(item.getPath(), tag.getPath() + TAG_PATH_SPLIT))
.filter(item -> StringUtils.equals(item.getPath(), tag.getPath()) || StringUtils.startsWith(item.getPath(), tag.getPath()))
.map(NbCbAccDetailTag::getId)
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(leafIds)) {
......@@ -855,7 +857,7 @@ public class NbCbAccDetailTagSummaryService {
for (NbCbAccDetailTag tag : filtered) {
ancestorIds.add(tag.getId());
if (StringUtils.isNotBlank(tag.getPath())) {
for (String item : StringUtils.split(tag.getPath(), TAG_PATH_SPLIT)) {
for (String item : StringUtils.split(tag.getPath(), TAG_PATH_SPLIT.charAt(0))) {
if (StringUtils.isNotBlank(item)) {
ancestorIds.add(Integer.parseInt(item));
}
......
......@@ -50,7 +50,7 @@
and financial_report_caliber != ''
</if>
<if test="query != null and query.path != null and query.path != ''">
and (path = #{query.path} or path like concat(#{query.path}, ',%'))
and path like concat(#{query.path}, '%')
</if>
</where>
order by level asc, sort_num asc, id asc
......@@ -89,7 +89,7 @@
select count(1)
from nb_cb_acc_detail_tag
where deleted_flag = 'N'
and path like concat(#{pathPrefix}, ',%')
and path like concat(#{pathPrefix}, '%')
</select>
<select id="listLeafTagIdsByPathPrefix" resultType="java.lang.Integer">
......@@ -97,7 +97,7 @@
from nb_cb_acc_detail_tag
where deleted_flag = 'N'
and level = 3
and path like concat(#{pathPrefix}, ',%')
and path like concat(#{pathPrefix}, '%')
order by sort_num asc, id asc
</select>
......@@ -113,7 +113,7 @@
select id
from nb_cb_acc_detail_tag
where deleted_flag = 'N'
and (path = #{pathPrefix} or path like concat(#{pathPrefix}, ',%'))
and path like concat(#{pathPrefix}, '%')
order by level asc, sort_num asc, id asc
</select>
......@@ -144,7 +144,7 @@
updated_by = #{operator},
updated_date = #{now}
where deleted_flag = 'N'
and path like concat(#{oldPath}, ',%')
and path like concat(#{oldPath}, '%')
</update>
</mapper>
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的专业审查报告。本次重构将树形结构的物化路径(Materialized Path)分隔符从 " - " 修改为标准的 , 包裹形式(如 ,1,2,),这是一个非常优秀的数据库树形结构设计实践,能够有效解决复杂的层级查询问题并支持索引。

    以下是代码中需要关注的三个核心问题与优化建议:

    一、关键问题与优化建议(Top 3)

    1. 历史存量数据的兼容与清洗(重要,涉及功能正确性)

    • 问题描述:代码将 path 的存储格式由 1 - 2 变更为 ,1,2,,同时 path_name 的分隔符也变更为 /。代码层面的重构逻辑非常严密,但未体现对数据库已有存量数据的处理。
    • 影响分析:如果生产环境数据库中已经存在旧格式的数据,新代码上线后,由于 XML 中的查询条件变更为 path like concat(#{query.path}, '%'),旧数据的树形查询、子节点统计以及路径更新功能将全部失效。
    • 优化建议:请确保在本次代码发版时,配套提供并执行数据库数据清洗脚本(如 Flyway/Liquibase 或手动 SQL),将存量数据刷新为新格式。 (参考 SQL 示例:UPDATE nb_cb_acc_detail_tag SET path = concat(',', replace(path, ' - ', ','), ',') WHERE path NOT LIKE ',%';)

    2. XML 映射文件中缺少对空字符串的边界防护(健壮性风险)

    • 问题描述:在 NbCbAccDetailTagMapper.xml 中,诸如 countLeafTagsByPathPrefix、listLeafTagIdsByPathPrefix 等查询,直接使用了 path like concat(#{pathPrefix}, '%'),外层没有 <if> 标签进行非空校验。
    • 影响分析:如果上游业务逻辑出现异常,传入了空字符串 "" 作为 pathPrefix,SQL 将解析为 path like '%',这会导致匹配表中的所有记录,可能引发严重的业务逻辑错误(如误查所有叶子节点或误更新大量数据)。
    • 优化建议:在 XML 中增加非空判断,或在 Java 业务层方法入口处增加前置校验(Fail-fast)。 xml <!-- 建议在 XML 中增加校验 --> <select id="countLeafTagsByPathPrefix" resultType="java.lang.Integer"> select count(1) from nb_cb_acc_detail_tag where deleted_flag = 'N' <if test="pathPrefix != null and pathPrefix != ''"> and path like concat(#{pathPrefix}, '%') </if> <if test="pathPrefix == null or pathPrefix == ''"> and 1 = 0 <!-- 阻断非法查询 --> </if> </select>

    3. 数据库 path 字段的索引确认(性能与最佳实践)

    • 问题描述:本次重构最大的亮点之一是将路径查询统一改为了前缀匹配(path like concat(#{pathPrefix}, '%'))。在 MySQL 等关系型数据库中,前缀 LIKE 查询是可以命中 B-Tree 索引的。
    • 影响分析:如果 nb_cb_acc_detail_tag 表的 path 字段当前没有建立索引,随着标签层级和数量的增加,所有的层级查询仍将是全表扫描,未能完全发挥本次重构带来的性能优势。
    • 优化建议:建议检查数据库表结构,确保为 path 字段建立了普通索引(Index)。

    二、评分明细

    • 功能实现的正确性与健壮性 (36/40分): 业务逻辑重构非常准确,完美实现了标准的物化路径模式,避免了同级节点(如 ,1, 误匹配 ,11,)的经典 Bug。扣分点在于 XML 查询对空参数("")的边界防护不足,存在一定隐患。
    • 安全性与潜在风险 (30/30分): MyBatis 的 SQL 均使用 #{} 进行预编译参数化处理,无 SQL 注入风险,也未引入未授权访问或敏感信息泄露问题。
    • 最佳实践与可维护性 (18/20分): 代码结构清晰,常量的提取和使用规范(如 TAG_PATH_SPLIT)。废弃了无用的转换方法,提升了可维护性。扣除2分是因为 StringUtils.split(..., TAG_PATH_SPLIT.charAt(0)) 的写法略显生硬,直接传入 String 或使用原生 split 即可。
    • 性能与资源利用 (5/5分): 重构后的前缀匹配方式极大地优化了树形结构的查询性能潜力(支持走索引),资源利用合理。
    • 提交信息质量 (5/5分): Commit message 符合 Angular 规范(refactor:),清晰且有条理地列出了修改点及原因,具备极高的可追溯性。

    总分:94分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!