Commit be9898b6 by chenxi

fix(stock): 修正库存扣减逻辑并新增退料入库库存类型

- 新增GoodsWarehousingInfo实体字段inventoryType,标识库存管控模式(虚拟/实物)
- 退料入库时记录库存管控模式,并据此区别处理虚拟库存跳过实际增库存
- 修改库存扣减调用方式,统一使用StockUpdateTypeEnum.REDUCE替代REDUCE_ONLY_ENOUGH
- GoodsWarehousingInfoMapper映射文件同步新增inventory_type字段支持
- 日志中新增库存管控模式输出,便于退料入库流程跟踪
- 优化相关服务代码,确保库存管控模式逻辑正确执行
1 parent 02598209
...@@ -172,4 +172,11 @@ public class GoodsWarehousingInfo implements Serializable { ...@@ -172,4 +172,11 @@ public class GoodsWarehousingInfo implements Serializable {
*/ */
@Column(name = "updated_user_name") @Column(name = "updated_user_name")
private String updatedUserName; private String updatedUserName;
/**
* 库存管控模式 1-虚拟、2-实物(仅退料入库时记录,其他类型为空)
*/
@Column(name = "inventory_type")
@ApiModelProperty(notes = "库存管控模式 1-虚拟、2-实物")
private Byte inventoryType;
} }
\ No newline at end of file \ No newline at end of file
...@@ -4703,7 +4703,10 @@ public class GoodsOrderService { ...@@ -4703,7 +4703,10 @@ public class GoodsOrderService {
.productCode(actualUpgradeGoods != null ? actualUpgradeGoods.getType() : null).increaseStockNum(detail.getIncreaseStockNum()) .productCode(actualUpgradeGoods != null ? actualUpgradeGoods.getType() : null).increaseStockNum(detail.getIncreaseStockNum())
.filmRollNo(detail.getFilmRollNo()) .filmRollNo(detail.getFilmRollNo())
.updatedUserName(dto.getOperator()) .updatedUserName(dto.getOperator())
.inventoryType(matchedReduceList.get(0).getInventoryType())
.upgradeFlag(orderDTO.getUpgradeFlag()).updatedBy(CommonConstants.DEFAULT_UPDATE_BY).build(); .upgradeFlag(orderDTO.getUpgradeFlag()).updatedBy(CommonConstants.DEFAULT_UPDATE_BY).build();
log.info("退料入库,库存管控模式 inventoryType:{}, upgradeGoodsId:{}, orderCode:{}",
matchedReduceList.get(0).getInventoryType(), actualUpgradeGoodsId, dto.getOrderCode());
goodsWarehousingInfoService.addGoodsWarehousingInfo(info); goodsWarehousingInfoService.addGoodsWarehousingInfo(info);
} }
} catch (Exception e) { } catch (Exception e) {
......
...@@ -5,6 +5,7 @@ import cn.com.glsx.dj.smartcarlife.constant.CommonConstants; ...@@ -5,6 +5,7 @@ import cn.com.glsx.dj.smartcarlife.constant.CommonConstants;
import cn.com.glsx.dj.smartcarlife.converter.MerchantGoodsStockMonthReportDTOConvert; import cn.com.glsx.dj.smartcarlife.converter.MerchantGoodsStockMonthReportDTOConvert;
import cn.com.glsx.dj.smartcarlife.dto.*; import cn.com.glsx.dj.smartcarlife.dto.*;
import cn.com.glsx.dj.smartcarlife.enums.IncreaseStockTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.IncreaseStockTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.InventoryTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.SceneTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.SceneTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.StockShopTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.StockShopTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.StockUpdateTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.StockUpdateTypeEnum;
...@@ -85,6 +86,13 @@ public class GoodsWarehousingInfoService { ...@@ -85,6 +86,13 @@ public class GoodsWarehousingInfoService {
public Boolean addGoodsWarehousingInfo(GoodsWarehousingInfoDTO dto) { public Boolean addGoodsWarehousingInfo(GoodsWarehousingInfoDTO dto) {
try{ try{
thiz.checkForAdd(dto); thiz.checkForAdd(dto);
// 虚拟库存退料入库:跳过实际增加库存,仅写入库记录
if (Objects.equals(dto.getInventoryType(), InventoryTypeEnum.VIRTUAL.getValue())) {
log.info("退料入库-虚拟库存模式,跳过增库存,仅记录入库流水,upgradeGoodsId:{}, businessCode:{}",
dto.getUpgradeGoodsId(), dto.getBusinessCode());
thiz.save2DB(dto);
return true;
}
MerchantGoodsStockMonthReportDTO monthReport = MerchantGoodsStockMonthReportDTOConvert.INSTANCE.fromGoodsWarehousingInfoDTO(dto); MerchantGoodsStockMonthReportDTO monthReport = MerchantGoodsStockMonthReportDTOConvert.INSTANCE.fromGoodsWarehousingInfoDTO(dto);
if (IncreaseStockTypeEnum.PURCHASE.getValue() == dto.getIncreaseStockType() || if (IncreaseStockTypeEnum.PURCHASE.getValue() == dto.getIncreaseStockType() ||
IncreaseStockTypeEnum.SELF_PURCHASE.getValue() == dto.getIncreaseStockType()) { IncreaseStockTypeEnum.SELF_PURCHASE.getValue() == dto.getIncreaseStockType()) {
......
...@@ -336,7 +336,7 @@ public class PurchaseOrderDeliveryService { ...@@ -336,7 +336,7 @@ public class PurchaseOrderDeliveryService {
.reduceStockNum(detailDTO.getPurchaseNumber()).updatedBy(record.getUpdatedBy()) .reduceStockNum(detailDTO.getPurchaseNumber()).updatedBy(record.getUpdatedBy())
.updatedUserName(record.getOperatorName()) .updatedUserName(record.getOperatorName())
.build(); .build();
merchantGoodsStockReduceRecordService.reduceStock(stockReduceRecord, StockUpdateTypeEnum.REDUCE_ONLY_ENOUGH, cn.com.glsx.dj.smartcarlife.enums.InventoryTypeEnum.PHYSICAL.getValue()); merchantGoodsStockReduceRecordService.reduceStock(stockReduceRecord, StockUpdateTypeEnum.REDUCE, cn.com.glsx.dj.smartcarlife.enums.InventoryTypeEnum.PHYSICAL.getValue());
} catch (RpcServiceException e) { } catch (RpcServiceException e) {
if (Objects.equals(e.getMessage(), CommonConstants.STOCK_INSUFFICIENT)) { if (Objects.equals(e.getMessage(), CommonConstants.STOCK_INSUFFICIENT)) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, String.format("商品:%s库存不足,无法发货", allocateOrderGoods.getGoodsName())); throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, String.format("商品:%s库存不足,无法发货", allocateOrderGoods.getGoodsName()));
......
...@@ -177,7 +177,7 @@ public class StockCheckRecordService { ...@@ -177,7 +177,7 @@ public class StockCheckRecordService {
.inventoryType(InventoryTypeEnum.PHYSICAL.getValue()) .inventoryType(InventoryTypeEnum.PHYSICAL.getValue())
.updatedUserName(record.getOperator()) .updatedUserName(record.getOperator())
.build(); .build();
merchantGoodsStockReduceRecordService.reduceStock(stockReduceRecord, StockUpdateTypeEnum.REDUCE_ONLY_ENOUGH, cn.com.glsx.dj.smartcarlife.enums.InventoryTypeEnum.PHYSICAL.getValue()); merchantGoodsStockReduceRecordService.reduceStock(stockReduceRecord, StockUpdateTypeEnum.REDUCE, cn.com.glsx.dj.smartcarlife.enums.InventoryTypeEnum.PHYSICAL.getValue());
} }
if (BigDecimal.ZERO.compareTo(profitAndLoss) < 0) { if (BigDecimal.ZERO.compareTo(profitAndLoss) < 0) {
//盘盈,进行入库操作,增加库存,写入入库记录 //盘盈,进行入库操作,增加库存,写入入库记录
......
...@@ -224,7 +224,7 @@ public class StockRefundRecordService { ...@@ -224,7 +224,7 @@ public class StockRefundRecordService {
.filmRollNo(detail.getFilmRollNo()) .filmRollNo(detail.getFilmRollNo())
.updatedUserName(record.getUpdatedBy()) .updatedUserName(record.getUpdatedBy())
.build(); .build();
merchantGoodsStockReduceRecordService.reduceStock(stockReduceRecord, StockUpdateTypeEnum.REDUCE_ONLY_ENOUGH, cn.com.glsx.dj.smartcarlife.enums.InventoryTypeEnum.PHYSICAL.getValue()); merchantGoodsStockReduceRecordService.reduceStock(stockReduceRecord, StockUpdateTypeEnum.REDUCE, cn.com.glsx.dj.smartcarlife.enums.InventoryTypeEnum.PHYSICAL.getValue());
} }
} }
if(Objects.equals(status,RefundStatus.REJECT_PENDING_SUBMISSION)) { if(Objects.equals(status,RefundStatus.REJECT_PENDING_SUBMISSION)) {
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
<result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate" /> <result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate" />
<result column="deleted_flag" jdbcType="CHAR" property="deletedFlag" /> <result column="deleted_flag" jdbcType="CHAR" property="deletedFlag" />
<result column="updated_user_name" property="updatedUserName" jdbcType="VARCHAR" /> <result column="updated_user_name" property="updatedUserName" jdbcType="VARCHAR" />
<result column="inventory_type" jdbcType="TINYINT" property="inventoryType" />
</resultMap> </resultMap>
<sql id="Base_Column_List"> <sql id="Base_Column_List">
<!-- <!--
...@@ -37,7 +38,7 @@ ...@@ -37,7 +38,7 @@
--> -->
id, upgrade_goods_id, increase_time, increase_stock_num, increase_stock_money, increase_stock_type, business_code, id, upgrade_goods_id, increase_time, increase_stock_num, increase_stock_money, increase_stock_type, business_code,
delivery_number, yq_merchant_id, yq_merchant_name, shop_type, merchant_id, merchant_name,purchase_order_goods_id,allocate_order_goods_id, delivery_number, yq_merchant_id, yq_merchant_name, shop_type, merchant_id, merchant_name,purchase_order_goods_id,allocate_order_goods_id,
created_by, created_date, updated_by, updated_date, deleted_flag, updated_user_name created_by, created_date, updated_by, updated_date, deleted_flag, updated_user_name, inventory_type
</sql> </sql>
<resultMap extends="BaseResultMap" id="DTOResultMap" type="cn.com.glsx.dj.smartcarlife.dto.GoodsWarehousingInfoDTO"> <resultMap extends="BaseResultMap" id="DTOResultMap" type="cn.com.glsx.dj.smartcarlife.dto.GoodsWarehousingInfoDTO">
...@@ -154,7 +155,7 @@ ...@@ -154,7 +155,7 @@
delivery_number, yq_merchant_id, yq_merchant_name, shop_type, dclgwi.merchant_id, merchant_name,dclgwi.upgrade_flag, dclgwi.delivery_detail_id, delivery_number, yq_merchant_id, yq_merchant_name, shop_type, dclgwi.merchant_id, merchant_name,dclgwi.upgrade_flag, dclgwi.delivery_detail_id,
dclgwi.purchase_order_goods_id,dclgwi.allocate_order_goods_id, dclgwi.purchase_order_goods_id,dclgwi.allocate_order_goods_id,
dclgwi.created_by, dclgwi.created_date, dclgwi.updated_by, dclgwi.updated_date, dclgwi.deleted_flag, dclgwi.updated_user_name, dclug.type as product_code, dclgwi.created_by, dclgwi.created_date, dclgwi.updated_by, dclgwi.updated_date, dclgwi.deleted_flag, dclgwi.updated_user_name, dclug.type as product_code,
dclug.specification_data,dclug.specification_type_sequence dclug.specification_data,dclug.specification_type_sequence, dclgwi.inventory_type
from dj_car_life_goods_warehousing_info dclgwi from dj_car_life_goods_warehousing_info dclgwi
left join dj_car_life_upgrade_goods dclug on dclgwi.upgrade_goods_id = dclug.id left join dj_car_life_upgrade_goods dclug on dclgwi.upgrade_goods_id = dclug.id
left join dj_car_life_goods_info dclgi on dclgi.deleted_flag = 'N' and dclgi.goods_code = dclug.goods_code left join dj_car_life_goods_info dclgi on dclgi.deleted_flag = 'N' and dclgi.goods_code = dclug.goods_code
......
  • @wangxw1
    commented
    Master

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

    1. 潜在的空指针与数组越界异常(功能正确性与健壮性)

    • 问题描述:在 GoodsOrderService.java 中,直接使用了 matchedReduceList.get(0).getInventoryType() 来获取库存类型并构建对象和打印日志。
    • 影响分析:如果 matchedReduceList 为 null 或空集合(size == 0),此处将直接抛出 NullPointerException 或 IndexOutOfBoundsException,导致退料入库流程中断,进而引发线上业务异常。
    • 优化建议:在获取元素前,必须对集合进行判空和大小校验,或者提供默认回退逻辑。 ```java // 建议在构建 info 之前提取变量并校验 Byte inventoryType = null; if (CollectionUtils.isNotEmpty(matchedReduceList) && matchedReduceList.get(0) != null) { inventoryType = matchedReduceList.get(0).getInventoryType(); }

    // 构建时传入 .inventoryType(inventoryType)

    
    **2. `Objects.equals` 潜在的类型不匹配导致逻辑失效(功能正确性)**
    - **问题描述**:在 `GoodsWarehousingInfoService.java` 中,使用了 `Objects.equals(dto.getInventoryType(), InventoryTypeEnum.VIRTUAL.getValue())` 来判断是否为虚拟库存。
    - **影响分析**:`GoodsWarehousingInfo` 中 `inventoryType` 的类型为 `Byte`。如果 `InventoryTypeEnum.VIRTUAL.getValue()` 返回的是 `Integer` 类型,由于 `Objects.equals` 内部会进行强类型比对(`Byte` 和 `Integer` 即使数值相同也会返回 `false`),这将导致虚拟库存的判断永远失效,错误地走入后续的实体库存增加逻辑。
    - **优化建议**:统一数据类型进行比较,建议将两边都转换为 `int` 或确保枚举返回的也是 `Byte`。
      ```java
      // 建议修改为值比较(需先判空)
      if (dto.getInventoryType() != null && 
          dto.getInventoryType().intValue() == InventoryTypeEnum.VIRTUAL.getValue().intValue()) {
          // ...
      }
    

    3. 库存扣减枚举变更带来的业务逻辑风险(健壮性)

    • 问题描述:在 PurchaseOrderDeliveryService 等多个类中,将库存扣减类型从 StockUpdateTypeEnum.REDUCE_ONLY_ENOUGH 修改为了 StockUpdateTypeEnum.REDUCE。
    • 影响分析:从命名推测,REDUCE_ONLY_ENOUGH 意味着“仅库存充足时扣减”,而 REDUCE 可能允许将库存扣减为负数。在 PurchaseOrderDeliveryService 的 catch 块中,强依赖了 CommonConstants.STOCK_INSUFFICIENT 异常来阻断发货。如果 REDUCE 模式在库存不足时不再抛出该异常,将导致超卖或发货逻辑错误。
    • 优化建议:请再次确认 merchantGoodsStockReduceRecordService.reduceStock 方法在使用 REDUCE 枚举时,是否依然能够正确拦截库存不足的情况并抛出预期的 STOCK_INSUFFICIENT 异常。如果逻辑已在底层统一,请在 PR 描述或注释中简单说明,防范回归测试遗漏。

    二、评分明细

    • 功能实现的正确性与健壮性(30/40分): 扣分主要在于 matchedReduceList.get(0) 缺乏边界保护,以及 Objects.equals 存在高风险的包装类类型不匹配问题,这两点极易在生产环境引发 Bug。代码中无 MyBatis 子查询。
    • 安全性与潜在风险(30/30分): 未发现 SQL 注入、越权、XSS 或敏感信息泄露等安全隐患。
    • 最佳实践与可维护性(17/20分): 新增了必要的日志记录,有助于问题排查,符合最佳实践。扣分点在于硬编码获取集合首个元素的习惯较差,降低了代码的健壮性和可维护性。
    • 性能与资源利用(5/5分): 虚拟库存直接跳过实际库存增加步骤,减少了不必要的数据库操作和计算,性能处理合理,无资源浪费。
    • 提交信息质量(5/5分): Commit Message 遵循了 Angular 规范(fix(stock): ...),且详细列出了修改点、修改原因以及影响范围,非常清晰且易于追溯。

    总分:87分

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