Commit 9b33a9b8 by zhoulm

服务商分润金额计算包含分配给服务商2的

1 parent 0e531742
......@@ -1144,14 +1144,16 @@ public class CreateBusinessProductService {
}
BigDecimal owningServiceProviderProfitOp = testRuleDTO.getMerchantDistributions().stream()
.filter(o -> o != null
&& Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER.getValue(), o.getTargetMerchantType()) &&
&& (Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER.getValue(), o.getTargetMerchantType()) ||
Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER2.getValue(), o.getTargetMerchantType())) &&
Objects.equals(o.getRuleType(), DistributionRuleTypeEnum.OWNING_DISTRIBUTE.getValue()))
.map(MerchantDistributionDTO::getShouldDivisionMoney)
.findFirst().orElse(null);
BigDecimal serviceProviderProfitOp = testRuleDTO.getMerchantDistributions().stream()
.filter(o -> o != null
&& Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER.getValue(), o.getTargetMerchantType()) &&
&& (Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER.getValue(), o.getTargetMerchantType()) ||
Objects.equals(MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER2.getValue(), o.getTargetMerchantType())) &&
Objects.equals(o.getRuleType(), DistributionRuleTypeEnum.ACTUAL_DISTRIBUTE.getValue()))
.map(MerchantDistributionDTO::getShouldDivisionMoney)
.findFirst().orElse(null);
......
  • @wangxw1
    commented
    Master

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

    1. 代码冗余与可读性较差(最佳实践)

    • 问题描述:在计算 owningServiceProviderProfitOp 和 serviceProviderProfitOp 时,Lambda 表达式中的过滤条件非常长。特别是新增了 PARTNER2 的判断后,两个流操作中存在大段完全重复的逻辑,导致代码可读性下降,后续若继续增加商户类型,代码将变得臃肿。
    • 影响分析:违反了 DRY(Don't Repeat Yourself)原则,增加了代码维护成本和阅读难度。
    • 优化建议:将目标商户类型的判断提取为集合的 contains 方法,或者将公共过滤条件提取为一个 Predicate。

    2. 对同一集合进行多次遍历(性能与最佳实践)

    • 问题描述:对 testRuleDTO.getMerchantDistributions() 进行了两次流遍历,仅为了查找不同 RuleType 的首个匹配项。
    • 影响分析:虽然在数据量较小的情况下性能损耗可以忽略不计,但多次遍历同一集合去寻找不同属性值的做法不够优雅。
    • 优化建议:考虑到业务代码的简洁性,至少应通过提取公共变量来优化。如果对性能有极致要求,可采用一次遍历完成两个变量的赋值。

    优化代码示例:

    // 提取合法的商户类型集合
    Set<Integer> validPartnerTypes = new HashSet<>(Arrays.asList(
            MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER.getValue(),
            MerchantDistributionRuleTargetMerchantTypeEnum.PARTNER2.getValue()
    ));
    
    // 提取公共过滤条件
    Predicate<MerchantDistributionDTO> baseFilter = o -> o != null 
            && validPartnerTypes.contains(o.getTargetMerchantType());
    
    BigDecimal owningServiceProviderProfitOp = testRuleDTO.getMerchantDistributions().stream()
            .filter(baseFilter.and(o -> Objects.equals(o.getRuleType(), DistributionRuleTypeEnum.OWNING_DISTRIBUTE.getValue())))
            .map(MerchantDistributionDTO::getShouldDivisionMoney)
            .findFirst().orElse(null);
    
    BigDecimal serviceProviderProfitOp = testRuleDTO.getMerchantDistributions().stream()
            .filter(baseFilter.and(o -> Objects.equals(o.getRuleType(), DistributionRuleTypeEnum.ACTUAL_DISTRIBUTE.getValue())))
            .map(MerchantDistributionDTO::getShouldDivisionMoney)
            .findFirst().orElse(null);
    

    二、评分明细

    1. 功能实现的正确性与健壮性(40/40分):逻辑修改正确,成功将 PARTNER2 纳入分润金额的计算范围,空指针等边界情况也已由 o != null 处理,无子查询问题。
    2. 安全性与潜在风险(30/30分):当前修改仅涉及内存数据的过滤与计算,不存在 SQL 注入、XSS 或越权等安全风险。
    3. 最佳实践与可维护性(15/20分):Lambda 表达式过长且存在重复的硬编码条件判断,代码可读性和可维护性有待提升,建议提取公共逻辑。
    4. 性能与资源利用(4/5分):存在对同一集合的重复遍历,但通常此类 DTO 集合数据量不大,不会造成明显性能瓶颈。
    5. 提交信息质量(4/5分):Commit 信息(“服务商分润金额计算包含分配给服务商2的”)基本清晰地描述了业务意图,但若能简要说明修改的具体字段会更好。

    总分:93分

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