Commit cfa15757 by zhoulm

feat(approval): add update opr time logic for scene merchant ref

add new mapper and call update opr time method in approval service after processing merchant refs
1 parent 92181021
package cn.com.glsx.dj.goods.listing.mapper.old;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.oreframework.datasource.mybatis.mapper.OreMapper;
import cn.com.glsx.dj.smartcarlife.model.SceneMerchantRefNew;
@Mapper
public interface SmartcarSceneMerchantRefNewMapper extends OreMapper<SceneMerchantRefNew> {
int updateOprTime(@Param("mainMerchantIdList") List<Integer> mainMerchantIdList, @Param("updateBy") String updateBy, @Param("baseSceneCode") String baseSceneCode);
}
\ No newline at end of file \ No newline at end of file
...@@ -137,6 +137,8 @@ public class GoodsListingApprovalService { ...@@ -137,6 +137,8 @@ public class GoodsListingApprovalService {
private TaskExecutor taskExecutor; private TaskExecutor taskExecutor;
@Autowired @Autowired
private MerchantGoodsInfoRemote merchantGoodsInfoRemote; private MerchantGoodsInfoRemote merchantGoodsInfoRemote;
@Autowired
private SmartcarSceneMerchantRefNewMapper smartcarSceneMerchantRefNewMapper;
public Page<GoodsListingApprovalDTO> listWithPage(RpcPagination<GoodsListingApprovalDTO> pagination) { public Page<GoodsListingApprovalDTO> listWithPage(RpcPagination<GoodsListingApprovalDTO> pagination) {
try { try {
...@@ -1332,6 +1334,9 @@ public class GoodsListingApprovalService { ...@@ -1332,6 +1334,9 @@ public class GoodsListingApprovalService {
ruleIds.reconciliationRuleId == null ? merchantRef.getReconciliationRuleId() : ruleIds.reconciliationRuleId, ruleIds.reconciliationRuleId == null ? merchantRef.getReconciliationRuleId() : ruleIds.reconciliationRuleId,
context.updatedBy); context.updatedBy);
} }
List<Integer> mainMerchantIdList = context.merchantRefs.stream()
.map(GoodsListingMerchantRef::getMerchantId).collect(Collectors.toList());
smartcarSceneMerchantRefNewMapper.updateOprTime(mainMerchantIdList, context.updatedBy, context.baseSceneCode);
} }
private void putGoodsCodeMapping(ApprovalPublishContext context, GoodsListingMerchantRef merchantRef, private void putGoodsCodeMapping(ApprovalPublishContext context, GoodsListingMerchantRef merchantRef,
......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.com.glsx.dj.goods.listing.mapper.old.SmartcarSceneMerchantRefNewMapper" >
<update id="updateOprTime">
update dj_car_life_scene_merchant_ref_new set updated_by = #{updateBy}, updated_date = now()
<where>
deleted_flag = 'N' and main_merchant_type in (1,2,3,4,5)
and base_scene_code = #{baseSceneCode}
and main_merchant_id in
<foreach collection="mainMerchantIdList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</where>
</update>
</mapper>
\ No newline at end of file \ No newline at end of file
  • @wangxw1
    commented
    Master

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

    1. 严重缺陷:未处理集合为空的场景,可能导致 SQL 语法错误或空指针异常(NPE)

    • 问题描述:在 GoodsListingApprovalService.java 中,直接对 context.merchantRefs 进行了 stream 映射操作并传递给 Mapper。如果 context.merchantRefs 为 null,会抛出 NullPointerException;如果集合为空,mainMerchantIdList 也为空,MyBatis 的 <foreach> 标签会生成 main_merchant_id in () 这样的非法 SQL,导致数据库抛出语法错误异常。
    • 影响分析:在边界场景下会导致核心审批流程中断,引发事务回滚或接口报错。
    • 优化建议:在 Java 代码层进行防御性编程,判断集合是否为空;若为空,则直接跳过数据库更新操作,避免不必要的 DB 请求和报错。

    代码示例:

    // GoodsListingApprovalService.java
    if (context.merchantRefs != null && !context.merchantRefs.isEmpty()) {
        List<Integer> mainMerchantIdList = context.merchantRefs.stream()
                .map(GoodsListingMerchantRef::getMerchantId)
                .distinct() // 建议加上去重,减少SQL参数
                .collect(Collectors.toList());
        smartcarSceneMerchantRefNewMapper.updateOprTime(mainMerchantIdList, context.updatedBy, context.baseSceneCode);
    }
    

    2. 稳定性风险:IN 子句参数过多可能引发性能问题

    • 问题描述:MyBatis XML 中的 <foreach> 用于构建 IN 条件。如果 mainMerchantIdList 的数据量非常大(例如超过 1000 个),会导致生成的 SQL 语句过长,不仅解析耗时,还可能触发 MySQL 的 max_allowed_packet 限制或引起全表扫描。
    • 影响分析:在极端批量审批场景下,可能导致数据库性能急剧下降或执行失败。
    • 优化建议:评估业务场景中 merchantRefs 的最大数量。如果存在大批量数据的可能,建议在 Java 层对 List 进行分批(如使用 Lists.partition 每 500 条执行一次),或者通过其他关联更新的方式处理。

    3. 可维护性问题:SQL 中存在硬编码(魔法值)

    • 问题描述:在 SmartcarSceneMerchantRefNewMapper.xml 中,存在硬编码条件 main_merchant_type in (1,2,3,4,5)。
    • 影响分析:降低了代码的可维护性。如果未来业务扩展,新增了类型 6,开发人员很难追踪到需要修改这段 XML,容易引发业务 Bug。
    • 优化建议:建议将这些类型定义为 Java 层的 Enum 或常量,并作为参数传入 Mapper,或者在代码注释/文档中明确说明此处写死 (1,2,3,4,5) 的业务背景,以便后人维护。

    二、评分明细

    • 1. 功能实现的正确性与健壮性(32/40分)

      • 扣分原因:未考虑 merchantRefs 为 null 或 empty 的边界情况,会导致 NPE 或 SQL IN () 语法错误。
      • 优点:严格遵守了“不使用子查询”和“不修改 Dubbo 接口参数”的红线规则。
    • 2. 安全性与潜在风险(30/30分)

      • 得分原因:MyBatis 参数均正确使用了 #{} 占位符,无 SQL 注入风险;未发现越权或敏感信息泄露问题。
    • 3. 最佳实践与可维护性(17/20分)

      • 扣分原因:XML 中存在 main_merchant_type in (1,2,3,4,5) 的硬编码魔法值,不利于后续业务扩展和维护。
    • 4. 性能与资源利用(4/5分)

      • 扣分原因:对于 IN 集合查询没有做去重(distinct)和容量防范,存在潜在的 SQL 性能隐患。
    • 5. 提交信息质量(5/5分)

      • 得分原因:Commit message 采用了标准规范(feat(approval): ...),清晰地描述了变更的目的和具体实现逻辑,易于追溯。

    总分:88分

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