Commit 3e69ee33 by chenxi

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

1 parent 72f90398
......@@ -13,6 +13,7 @@ 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.CarLifeEnum.GoodsReviewTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.*;
import cn.com.glsx.dj.smartcarlife.enums.createBusinessApprove.ProductTypeEnum;
import cn.com.glsx.dj.smartcarlife.model.*;
......@@ -47,6 +48,7 @@ import org.springframework.transaction.support.TransactionSynchronizationAdapter
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
......@@ -152,6 +154,9 @@ public class GoodsListingApprovalService {
private MerchantGoodsInfoRemote merchantGoodsInfoRemote;
@Autowired
private GoodsReviewLogMapper goodsReviewLogMapper;
@Autowired
private SmartcarSceneMerchantRefNewMapper smartcarSceneMerchantRefNewMapper;
@Autowired
......@@ -399,6 +404,7 @@ public class GoodsListingApprovalService {
try {
ApprovalPublishContext context = buildApprovalPublishContext(businessCode, updatedBy);
publishApprovedProducts(context);
createGoodsReviewLogsForListing(context);
publishApprovedRulesByMerchant(context);
goodsListingApprovalMapper.updateByPrimaryKeySelective(GoodsListingApproval.builder()
.id(context.approval.getId()).systemStatus(SystemStatus.APPROVED.getValue())
......@@ -2013,6 +2019,71 @@ 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) {
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<>();
for (GoodsListingProductBaseDTO product : context.products) {
List<GoodsListingProductDetailDTO> details = getCachedList(context.detailsByProductBaseIdMap, product.getId());
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));
}
} else {
if (CollectionUtils.isNotEmpty(details)) {
for (GoodsListingProductDetailDTO detail : details) {
MerchantGoodsInfo condition = new MerchantGoodsInfo();
condition.setMerchantGoodsCode(detail.getMerchantGoodsCode());
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));
}
}
}
}
}
if (CollectionUtils.isNotEmpty(allLogs)) {
goodsReviewLogMapper.insertList(allLogs);
}
}
private GoodsReviewLog buildReviewLog(String packageCode, Integer goodsId,
GoodsReviewTypeEnum type, String operator, Date reviewDate) {
GoodsReviewLog log = new GoodsReviewLog();
log.setPackageCode(packageCode);
log.setGoodsId(goodsId);
log.setReviewType(type.getValue());
log.setReviewMessage(MessageFormat.format(type.getReviewMessage(), operator));
log.setReviewDate(reviewDate);
log.setCreatedBy(operator);
log.setCreatedDate(reviewDate);
log.setUpdatedBy(operator);
log.setUpdatedDate(reviewDate);
log.setDeletedFlag(DeletedFlagEnum.N.name());
return log;
}
private static class ApprovalPublishContext {
private String businessCode;
private String updatedBy;
......
  • @wangxw1
    commented
    Master

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

    1. 严重的性能瓶颈:在嵌套循环中执行数据库查询(N+1问题)

    • 问题描述:在 createGoodsReviewLogsForListing 方法中,针对非套餐类型的商品,代码在两层嵌套循环(product 和 detail)内部调用了 smartcarMerchantGoodsInfoMapper.selectOne(condition) 进行数据库查询。
    • 影响分析:这是经典的 N+1 性能问题。如果一个审批单包含多个商品,且每个商品有多个明细,将会产生大量的数据库单次查询请求。这极易耗尽数据库连接池资源,导致整个审批接口响应时间剧增甚至超时。
    • 优化建议:将循环内的查询改为批量查询。先遍历提取所有的 merchantGoodsCode,通过一次 IN 查询获取所有的 MerchantGoodsInfo,将其放入 Map<String, MerchantGoodsInfo> 中,然后在循环中从内存 Map 取值。
    • 代码示例: ```java // 1. 提前收集所有的 merchantGoodsCode Set goodsCodes = new HashSet<>(); for (GoodsListingProductBaseDTO product : context.products) { if (!Objects.equals(product.getProductType(), ProductTypeEnum.PACKAGE.getValue())) { List details = getCachedList(context.detailsByProductBaseIdMap, product.getId()); if (CollectionUtils.isNotEmpty(details)) { details.forEach(d -> goodsCodes.add(d.getMerchantGoodsCode())); } } }

    // 2. 批量查询并构建 Map (需在 Mapper 中新增 selectByCodes 方法) Map goodsInfoMap = new HashMap<>(); if (CollectionUtils.isNotEmpty(goodsCodes)) { List goodsList = smartcarMerchantGoodsInfoMapper.selectByCodes(goodsCodes); goodsInfoMap = goodsList.stream().collect(Collectors.toMap(MerchantGoodsInfo::getMerchantGoodsCode, g -> g)); }

    // 3. 在原有循环中从 Map 获取数据,替换 selectOne // MerchantGoodsInfo goodsInfo = goodsInfoMap.get(detail.getMerchantGoodsCode());

    
    **2. 健壮性风险:缺少对集合的判空保护,存在 NPE 风险**
    - **问题描述**:在 `createGoodsReviewLogsForListing` 方法中,直接对 `context.products` 进行了增强 for 循环遍历:`for (GoodsListingProductBaseDTO product : context.products)`。
    - **影响分析**:如果由于上游逻辑异常或脏数据导致 `context.products` 为 `null`,此处将抛出 `NullPointerException`,从而导致整个核心审批流程(`processApproval`)异常中断并回滚。
    - **优化建议**:在执行循环前,使用 `CollectionUtils.isEmpty(context.products)` 进行前置防御性检查。
    - **代码示例**:
    ```java
    if (CollectionUtils.isEmpty(context.products)) {
        return;
    }
    for (GoodsListingProductBaseDTO product : context.products) {
        // ...
    }
    

    3. 架构与最佳实践:边缘业务(日志记录)与核心业务强耦合

    • 问题描述:createGoodsReviewLogsForListing 作为新增的日志记录逻辑,被同步调用并嵌入在核心的商品审批流程(processApproval)中。
    • 影响分析:日志记录属于非核心边缘业务,如果 goodsReviewLogMapper.insertList(allLogs) 因为数据库抖动或数据超长等原因抛出异常,会导致核心的审批业务失败回滚。这违背了高可用设计中的“核心与非核心业务隔离”原则。
    • 优化建议:建议将该日志记录方法使用 try-catch 包裹,或者更好的方案是利用 Spring 的 @Async 异步执行,亦或通过 Spring ApplicationEventPublisher 发布事件,实现与主事务的解耦。

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分):业务逻辑基本实现需求,能正确区分套餐与普通商品并生成对应日志。但缺少对核心集合对象的判空处理,存在潜在的 NPE 风险,扣除5分。
    • 安全性与潜在风险(30/30分):未发现 SQL 注入、XSS、越权等安全漏洞。没有违反 MyBatis 子查询和 Dubbo 接口参数变更的红线规则。
    • 最佳实践与可维护性(15/20分):代码结构尚可,提取了 buildReviewLog 方法提升了复用性。但将日志记录与核心事务强耦合,未做隔离或容错处理,扣除5分。
    • 性能与资源利用(1/5分):存在严重的 For 循环内执行数据库查询(N+1)的反模式,在生产环境下极易引发性能瓶颈,扣除4分。
    • 提交信息质量(4/5分):Commit 信息(5.8.4 商品上架审核后,新增商品、套餐操作日志)清晰描述了业务动作和目的,具备可追溯性。若能简述实现方式会更好,扣除1分。

    总分:85分

  • @chenxi

    mentioned in commit 7df725d6

    mentioned in commit 7df725d6

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