Commit 26a7065b by 崔彦峰

refactor(goods-listing): 重构商品上架财务对账规则处理逻辑

- 移除审核发布阶段动态计算OTHER_FEE1~4的逻辑,改为导入阶段直接写入中间表
- 优化费用类目映射注释,明确对账品类分组关联的为财务品类分组
- 合并财务规则和对账规则的构建流程,确保两者使用同一套合并边界
- 新增RuleModuleBuildPair和CombinedRuleKey内部类支持规则模块配对构建
- 调整费用类目分账目标处理,固定OTHER_FEE1~4的费用归属和实际分账为平台方
- 优化导入进度计算,将规则模块构建步骤合并为单次处理
- 修复保存规则分组时的关联财务分组ID映射逻辑
1 parent 63dd1df2
package cn.com.glsx.dj.goods.listing.model; package cn.com.glsx.dj.goods.listing.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -11,6 +8,10 @@ import lombok.NoArgsConstructor; ...@@ -11,6 +8,10 @@ import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@ApiModel(value = "GoodsListingGoodsTypeGroup", description = "商品上架商品类型分组") @ApiModel(value = "GoodsListingGoodsTypeGroup", description = "商品上架商品类型分组")
@Table(name = "dj_car_life_goods_listing_goods_type_group") @Table(name = "dj_car_life_goods_listing_goods_type_group")
@Data @Data
...@@ -64,7 +65,7 @@ public class GoodsListingGoodsTypeGroup implements Serializable { ...@@ -64,7 +65,7 @@ public class GoodsListingGoodsTypeGroup implements Serializable {
/** /**
* 对账品类分组关联的分账品类分组id * 对账品类分组关联的分账品类分组id
*/ */
@ApiModelProperty(name = "relationDivId", notes = "对账品类分组关联的分账品类分组id") @ApiModelProperty(name = "relationDivId", notes = "对账品类分组关联的财务品类分组id")
@Column(name = "relation_div_id") @Column(name = "relation_div_id")
private Integer relationDivId; private Integer relationDivId;
......
...@@ -1530,8 +1530,9 @@ public class GoodsListingApprovalService { ...@@ -1530,8 +1530,9 @@ public class GoodsListingApprovalService {
MerchantGoodsTypeGroupSettleRule settleGroupRule = buildMerchantGoodsTypeGroupSettleRule(context, settleRuleId, oldGroup.getId(), group.getGroupType()); MerchantGoodsTypeGroupSettleRule settleGroupRule = buildMerchantGoodsTypeGroupSettleRule(context, settleRuleId, oldGroup.getId(), group.getGroupType());
smartcarMerchantGoodsTypeGroupSettleRuleMapper.insertUseGeneratedKeys(settleGroupRule); smartcarMerchantGoodsTypeGroupSettleRuleMapper.insertUseGeneratedKeys(settleGroupRule);
List<GoodsListingExpenseCategoryRule> expenses = appendPrerequisiteOtherExpenseRules(context, group, // OTHER_FEE1~4已经在Excel导入解析阶段按同一行对账市场费用字段写入财务费用类目中间表。
getCachedList(context.expenseRulesByGroupIdMap, group.getId())); // 发布阶段直接使用中间表数据,避免再次按对账分组动态计算导致重复或覆盖导入结果。
List<GoodsListingExpenseCategoryRule> expenses = getCachedList(context.expenseRulesByGroupIdMap, group.getId());
// 默认费用类目只在审核通过发布旧表时补齐,不回写商品上架草稿中间表。 // 默认费用类目只在审核通过发布旧表时补齐,不回写商品上架草稿中间表。
List<GoodsListingExpenseCategoryRule> defaultExpenses = buildDefaultSettleExpenseRules(context, group.getId(), expenses); List<GoodsListingExpenseCategoryRule> defaultExpenses = buildDefaultSettleExpenseRules(context, group.getId(), expenses);
insertExpenseCategoryRules(context, merchantRef, RuleTypeEnum.SETTLE_ACCOUNT_RULE.getValue(), settleGroupRule.getId(), insertExpenseCategoryRules(context, merchantRef, RuleTypeEnum.SETTLE_ACCOUNT_RULE.getValue(), settleGroupRule.getId(),
...@@ -1546,32 +1547,6 @@ public class GoodsListingApprovalService { ...@@ -1546,32 +1547,6 @@ public class GoodsListingApprovalService {
publishDistributions); publishDistributions);
} }
private List<GoodsListingExpenseCategoryRule> appendPrerequisiteOtherExpenseRules(ApprovalPublishContext context,
GoodsListingGoodsTypeGroup group,
List<GoodsListingExpenseCategoryRule> expenses) {
Map<Integer, GoodsListingExpenseCategoryRule> expenseMap = toExpenseRuleMap(expenses);
GoodsListingGoodsTypeGroup reconciliationGroup = findMatchedReconciliationGroup(context, group);
List<GoodsListingExpenseCategoryRule> sourceExpenses = reconciliationGroup == null
? Collections.emptyList()
: getCachedList(context.expenseRulesByGroupIdMap, reconciliationGroup.getId());
Map<Integer, GoodsListingExpenseCategoryRule> sourceExpenseMap = toExpenseRuleMap(sourceExpenses);
boolean marketingFeesPrerequisite = isFlagYes(context.baseRule == null ? null : context.baseRule.getManagementMarketingFeesPrerequisite());
boolean sunshinePrerequisite = isFlagYes(context.baseRule == null ? null : context.baseRule.getFirstLineSunshinePrerequisite());
// 其他费用1~4不是Excel导入的独立费用类目,不写入商品上架费用类目中间表。
// 这里仅在审核通过同步旧财务规则时动态补齐,保证旧财务规则能按前置开关计算这四个费用。
// 对应的费用归属/实际分账规则会在appendPrerequisiteOtherDistributionRules中统一补给平台方。
putPrerequisiteOtherExpenseRule(context, group.getId(), expenseMap, FeeCategoryEnum.OTHER_FEE1.getValue(),
marketingFeesPrerequisite ? sourceExpenseMap.get(FeeCategoryEnum.MANAGER_MARKET_FEE1.getValue()) : null);
putPrerequisiteOtherExpenseRule(context, group.getId(), expenseMap, FeeCategoryEnum.OTHER_FEE2.getValue(),
marketingFeesPrerequisite ? sourceExpenseMap.get(FeeCategoryEnum.MANAGER_MARKET_FEE2.getValue()) : null);
putPrerequisiteOtherExpenseRule(context, group.getId(), expenseMap, FeeCategoryEnum.OTHER_FEE3.getValue(),
sunshinePrerequisite ? sourceExpenseMap.get(FeeCategoryEnum.PLATFORM_SUNSHINE_INCENTIVE_FEE_POINT1.getValue()) : null);
putPrerequisiteOtherExpenseRule(context, group.getId(), expenseMap, FeeCategoryEnum.OTHER_FEE4.getValue(),
sunshinePrerequisite ? sourceExpenseMap.get(FeeCategoryEnum.PLATFORM_SUNSHINE_INCENTIVE_FEE_POINT2.getValue()) : null);
return new ArrayList<>(expenseMap.values());
}
private void putPrerequisiteOtherExpenseRule(ApprovalPublishContext context, private void putPrerequisiteOtherExpenseRule(ApprovalPublishContext context,
Integer goodsTypeGroupId, Integer goodsTypeGroupId,
Map<Integer, GoodsListingExpenseCategoryRule> expenseMap, Map<Integer, GoodsListingExpenseCategoryRule> expenseMap,
...@@ -1754,9 +1729,8 @@ public class GoodsListingApprovalService { ...@@ -1754,9 +1729,8 @@ public class GoodsListingApprovalService {
if (expense == null || !isPlatformOtherFeeCategory(expense.getExpenseCategoryId())) { if (expense == null || !isPlatformOtherFeeCategory(expense.getExpenseCategoryId())) {
continue; continue;
} }
// 其他费用1~4是发布期动态补齐的财务费用,业务要求费用归属和实际分账都默认给平台方。 // 其他费用1~4在导入阶段已作为财务费用类目落库,但模板不要求用户配置它们的分账对象。
// 因为这些费用不经过Excel分账模板,也不会写入goods_listing_distribution_rule中间表, // 因此发布旧表时仍要兜底补齐费用归属和实际分账两条记录,且目标固定为平台方。
// 所以必须在同步旧表前手工补两条发布用分账记录,最终落到dj_car_life_merchant_distribution_rule。
putPlatformOtherDistributionRule(context, distributionMap, expense, DistributionRuleTypeEnum.OWNING_DISTRIBUTE.getValue()); putPlatformOtherDistributionRule(context, distributionMap, expense, DistributionRuleTypeEnum.OWNING_DISTRIBUTE.getValue());
putPlatformOtherDistributionRule(context, distributionMap, expense, DistributionRuleTypeEnum.ACTUAL_DISTRIBUTE.getValue()); putPlatformOtherDistributionRule(context, distributionMap, expense, DistributionRuleTypeEnum.ACTUAL_DISTRIBUTE.getValue());
} }
......
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的专业审查报告。本次代码变更主要重构了商品上架财务与对账规则的处理逻辑,将部分运行期动态计算前置到了导入阶段,整体思路清晰,有助于提升系统发布阶段的稳定性。

    以下是代码审查的详细情况:

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

    1. 【逻辑正确性】规则冲突时可能导致错误提示信息重复或被掩盖

    • 问题描述:在 checkAndBuildRuleModules 方法中,如果 buildCombinedRuleBuildResult(submitDTO) 返回 null,系统会追加一条错误信息 "可用的财务/对账规则商品信息为空,无法保存规则"。然而,buildCombinedRuleBuildResult 返回 null 的原因可能是内部调用 checkGoodsRuleConflict 失败,此时已经向 errorList 中添加了明确的冲突原因("商品财务/对账规则与套餐财务/对账规则不一致,请检查")。
    • 影响分析:这会导致前端展示给用户的错误信息出现重复,或者用宽泛的“商品信息为空”掩盖了真正具体的“规则不一致”错误,降低了系统的易用性和排障效率。
    • 优化建议:在追加兜底错误信息前,判断 errorList 是否已经由于本次操作新增了错误。 java RuleModuleBuildPair buildPair = buildCombinedRuleBuildResult(submitDTO); if (buildPair == null || ...) { // 仅在没有其他具体错误时,才给出兜底提示 if (!hasError(submitDTO.getGlobalContext().getErrorList())) { setFirstLineError(submitDTO.getGlobalContext().getErrorList(), "可用的财务/对账规则商品信息为空,无法保存规则"); } return null; }

    2. 【最佳实践】代码中存在未被枚举化的“魔法值(Magic Number)”

    • 问题描述:在 GoodsListingSubmitService.java 中,多次直接使用了 (byte) 1 和 (byte) 3 来指代财务规则和对账规则。例如:new RuleModuleBuildResult((byte) 1, sharedDrafts) 以及 if (Objects.equals(buildResult.getRuleType(), (byte) 3))。
    • 影响分析:硬编码的魔法值降低了代码的可读性和可维护性。由于代码其他地方已经大量使用了枚举(如 FeeCategoryEnum.OTHER_FEE1, DistributionRuleTypeEnum 等),此处不使用枚举属于规范不一致。
    • 优化建议:将 (byte) 1 和 (byte) 3 替换为现有的规则类型枚举(推测系统内存在类似 RuleTypeEnum.SETTLE.getValue() 的定义)。

    3. 【性能与最佳实践】buildIndexGroupKeySet 中基于字符串拼接的 Set 去重效率较低

    • 问题描述:在对套餐合并范围进行一致性校验时,buildIndexGroupKeySet 方法将 List<Integer> 排序后,通过 Stream 转换为逗号分隔的 String(如 "1,2,3"),再放入 Set<String> 中进行去重和比较。
    • 影响分析:虽然 Excel 导入的数据量通常不会引发严重的性能瓶颈,但在循环中频繁创建 ArrayList、Stream 以及 String 对象是不必要的资源消耗。在 Java 中,List 接口的 equals 和 hashCode 方法原生就是基于其内部元素计算的。
    • 优化建议:直接使用 Set<List<Integer>> 进行存储和比较,代码更简洁且性能更好。 java private Set<List<Integer>> buildIndexGroupKeySet(List<List<Integer>> indexGroups) { Set<List<Integer>> result = new LinkedHashSet<>(); if (CollectionUtils.isEmpty(indexGroups)) { return result; } for (List<Integer> indexGroup : indexGroups) { if (CollectionUtils.isEmpty(indexGroup)) { continue; } List<Integer> copied = new ArrayList<>(indexGroup); copied.sort(Comparator.naturalOrder()); result.add(copied); // 直接将 List 放入 Set,Java 原生支持按元素内容判断重复 } return result; }

    二、评分明细

    • 功能实现的正确性与健壮性 (38/40分)
      • 理由:核心重构逻辑严密,正确处理了 OTHER_FEE1~4 的前置落库。CombinedRuleKey 和 IdentityHashMap 的使用非常巧妙且安全(因为同一批 Draft 对象引用一致)。扣除 2 分是因为异常提示信息的叠加问题(见 Top 1)。未发现 MyBatis 子查询及 Dubbo 接口参数变更等违规操作。
    • 安全性与潜在风险 (30/30分)
      • 理由:代码主要为业务逻辑重构,未引入新的外部输入点,不存在 SQL 注入、越权等安全风险。空指针防御(如 saveBaseRule == null ? null : ...)做得比较到位。
    • 最佳实践与可维护性 (17/20分)
      • 理由:代码结构清晰,注释详尽(尤其解释了为什么财务和对账要共享 Draft 但分别落库的原因,极大提高了可读性)。扣除 3 分主要是因为魔法值的硬编码(见 Top 2)以及部分 DTO(如 CombinedRuleKey 内部属性)依赖默认 equals 行为的潜在隐患。
    • 性能与资源利用 (4/5分)
      • 理由:整体性能良好,但在集合去重校验的实现上存在不必要的字符串转换开销(见 Top 3),稍微浪费了内存资源。
    • 提交信息质量 (5/5分)
      • 理由:Commit 信息非常优秀。采用了标准规范(refactor:),并且通过多行列表清晰地阐述了本次变更的背景、动作和目的(如“移除审核发布阶段动态计算...”、“合并财务规则和对账规则的构建流程”),可追溯性极强。

    总分:94分

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