Commit b8c03445 by 崔彦峰

```

feat(goods): 新增商品上架审批参数验证功能

- 在商品确认步骤中添加虚拟库存校验
- 新增verifyParams方法验证商品列表参数
- 过滤upgrade_product_cost_style为2的数据进行特殊处理
- 检查service_item_id是否为空并抛出相应错误
- 添加商品名称到错误信息提示中
```
1 parent 65f37a76
...@@ -3092,10 +3092,13 @@ public class GoodsListingApprovalService { ...@@ -3092,10 +3092,13 @@ public class GoodsListingApprovalService {
//校验单店原上架id //校验单店原上架id
verifyOldMerchantGoodsCode(dto, goodsListingApproval, curStep); verifyOldMerchantGoodsCode(dto, goodsListingApproval, curStep);
// 校验虚拟库存
if (Objects.equals(dto.getNextStep(), Boolean.TRUE) && Objects.equals(curStep, ConfirmStep.GOODS)) { if (Objects.equals(dto.getNextStep(), Boolean.TRUE) && Objects.equals(curStep, ConfirmStep.GOODS)) {
// 校验虚拟库存
verifyVirtualInventory(dto.getBusinessCode()); verifyVirtualInventory(dto.getBusinessCode());
//校验参数服务项目
verifyParams(dto);
} }
ConfirmStep nextStep = null; ConfirmStep nextStep = null;
if (Objects.equals(goodsListingApproval.getShelfModel(),ShelfModelEnum.SPECIAL_SHELF.getValue())) { if (Objects.equals(goodsListingApproval.getShelfModel(),ShelfModelEnum.SPECIAL_SHELF.getValue())) {
if(Objects.equals(dto.getNextStep(),true)){ if(Objects.equals(dto.getNextStep(),true)){
...@@ -3191,6 +3194,27 @@ public class GoodsListingApprovalService { ...@@ -3191,6 +3194,27 @@ public class GoodsListingApprovalService {
} }
/** /**
* 验证参数
* @param dto
*/
private void verifyParams(GoodsListingApprovalDTO dto) {
List<GoodsListingProductDetailDTO> goodsListingProductDetailDTOS = goodsListingProductDetailService.listByBusinessCode(dto.getBusinessCode());
if (CollectionUtils.isNotEmpty(goodsListingProductDetailDTOS)) {
//过滤upgrade_product_cost_style 为2 的数据
List<GoodsListingProductDetailDTO> upgradeProductCostStyle2List =
goodsListingProductDetailDTOS.stream().filter(detail -> Objects.equals(detail.getUpgradeProductCostStyle().intValue(), 2)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(upgradeProductCostStyle2List)) {
//筛选service_item_id为空的第一条
GoodsListingProductDetailDTO upgradeProductCostStyle2 = upgradeProductCostStyle2List.stream()
.filter(detail -> Objects.equals(detail.getServiceItemId(), null)).findFirst().orElse(null);
if (upgradeProductCostStyle2 != null) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "商品:" + upgradeProductCostStyle2.getUpgradeGoodsName() + "服务项目不能为空");
}
}
}
}
/**
* 验证单店上架id * 验证单店上架id
* @param dto * @param dto
* @param goodsListingApproval * @param goodsListingApproval
......
  • @wangxw1
    commented
    Master

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

    1. 潜在的空指针异常(NPE)风险

    • 问题描述:在 verifyParams 方法的 Stream 过滤中,使用了 detail.getUpgradeProductCostStyle().intValue()。如果 DTO 中的 UpgradeProductCostStyle 字段为 null,直接调用 .intValue() 会抛出 NullPointerException。
    • 影响分析:会导致正常的商品上架审批流程直接中断并抛出系统异常,严重影响系统的健壮性和用户体验。
    • 优化建议:去除不必要的 .intValue() 拆箱操作,直接利用 Objects.equals 比较包装类,或者提前进行判空。 java // 建议修改为: Objects.equals(detail.getUpgradeProductCostStyle(), 2) // 或 Integer.valueOf(2).equals(detail.getUpgradeProductCostStyle())

    2. Stream API 使用繁琐与中间集合浪费

    • 问题描述:当前代码在验证逻辑中,先通过 Stream 过滤出 upgradeProductCostStyle == 2 的数据并收集为 List,判断非空后,又对该 List 开启第二次 Stream 进行 serviceItemId 的判空筛选。
    • 影响分析:代码显得臃肿,且产生不必要的中间集合(upgradeProductCostStyle2List),虽然在数据量小的情况下性能损耗可忽略,但不符合工程最佳实践。
    • 优化建议:将两次过滤条件合并,一次 Stream 遍历即可完成校验,提升代码可读性与执行效率。

      // 优化后的代码示例:
      private void verifyParams(GoodsListingApprovalDTO dto) {
        List<GoodsListingProductDetailDTO> details = goodsListingProductDetailService.listByBusinessCode(dto.getBusinessCode());
        if (CollectionUtils.isEmpty(details)) {
            return;
        }
      
        GoodsListingProductDetailDTO invalidDetail = details.stream()
                .filter(detail -> Objects.equals(detail.getUpgradeProductCostStyle(), 2))
                .filter(detail -> Objects.equals(detail.getServiceItemId(), null))
                .findFirst()
                .orElse(null);
      
        if (invalidDetail != null) {
            throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "商品:" + invalidDetail.getUpgradeGoodsName() + " 的服务项目不能为空");
        }
      }
      

    3. 异常类型定义不准确(语义问题)

    • 问题描述:当校验发现“服务项目为空”时,抛出的异常错误码为 DefaultErrorEnum.SERVER_ERROR(通常对应 HTTP 500 或系统内部兜底错误)。
    • 影响分析:这是一个典型的“业务校验/参数错误”,抛出 SERVER_ERROR 会导致调用方无法区分是系统崩溃还是参数不合法,同时也容易在监控系统中触发不必要的服务端错误告警。
    • 优化建议:建议将 DefaultErrorEnum.SERVER_ERROR 替换为代表“参数错误”或“业务逻辑错误”的枚举(如 PARAM_ERROR 或 BUSINESS_ERROR 等,具体视项目中已有的错误码定义而定)。

    二、评分明细

    • 功能实现的正确性与健壮性(32/40分): 扣除8分。主要原因是代码中存在明显的拆箱空指针风险(NPE),在处理 DTO 数据时未能充分考虑字段可能为 null 的边界情况。

    • 安全性与潜在风险(30/30分): 满分。此次变更属于纯业务逻辑的参数校验,未引入 SQL 注入、越权等安全漏洞,且未涉及 Dubbo 接口签名的危险变更及 MyBatis 子查询。

    • 最佳实践与可维护性(15/20分): 扣除5分。Stream API 的使用不够精简,导致了代码层级过深(多次 if 嵌套)和中间变量的浪费;异常枚举的使用不符合业务语义。

    • 性能与资源利用(4/5分): 扣除1分。创建了不必要的中间 List 集合,虽然对整体性能影响微乎其微,但存在优化空间。

    • 提交信息质量(5/5分): 满分。Commit message 采用了标准的 feat: 前缀,且通过列表清晰地描述了本次提交的具体改动点和目的,具备良好的可追溯性。

    总分:86分

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