Commit f8eb2449 by 林永健

feat(tag): 添加标签路径查询和子孙节点功能

- 在NbCbAccDetailTagMapper中新增selectPathById方法用于查询标签路径
- 新增listDescendantIdsByPath方法用于查询指定路径下所有子孙节点ID
- 在NbCbAccDetailService中注入NbCbAccDetailTagMapper并实现标签树查询逻辑
- 当传入accDetailTagId时自动查询其子孙节点ID列表并设置到查询条件中
- 修改NbCbAccDetailMapper.xml中的查询条件使用descendantTagIds替代原树形查询
- 在NbCbAccDetailDTO中添加descendantTagIds字段用于传递子孙标签ID列表
1 parent 58418af4
...@@ -143,4 +143,10 @@ public class NbCbAccDetailDTO extends NbCbAccDetail { ...@@ -143,4 +143,10 @@ public class NbCbAccDetailDTO extends NbCbAccDetail {
@ApiModelProperty(value = "财务报表口径可选项") @ApiModelProperty(value = "财务报表口径可选项")
private List<String> financialReportCaliberOptions; private List<String> financialReportCaliberOptions;
/**
* 子孙标签ID列表(协助查询)
*/
@ApiModelProperty(value = "子孙标签ID列表", hidden = true)
private List<Integer> descendantTagIds;
} }
...@@ -64,6 +64,22 @@ public interface NbCbAccDetailTagMapper extends OreMapper<NbCbAccDetailTag> { ...@@ -64,6 +64,22 @@ public interface NbCbAccDetailTagMapper extends OreMapper<NbCbAccDetailTag> {
List<Integer> listLeafTagIdsByPathPrefix(@Param("pathPrefix") String pathPrefix); List<Integer> listLeafTagIdsByPathPrefix(@Param("pathPrefix") String pathPrefix);
/** /**
* 根据标签ID查询标签的path
*
* @param tagId 标签ID
* @return 标签path
*/
String selectPathById(@Param("tagId") Integer tagId);
/**
* 查询指定路径下的所有子孙节点ID(包含当前节点)
*
* @param pathPrefix 路径前缀
* @return 节点ID集合
*/
List<Integer> listDescendantIdsByPath(@Param("pathPrefix") String pathPrefix);
/**
* 按同父级同层级统计重名标签数 * 按同父级同层级统计重名标签数
* *
* @param currentId 当前标签ID * @param currentId 当前标签ID
......
...@@ -5,10 +5,7 @@ import cn.com.glsx.dj.financial.constants.CommonConstants; ...@@ -5,10 +5,7 @@ import cn.com.glsx.dj.financial.constants.CommonConstants;
import cn.com.glsx.dj.financial.constants.NbCbDetailLogType; import cn.com.glsx.dj.financial.constants.NbCbDetailLogType;
import cn.com.glsx.dj.financial.dto.*; import cn.com.glsx.dj.financial.dto.*;
import cn.com.glsx.dj.financial.enums.*; import cn.com.glsx.dj.financial.enums.*;
import cn.com.glsx.dj.financial.mapper.ApprovalRecordsMapper; import cn.com.glsx.dj.financial.mapper.*;
import cn.com.glsx.dj.financial.mapper.NbCbAccDetailMapper;
import cn.com.glsx.dj.financial.mapper.NbCbDailyReportAccountMapper;
import cn.com.glsx.dj.financial.mapper.NbCbDetailLogMapper;
import cn.com.glsx.dj.financial.model.*; import cn.com.glsx.dj.financial.model.*;
import cn.com.glsx.dj.financial.service.jd.JdInterfaceProxy; import cn.com.glsx.dj.financial.service.jd.JdInterfaceProxy;
import cn.com.glsx.dj.financial.util.DateUtils; import cn.com.glsx.dj.financial.util.DateUtils;
...@@ -134,6 +131,9 @@ public class NbCbAccDetailService { ...@@ -134,6 +131,9 @@ public class NbCbAccDetailService {
@Autowired @Autowired
private NbCbCustInfoService nbCbCustInfoService; private NbCbCustInfoService nbCbCustInfoService;
@Autowired
private NbCbAccDetailTagMapper nbCbAccDetailTagMapper;
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class) @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public void insert(NbCbAccDetail nbAccDetail) { public void insert(NbCbAccDetail nbAccDetail) {
RpcAssert.assertNotNull(nbAccDetail, DefaultErrorEnum.DATA_NULL, "nbCbReceipt is null"); RpcAssert.assertNotNull(nbAccDetail, DefaultErrorEnum.DATA_NULL, "nbCbReceipt is null");
...@@ -612,6 +612,16 @@ public class NbCbAccDetailService { ...@@ -612,6 +612,16 @@ public class NbCbAccDetailService {
//获取主体配置 //获取主体配置
String custId = nbCbCustInfoService.getCustIdByType(pagination.getCondition().getCustType()); String custId = nbCbCustInfoService.getCustIdByType(pagination.getCondition().getCustType());
NbCbAccDetailDTO condition = pagination.getCondition(); NbCbAccDetailDTO condition = pagination.getCondition();
// 如果传入了 accDetailTagId,先查询其子孙节点ID列表
if (condition.getAccDetailTagId() != null) {
String path = nbCbAccDetailTagMapper.selectPathById(condition.getAccDetailTagId());
if (path != null) {
List<Integer> descendantIds = nbCbAccDetailTagMapper.listDescendantIdsByPath(path);
condition.setDescendantTagIds(descendantIds);
}
}
Page<NbCbAccDetailDTO> page = PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize(), pagination.isNeedCount()); Page<NbCbAccDetailDTO> page = PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize(), pagination.isNeedCount());
// condition.setCustId(env.getProperty("nbcb.custId", "9000919969")); // condition.setCustId(env.getProperty("nbcb.custId", "9000919969"));
condition.setCustId(custId); condition.setCustId(custId);
......
...@@ -294,16 +294,11 @@ ...@@ -294,16 +294,11 @@
and ad.data_source = #{dataSource} and ad.data_source = #{dataSource}
</if> </if>
<if test="accDetailTagId != null"> <if test="descendantTagIds != null and descendantTagIds.size() > 0">
and ad.acc_detail_tag_id in ( and ad.acc_detail_tag_id in
select child.id <foreach collection="descendantTagIds" item="tagId" open="(" separator="," close=")">
from nb_cb_acc_detail_tag current_tag #{tagId}
inner join nb_cb_acc_detail_tag child </foreach>
on child.deleted_flag = 'N'
and (child.path = current_tag.path or child.path like CONCAT(current_tag.path, ',%'))
where current_tag.id = #{accDetailTagId}
and current_tag.deleted_flag = 'N'
)
</if> </if>
<if test="financialReportCaliber != null and financialReportCaliber != ''"> <if test="financialReportCaliber != null and financialReportCaliber != ''">
...@@ -415,16 +410,11 @@ ...@@ -415,16 +410,11 @@
and ad.data_source = #{dataSource} and ad.data_source = #{dataSource}
</if> </if>
<if test="accDetailTagId != null"> <if test="descendantTagIds != null and descendantTagIds.size() > 0">
and ad.acc_detail_tag_id in ( and ad.acc_detail_tag_id in
select child.id <foreach collection="descendantTagIds" item="tagId" open="(" separator="," close=")">
from nb_cb_acc_detail_tag current_tag #{tagId}
inner join nb_cb_acc_detail_tag child </foreach>
on child.deleted_flag = 'N'
and (child.path = current_tag.path or child.path like CONCAT(current_tag.path, ',%'))
where current_tag.id = #{accDetailTagId}
and current_tag.deleted_flag = 'N'
)
</if> </if>
<if test="financialReportCaliber != null and financialReportCaliber != ''"> <if test="financialReportCaliber != null and financialReportCaliber != ''">
......
...@@ -101,6 +101,22 @@ ...@@ -101,6 +101,22 @@
order by sort_num asc, id asc order by sort_num asc, id asc
</select> </select>
<select id="selectPathById" resultType="java.lang.String">
select path
from nb_cb_acc_detail_tag
where id = #{tagId}
and deleted_flag = 'N'
limit 1
</select>
<select id="listDescendantIdsByPath" resultType="java.lang.Integer">
select id
from nb_cb_acc_detail_tag
where deleted_flag = 'N'
and (path = #{pathPrefix} or path like concat(#{pathPrefix}, ',%'))
order by level asc, sort_num asc, id asc
</select>
<select id="countDuplicateByParentAndLevel" resultType="java.lang.Integer"> <select id="countDuplicateByParentAndLevel" resultType="java.lang.Integer">
select count(1) select count(1)
from nb_cb_acc_detail_tag from nb_cb_acc_detail_tag
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 严重设计缺陷与规则违背:Dubbo Remote接口DTO被污染(违反重点规则)

    • 问题描述:在 NbCbAccDetailDTO 中新增了 descendantTagIds 字段。该 DTO 作为 Dubbo 服务的请求参数,对其进行修改直接违反了“dubbo服务提供者Remote接口中的已有请求参数变更视为危险实现”的规则。此外,该字段仅是为了将 Service 层的中间查询结果传递给 Mapper 层(DAO层内部逻辑),将其暴露在公共 API 的 DTO 中属于严重的职责越界。
    • 影响分析:
      1. 破坏了 RPC 接口的稳定性与契约,调用方可能会误传该字段导致不可预期的结果。
      2. 暴露了系统内部的底层 SQL 查询实现细节(API层被持久层逻辑污染)。
    • 优化建议:将 descendantTagIds 从 NbCbAccDetailDTO 中移除。通过修改 Mapper 接口的方法签名,使用 @Param 传递额外的内部参数,或者在 Service 层构建专用的内部 Query/BO 对象。
    • 代码示例: ```java // 1. 恢复 NbCbAccDetailDTO 原状,删除 descendantTagIds 字段。

    // 2. 修改 NbCbAccDetailMapper 接口(假设原方法名为 selectList) List selectList(@Param("condition") NbCbAccDetailDTO condition, @Param("descendantTagIds") List descendantTagIds);

    // 3. 修改 Service 层调用逻辑 List descendantIds = null; if (condition.getAccDetailTagId() != null) { String path = nbCbAccDetailTagMapper.selectPathById(condition.getAccDetailTagId()); if (path != null) { descendantIds = nbCbAccDetailTagMapper.listDescendantIdsByPath(path); // 防御性编程:如果查不到子节点,为了防止查出全部数据,可以给一个不存在的默认值,如 -1 if (CollectionUtils.isEmpty(descendantIds)) { descendantIds = Collections.singletonList(-1); } } else { descendantIds = Collections.singletonList(-1); } } // 传入 Mapper Page page = (Page) nbCbAccDetailMapper.selectList(condition, descendantIds);

    
    **2. 严重逻辑漏洞:空集合导致 SQL 过滤条件失效(数据越权/泄漏风险)**
    - **问题描述**:在 `NbCbAccDetailMapper.xml` 中,动态 SQL 这样写:`<if test="descendantTagIds != null and descendantTagIds.size() > 0">`。如果在 Service 层查询到了 `path`,但 `listDescendantIdsByPath` 返回了空集合,或者前端传入了无效的 `accDetailTagId` 导致根本没去查子孙节点,此时 `descendantTagIds` 为空或 null。
    - **影响分析**:当 `descendantTagIds` 为空时,`<if>` 标签内的 SQL 片段会被直接丢弃。原本用户是想查询某个特定标签下的数据,结果系统却返回了**全表/全量数据**。这在财务系统中是极度危险的数据越权或泄漏漏洞。
    - **优化建议**:必须处理“有查询条件但无匹配子集”的情况。如果 `accDetailTagId` 不为空,但 `descendantTagIds` 为空,应在 SQL 中强制返回空结果(例如拼接 `AND 1 = 0`),或者在 Service 层直接拦截,不发起数据库查询直接返回空分页。
    
    **3. 性能与稳定性隐患:大批量 IN 查询导致 SQL 性能瓶颈**
    - **问题描述**:为了规避 MyBatis 中使用子查询的规则,开发者改用了在 Java 层查出所有子孙节点 ID,再通过 `<foreach>` 拼接成 `IN (...)` 语句。
    - **影响分析**:如果标签树的层级较深或子节点非常多,`descendantTagIds` 的 size 可能会达到数千甚至上万。这会导致生成的 SQL 语句过长,不仅会消耗大量的数据库解析时间,还可能触发数据库 `max_allowed_packet` 限制导致 SQL 执行报错,或者导致索引失效从而引发全表扫描。
    - **优化建议**:
      1. 评估标签树的最大节点数,如果数量级可控(例如 < 500),当前实现勉强可接受。
      2. 更好的替代方案是:在 `nb_cb_acc_detail` 表中冗余存储 `tag_path` 字段,查询时直接使用 `AND tag_path LIKE CONCAT(#{path}, '%')`,这样既避免了子查询,又避免了庞大的 IN 集合,同时减少了两次额外的 DB 查询。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(25/40分)**:
      - 优点:成功去除了 MyBatis 中的子查询,符合特定规则要求。
      - 扣分原因:未处理 `descendantTagIds` 为空集合时的边界情况,导致原有的 `accDetailTagId` 过滤条件完全失效,引发严重逻辑错误(扣15分)。
    - **安全性与潜在风险(20/30分)**:
      - 扣分原因:上述过滤条件失效的逻辑 Bug 直接导致数据范围失控,在财务系统中极易引发越权查询或敏感数据泄漏风险(扣10分)。
    - **最佳实践与可维护性(10/20分)**:
      - 扣分原因:将内部 DAO 层的辅助查询字段加入到对外暴露的 Dubbo DTO 中,严重违背了“已有请求参数变更”的红线规则,破坏了 API 契约与分层架构职责(扣10分)。
    - **性能与资源利用(3/5分)**:
      - 扣分原因:每次查询列表都需要额外发起两次数据库查询来获取标签树 ID,且大集合的 `IN` 查询存在导致 SQL 解析变慢或报错的隐患(扣2分)。
    - **提交信息质量(5/5分)**:
      - 评分理由:Commit message 格式规范,清晰地描述了修改的背景、涉及的文件以及具体的实现逻辑,可追溯性强。
    
    总分:63分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!