Commit 2cd18955 by 崔彦峰

优化审核通过后代码批量插入逻辑sql。

1 parent 6c83f51f
...@@ -4,6 +4,7 @@ import cn.com.glsx.dj.goods.listing.dto.GoodsListingMerchantRefDTO; ...@@ -4,6 +4,7 @@ import cn.com.glsx.dj.goods.listing.dto.GoodsListingMerchantRefDTO;
import cn.com.glsx.dj.goods.listing.model.GoodsListingMerchantRef; import cn.com.glsx.dj.goods.listing.model.GoodsListingMerchantRef;
import java.util.List; import java.util.List;
import java.util.Date;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
...@@ -16,4 +17,8 @@ public interface GoodsListingMerchantRefMapper extends OreMapper<GoodsListingMer ...@@ -16,4 +17,8 @@ public interface GoodsListingMerchantRefMapper extends OreMapper<GoodsListingMer
GoodsListingMerchantRef getFirstOne(@Param("businessCode") String businessCode); GoodsListingMerchantRef getFirstOne(@Param("businessCode") String businessCode);
List<GoodsListingMerchantRef> list(GoodsListingMerchantRefDTO condition); List<GoodsListingMerchantRef> list(GoodsListingMerchantRefDTO condition);
int updateRuleIdsBatch(@Param("list") List<GoodsListingMerchantRef> list,
@Param("updatedBy") String updatedBy,
@Param("updatedDate") Date updatedDate);
} }
package cn.com.glsx.dj.goods.listing.mapper.old; package cn.com.glsx.dj.goods.listing.mapper.old;
import java.util.List;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.oreframework.datasource.mybatis.mapper.OreMapper; import org.oreframework.datasource.mybatis.mapper.OreMapper;
import cn.com.glsx.dj.smartcarlife.dto.DivisionRuleEffectRecordDTO; import cn.com.glsx.dj.smartcarlife.dto.DivisionRuleEffectRecordDTO;
import cn.com.glsx.dj.smartcarlife.model.DivisionRuleEffectRecord; import cn.com.glsx.dj.smartcarlife.model.DivisionRuleEffectRecord;
import java.util.Date;
import java.util.List;
@Mapper @Mapper
public interface SmartcarDivisionRuleEffectRecordMapper extends OreMapper<DivisionRuleEffectRecord> { public interface SmartcarDivisionRuleEffectRecordMapper extends OreMapper<DivisionRuleEffectRecord> {
List<DivisionRuleEffectRecord> list(DivisionRuleEffectRecordDTO condition); List<DivisionRuleEffectRecord> list(DivisionRuleEffectRecordDTO condition);
int batchCloseByIds(@Param("ids") List<Integer> ids,
@Param("effectEndDate") Date effectEndDate,
@Param("updatedBy") String updatedBy,
@Param("updatedDate") Date updatedDate);
} }
...@@ -127,4 +127,18 @@ public class GoodsListingMerchantRefService { ...@@ -127,4 +127,18 @@ public class GoodsListingMerchantRefService {
} }
} }
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public void updateRuleIdsBatch(List<GoodsListingMerchantRef> updateList, String updatedBy, Date updatedDate) {
if (CollectionUtils.isEmpty(updateList)) {
return;
}
RpcAssert.assertNotEmpty(updatedBy, DefaultErrorEnum.DATA_NULL, "updatedBy不能为空");
try {
goodsListingMerchantRefMapper.updateRuleIdsBatch(updateList, updatedBy, updatedDate == null ? new Date() : updatedDate);
}catch (Exception e){
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
} }
...@@ -60,4 +60,16 @@ ...@@ -60,4 +60,16 @@
</where> </where>
order by er.id desc order by er.id desc
</select> </select>
<update id="batchCloseByIds">
update dj_car_life_division_rule_effect_record
set effect_end_date = #{effectEndDate},
updated_by = #{updatedBy},
updated_date = #{updatedDate}
where deleted_flag = 'N'
and id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update>
</mapper> </mapper>
...@@ -77,4 +77,31 @@ ...@@ -77,4 +77,31 @@
</if> </if>
</where> </where>
</select> </select>
<update id="updateRuleIdsBatch">
update dj_car_life_goods_listing_merchant_ref
set
settle_rule_id = case id
<foreach collection="list" item="item">
when #{item.id} then #{item.settleRuleId,jdbcType=INTEGER}
</foreach>
end,
division_rule_id = case id
<foreach collection="list" item="item">
when #{item.id} then #{item.divisionRuleId,jdbcType=INTEGER}
</foreach>
end,
reconciliation_rule_id = case id
<foreach collection="list" item="item">
when #{item.id} then #{item.reconciliationRuleId,jdbcType=INTEGER}
</foreach>
end,
updated_by = #{updatedBy},
updated_date = #{updatedDate}
where deleted_flag = 'N'
and id in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item.id}
</foreach>
</update>
</mapper> </mapper>
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的专业审查报告。本次修改将原有的循环单条数据库操作(O(N))优化为批量操作(O(1)),并引入了内存 Map 替代嵌套遍历,极大地提升了系统性能,是一次非常优秀的代码重构。

    以下是代码中存在的几个需要注意的关键问题及优化建议:

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

    1. 批量操作未进行分片(Chunking)处理,存在大事务和 SQL 报错风险(重要)

    • 问题描述:在 GoodsListingApprovalService 中,batchCloseByIds、insertList 以及 updateRuleIdsBatch 均是直接将整个 List 传入 MyBatis 进行批量操作。
    • 影响分析:如果 merchantRefs 或 pendingRuleEffectRecords 的数量较大(例如超过 1000 条),生成的 SQL 语句会非常长,不仅会导致数据库执行大事务锁表,还极易触发 MySQL 的 max_allowed_packet 限制或 Prepared statement contains too many placeholders 异常,导致整个审核发布流程崩溃。
    • 优化建议:在 Java 服务层调用批量 SQL 之前,使用工具类(如 Guava 的 Lists.partition 或 Hutool 的 ListUtil.partition)将大集合拆分为小批次(建议每批 500 条)进行处理。 代码示例: java // 示例:对批量更新进行分片 if (CollectionUtils.isNotEmpty(updateList)) { List<List<GoodsListingMerchantRef>> partitions = Lists.partition(updateList, 500); for (List<GoodsListingMerchantRef> partition : partitions) { goodsListingMerchantRefService.updateRuleIdsBatch(partition, context.updatedBy, context.now); } }

    2. MyBatis CASE WHEN 批量更新缺少 ELSE 兜底(健壮性)

    • 问题描述:GoodsListingMerchantRefMapper.xml 中的 updateRuleIdsBatch 使用了 CASE WHEN ... THEN ... END 语法,但没有编写 ELSE 分支。
    • 影响分析:虽然当前 SQL 末尾的 WHERE id IN (...) 严格限制了更新范围,保证了 CASE WHEN 能完全匹配。但作为防御性编程的最佳实践,如果未来代码变动导致 IN 条件里的 ID 多于 CASE 节点里列出的 ID,那么多出来的记录对应的字段将会被错误地更新为 NULL,造成严重的数据破坏。
    • 优化建议:为每个 CASE 语句增加 ELSE 原字段名的兜底逻辑。 代码示例: xml settle_rule_id = case id <foreach collection="list" item="item"> when #{item.id} then #{item.settleRuleId,jdbcType=INTEGER} </foreach> else settle_rule_id <!-- 增加 ELSE 兜底,防止意外置空 --> end,

    3. 异常处理中丢失了原始异常堆栈(最佳实践)

    • 问题描述:在 GoodsListingMerchantRefService.java 的 updateRuleIdsBatch 方法中,捕获了 Exception 并抛出了自定义的 RpcServiceException,但没有将原始异常 e 作为 cause 传入。
    • 影响分析:虽然使用了 log.error 打印了日志,但在微服务架构中,调用链路监控工具(如 SkyWalking、Cat 等)或全局异常处理器只能捕获到顶层的 RpcServiceException,丢失了底层的真实原因(如 SQL 语法错误、死锁等),增加了排查线上问题的难度。
    • 优化建议:在抛出自定义异常时,保留原始异常的上下文。 代码示例: java } catch (Exception e) { log.error("批量更新商户规则ID失败: {}", e.getMessage(), e); // 假设 RpcServiceException 支持传入 cause,若不支持建议扩展该构造方法 throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, e); }

    二、评分明细

    • 功能实现的正确性与健壮性 (36/40分): 业务逻辑重构正确,正确使用了 Map 缓存和批量处理。特别表扬在执行批量 SQL 前都进行了 CollectionUtils.isEmpty 校验。扣分项在于未对大批量 List 进行分片(Partition)处理,在数据量突增时存在健壮性隐患;同时已确认 未发现 MyBatis 子查询 及 Dubbo 接口参数变更。
    • 安全性与潜在风险 (30/30分): SQL 拼接均规范使用了 #{} 占位符,无 SQL 注入风险;无越权及敏感信息泄露问题。
    • 最佳实践与可维护性 (17/20分): 将循环查库/写库优化为批量操作,极大地提升了代码可维护性。扣分项为 CASE WHEN 缺少 ELSE 保护机制,以及 Service 层抛出异常时吞掉了原始堆栈。
    • 性能与资源利用 (5/5分): 满分。本次提交的核心亮点即为性能优化,消除了 O(N) 的 N+1 查询与更新问题,大幅降低了数据库连接占用和网络 I/O 开销。
    • 提交信息质量 (4/5分): Commit 信息("优化审核通过后代码批量插入逻辑sql")清晰表达了修改目的。若能进一步简述优化的具体模块(如:商户规则、生效记录)会更加完美。

    总分:92分

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