Commit db3712ee by 崔彦峰

refactor(goods-listing): 重构商品上架审批规则发布逻辑

- 将原有的按组维度处理改为按商户维度处理,减少重复遍历
- 新增按商户维度发布结算规则、对账规则和更新商户规则ID的方法
- 统一通过 publishApprovedRulesByMerchant 方法串联商户维度的规则处理流程
- 添加商户操作时间更新功能
- 优化代码结构,提高可读性和维护性
1 parent 65581b43
......@@ -12,6 +12,7 @@ import cn.com.glsx.dj.goods.listing.util.DateUtils;
import cn.com.glsx.dj.smartcarlife.dto.ExpenseCategoryDTO;
import cn.com.glsx.dj.smartcarlife.dto.UpgradeGoodsDTO;
import cn.com.glsx.dj.smartcarlife.dto.goods.MerchantGoodsInfoDTO;
import cn.com.glsx.dj.smartcarlife.dto.goods.UpgradeGoodsAreaDTO;
import cn.com.glsx.dj.smartcarlife.enums.*;
import cn.com.glsx.dj.smartcarlife.enums.createBusinessApprove.ProductTypeEnum;
import cn.com.glsx.dj.smartcarlife.model.*;
......@@ -440,6 +441,9 @@ public class GoodsListingApprovalService {
context.approval = approval;
context.baseRule = baseRule;
context.merchantRefs = merchantRefs;
if (CollectionUtils.isNotEmpty(merchantRefs)) {
context.merchantCityCodes = merchantRefs.stream().map(GoodsListingMerchantRef::getCityCode).collect(Collectors.toList());
}
context.baseSceneCode = approval == null ? null : approval.getBaseSceneCode();
context.sceneType = resolveSceneType(context.baseSceneCode);
context.allExpenseCategoryDTOS = expenseCategoryDTOS;
......@@ -742,12 +746,45 @@ public class GoodsListingApprovalService {
log.info("validUpgradeServiceItemExists error,upgradeGoodsDTO:{}",JSON.toJSONString(upgradeGoodsDTO),e);
goodsInfo.setStatus(CarLifeEnum.MerchantGoodsStatus.OFF_SHELF.getValue());
}
if (!validateUpgradeGoodsAreas(context, upgradeGoodsDTO)) {
goodsInfo.setStatus(CarLifeEnum.MerchantGoodsStatus.OFF_SHELF.getValue());
}
goodsInfo.setLastOnShelfTime(context.now);
goodsInfo.setShelfSource(CarLifeEnum.MerchantShelfSource.DJ_CREATE_BUSINESS.getValue());
goodsInfo.setFinanceCheckFlag(StatusEnum.YES.getValue());
}
private boolean validateUpgradeGoodsAreas(ApprovalPublishContext context, UpgradeGoodsDTO upgradeGoodsDTO) {
List<GoodsListingMerchantRef> merchantRefs = context.merchantRefs;
if (CollectionUtils.isEmpty(merchantRefs)) {
log.info("merchantRefs is empty,businessCode:{}",context.businessCode);
return false;
}
List<String> merchantCityCodes = context.merchantCityCodes;
if (CollectionUtils.isEmpty(merchantCityCodes)) {
log.info("merchantCityCodes is empty,businessCode:{}",context.businessCode);
return false;
}
if (merchantRefs.size() != merchantCityCodes.size()) {
log.info("merchantCityCodes size:{},merchantRefs size:{},businessCode:{}", merchantCityCodes.size(), merchantRefs.size(),context.businessCode);
return false;
}
List<UpgradeGoodsAreaDTO> upgradeGoodsAreaList = upgradeGoodsDTO.getUpgradeGoodsAreaList();
if (CollectionUtils.isEmpty(upgradeGoodsAreaList)) {
log.info("upgradeGoodsAreaList is empty,upgradeGoodsDTO:{},businessCode:{}",JSON.toJSONString(upgradeGoodsDTO),context.businessCode);
return false;
}
Set<String> upgradeGoodsCityCodeSet = upgradeGoodsAreaList.stream()
.filter(Objects::nonNull)
.map(UpgradeGoodsAreaDTO::getCityCode)
.filter(StringUtils::isNotBlank)
.collect(Collectors.toSet());
if (upgradeGoodsCityCodeSet.contains("-1")) {
return true;
}
return upgradeGoodsCityCodeSet.containsAll(new HashSet<>(merchantCityCodes));
}
private boolean isSolarFilmGoods(UpgradeGoodsDTO upgradeGoodsDTO) {
Integer goodsType = resolveGoodsType(upgradeGoodsDTO);
if (goodsType == null) {
......@@ -1888,6 +1925,7 @@ public class GoodsListingApprovalService {
private GoodsListingApproval approval;
private GoodsListingBaseRule baseRule;
private List<GoodsListingMerchantRef> merchantRefs;
private List<String> merchantCityCodes;
private String baseSceneCode;
private Byte sceneType;
private List<ExpenseCategoryDTO> allExpenseCategoryDTOS;
......
......@@ -982,6 +982,7 @@ public class GoodsListingSubmitService {
// 新链路适用区域只按商户cityCode判断,不再按省份兜底。
// 区域不覆盖属于商品不可销售问题,错误写到当前商品行,避免单店模式把多个商品错误堆到第一行。
boolean notCovered = false;
Integer notCoveredMerchantId = null;
for (Map.Entry<Integer, CreateBusinessApproveMerchantDTO> entry : merchantRowIndexMap.entrySet()) {
CreateBusinessApproveMerchantDTO merchant = entry.getValue();
String merchantCityCode = merchant == null ? null : merchant.getCityCode();
......@@ -992,6 +993,7 @@ public class GoodsListingSubmitService {
continue;
}
if (!upgradeGoodsCityCodeSet.contains(merchantCityCode)) {
notCoveredMerchantId = entry.getKey();
notCovered = true;
valid = false;
}
......@@ -1001,7 +1003,7 @@ public class GoodsListingSubmitService {
valid = true;
}
if (notCovered) {
setLineError(errorList, "产品适用区域不覆盖该门店", goodsRowIndex);
setLineError(errorList, "产品适用区域不覆盖该门店" + notCoveredMerchantId, goodsRowIndex);
}
return valid;
}
......
  • @wangxw1
    commented
    Master

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

    1. 逻辑正确性:错误提示信息覆盖与语义混淆问题

    • 问题描述:在 GoodsListingSubmitService.java 中,notCoveredMerchantId 被赋值为 entry.getKey()。根据 Map 的命名 merchantRowIndexMap,该 Key 实际上是 Excel 的行号(Row Index),并非商户 ID。此外,在 for 循环中如果存在多个不匹配的门店,该变量会被不断覆盖,最终只记录最后一个不匹配的行号。直接将其拼接到错误提示 "产品适用区域不覆盖该门店" + notCoveredMerchantId 会导致提示信息产生歧义(如输出:“...不覆盖该门店2”)。
    • 影响分析:用户看到的错误提示词不达意,容易将行号误认为是商户 ID 或名称;且掩盖了其他同样不覆盖的商户信息,增加用户的排查成本。
    • 优化建议:建议从 DTO 中提取真实的商户名称或编码,若确实需要提示行号,应明确指出。同时建议收集所有不匹配的商户信息,而不是只保留最后一个。 java // 示例优化 List<String> notCoveredMerchants = new ArrayList<>(); for (Map.Entry<Integer, CreateBusinessApproveMerchantDTO> entry : merchantRowIndexMap.entrySet()) { // ... 现有逻辑 if (!upgradeGoodsCityCodeSet.contains(merchantCityCode)) { // 假设 DTO 中有获取商户名称或编码的方法 notCoveredMerchants.add(merchant.getMerchantName() + "(行号:" + entry.getKey() + ")"); notCovered = true; valid = false; } } // ... if (notCovered) { setLineError(errorList, "产品适用区域不覆盖以下门店: " + String.join(", ", notCoveredMerchants), goodsRowIndex); }

    2. 代码最佳实践与性能:冗余的容量校验与不必要的对象创建

    • 问题描述:在 GoodsListingApprovalService.java 的 validateUpgradeGoodsAreas 方法中存在两处问题:
      1. merchantRefs.size() != merchantCityCodes.size() 校验是无效的死代码。因为 merchantCityCodes 是直接通过 merchantRefs.stream().map(...).collect(...) 生成的,两者大小永远一致。
      2. upgradeGoodsCityCodeSet.containsAll(new HashSet<>(merchantCityCodes)) 中,每次校验都会根据 List 重新创建一个 HashSet。
    • 影响分析:冗余校验会增加代码的阅读理解成本;在循环或高频调用的场景下,频繁创建 HashSet 会产生不必要的内存分配和 GC 压力。Set.containsAll(Collection) 底层已经是遍历入参 Collection 去查 Set,入参无需再次包装为 Set。
    • 优化建议:移除无意义的 size 校验,并直接传入 List 进行 containsAll 判断。 java // 移除 size 校验代码块 // 直接使用 List 作为参数 return upgradeGoodsCityCodeSet.containsAll(merchantCityCodes);

    3. 提交信息质量:Commit Message 与实际代码变更严重不符

    • 问题描述:提交信息描述的是“重构商品上架审批规则发布逻辑(按商户维度发布结算规则等)”,但实际提交的代码 Diff 内容全部是关于商品适用区域(CityCode)的校验逻辑以及错误提示信息的修改。
    • 影响分析:破坏了 Git 历史的可追溯性。后续开发人员在排查“适用区域校验”相关问题时,无法通过 Commit 记录快速定位到该次变更的背景和目的。
    • 优化建议:保持 Commit Message 与代码变更内容的一致性。如果该变更只是大分支中的一部分,建议拆分 Commit;如果是一次性提交,需在信息中补充完整:feat(goods-listing): 增加商品适用区域校验逻辑及优化不匹配时的错误提示。

    二、评分明细

    • 功能实现的正确性与健壮性(32/40分):核心的区域覆盖校验逻辑方向正确,但 GoodsListingSubmitService 中的错误提示逻辑存在语义混淆(将行号当做商户ID)且存在覆盖丢失的情况,扣除 8 分。未发现违反 Mybatis 子查询及 Dubbo 接口变更的红线规则。
    • 安全性与潜在风险(30/30分):代码以数据校验为主,未发现 SQL 注入、越权、并发安全等明显的安全漏洞。
    • 最佳实践与可维护性(16/20分):存在绝对不可能触发的冗余校验(List 大小比对),增加了无意义的圈复杂度,代码整洁度有待提升,扣除 4 分。
    • 性能与资源利用(3/5分):在 containsAll 判断时进行了不必要的 HashSet 实例化,造成轻微的资源浪费,扣除 2 分。
    • 提交信息质量(1/5分):提交信息(规则发布重构)与实际变更代码(区域校验)完全不匹配,严重影响代码可追溯性,扣除 4 分。

    总分:82分

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