Commit fc3f4d71 by 崔彦峰

refactor(goods): 优化商品上架服务的商品ID验证逻辑

- 将商品代码验证中的集合操作改为直接查找首个不匹配项
- 为商品上架提交服务添加货架类型枚举依赖
- 修改商品字段验证方法以支持货架类型参数传递
- 移除原有的货架样式和原商户商品代码验证逻辑
- 添加单店模式下的修改字段验证功能
- 重构验证方法结构以支持不同类型的商品验证场景
1 parent 2b6794e6
...@@ -3266,10 +3266,11 @@ public class GoodsListingApprovalService { ...@@ -3266,10 +3266,11 @@ public class GoodsListingApprovalService {
//筛选出在oldMerchantGoodsCodes中有goodsCodes没有的上架id //筛选出在oldMerchantGoodsCodes中有goodsCodes没有的上架id
Set<String> goodsCodeSet = result.getList().stream().map(GoodsRespDTO::getGoodsCode).collect(Collectors.toSet()); Set<String> goodsCodeSet = result.getList().stream().map(GoodsRespDTO::getGoodsCode).collect(Collectors.toSet());
// 过滤出 oldMerchantGoodsCodes 中存在、但 goodsCodeSet 中不存在的 ID // 过滤出 oldMerchantGoodsCodes 中存在、但 goodsCodeSet 中不存在的 ID
List<String> onlyInOldMerchant = oldMerchantGoodsCodes.stream() String merchantGoodsCode = oldMerchantGoodsCodes.stream()
.filter(code -> !goodsCodeSet.contains(code)) .filter(code -> !goodsCodeSet.contains(code))
.collect(Collectors.toList()); .findFirst()
RpcAssert.assertIsTrue(CollectionUtils.isEmpty(onlyInOldMerchant), DefaultErrorEnum.SERVICE_ERROR, onlyInOldMerchant.get(0) + "商品上架id不匹配"); .orElse(null);
RpcAssert.assertIsTrue(StringUtils.isBlank(merchantGoodsCode), DefaultErrorEnum.SERVICE_ERROR, merchantGoodsCode + "商品上架id不匹配");
} }
} }
...@@ -3295,10 +3296,11 @@ public class GoodsListingApprovalService { ...@@ -3295,10 +3296,11 @@ public class GoodsListingApprovalService {
} }
Set<String> packageIdSet = result.getList().stream().map(PackageRespDTO::getPackageId).collect(Collectors.toSet()); Set<String> packageIdSet = result.getList().stream().map(PackageRespDTO::getPackageId).collect(Collectors.toSet());
// 过滤出 oldMerchantGoodsCodes 中存在、但 packageIdSet 中不存在的 ID // 过滤出 oldMerchantGoodsCodes 中存在、但 packageIdSet 中不存在的 ID
List<String> onlyInOldMerchant = oldMerchantGoodsCodes.stream() String merchantGoodsCode = oldMerchantGoodsCodes.stream()
.filter(code -> !packageIdSet.contains(code)) .filter(code -> !packageIdSet.contains(code))
.collect(Collectors.toList()); .findFirst()
RpcAssert.assertIsTrue(CollectionUtils.isEmpty(onlyInOldMerchant), DefaultErrorEnum.SERVICE_ERROR, onlyInOldMerchant.get(0) + "套餐上架id不匹配"); .orElse(null);
RpcAssert.assertIsTrue(StringUtils.isBlank(merchantGoodsCode), DefaultErrorEnum.SERVICE_ERROR, merchantGoodsCode + "套餐上架id不匹配");
} }
} }
......
...@@ -25,6 +25,7 @@ import cn.com.glsx.dj.smartcarlife.enums.*; ...@@ -25,6 +25,7 @@ import cn.com.glsx.dj.smartcarlife.enums.*;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.DivisionType; import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.DivisionType;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.PositionEnum; import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.PositionEnum;
import cn.com.glsx.dj.smartcarlife.enums.createBusinessApprove.MerchantDivideTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.createBusinessApprove.MerchantDivideTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.createBusinessApprove.ShelfTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.vd.OperationModeEnum; import cn.com.glsx.dj.smartcarlife.enums.vd.OperationModeEnum;
import cn.com.glsx.dj.smartcarlife.model.ConstructionServiceItmes; import cn.com.glsx.dj.smartcarlife.model.ConstructionServiceItmes;
import cn.com.glsx.dj.smartcarlife.model.YqMerchantServiceDetails; import cn.com.glsx.dj.smartcarlife.model.YqMerchantServiceDetails;
...@@ -808,7 +809,14 @@ public class GoodsListingSubmitService { ...@@ -808,7 +809,14 @@ public class GoodsListingSubmitService {
continue; continue;
} }
if (!validateRequiredGoodsFields(row, i, list, baseLeaderIndexMap, packageGoodsIndexList, submitDTO.getBaseSceneCode(), errorList)) { if (!validateRequiredGoodsFields(row,
i,
list,
baseLeaderIndexMap,
packageGoodsIndexList,
submitDTO.getBaseSceneCode(),
submitDTO.getShelfType(),
errorList)) {
continue; continue;
} }
...@@ -1010,7 +1018,14 @@ public class GoodsListingSubmitService { ...@@ -1010,7 +1018,14 @@ public class GoodsListingSubmitService {
continue; continue;
} }
if (!validateOrdinaryGoodsFields(row, i, list, baseLeaderIndexMap, packageGoodsIndexList, submitDTO.getBaseSceneCode(), errorList)) { if (!validateOrdinaryGoodsFields(row,
i,
list,
baseLeaderIndexMap,
packageGoodsIndexList,
submitDTO.getBaseSceneCode(),
submitDTO.getShelfType(),
errorList)) {
continue; continue;
} }
...@@ -1260,6 +1275,7 @@ public class GoodsListingSubmitService { ...@@ -1260,6 +1275,7 @@ public class GoodsListingSubmitService {
Map<Integer, Integer> baseLeaderIndexMap, Map<Integer, Integer> baseLeaderIndexMap,
List<List<Integer>> packageGoodsIndexList, List<List<Integer>> packageGoodsIndexList,
String baseSceneCode, String baseSceneCode,
Byte shelfType,
List<StringBuilder> errorList) { List<StringBuilder> errorList) {
Integer leaderIndex = baseLeaderIndexMap.getOrDefault(rowIndex, rowIndex); Integer leaderIndex = baseLeaderIndexMap.getOrDefault(rowIndex, rowIndex);
boolean valid = true; boolean valid = true;
...@@ -1321,25 +1337,12 @@ public class GoodsListingSubmitService { ...@@ -1321,25 +1337,12 @@ public class GoodsListingSubmitService {
} }
} else if (isSceneBoutique(baseSceneCode)) { } else if (isSceneBoutique(baseSceneCode)) {
String upgradeProductCostTarget = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getUpgradeProductCostTarget); String upgradeProductCostTarget = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getUpgradeProductCostTarget);
String shelfStyleText = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getShelfStyle);
String oldMerchantGoodsCode = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getOldMerchantGoodsCode);
String upgradeProductCostStyle = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getUpgradeProductCostStyle); String upgradeProductCostStyle = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getUpgradeProductCostStyle);
String isGiftCost = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getIsGiftCost); String isGiftCost = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getIsGiftCost);
// String managementMarketingFeesPrerequisite = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getManagementMarketingFeesPrerequisite);
// String firstLineSunshinePrerequisite = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getFirstLineSunshinePrerequisite);
if (StringUtils.isBlank(upgradeProductCostTarget)) { if (StringUtils.isBlank(upgradeProductCostTarget)) {
setLineError(errorList, "产品成本归属不能为空", rowIndex); setLineError(errorList, "产品成本归属不能为空", rowIndex);
valid = false; valid = false;
} }
Byte shelfStyle = parseShelfStyle(shelfStyleText);
if (shelfStyle == null) {
setLineError(errorList, "类型只能为新增或修改", rowIndex);
valid = false;
}
if (Objects.equals(shelfStyle, (byte) 2) && StringUtils.isBlank(oldMerchantGoodsCode)) {
setLineError(errorList, "修改类型时原上架ID不能为空", rowIndex);
valid = false;
}
Byte costStyle = parseUpgradeProductCostStyle(upgradeProductCostStyle); Byte costStyle = parseUpgradeProductCostStyle(upgradeProductCostStyle);
if (costStyle == null) { if (costStyle == null) {
setLineError(errorList, "产品成本获取方式只能为默认带入产品成本或自定义成本", rowIndex); setLineError(errorList, "产品成本获取方式只能为默认带入产品成本或自定义成本", rowIndex);
...@@ -1357,10 +1360,10 @@ public class GoodsListingSubmitService { ...@@ -1357,10 +1360,10 @@ public class GoodsListingSubmitService {
setLineError(errorList, "是否作为赠送产品只能为是或否", rowIndex); setLineError(errorList, "是否作为赠送产品只能为是或否", rowIndex);
valid = false; valid = false;
} }
/*if (StringUtils.equals(normalize(isGiftCost), "是") && !Objects.equals(costStyle, (byte) 1)) { }
setLineError(errorList, "是否作为赠送产品为是时,产品成本获取方式必须为自定义成本", rowIndex); if (Objects.equals(shelfType, ShelfTypeEnum.SINGLE_STORE.getValue())
valid = false; && !validateSingleStoreModifyFields(row, rowIndex, list, baseLeaderIndexMap, errorList)) {
}*/ valid = false;
} }
return valid; return valid;
} }
...@@ -1371,6 +1374,7 @@ public class GoodsListingSubmitService { ...@@ -1371,6 +1374,7 @@ public class GoodsListingSubmitService {
Map<Integer, Integer> baseLeaderIndexMap, Map<Integer, Integer> baseLeaderIndexMap,
List<List<Integer>> packageGoodsIndexList, List<List<Integer>> packageGoodsIndexList,
String baseSceneCode, String baseSceneCode,
Byte shelfType,
List<StringBuilder> errorList) { List<StringBuilder> errorList) {
Integer leaderIndex = baseLeaderIndexMap.getOrDefault(rowIndex, rowIndex); Integer leaderIndex = baseLeaderIndexMap.getOrDefault(rowIndex, rowIndex);
boolean valid = true; boolean valid = true;
...@@ -1408,6 +1412,11 @@ public class GoodsListingSubmitService { ...@@ -1408,6 +1412,11 @@ public class GoodsListingSubmitService {
valid = false; valid = false;
} }
if (Objects.equals(shelfType, ShelfTypeEnum.SINGLE_STORE.getValue())
&& !validateSingleStoreModifyFields(row, rowIndex, list, baseLeaderIndexMap, errorList)) {
valid = false;
}
BigDecimal sellPrice = parseAmountWithRange(row.getSellPrice(), BigDecimal.ZERO, CommonConstants.DECIMAL_VALUE_ALMOST_MILLION); BigDecimal sellPrice = parseAmountWithRange(row.getSellPrice(), BigDecimal.ZERO, CommonConstants.DECIMAL_VALUE_ALMOST_MILLION);
BigDecimal minSellPrice = parseAmountWithRange(row.getMinSellPrice(), BigDecimal.ZERO, CommonConstants.DECIMAL_VALUE_ALMOST_MILLION); BigDecimal minSellPrice = parseAmountWithRange(row.getMinSellPrice(), BigDecimal.ZERO, CommonConstants.DECIMAL_VALUE_ALMOST_MILLION);
if (sellPrice == null) { if (sellPrice == null) {
...@@ -1448,19 +1457,8 @@ public class GoodsListingSubmitService { ...@@ -1448,19 +1457,8 @@ public class GoodsListingSubmitService {
valid = false; valid = false;
} }
} else if (isSceneBoutique(baseSceneCode)) { } else if (isSceneBoutique(baseSceneCode)) {
String shelfStyleText = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getShelfStyle);
String oldMerchantGoodsCode = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getOldMerchantGoodsCode);
String upgradeProductCostStyle = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getUpgradeProductCostStyle); String upgradeProductCostStyle = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getUpgradeProductCostStyle);
String isGiftCost = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getIsGiftCost); String isGiftCost = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getIsGiftCost);
Byte shelfStyle = parseShelfStyle(shelfStyleText);
if (shelfStyle == null) {
setLineError(errorList, "类型只能为新增或修改", rowIndex);
valid = false;
}
if (Objects.equals(shelfStyle, (byte) 2) && StringUtils.isBlank(oldMerchantGoodsCode)) {
setLineError(errorList, "修改类型时原上架ID不能为空", rowIndex);
valid = false;
}
Byte costStyle = parseUpgradeProductCostStyle(upgradeProductCostStyle); Byte costStyle = parseUpgradeProductCostStyle(upgradeProductCostStyle);
if (costStyle == null) { if (costStyle == null) {
setLineError(errorList, "产品成本获取方式只能为默认带入产品成本或自定义成本", rowIndex); setLineError(errorList, "产品成本获取方式只能为默认带入产品成本或自定义成本", rowIndex);
...@@ -1482,6 +1480,27 @@ public class GoodsListingSubmitService { ...@@ -1482,6 +1480,27 @@ public class GoodsListingSubmitService {
return valid; return valid;
} }
private boolean validateSingleStoreModifyFields(CreateBusinessApproveAllImportVo row,
int rowIndex,
List<CreateBusinessApproveAllImportVo> list,
Map<Integer, Integer> baseLeaderIndexMap,
List<StringBuilder> errorList) {
// 单店模式下,不区分常规商品和特殊商品,两条导入链路都要校验“类型”和“原上架ID”。
String shelfStyleText = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getShelfStyle);
String oldMerchantGoodsCode = resolveBaseMergedText(list, rowIndex, baseLeaderIndexMap, CreateBusinessApproveAllImportVo::getOldMerchantGoodsCode);
Byte shelfStyle = parseShelfStyle(shelfStyleText);
boolean valid = true;
if (shelfStyle == null) {
setLineError(errorList, "类型只能为新增或修改", rowIndex);
valid = false;
}
if (Objects.equals(shelfStyle, (byte) 2) && StringUtils.isBlank(oldMerchantGoodsCode)) {
setLineError(errorList, "修改类型时原上架ID不能为空", rowIndex);
valid = false;
}
return valid;
}
private boolean validateTextMaxLength(List<StringBuilder> errorList, private boolean validateTextMaxLength(List<StringBuilder> errorList,
int rowIndex, int rowIndex,
String fieldName, String fieldName,
......
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的审查报告。本次修改在流处理性能优化和代码复用(DRY)方面表现不错,但在语义准确性和代码结构上还有进一步优化的空间。

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

    1. 逻辑语义不准确:findFirst().orElse(null) 后的判空方式存在隐患

    • 问题描述:在 GoodsListingApprovalService.java 中,你将全量收集 collect(Collectors.toList()) 优化为了短路查找 findFirst().orElse(null),这是一个很好的性能优化。但是,后续的断言使用了 StringUtils.isBlank(merchantGoodsCode) 来判断是否找到了不匹配的 ID。
    • 影响分析:orElse(null) 明确使用 null 作为“未找到”的标识。而 StringUtils.isBlank() 对于 null、空字符串 "" 以及纯空格 " " 都会返回 true。如果 oldMerchantGoodsCodes 中由于脏数据存在纯空格或空字符串,且该脏数据在 goodsCodeSet 中不存在,isBlank 会返回 true,导致断言错误地通过,从而掩盖了数据不匹配的问题。
    • 优化建议:直接使用 == null 来判断是否未找到不匹配项,语义更严谨。 java // 建议修改代码 String merchantGoodsCode = oldMerchantGoodsCodes.stream() .filter(code -> !goodsCodeSet.contains(code)) .findFirst() .orElse(null); // 直接判断是否为 null RpcAssert.assertIsTrue(merchantGoodsCode == null, DefaultErrorEnum.SERVICE_ERROR, merchantGoodsCode + "商品上架id不匹配"); (注:同理适用于下方套餐上架 ID 的匹配校验)

    2. 代码可维护性:方法参数过多(Long Parameter List 坏味道)

    • 问题描述:在 GoodsListingSubmitService.java 中,validateRequiredGoodsFields 和 validateOrdinaryGoodsFields 方法在新增了 shelfType 参数后,参数列表长度达到了 8 个。
    • 影响分析:过长的方法参数列表会严重降低代码的可读性,增加调用时的心智负担,且极易在传递参数时发生顺序错误。
    • 优化建议:由于 row、rowIndex、list、baseLeaderIndexMap、baseSceneCode、shelfType 等参数在多个校验方法中成组出现,建议重构引入一个 ValidationContext(校验上下文)对象来封装这些参数。 java // 示例参考 public class GoodsValidationContext { private CreateBusinessApproveAllImportVo row; private int rowIndex; private List<CreateBusinessApproveAllImportVo> list; private String baseSceneCode; private Byte shelfType; // ... getters/setters } // 方法签名可简化为: private boolean validateRequiredGoodsFields(GoodsValidationContext context, List<StringBuilder> errorList)

    3. 逻辑结构优化:校验方法的条件分支可以更紧凑

    • 问题描述:在新增的 validateSingleStoreModifyFields 方法中,对 shelfStyle 的校验逻辑略显松散。
    • 影响分析:虽然当前的写法不会引发 Bug(因为 Objects.equals(null, (byte) 2) 是安全的),但当 shelfStyle == null 成立并设置 valid = false 后,代码仍会继续执行后续的 if 判断,存在不必要的计算。
    • 优化建议:使用 else if 使逻辑互斥,或者在校验失败时直接 return false(如果业务允许短路校验的话),使代码意图更清晰。 java // 建议修改代码 if (shelfStyle == null) { setLineError(errorList, "类型只能为新增或修改", rowIndex); valid = false; } else if (Objects.equals(shelfStyle, (byte) 2) && StringUtils.isBlank(oldMerchantGoodsCode)) { setLineError(errorList, "修改类型时原上架ID不能为空", rowIndex); valid = false; } return valid;

    二、评分明细

    • 功能实现的正确性与健壮性(36/40分): 核心业务逻辑(提取校验逻辑、引入单店模式判断)重构正确。扣分点在于 findFirst().orElse(null) 配合 StringUtils.isBlank 带来的边界情况处理不够严谨,可能因脏数据导致假阳性通过。
    • 安全性与潜在风险(30/30分): 未发现 SQL 注入、越权等安全漏洞。没有违反 MyBatis 子查询及 Dubbo 接口变更的强制规则。
    • 最佳实践与可维护性(17/20分): 将 collect(toList) 改为 findFirst 是优秀的实践。提取 validateSingleStoreModifyFields 提升了代码复用率。扣分点在于私有方法的参数列表过长,且部分条件判断分支不够紧凑。
    • 性能与资源利用(5/5分): 流处理的短路优化(Short-circuiting)有效避免了在发现不匹配项后继续遍历整个集合,减少了不必要的 CPU 消耗,性能表现优秀。
    • 提交信息质量(5/5分): Commit message 格式规范,清晰地描述了重构点、新增枚举依赖以及移除/新增的校验逻辑,具有很强的可追溯性。

    总分:93分

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