Commit e6f00309 by zhoulm

服务商分入计算bug修复

1 parent 9fe3337f
......@@ -400,10 +400,7 @@ public class MerchantSettleDistributeRuleService {
RpcAssert.assertNotEmpty(merchantExpenseDistributions, DefaultErrorEnum.DATA_NULL,"merchantExpenseDistributions not be empty");
Map<String, List<MerchantExpenseDistribution>> map = merchantExpenseDistributions.stream().collect(Collectors.groupingBy(o->o.getMerchantId()+"_"+o.getRuleType()));
return merchantDistributions.stream()
.filter(merchantDistribution -> Objects.equals(merchantDistribution.getRuleType(),
DistributionRuleTypeEnum.ACTUAL_DISTRIBUTE.getValue()))
.map(merchantDistribution -> {
return merchantDistributions.stream().map(merchantDistribution -> {
MerchantDistributionDTO merchantDistributionDTO = MerchantDistributionDTOConvert.INSTANCE.fromMerchantDistribution(merchantDistribution);
merchantDistributionDTO.setMerchantExpenseDistributions(map.get(merchantDistributionDTO.getMerchantId()+"_"+merchantDistribution.getRuleType()));
return merchantDistributionDTO;
......
......@@ -1172,7 +1172,7 @@ public class CreateBusinessProductService {
Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER2.getValue(), o.getTargetMerchantType())) &&
Objects.equals(o.getRuleType(), DistributionRuleTypeEnum.OWNING_DISTRIBUTE.getValue()))
.map(MerchantDistributionDTO::getShouldDivisionMoney)
.findFirst().orElse(null);
.filter(Objects::nonNull).reduce(BigDecimal::add).orElse(null);
BigDecimal serviceProviderProfitOp = testRuleDTO.getMerchantDistributions().stream()
.filter(o -> o != null
......@@ -1180,7 +1180,7 @@ public class CreateBusinessProductService {
Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER2.getValue(), o.getTargetMerchantType())) &&
Objects.equals(o.getRuleType(), DistributionRuleTypeEnum.ACTUAL_DISTRIBUTE.getValue()))
.map(MerchantDistributionDTO::getShouldDivisionMoney)
.findFirst().orElse(null);
.filter(Objects::nonNull).reduce(BigDecimal::add).orElse(null);
productDTO.setServiceProviderProfit(owningServiceProviderProfitOp != null ? owningServiceProviderProfitOp : serviceProviderProfitOp);
if (productDTO.getServiceProviderProfit() == null
......
  • @wangxw1
    commented
    Master

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

    1. 最佳实践与可维护性:Stream 过滤条件过于冗长且存在代码重复

    • 问题描述:在 CreateBusinessProductService.java 中,对 testRuleDTO.getMerchantDistributions().stream() 进行过滤时,filter 内部的逻辑判断非常冗长。并且计算 owningServiceProviderProfitOp 和 serviceProviderProfitOp 的两次过滤逻辑几乎完全一致(仅 RuleType 不同),存在明显的代码重复。
    • 影响分析:长篇幅的逻辑判断降低了代码的可读性,且重复代码增加了后续维护的成本。如果未来目标商户类型(TargetMerchantType)增加或修改,需要在多处同时修改,容易遗漏。
    • 优化建议:将重复的过滤逻辑提取为独立的 Predicate 或私有方法,提升代码的整洁度和复用性。 代码示例: ```java // 提取公共的 Predicate Predicate isPartnerPredicate = o -> o != null && (Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER1.getValue(), o.getTargetMerchantType()) || Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER2.getValue(), o.getTargetMerchantType()));

    // 使用 Predicate 简化 Stream BigDecimal owningServiceProviderProfitOp = testRuleDTO.getMerchantDistributions().stream() .filter(isPartnerPredicate) .filter(o -> Objects.equals(o.getRuleType(), DistributionRuleTypeEnum.OWNING_DISTRIBUTE.getValue())) .map(MerchantDistributionDTO::getShouldDivisionMoney) .filter(Objects::nonNull) .reduce(BigDecimal::add).orElse(null);

    
    **2. 功能实现的正确性与健壮性:移除过滤条件后的下游兼容性评估**
    - **问题描述**:在 `MerchantSettleDistributeRuleService.java` 中,移除了 `.filter(merchantDistribution -> Objects.equals(merchantDistribution.getRuleType(), DistributionRuleTypeEnum.ACTUAL_DISTRIBUTE.getValue()))` 这一过滤条件。
    - **影响分析**:这会导致方法返回的 `MerchantDistributionDTO` 列表包含所有 `RuleType` 的数据,而不仅仅是 `ACTUAL_DISTRIBUTE`。虽然这符合业务 Bug 修复的预期,但需警惕调用该方法的下游业务是否已经做好了处理多种 `RuleType` 的准备,否则可能导致下游业务逻辑错误或数据重复计算。
    - **优化建议**:确保下游调用方能够兼容接收包含多种 `RuleType` 的列表数据。建议在方法注释中明确说明该方法会返回所有规则类型的数据,提醒调用者注意。
    
    **3. 提交信息质量:Commit 描述过于简略**
    - **问题描述**:提交信息仅为 “服务商分入计算bug修复 (by zhoulm)”,缺乏关于 Bug 产生的原因和修复逻辑的具体说明。
    - **影响分析**:过于简略的提交信息无法体现代码变更的意图(例如:为什么之前用 `findFirst` 是错的,为什么要改成累加 `reduce(BigDecimal::add)`;为什么要移除规则类型的过滤),不利于团队协作、代码 Review 以及后期的线上问题追溯。
    - **优化建议**:遵循规范的 Commit 格式,简要说明“问题现象”、“根本原因”以及“修复方案”。例如:“fix: 修复服务商分入计算金额遗漏的Bug。1. 将金额获取由 findFirst 改为累加(reduce);2. 移除结算规则类型的过滤以支持多规则计算”。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(38 / 40分)**:
      修复了金额未正确累加(由 `findFirst` 改为 `reduce`)以及规则类型遗漏的业务 Bug。加入了 `.filter(Objects::nonNull)` 避免了 `BigDecimal` 累加时潜在的空指针异常,逻辑健壮性较好。扣除2分是因为移除过滤条件可能带来的下游兼容性隐患。
    - **安全性与潜在风险(30 / 30分)**:
      本次代码变更主要集中在业务逻辑和内存数据处理,未引入 SQL 注入、越权等安全风险,无敏感信息泄露问题。
    - **最佳实践与可维护性(15 / 20分)**:
      使用了 Java 8 的 Stream API 进行集合处理,符合现代 Java 编程习惯。但复杂的条件判断存在重复,未进行合理的抽象和提取,可读性有待提升。
    - **性能与资源利用(5 / 5分)**:
      Stream 操作对于常规数量的集合开销极小,未发现明显的性能瓶颈或内存浪费。
    - **提交信息质量(3 / 5分)**:
      提交信息说明了变更的业务目的(修复Bug),但缺乏具体的技术实现细节和上下文,不利于长期维护。
    
    总分:91分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!