Commit 9163a763 by 周立明

利润率测算前校验项目制批量上架的门店必须全部为优加门店或全部为非优加门店

1 parent 47880f42
...@@ -21,6 +21,8 @@ public class GoodsListingRemoteImpl implements GoodsListingRemote { ...@@ -21,6 +21,8 @@ public class GoodsListingRemoteImpl implements GoodsListingRemote {
@Override @Override
public RpcResponse<Boolean> updateCreateBusinessProductProfit(@NotBlank String businessCode) { public RpcResponse<Boolean> updateCreateBusinessProductProfit(@NotBlank String businessCode) {
try{ try{
// 项目制批量上架:门店优加状态必须一致,否则同步拦截并返回报错
goodsListingProfitRateService.validateProjectBatchYoujiaUniform(businessCode);
taskExecutor.execute(()->goodsListingProfitRateService.updateCreateBusinessProductProfit(businessCode)); taskExecutor.execute(()->goodsListingProfitRateService.updateCreateBusinessProductProfit(businessCode));
return RpcResponse.success(true); return RpcResponse.success(true);
}catch(RpcServiceException e){ }catch(RpcServiceException e){
......
...@@ -857,6 +857,34 @@ public class GoodsListingProfitRateService { ...@@ -857,6 +857,34 @@ public class GoodsListingProfitRateService {
return Objects.equals(yqConfig.getPartnerMerchantId(), youjiaMerchantId); return Objects.equals(yqConfig.getPartnerMerchantId(), youjiaMerchantId);
} }
/**
* 项目制批量上架门店优加一致性校验:
* 上架类型为“项目制批量上架”(ShelfTypeEnum.PROJECT_BATCH) 时,批次内所有门店必须同为优加门店或同为非优加门店,
* 不允许优加门店与非优加门店混批。不一致时抛出 RpcServiceException,交由 Remote 层同步返回报错给调用方。
* 非项目制批量上架、无门店或审批信息缺失时直接放行。
*/
public void validateProjectBatchYoujiaUniform(String businessCode) {
GoodsListingApproval goodsListingApproval = goodsListingProfitRateMapper.getGoodsListingApproval(businessCode);
if (goodsListingApproval == null
|| !Objects.equals(goodsListingApproval.getShelfType(), ShelfTypeEnum.PROJECT_BATCH.getValue())) {
return;
}
List<GoodsListingMerchantRef> merchants = goodsListingProfitRateMapper.listMerchant(businessCode);
if (CollectionUtils.isEmpty(merchants)) {
return;
}
Boolean uniformYoujia = null;
for (GoodsListingMerchantRef merchant : merchants) {
boolean youjia = isYoujiaStore(merchant);
if (uniformYoujia == null) {
uniformYoujia = youjia;
} else if (uniformYoujia != youjia) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"项目制批量上架的门店必须全部为优加门店或全部为非优加门店,当前存在优加与非优加混批,请检查门店配置");
}
}
}
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class) @Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public String getAverageProfitGrade(CreateBusinessApproveDTO record) { public String getAverageProfitGrade(CreateBusinessApproveDTO record) {
String businessCode = record.getBusinessCode(); String businessCode = record.getBusinessCode();
......
  • @wangxw1
    commented
    Master

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

    1. 潜在的 N+1 性能风险(循环内方法调用)

    • 问题描述:在 validateProjectBatchYoujiaUniform 方法中,通过 for 循环遍历门店列表 merchants,并逐一调用 isYoujiaStore(merchant) 方法进行判断。
    • 影响分析:由于无法看到 isYoujiaStore 的完整实现,若该方法内部包含数据库查询或远程 RPC 调用,在批量上架门店数量较多时,将产生严重的 N+1 查询性能问题,耗时呈线性增长,极易导致 Dubbo 接口超时。
    • 优化建议:请确认 isYoujiaStore 的底层实现。若仅为内存字段判断则无妨;若涉及 I/O 操作(如查库),强烈建议在循环外进行批量查询(如使用 WHERE merchant_id IN (...)),将结果加载到内存的 Map 中,然后在循环中从内存匹配。

    2. 潜在的类型比对失效风险(Objects.equals 类型严格性)

    • 问题描述:代码中使用 Objects.equals(goodsListingApproval.getShelfType(), ShelfTypeEnum.PROJECT_BATCH.getValue()) 来判断上架类型。
    • 影响分析:Objects.equals 会严格校验对象的数据类型。如果实体类 getShelfType() 的返回值数据类型(如 Integer)与枚举 getValue() 的返回值数据类型(如 Byte 或 Short)不完全一致,比较结果将永远返回 false,导致此拦截校验逻辑被直接跳过,完全失效。
    • 优化建议:检查并确认两者的返回值类型是否完全一致。如果不一致,建议使用基本数据类型的值进行比对,例如: java if (goodsListingApproval == null || goodsListingApproval.getShelfType() == null || goodsListingApproval.getShelfType().intValue() != ShelfTypeEnum.PROJECT_BATCH.getValue().intValue()) { return; }

    3. 健壮性与空指针异常防范

    • 问题描述:在遍历 merchants 列表时,直接将迭代元素 merchant 传入 isYoujiaStore(merchant),未对集合元素本身进行非空校验。
    • 影响分析:虽然 listMerchant 返回的集合通常不会包含 null,但如果在极端或脏数据场景下集合中存在 null 元素,可能会在 isYoujiaStore 内部或后续逻辑中引发 NullPointerException,导致正常业务非预期中断。
    • 优化建议:在循环体首行增加对 merchant 的非空校验,提升代码的健壮性。 java for (GoodsListingMerchantRef merchant : merchants) { if (merchant == null) { continue; } boolean youjia = isYoujiaStore(merchant); // ... }

    二、评分明细

    1. 功能实现的正确性与健壮性(36 / 40分)

      • 理由:业务校验逻辑清晰,能够正确拦截混合批次并抛出异常,符合预期。未发现违反“禁用 Mybatis 子查询”及“变更 Remote 请求参数”的红线规则。扣分项在于 Objects.equals 潜在的类型不匹配风险以及集合元素的非空防范。
    2. 安全性与潜在风险(30 / 30分)

      • 理由:未发现 SQL 注入、XSS、越权或敏感信息泄露等安全漏洞。异常提示信息为正常的业务文案,向外暴露是安全的。
    3. 最佳实践与可维护性(18 / 20分)

      • 理由:代码结构清晰,方法职责单一,变量命名(如 uniformYoujia)具备良好的可读性,且补充了详细的业务注释。扣分项在于循环内直接调用外部方法,在防御性编程上略有欠缺。
    4. 性能与资源利用(4 / 5分)

      • 理由:整体资源利用合理,但需要防范 isYoujiaStore 可能引发的 N+1 性能瓶颈。
    5. 提交信息质量(5 / 5分)

      • 理由:Commit 信息(“利润率测算前校验项目制批量上架的门店必须全部为优加门店或全部为非优加门店”)非常清晰、准确,完美概括了本次代码变更的业务意图,具有极高的可追溯性。

    总分:93分

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