Commit d1ce1a9b by chenxi

5.8.4 商品上架审核后,新增更新商品、套餐日志

1 parent 448bf1a1
......@@ -553,6 +553,16 @@ public class GoodsListingApprovalService {
context.settleRulEffectRecordByMerchantIdMap = settleRulEffectRecords.stream().collect(Collectors.groupingBy(DivisionRuleEffectRecord::getMerchantId));
}
}
List<GoodsListingLog> logs = goodsListingLogService.listByBusinessCode(businessCode);
for (GoodsListingLog logItem : logs) {
if (Objects.equals(LogOperatorType.INITIATE.getValue(), logItem.getOperatorType())) {
context.listingCreateBy = logItem.getCreatedBy();
context.listingCreateDate = logItem.getCreatedDate();
} else if (Objects.equals(LogOperatorType.APPROVE.getValue(), logItem.getOperatorType())) {
context.listingApproveBy = logItem.getCreatedBy();
context.listingApproveDate = logItem.getCreatedDate();
}
}
return context;
}
......@@ -738,6 +748,7 @@ public class GoodsListingApprovalService {
GoodsListingProductDetailDTO detail = details.get(0);
MerchantGoodsInfo oldGoods = getActiveMerchantGoodsByCode(product.getOldMerchantGoodsCode());
RpcAssert.assertNotNull(oldGoods, DefaultErrorEnum.DATA_NOT_EXIST, "未找到原上架商品");
insertUpdateReviewLog(context, null, oldGoods.getId());
long refCount = countMerchantGoodsRefByGoodsId(oldGoods.getId());
if (refCount <= 1) {
publishSingleProductUpdateInPlace(context, detail, currentMerchantRef, oldGoods);
......@@ -796,6 +807,7 @@ public class GoodsListingApprovalService {
if (isSingleStoreModifyProduct(context, product)) {
MerchantPackageGoodsInfo oldPackage = getActiveMerchantPackageByCode(product.getOldMerchantGoodsCode());
RpcAssert.assertNotNull(oldPackage, DefaultErrorEnum.DATA_NOT_EXIST, "未找到原上架套餐");
insertUpdateReviewLog(context, oldPackage.getPackageCode(), null);
long refCount = countMerchantGoodsRefByPackageCode(oldPackage.getPackageCode());
List<Integer> oldChildIds = listMerchantGoodsIdsByPackageCode(oldPackage.getPackageCode());
if (refCount <= 1) {
......@@ -2876,20 +2888,11 @@ public class GoodsListingApprovalService {
}
private void createGoodsReviewLogsForListing(ApprovalPublishContext context) {
List<GoodsListingLog> logs = goodsListingLogService.listByBusinessCode(context.businessCode);
GoodsListingLog initiateLog = logs.stream()
.filter(l -> Objects.equals(LogOperatorType.INITIATE.getValue(), l.getOperatorType()))
.findFirst().orElse(null);
GoodsListingLog approveLog = logs.stream()
.filter(l -> Objects.equals(LogOperatorType.APPROVE.getValue(), l.getOperatorType()))
.findFirst().orElse(null);
if (initiateLog == null || approveLog == null) {
if (context.listingCreateBy == null || context.listingApproveBy == null) {
log.warn("未找到完整的GoodsListingLog, 跳过GoodsReviewLog写入, businessCode={}", context.businessCode);
return;
}
Date initiateDate = initiateLog.getCreatedDate();
Date approveDate = approveLog.getCreatedDate();
List<GoodsReviewLog> allLogs = new ArrayList<>();
Set<String> loggedPackageCodes = new HashSet<>();
......@@ -2898,9 +2901,9 @@ public class GoodsListingApprovalService {
if (Objects.equals(product.getProductType(), ProductTypeEnum.PACKAGE.getValue())) {
String packageCode = product.getPackageCode();
if (StringUtils.isNotBlank(packageCode) && loggedPackageCodes.add(packageCode)) {
allLogs.add(buildReviewLog(packageCode, null, GoodsReviewTypeEnum.CREATE, initiateLog.getCreatedBy(), initiateDate));
allLogs.add(buildReviewLog(packageCode, null, GoodsReviewTypeEnum.TO_AUDIT, initiateLog.getCreatedBy(), initiateDate));
allLogs.add(buildReviewLog(packageCode, null, GoodsReviewTypeEnum.AUDIT_TO_ON_SHELF, approveLog.getCreatedBy(), approveDate));
allLogs.add(buildReviewLog(packageCode, null, GoodsReviewTypeEnum.CREATE, context.listingCreateBy, context.listingCreateDate));
allLogs.add(buildReviewLog(packageCode, null, GoodsReviewTypeEnum.TO_AUDIT, context.listingCreateBy, context.listingCreateDate));
allLogs.add(buildReviewLog(packageCode, null, GoodsReviewTypeEnum.AUDIT_TO_ON_SHELF, context.listingApproveBy, context.listingApproveDate));
}
} else {
if (CollectionUtils.isNotEmpty(details)) {
......@@ -2910,9 +2913,9 @@ public class GoodsListingApprovalService {
condition.setDeletedFlag(DeletedFlagEnum.N.name());
MerchantGoodsInfo goodsInfo = smartcarMerchantGoodsInfoMapper.selectOne(condition);
if (goodsInfo != null) {
allLogs.add(buildReviewLog(null, goodsInfo.getId(), GoodsReviewTypeEnum.CREATE, initiateLog.getCreatedBy(), initiateDate));
allLogs.add(buildReviewLog(null, goodsInfo.getId(), GoodsReviewTypeEnum.TO_AUDIT, initiateLog.getCreatedBy(), initiateDate));
allLogs.add(buildReviewLog(null, goodsInfo.getId(), GoodsReviewTypeEnum.AUDIT_TO_ON_SHELF, approveLog.getCreatedBy(), approveDate));
allLogs.add(buildReviewLog(null, goodsInfo.getId(), GoodsReviewTypeEnum.CREATE, context.listingCreateBy, context.listingCreateDate));
allLogs.add(buildReviewLog(null, goodsInfo.getId(), GoodsReviewTypeEnum.TO_AUDIT, context.listingCreateBy, context.listingCreateDate));
allLogs.add(buildReviewLog(null, goodsInfo.getId(), GoodsReviewTypeEnum.AUDIT_TO_ON_SHELF, context.listingApproveBy, context.listingApproveDate));
}
}
}
......@@ -2940,6 +2943,15 @@ public class GoodsListingApprovalService {
return log;
}
private void insertUpdateReviewLog(ApprovalPublishContext context, String packageCode, Integer goodsId) {
if (context.listingApproveBy == null) {
return;
}
GoodsReviewLog log = buildReviewLog(packageCode, goodsId, GoodsReviewTypeEnum.UPDATE,
context.listingApproveBy, context.listingApproveDate);
goodsReviewLogMapper.insertSelective(log);
}
private static class ApprovalPublishContext {
private String businessCode;
private String updatedBy;
......@@ -2975,6 +2987,10 @@ public class GoodsListingApprovalService {
private Map<Integer, Boolean> settleRuleInPlaceUpdateMerchantMap = new HashMap<>();
private Map<Integer, Boolean> reconciliationRuleSplitMerchantMap = new HashMap<>();
private Map<Integer, Boolean> settleRuleSplitMerchantMap = new HashMap<>();
private String listingCreateBy;
private Date listingCreateDate;
private String listingApproveBy;
private Date listingApproveDate;
private Set<String> modifyExcludeMerchantGoodsCodes = new HashSet<>();
private Set<String> modifyExcludePackageCodes = new HashSet<>();
private List<DivisionRuleEffectRecord> pendingRuleEffectRecords = new ArrayList<>();
......
  • @wangxw1
    commented
    Master

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

    1. 逻辑一致性问题:日志遍历覆盖与原 findFirst() 语义不一致

    • 问题描述:在初始化 context 时(第 553-563 行),代码通过 for 循环遍历 logs 并赋值给 context.listingCreateBy 等变量。如果列表中存在多条 INITIATE 或 APPROVE 类型的日志,循环会不断覆盖变量,最终保留的是最后一条记录。而原代码(第 2888-2895 行)使用的是 .findFirst().orElse(null),获取的是第一条记录。
    • 影响分析:这会导致业务逻辑发生隐性变更。如果业务场景中存在多次发起或多次审核的情况,日志记录的操作人和时间将与重构前不一致,可能引发后续的数据追溯问题。
    • 优化建议:明确业务需求是需要“首次”还是“最后一次”操作记录。如果为了保持与原逻辑一致(取首次),建议在赋值前增加非空判断,或者改回 Stream 的 findFirst() 写法。 java // 建议修改方式(保持原 findFirst 语义): for (GoodsListingLog logItem : logs) { if (Objects.equals(LogOperatorType.INITIATE.getValue(), logItem.getOperatorType()) && context.listingCreateBy == null) { context.listingCreateBy = logItem.getCreatedBy(); context.listingCreateDate = logItem.getCreatedDate(); } else if (Objects.equals(LogOperatorType.APPROVE.getValue(), logItem.getOperatorType()) && context.listingApproveBy == null) { context.listingApproveBy = logItem.getCreatedBy(); context.listingApproveDate = logItem.getCreatedDate(); } }

    2. 健壮性问题:新增方法缺乏完整的防御性校验

    • 问题描述:在新增的 insertUpdateReviewLog 方法中(第 2943 行),仅对 context.listingApproveBy == null 进行了拦截返回,但没有对 context.listingApproveDate 进行校验。
    • 影响分析:虽然通常情况下 By 和 Date 会同时存在,但在极端脏数据或异常情况下,如果 Date 为空,可能会导致 buildReviewLog 构造出带有 null 时间的实体。如果数据库对应的时间字段(如 created_date)设置了 NOT NULL 约束,将会触发 SQL 异常,导致整个上架审核事务回滚。
    • 优化建议:完善空值校验,或者在 buildReviewLog 内部确保时间字段有默认值。 java private void insertUpdateReviewLog(ApprovalPublishContext context, String packageCode, Integer goodsId) { if (context.listingApproveBy == null || context.listingApproveDate == null) { return; } // ... }

    二、评分明细

    1. 功能实现的正确性与健壮性(37/40分)

      • 理由:整体功能实现达标,成功将局部查询提升至上下文(Context)中复用,减少了重复查询。但重构过程中的 for 循环赋值改变了原有 findFirst() 的语义,存在逻辑隐患;新增方法的防御性编程细节有待加强。未发现违反“子查询”或“Dubbo接口参数变更”的红线规则。
    2. 安全性与潜在风险(30/30分)

      • 理由:本次变更为纯后端业务逻辑调整,不涉及直接的用户输入拼接,无 SQL 注入、XSS 等安全风险,未发现越权或敏感信息泄露问题。
    3. 最佳实践与可维护性(19/20分)

      • 理由:代码结构清晰,使用了 Context 对象传递参数,避免了方法签名过长,符合面向对象设计原则。变量命名规范,可读性良好。扣除1分是因为 context 中的字段直接暴露(包级私有),虽在内部类中可接受,但若能提供 getter/setter 会更符合规范。
    4. 性能与资源利用(5/5分)

      • 理由:将原本在 createGoodsReviewLogsForListing 方法内部的数据库查询 goodsListingLogService.listByBusinessCode 提前到上下文初始化阶段,如果在其他地方也需要用到这些日志信息,能有效避免 N+1 或重复查询问题,属于正向的性能优化。
    5. 提交信息质量(5/5分)

      • 理由:Commit 信息(5.8.4 商品上架审核后,新增更新商品、套餐日志 (by chenxi))包含了版本号、业务动作及修改人,清晰准确,具有良好的可追溯性。

    总分:96分

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