Commit 448bf1a1 by chenxi

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

1 parent 485e5546
......@@ -15,6 +15,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.enums.createBusinessApprove.ShelfTypeEnum;
......@@ -57,6 +58,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
import tk.mybatis.mapper.entity.Example;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
......@@ -162,6 +164,9 @@ public class GoodsListingApprovalService {
private MerchantGoodsInfoRemote merchantGoodsInfoRemote;
@Autowired
private GoodsReviewLogMapper goodsReviewLogMapper;
@Autowired
private SmartcarSceneMerchantRefNewMapper smartcarSceneMerchantRefNewMapper;
@Autowired
......@@ -452,6 +457,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())
......@@ -2869,6 +2875,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 方法中,当商品类型不为 PACKAGE 时,代码在两层嵌套的 for 循环内部调用了 smartcarMerchantGoodsInfoMapper.selectOne(condition) 进行数据库查询。
    • 影响分析:这是典型的 N+1 性能反模式。如果一个审批单包含几十个商品,每个商品包含多个明细,将会产生大量的单次数据库查询。这不仅会导致接口响应极慢,在高并发下极易耗尽数据库连接池,拖垮整个数据库。
    • 优化建议:应在循环前将所有需要的 merchantGoodsCode 收集起来,使用 IN 语句一次性查出所有对应的 MerchantGoodsInfo,并转换为 Map 存入内存,随后在循环中直接从 Map 中获取。
    • 代码示例: ```java // 1. 提前收集所有的 merchantGoodsCode Set merchantGoodsCodes = new HashSet<>(); if (CollectionUtils.isNotEmpty(context.products)) { 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 -> merchantGoodsCodes.add(d.getMerchantGoodsCode())); } } } }

    // 2. 批量查询并构建内存映射 (Map) Map goodsIdMap = new HashMap<>(); if (CollectionUtils.isNotEmpty(merchantGoodsCodes)) { Example example = new Example(MerchantGoodsInfo.class); example.createCriteria() .andIn("merchantGoodsCode", merchantGoodsCodes) .andEqualTo("deletedFlag", DeletedFlagEnum.N.name()); List goodsInfos = smartcarMerchantGoodsInfoMapper.selectByExample(example); goodsInfos.forEach(g -> goodsIdMap.put(g.getMerchantGoodsCode(), g.getId())); }

    // 3. 在原循环中直接从 Map 获取 ID,避免查库 // ... Integer goodsId = goodsIdMap.get(detail.getMerchantGoodsCode()); if (goodsId != null) { allLogs.add(buildReviewLog(null, goodsId, ...)); } // ...

    
    **2. 批量插入数据未做分片处理(潜在的 SQL 执行异常风险)**
    - **问题描述**:方法末尾直接调用了 `goodsReviewLogMapper.insertList(allLogs);`。MyBatis 的 `insertList` 通常会将其拼接成一条巨大的 `INSERT INTO ... VALUES (...), (...)...` SQL 语句。
    - **影响分析**:如果 `allLogs` 的数据量较大(例如超过几百条),拼接出的 SQL 语句长度可能会超过 MySQL 服务器配置的 `max_allowed_packet` 限制,导致 `PacketTooBigException` 异常,从而引起整个审批事务回滚失败。
    - **优化建议**:建议对 `allLogs` 集合进行分片(Chunk)插入,通常每批次 500 条左右为宜。可以使用 Guava 的 `Lists.partition` 或 Hutool 的 `ListUtil.split`。
    - **代码示例**:
    ```java
    if (CollectionUtils.isNotEmpty(allLogs)) {
        // 假设使用 Guava 的 Lists.partition
        List<List<GoodsReviewLog>> partitions = Lists.partition(allLogs, 500);
        for (List<GoodsReviewLog> partition : partitions) {
            goodsReviewLogMapper.insertList(partition);
        }
    }
    

    3. 集合遍历前缺少 Null 检查(潜在的 NullPointerException)

    • 问题描述:代码直接执行了 for (GoodsListingProductBaseDTO product : context.products),未对 context.products 进行判空处理。
    • 影响分析:虽然在上下文构建时可能赋予了初始值,但作为健壮性防御,如果 context.products 为 null,此处将抛出 NullPointerException,导致核心的上架审批主流程异常中断。
    • 优化建议:在执行遍历逻辑前,增加对 context.products 的判空防御。
    • 代码示例: java if (CollectionUtils.isEmpty(context.products)) { log.warn("上架审批上下文中产品列表为空, 跳过日志生成, businessCode={}", context.businessCode); return; } for (GoodsListingProductBaseDTO product : context.products) { // ... }

    二、评分明细

    • 功能实现的正确性与健壮性(32/40分): 业务逻辑实现基本满足需求,且未触碰“禁止使用子查询”和“Dubbo接口参数变更”的红线。但缺少对关键集合对象(如 context.products)的防御性判空检查,批量插入大集合存在使事务崩溃的隐患,扣除 8 分。
    • 安全性与潜在风险(30/30分): 代码使用了 MyBatis 框架提供的标准方法,有效防止了 SQL 注入;无明显的 XSS、越权或敏感信息泄露问题。满分。
    • 最佳实践与可维护性(12/20分): 方法命名语义清晰,代码结构可读性尚可。但出现了循环内查库的严重反模式(N+1),违反了服务端开发的基本最佳实践,扣除 8 分。
    • 性能与资源利用(0/5分): 由于在嵌套循环中执行单条数据库查询(selectOne),在数据量稍大时会导致严重的性能瓶颈和数据库连接资源浪费,此项不得分。
    • 提交信息质量(5/5分): Commit 信息(5.8.4 商品上架审核后,新增商品、套餐操作日志 (by chenxi))清晰说明了变更的版本、业务场景以及具体操作,具备良好的可追溯性。满分。

    总分:79分

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