Commit e2b14d2d by 周立明

refactor(goodsSubOrder): 调整子订单字段赋值逻辑

将普通测试订单的特殊字段处理逻辑提前,统一复用基础赋值后再做覆盖,优化代码结构
1 parent fade8ead
......@@ -235,8 +235,14 @@ public class GoodsSubOrderService {
subOrder.setUpgradeThumbUrl(goodsInfo.getThumbUrl());
subOrder.setSupplierCode(goodsInfo.getSupplierCode());
subOrder.setSupplierName(goodsInfo.getSupplierName());
subOrder.setUpgradeShopMinPrice(merchantGoodsInfo.getMinSellPrice());
subOrder.setVirtualAccountCost(merchantGoodsInfo.getVirtualAccountCost());
subOrder.setRecoveryCost(merchantGoodsInfo.getRecoveryCost());
if (Objects.equals(CarLifeEnum.OrderType.NORMAL_TEST.getValue(), subOrder.getOrderType())) {
subOrder.setUpgradePrice(Optional.ofNullable(subOrder.getTotalPrice()).orElse(BigDecimal.ZERO));
subOrder.setUpgradeShopMinPrice(BigDecimal.ZERO);
subOrder.setVirtualAccountCost(null);
subOrder.setRecoveryCost(null);
}else{
subOrder.setUpgradeShopPrice(merchantGoodsInfo.getUpgradeProductCost());
subOrder.setUpgradePrice(merchantGoodsInfo.getSellPrice() == null ? BigDecimal.ZERO:merchantGoodsInfo.getSellPrice());
......@@ -248,11 +254,8 @@ public class GoodsSubOrderService {
subOrder.setUpgradeRetailPrice(upgradeGoods.getRetailPrice());
subOrder.setUpgradeIncentiveType(upgradeGoods.getIncentiveType());
subOrder.setUpgradeIncentiveCost(upgradeGoods.getIncentiveCost());
subOrder.setUpgradeShopMinPrice(merchantGoodsInfo.getMinSellPrice());
subOrder.setUpgradeWarrantyPeriod(upgradeGoods.getWarrantyPeriod());
subOrder.setUpgradeThirdPartyCode(upgradeGoods.getThirdPartyCode());
subOrder.setVirtualAccountCost(merchantGoodsInfo.getVirtualAccountCost());
subOrder.setRecoveryCost(merchantGoodsInfo.getRecoveryCost());
subOrder.setUpgradeGoodsPosition(merchantGoodsInfo.getPosition());
subOrder.setConvertedValue(goodsInfo.getConvertedValue());
if (goodsInfo.getGoodsType() != null && merchantGoodsInfo.getPosition() != null) {
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 条件分支中的赋值逻辑不够优雅(先赋值后覆盖)

    • 问题描述:代码在 if-else 分支前统一对 UpgradeShopMinPrice、VirtualAccountCost 和 RecoveryCost 进行了赋值,随后在 if(普通测试订单)分支内又立即对这三个字段进行了覆盖重写。
    • 影响分析:当订单类型为 NORMAL_TEST 时,程序会先从 merchantGoodsInfo 对象中读取值并赋值给 subOrder,紧接着又将其覆盖为 BigDecimal.ZERO 和 null。这种“先赋值再覆盖”的做法不仅产生了无意义的取值与赋值开销(微小的性能损耗),在逻辑上也显得不够直观,降低了代码的可读性。
    • 优化建议:既然已经存在互斥的 if-else 分支,建议将默认的赋值逻辑直接放入 else 块中,使各个分支的逻辑相互独立且清晰,避免重复赋值。

    优化代码示例:

    // 移除 if 之前的这三行赋值
    if (Objects.equals(CarLifeEnum.OrderType.NORMAL_TEST.getValue(), subOrder.getOrderType())) {
        subOrder.setUpgradePrice(Optional.ofNullable(subOrder.getTotalPrice()).orElse(BigDecimal.ZERO));
        subOrder.setUpgradeShopMinPrice(BigDecimal.ZERO);
        subOrder.setVirtualAccountCost(null);
        subOrder.setRecoveryCost(null);
    } else {
        subOrder.setUpgradeShopPrice(merchantGoodsInfo.getUpgradeProductCost());
        subOrder.setUpgradePrice(merchantGoodsInfo.getSellPrice() == null ? BigDecimal.ZERO : merchantGoodsInfo.getSellPrice());
        // ... 其他原有的 else 内部逻辑 ...
    
        // 将常规赋值移入 else 分支
        subOrder.setUpgradeShopMinPrice(merchantGoodsInfo.getMinSellPrice());
        subOrder.setVirtualAccountCost(merchantGoodsInfo.getVirtualAccountCost());
        subOrder.setRecoveryCost(merchantGoodsInfo.getRecoveryCost());
    }
    

    二、评分明细

    • 功能实现的正确性与健壮性(38/40分): 代码逻辑基本正确,成功实现了测试订单与普通订单不同字段值的区分处理。扣除 2 分是因为“先赋值后覆盖”的写法在健壮性上不够严谨。

    • 安全性与潜在风险(30/30分): 仅涉及普通实体对象的属性赋值调整,未见 SQL 注入、越权等安全隐患。未触发 Mybatis 子查询及 Dubbo 接口变更等重点风险规则。

    • 最佳实践与可维护性(15/20分): 在存在明确 if-else 结构的情况下,依赖“后续覆盖”来修正前面的赋值并非良好的工程实践。将各自特有的逻辑放入对应的分支块内,更有利于后续代码的维护和阅读。

    • 性能与资源利用(4/5分): 对于命中 if 分支的场景,产生了额外且无用的对象方法调用及属性赋值操作,存在极轻微的不必要资源浪费。

    • 提交信息质量(5/5分): Commit 信息(refactor(goodsSubOrder): 调整子订单字段赋值逻辑)格式规范,且清晰地描述了变更意图和具体的修改动作,具备良好的可追溯性。

    总分:92分

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