Commit 9af95e14 by chenxi

feat(stock): 支持退料入库按原始商品ID匹配出库记录

- 在MerchantGoodsStockReduceRecord实体中新增orderUpgradeGoodsId字段,记录订单原始商品ID
- 在Mapper映射文件中新增order_upgrade_goods_id字段映射
- 调整GoodsOrderService退料入库逻辑,优先使用orderUpgradeGoodsId匹配出库记录,兼容老数据按upgradeGoodsId匹配
- 记录退料入库日志时增加订单原始商品ID和实际出库商品ID对比信息
- 确保入库使用实际出库商品的信息进行库存增加操作
1 parent b2cc7304
......@@ -188,4 +188,11 @@ public class MerchantGoodsStockReduceRecord implements Serializable {
@Column(name = "store_usage")
private String storeUsage;
/**
* 订单原始商品ID(chooseMerchantByType 预选前,订单中实际下单的商品upgradeGoodsId)
* 退料入库时按此字段匹配出库记录
*/
@Column(name = "order_upgrade_goods_id")
private Integer orderUpgradeGoodsId;
}
\ No newline at end of file
......@@ -2291,6 +2291,7 @@ public class GoodsOrderService {
.storeUsage(orderDTO.getMerchantName()).filmRollNo(null)
.technicianName(detail.getTechnicianName()).technicianPhone(detail.getTechnicianPhone())
.orderMerchantId(orderDTO.getMerchantId()).orderMerchantName(orderDTO.getMerchantName())
.orderUpgradeGoodsId(detail.getUpgradeGoodsId()) // 记录订单原始商品ID,退料入库时匹配用
.updatedBy(CommonConstants.DEFAULT_UPDATE_BY).reduceDate(dto.getReduceDate())
.updatedUserName(dto.getOperatorName())
.build();
......@@ -4670,20 +4671,36 @@ public class GoodsOrderService {
for(RefundWarehousingGoods detail:dto.getGoodsList()) {
UpgradeGoods upgradeGoods = upgradeGoodsService.getById(detail.getUpgradeGoodsId());
List<MerchantGoodsStockReduceRecordDTO> matchedReduceList = merchantGoodsStockReduceRecordList.stream().filter(x -> Objects.equals(x.getUpgradeGoodsId(), detail.getUpgradeGoodsId())).collect(Collectors.toList());
// 按订单原始商品ID匹配出库记录:
// 如果出库记录有 orderUpgradeGoodsId,用 orderUpgradeGoodsId 匹配(实际出库可能是 B,匹配键是订单原始 A);
// 否则向后兼容,按 upgradeGoodsId 匹配(老数据无 orderUpgradeGoodsId 字段)
List<MerchantGoodsStockReduceRecordDTO> matchedReduceList = merchantGoodsStockReduceRecordList.stream()
.filter(x -> {
if (x.getOrderUpgradeGoodsId() != null) {
return Objects.equals(x.getOrderUpgradeGoodsId(), detail.getUpgradeGoodsId());
} else {
return Objects.equals(x.getUpgradeGoodsId(), detail.getUpgradeGoodsId());
}
}).collect(Collectors.toList());
if (CollectionUtils.isEmpty(matchedReduceList)) {
log.info("退料入库,商品不存在出库记录,orderCode:{}, upgradeGoodsId:{}", dto.getOrderCode(), detail.getUpgradeGoodsId());
return;
} else {
log.info("退料入库,商品存在出库记录,orderCode:{}, upgradeGoodsId:{},", dto.getOrderCode(), matchedReduceList.get(0).getUpgradeGoodsId());
log.info("退料入库,商品存在出库记录,orderCode:{}, 订单原始商品upgradeGoodsId:{}, 实际出库商品upgradeGoodsId:{}",
dto.getOrderCode(), detail.getUpgradeGoodsId(), matchedReduceList.get(0).getUpgradeGoodsId());
}
// 实际出库商品(可能经过 chooseMerchantByType 预选后已从 A 改为 B)
Integer actualUpgradeGoodsId = matchedReduceList.get(0).getUpgradeGoodsId();
UpgradeGoods actualUpgradeGoods = Objects.equals(actualUpgradeGoodsId, detail.getUpgradeGoodsId())
? upgradeGoods : upgradeGoodsService.getById(actualUpgradeGoodsId);
GoodsWarehousingInfoDTO info = GoodsWarehousingInfoDTO.builder()
.yqMerchantId(matchedReduceList.get(0).getYqMerchantId()).yqMerchantName(matchedReduceList.get(0).getYqMerchantName())
.merchantId(matchedReduceList.get(0).getMerchantId()).merchantName(matchedReduceList.get(0).getMerchantName())
.businessCode(dto.getOrderCode()).upgradeGoodsId(detail.getUpgradeGoodsId())
.businessCode(dto.getOrderCode()).upgradeGoodsId(actualUpgradeGoodsId)
.increaseTime(new Date()).increaseStockType(IncreaseStockTypeEnum.ORDER.getValue())
.productCode(upgradeGoods.getType()).increaseStockNum(detail.getIncreaseStockNum())
.productCode(actualUpgradeGoods != null ? actualUpgradeGoods.getType() : null).increaseStockNum(detail.getIncreaseStockNum())
.filmRollNo(detail.getFilmRollNo())
.updatedUserName(dto.getOperator())
.upgradeFlag(orderDTO.getUpgradeFlag()).updatedBy(CommonConstants.DEFAULT_UPDATE_BY).build();
......
......@@ -31,6 +31,7 @@
<result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate" />
<result column="deleted_flag" jdbcType="CHAR" property="deletedFlag" />
<result column="store_usage" jdbcType="VARCHAR" property="storeUsage" />
<result column="order_upgrade_goods_id" jdbcType="INTEGER" property="orderUpgradeGoodsId" />
</resultMap>
<resultMap extends="BaseResultMap" id="DTOResultMap" type="cn.com.glsx.dj.smartcarlife.dto.MerchantGoodsStockReduceRecordDTO">
<result column="upgrade_goods_name" jdbcType="VARCHAR" property="upgradeGoodsName" />
......@@ -124,7 +125,7 @@
reduce_stock_money, business_code,dclmgsrr.upgrade_flag, dclmgsrr.film_roll_no,
event_type, dclmgsrr.technician_phone,dclmgsrr.technician_name,dclmgsrr.remark, dclmgsrr.inventory_type,
dclmgsrr.created_by, dclmgsrr.created_date, dclmgsrr.updated_by,dclmgsrr.updated_user_name, dclmgsrr.updated_date,
dclmgsrr.deleted_flag, dclmgsrr.store_usage, dclug.upgrade_goods_name, dclug.type as product_code,
dclmgsrr.deleted_flag, dclmgsrr.store_usage, dclmgsrr.order_upgrade_goods_id, dclug.upgrade_goods_name, dclug.type as product_code,
dclug.specification_data,dclug.specification_type_sequence, dclmgsrr.inventory_type
from dj_car_life_merchant_goods_stock_reduce_record dclmgsrr
left join dj_car_life_upgrade_goods dclug on dclmgsrr.upgrade_goods_id = dclug.id
......
  • @wangxw1
    commented
    Master

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

    1. 循环内使用 return 导致批量操作意外中断(功能实现的正确性与健壮性)

    • 问题描述:在 GoodsOrderService.java 的 for(RefundWarehousingGoods detail:dto.getGoodsList()) 循环中,当未匹配到出库记录时,代码执行了 return;。
    • 影响分析:这是一个严重的逻辑隐患(虽然可能是历史遗留代码,但在此次重构中应予以修复)。如果 goodsList 中有多个商品,其中某个商品未找到出库记录,return 会直接静默退出整个方法。这会导致同一批次中后续正常的商品无法被退料入库,且不会抛出异常触发事务回滚,造成数据不一致。
    • 优化建议:根据业务需求,如果是允许部分成功,应将 return; 改为 continue;;如果是要求强一致性,应抛出自定义业务异常(如 BusinessException)以回滚事务并提示前端。 java if (CollectionUtils.isEmpty(matchedReduceList)) { log.info("退料入库,商品不存在出库记录,orderCode:{}, upgradeGoodsId:{}", dto.getOrderCode(), detail.getUpgradeGoodsId()); // 建议改为 continue 或抛出异常 continue; }

    2. 循环内执行数据库查询导致 N+1 性能问题(性能与资源利用)

    • 问题描述:在 GoodsOrderService 的 for 循环内部,调用了 upgradeGoodsService.getById(...)。并且在后续判断中,如果实际出库商品 ID 与原始 ID 不一致,可能还会再次调用 getById。
    • 影响分析:如果退料的商品列表(goodsList)较长,会导致在循环中发起大量的数据库单条查询请求(N+1 问题),严重拖慢接口响应速度,并增加数据库连接压力。
    • 优化建议:在循环外提前将所有涉及的 upgradeGoodsId 收集起来,进行一次批量查询,并转换为 Map 以供循环内进行 O(1) 复杂度的内存查找。 ```java // 循环前批量查询(示例) Set goodsIds = dto.getGoodsList().stream().map(RefundWarehousingGoods::getUpgradeGoodsId).collect(Collectors.toSet()); // 若存在 orderUpgradeGoodsId 不同,也一并收集 merchantGoodsStockReduceRecordList.forEach(r -> goodsIds.add(r.getUpgradeGoodsId()));

    Map goodsMap = upgradeGoodsService.listByIds(goodsIds).stream() .collect(Collectors.toMap(UpgradeGoods::getId, Function.identity()));

    // 循环内部获取: UpgradeGoods upgradeGoods = goodsMap.get(detail.getUpgradeGoodsId()); UpgradeGoods actualUpgradeGoods = goodsMap.get(actualUpgradeGoodsId);

    
    **3. 集合 `.get(0)` 的硬编码使用存在隐患(最佳实践与可维护性)**
    - **问题描述**:代码在过滤出 `matchedReduceList` 后,直接使用 `matchedReduceList.get(0)` 获取出库记录。
    - **影响分析**:如果同一个订单内存在多个相同 `upgradeGoodsId` 的商品(导致有多条出库记录),使用 `.get(0)` 只能获取到第一条记录,这可能会导致库存数量对账不准或商品属性错乱。
    - **优化建议**:如果业务上能绝对保证 `orderCode` + `upgradeGoodsId` 唯一,这种写法勉强可行但不够健壮。建议增加对 `matchedReduceList.size() > 1` 的日志告警;或者在匹配逻辑中加入更精确的匹配条件(如明细 ID 等),确保一对一精准匹配。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(32/40分)**
      - **理由**:本次新增 `orderUpgradeGoodsId` 字段以支持原始订单商品溯源的思路清晰,兼容老数据的逻辑也处理得当。扣分项在于修改的代码块周边存在循环内直接 `return` 的严重健壮性缺陷,未在本次变更中顺手修复。
    - **安全性与潜在风险(30/30分)**
      - **理由**:未引入新的 SQL 注入、越权等安全漏洞。MyBatis 的 Mapper 映射和实体类更新规范安全。
    - **最佳实践与可维护性(16/20分)**
      - **理由**:代码可读性较好,实体类和复杂逻辑处补充了清晰的注释。扣分项在于集合操作直接依赖 `.get(0)`,不够严谨。
    - **性能与资源利用(2/5分)**
      - **理由**:在 `for` 循环中嵌套了数据库查询方法 `getById`,存在明显的 N+1 性能瓶颈。
    - **提交信息质量(5/5分)**
      - **理由**:Commit message 非常优秀。采用了标准的 Angular 规范(`feat(stock): ...`),详细列出了变更点、实现逻辑以及向下兼容策略,具备极高的可追溯性。
    
    总分:85分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!