Commit 5c599042 by chenxi

5.8.5 采购上架模块 采购价带入方式字段,修复code review发现的问题

1 parent 910ecaf8
...@@ -175,7 +175,7 @@ public class PurchaseGoodsInfoController { ...@@ -175,7 +175,7 @@ public class PurchaseGoodsInfoController {
} }
@ApiOperation("采购上架导入") @ApiOperation("采购上架导入")
@ServiceMethod(method = "dj.api.smartcarlife.purchaseGoodsInfo.import", needInSession = NeedInSessionType.YES, ignoreSign = IgnoreSignType.YES) @ServiceMethod(method = "dj.api.smartcarlife.purchaseGoodsInfo.import", needInSession = NeedInSessionType.YES)
public RopResponse<String> importProduct(SingleUploadRequest request) { public RopResponse<String> importProduct(SingleUploadRequest request) {
return purchaseGoodsInfoImportService.importExcel(request, PurchaseGoodsInfoImportVo.class); return purchaseGoodsInfoImportService.importExcel(request, PurchaseGoodsInfoImportVo.class);
} }
......
...@@ -146,11 +146,11 @@ public class PurchaseGoodsInfoImportService extends AbstractImportExcelTemplate< ...@@ -146,11 +146,11 @@ public class PurchaseGoodsInfoImportService extends AbstractImportExcelTemplate<
.purchaseCostType(costTypeEnum.getValue()) .purchaseCostType(costTypeEnum.getValue())
.merchantRefList(merchantRefList) .merchantRefList(merchantRefList)
.upgradeGoodsId(goods.getUpgradeGoodsList().get(0).getId()) .upgradeGoodsId(goods.getUpgradeGoodsList().get(0).getId())
.purchasePrice(Objects.isNull(importVo.getPurchasePrice()) ? null : new BigDecimal(importVo.getPurchasePrice())) .purchasePrice(StringUtils.isBlank(importVo.getPurchasePrice()) ? null : new BigDecimal(importVo.getPurchasePrice()))
.suitCarType(suitCarType.getValue()) .suitCarType(suitCarType.getValue())
.status(MerchantGoodsStatus.WAIT_AUDIT.getValue()) .status(MerchantGoodsStatus.WAIT_AUDIT.getValue())
.build(); .build();
if(ProductCostTypeEnum.CUSTOM.getValue() == costTypeEnum.getValue()) { if(PurchaseCostTypeEnum.CUSTOM.getValue() == costTypeEnum.getValue()) {
purchaseGoodsInfo.setPurchasePrice(new BigDecimal(importVo.getPurchasePrice())); purchaseGoodsInfo.setPurchasePrice(new BigDecimal(importVo.getPurchasePrice()));
} }
return RopResponse.success(purchaseGoodsInfo); return RopResponse.success(purchaseGoodsInfo);
......
  • @wangxw1
    commented
    Master

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

    1. [功能正确性/健壮性] BigDecimal 转换缺乏数字格式校验,存在 NumberFormatException 风险

    • 问题描述:在 PurchaseGoodsInfoImportService.java 中,StringUtils.isBlank(importVo.getPurchasePrice()) ? null : new BigDecimal(importVo.getPurchasePrice()) 仅校验了字符串是否为空。由于是 Excel 导入场景,如果用户输入了非数字字符(如 "12.3a" 或带有空格的数字),new BigDecimal(String) 将直接抛出 NumberFormatException,导致整个导入任务失败且无法给出友好的错误提示。
    • 优化建议:在转换前增加合法数字格式的校验(如正则表达式),或者在业务层统一捕获该异常并转换为友好的业务提示(如:“采购价格式不正确”)。

    2. [功能正确性/健壮性] if 代码块中存在 NullPointerException 风险及重复赋值

    • 问题描述:在 Builder 模式构建 purchaseGoodsInfo 时,已经对 purchasePrice 进行了安全的判空赋值。但在随后的 if(PurchaseCostTypeEnum.CUSTOM.getValue() == costTypeEnum.getValue()) 逻辑块中,再次执行了 purchaseGoodsInfo.setPurchasePrice(new BigDecimal(importVo.getPurchasePrice()));。此处没有进行判空操作! 如果类型为 CUSTOM 且 Excel 中该字段为空,将直接抛出异常。同时,这种重复赋值逻辑显得多余。
    • 优化建议:如果 Builder 中的逻辑已经足够,建议直接删除 if 块中的重复赋值;如果 CUSTOM 类型有特殊逻辑,必须在此处先进行 StringUtils.isBlank 判断。

    3. [可维护性/规范性] 包装类比较未使用 equals,违反阿里巴巴 Java 开发手册

    • 问题描述:PurchaseCostTypeEnum.CUSTOM.getValue() == costTypeEnum.getValue() 中使用了 == 进行比较。如果 getValue() 返回的是包装类型(如 Integer),当数值超出 -128 ~ 127 的缓存范围时,== 比较的是对象内存地址,必定返回 false,从而产生隐蔽的 Bug。
    • 优化建议:根据《阿里巴巴 Java 开发手册》规定,所有包装类对象之间值的比较,全部使用 equals 方法。应修改为:PurchaseCostTypeEnum.CUSTOM.getValue().equals(costTypeEnum.getValue())。

    二、评分明细

    评审项 权重 得分 扣分原因/加分项说明
    1. 功能正确性与健壮性 40分 20分 扣20分:Excel 导入场景下直接 new BigDecimal(String) 缺乏格式校验(扣10分);if 代码块中存在明显的未判空直接转换导致的异常风险及冗余逻辑(扣10分)。
    2. 安全性与潜在风险 30分 30分 满分:Controller 中移除了 ignoreSign = IgnoreSignType.YES,恢复了接口的签名校验,提升了接口安全性,这是一个很好的安全修复。
    3. 最佳实践与可维护性 20分 15分 扣5分:枚举的 value 比较使用了 == 而不是 equals,违反了阿里规约中关于包装类比较的强制规定。
    4. 性能与资源利用 5分 4分 扣1分:在 Builder 和 if 块中可能对同一个字符串进行两次 new BigDecimal() 对象的创建,造成轻微的资源浪费。
    5. 提交信息质量 5分 4分 扣1分:包含了模块、原因(修复 CR 问题),但未简要说明具体的技术变更点(如:恢复接口签名校验等),清晰度可进一步提升。

    总分:73分

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