Commit 19036d50 by 崔彦峰

refactor(goods): 优化商品上架单店修改更新逻辑

- 替换 updateByPrimaryKeySelective 为专用的 updateListingInPlace 方法
- 实现商品信息原地更新功能,支持将Excel清空的字段同步置为null
- 实现套餐信息原地更新功能,支持将套餐别名等字段置空
- 添加专用SQL映射器方法,只更新白名单字段
- 移除不再使用的 createdBy 和 createdDate 字段设置
- 更新数据访问层接口以支持新的更新策略
1 parent 916ba7c2
......@@ -6,4 +6,10 @@ import org.oreframework.datasource.mybatis.mapper.OreMapper;
@Mapper
public interface SmartcarMerchantGoodsInfoMapper extends OreMapper<MerchantGoodsInfo> {
/**
* 商品上架单店修改原地更新专用。
* 不能使用updateByPrimaryKeySelective,否则封装对象中需要清空的字段会被跳过。
*/
int updateListingInPlace(MerchantGoodsInfo goodsInfo);
}
......@@ -6,4 +6,10 @@ import org.oreframework.datasource.mybatis.mapper.OreMapper;
@Mapper
public interface SmartcarMerchantPackageGoodsInfoMapper extends OreMapper<MerchantPackageGoodsInfo> {
/**
* 商品上架单店修改套餐原地更新专用。
* 固定更新指定字段,允许把模板中已清空的字段同步更新为null。
*/
int updateListingInPlace(MerchantPackageGoodsInfo packageInfo);
}
......@@ -918,9 +918,8 @@ public class GoodsListingApprovalService {
MerchantGoodsInfo goodsInfo = buildMerchantGoodsInfo(context, detail, null);
goodsInfo.setId(oldGoods.getId());
goodsInfo.setCreatedBy(null);
goodsInfo.setCreatedDate(null);
smartcarMerchantGoodsInfoMapper.updateByPrimaryKeySelective(goodsInfo);
// 单店修改原地更新需要允许把Excel已清空的字段同步置为null,不能使用selective更新。
smartcarMerchantGoodsInfoMapper.updateListingInPlace(goodsInfo);
touchOrInsertMerchantGoodsRefByGoodsId(oldGoods.getId(), null, currentMerchantRef, context);
upsertInventoryTypeRef(goodsInfo.getId(), null, detail.getInventoryType(), currentMerchantRef, context);
}
......@@ -991,9 +990,8 @@ public class GoodsListingApprovalService {
MerchantPackageGoodsInfo packageInfo = buildMerchantPackageGoodsInfo(context, product, realPackageCode);
packageInfo.setId(oldPackage.getId());
packageInfo.setCreatedBy(null);
packageInfo.setCreatedDate(null);
smartcarMerchantPackageGoodsInfoMapper.updateByPrimaryKeySelective(packageInfo);
// 套餐原地更新同样要允许把套餐别名等字段置空,使用专用SQL只更新白名单字段。
smartcarMerchantPackageGoodsInfoMapper.updateListingInPlace(packageInfo);
touchOrInsertMerchantGoodsRefByPackageCode(realPackageCode, currentMerchantRef, context);
List<MerchantGoodsRef> refs = new ArrayList<>();
......
  • @wangxw1
    commented
    Master

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

    1. 缺失 MyBatis SQL 实现文件及子查询合规性风险(极高优)

    • 问题描述:本次提交仅包含了 Mapper 接口方法 updateListingInPlace 的定义以及 Service 层的调用,但未提供对应的 MyBatis XML 映射文件(或 @Update 注解实现)。此外,根据审查规范,MyBatis 的 SQL 语句绝对不允许使用子查询。
    • 影响分析:如果对应的 SQL 实现未提交或遗漏,代码在运行时将抛出 org.apache.ibatis.binding.BindingException,导致核心的上架修改功能直接瘫痪。如果 SQL 中使用了子查询,将违反系统核心开发规范。
    • 优化建议:
      1. 确认对应的 XML 文件是否在其他 commit 中或被遗漏,需一并提交审查。
      2. 在编写 updateListingInPlace 的 SQL 时,请严格遵守“禁止使用子查询”的规则,确保仅对白名单字段进行直接的 UPDATE 操作。

    2. 审计字段(更新人/更新时间)的维护风险(中优)

    • 问题描述:原代码依赖 updateByPrimaryKeySelective,通常底层框架或拦截器会自动处理 updated_by 和 updated_date。现在改为自定义的 updateListingInPlace 方法,且移除了对 createdBy 等字段的手动置空处理。
    • 影响分析:如果自定义的 SQL 语句中没有显式更新 updated_by 和 updated_date,或者系统的 MyBatis 拦截器未能拦截到这个自定义方法名,会导致数据更新时丢失审计更新时间,影响后续的问题排查和数据追溯。
    • 优化建议: 请确保在对应的 XML SQL 实现中,强制更新审计字段,或者确认全局 MyBatis 拦截器能够兼容 updateListingInPlace 命名前缀并自动注入更新时间和更新人。 示例(假想的 XML 实现): xml <update id="updateListingInPlace"> UPDATE t_merchant_goods_info SET allow_null_field = #{allowNullField}, other_field = #{otherField}, updated_by = #{updatedBy}, updated_date = NOW() WHERE id = #{id} </update>

    3. 字段置空引发的数据表约束冲突风险(低优)

    • 问题描述:注释中明确提到“允许把模板中已清空的字段同步更新为null”。Service 层直接将构建好的对象传给 Mapper 进行覆盖更新。
    • 影响分析:如果 Excel 模板中清空的字段在数据库表结构中被定义为 NOT NULL,直接将其 UPDATE 为 NULL 将导致 SQL 异常(如 DataIntegrityViolationException),从而导致整个审批/修改事务回滚。
    • 优化建议: 在实现 updateListingInPlace 的专用 SQL 时,严格把控白名单字段。确保只把业务上确实允许为空(且数据库结构允许为空)的字段纳入更新范围。建议在 Service 层或构建对象时,增加对必填字段的兜底校验。

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分): 业务逻辑调整方向正确,解决了 Selective 更新无法置空字段的痛点。扣分原因:缺失 SQL 实现文件,无法验证是否违反了“禁止使用子查询”的红线规则,且存在潜在的数据库 NOT NULL 约束冲突风险。

    • 安全性与潜在风险(30/30分): 未见明显的 SQL 注入、越权或 XSS 风险(假设底层 SQL 参数绑定使用了 #)。未修改 Dubbo Remote 接口参数,符合安全规范。

    • 最佳实践与可维护性(18/20分): 代码意图清晰,Mapper 接口增加了详尽的 Javadoc 注释,解释了为什么不使用通用 Mapper 方法,非常有利于后期维护。扣分原因:自定义更新方法需要特别注意审计字段(更新时间)的维护,当前代码上下文中未体现这部分的保障。

    • 性能与资源利用(5/5分): 采用原地定向更新(In-place update),避免了先查后改的开销,性能表现良好。

    • 提交信息质量(5/5分): Commit message 格式极其规范,采用了 Conventional Commits 规范(refactor(goods):),并且分点清晰地描述了修改的内容和原因,可追溯性极强。

    总分:93分

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