Commit 4bc8a7e3 by 周立明

refactor(goods-listing): optimize upgrade goods data query logic

1. 批量查询升级商品数据减少远程调用次数
2. 替换原循环单条查询为map缓存获取提升性能
3. 修复空列表返回问题,统一使用Collections.emptyList()
1 parent d5c85c85
......@@ -24,6 +24,7 @@ import cn.com.glsx.framework.core.util.DateUtils;
import cn.com.glsx.framework.core.util.RpcAssert;
import cn.com.glsx.scrm.equity.dto.service.ServiceItemsGradeConfigDTO;
import cn.com.glsx.scrm.equity.remote.ServiceItemsRemote;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
......@@ -263,9 +264,18 @@ public class GoodsListingProductBaseService {
//延保类、权益类
extendedInsuranceTypeIdList.addAll(equityTypeIdList);
List<Integer> allServiceItemIdList = this.getAllServiceItemIdList();
productBaseList.forEach(record ->{
List<Integer> upgradeGoodsIds = productBaseList.stream().flatMap(o -> o.getDetailList().stream()).filter(o -> Objects.nonNull(o.getUpgradeGoodsId()))
.map(GoodsListingProductDetailDTO::getUpgradeGoodsId).collect(Collectors.toList());
Map<Integer, UpgradeGoodsDTO> upgradeGoodsMap = new HashMap<>();
if(CollectionUtils.isNotEmpty(upgradeGoodsIds)) {
List<UpgradeGoodsDTO> upgradeGoodsList = goodsListingProductDetailService.getUpgradeGoodsByUpgradeGoodsIds(upgradeGoodsIds);
if(CollectionUtils.isNotEmpty(upgradeGoodsList)) {
upgradeGoodsMap = upgradeGoodsList.stream().collect(Collectors.toMap(UpgradeGoodsDTO::getId, o -> o, (o1, o2) -> o2));
}
}
for(GoodsListingProductBaseDTO record:productBaseList) {
Set<String> upgradeGoodsKeySet = new HashSet<>();
record.getDetailList().forEach(o->{
for(GoodsListingProductDetailDTO o:record.getDetailList()) {
RpcAssert.assertNotNull(o.getUpgradeGoodsId(), DefaultErrorEnum.DATA_NULL, "升级商品ID is null");
RpcAssert.assertNotNull(o.getSellPrice(), DefaultErrorEnum.DATA_NULL, "销售价格 is null");
RpcAssert.assertIsTrue(o.getSellPrice().compareTo(BigDecimal.ZERO) > 0, DefaultErrorEnum.SERVER_ERROR,"售价需大于0");
......@@ -277,7 +287,7 @@ public class GoodsListingProductBaseService {
}else {
RpcAssert.assertNotNull(o.getUpgradeProductCost(), DefaultErrorEnum.DATA_NULL, "升级成本 is null");
}
UpgradeGoodsDTO upgradeGoods = goodsListingProductDetailService.getUpgradeGoodsByUpgradeGoodsId(o.getUpgradeGoodsId());
UpgradeGoodsDTO upgradeGoods = upgradeGoodsMap.get(o.getUpgradeGoodsId());
RpcAssert.assertNotNull(upgradeGoods, DefaultErrorEnum.SERVER_ERROR, "升级商品不存在");
if(isScene0) {
//会员权益场景仅权益和延保商品品类
......@@ -305,8 +315,8 @@ public class GoodsListingProductBaseService {
upgradeGoodsKeySet.add(upgradeGoodsKey);
}
o.setUpgradeGoods(upgradeGoods);
});
});
}
}
} catch (RpcServiceException rpcServiceException) {
throw rpcServiceException;
} catch (Exception e) {
......
package cn.com.glsx.dj.goods.listing.service;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
......@@ -69,6 +70,22 @@ public class GoodsListingProductDetailService {
}
}
public List<UpgradeGoodsDTO> getUpgradeGoodsByUpgradeGoodsIds(List<Integer> upgradeGoodsIds) {
RpcAssert.assertNotEmpty(upgradeGoodsIds, DefaultErrorEnum.DATA_NULL, "upgradeGoodsIds不能为空");
try{
RpcPagination<UpgradeGoodsDTO> pagination = new RpcPagination<>();
pagination.setCondition(UpgradeGoodsDTO.builder().ids(upgradeGoodsIds).build());
pagination = upgradeGoodsRemote.listWithPage(pagination).getResult();
if(pagination != null && CollectionUtils.isNotEmpty(pagination.getList())) {
return pagination.getList();
}
return Collections.emptyList();
}catch (Exception e){
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
public List<GoodsListingProductDetailDTO> listByProductBaseId(Integer productBaseId) {
RpcAssert.assertNotNull(productBaseId, DefaultErrorEnum.DATA_NULL, "productBaseId不能为空");
try{
......
  • @wangxw1
    commented
    Master

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

    1. 批量查询使用分页接口未设置 PageSize,可能导致数据丢失(功能正确性,最重要)

    • 问题描述:在 GoodsListingProductDetailService.java 的 getUpgradeGoodsByUpgradeGoodsIds 方法中,使用了 listWithPage 分页接口来批量查询商品,但并未显式设置 pageSize。
    • 影响分析:通常分页对象(如 RpcPagination)会有一个默认的 pageSize(例如 10 或 20)。如果传入的 upgradeGoodsIds 数量超过默认页大小,RPC 服务端只会返回第一页的数据。这将导致外层组装的 upgradeGoodsMap 数据缺失,进而使后续校验 RpcAssert.assertNotNull(upgradeGoods, ... "升级商品不存在") 产生误判,直接阻断正常业务流程。
    • 优化建议:应根据传入的 ID 列表大小动态设置 pageSize,或者更好的是,在 Remote 接口中提供一个非分页的 listByIds 方法。 java RpcPagination<UpgradeGoodsDTO> pagination = new RpcPagination<>(); pagination.setCondition(UpgradeGoodsDTO.builder().ids(upgradeGoodsIds).build()); // 建议补充设置 pageSize,确保能查出所有传入的 ID 对应的数据 pagination.setPageSize(upgradeGoodsIds.size()); pagination = upgradeGoodsRemote.listWithPage(pagination).getResult();

    2. 收集批量查询 ID 时未去重,存在性能损耗(性能与资源利用)

    • 问题描述:在 GoodsListingProductBaseService.java 中,通过 Stream 提取 upgradeGoodsIds 时,未对提取出的 ID 进行去重处理。
    • 影响分析:如果 productBaseList 中的多个详情包含相同的 upgradeGoodsId,列表中会出现大量重复 ID。这不仅会增加 RPC 调用的网络传输负担,还会增加数据库执行 IN 查询时的解析成本。
    • 优化建议:在 Stream 采集中增加 .distinct() 操作。 java List<Integer> upgradeGoodsIds = productBaseList.stream() .flatMap(o -> o.getDetailList().stream()) .filter(o -> Objects.nonNull(o.getUpgradeGoodsId())) .map(GoodsListingProductDetailDTO::getUpgradeGoodsId) .distinct() // 增加去重 .collect(Collectors.toList());

    3. Stream 的 flatMap 操作存在潜在的 NullPointerException 风险(健壮性)

    • 问题描述:在提取 ID 的 Stream 操作中,直接调用了 o.getDetailList().stream()。
    • 影响分析:如果某个 GoodsListingProductBaseDTO 对象的 detailList 属性为 null,调用 .stream() 时将直接抛出 NullPointerException,导致整个业务流程异常中断。
    • 优化建议:在进行 flatMap 时进行防空处理。 java List<Integer> upgradeGoodsIds = productBaseList.stream() .flatMap(o -> Optional.ofNullable(o.getDetailList()).orElse(Collections.emptyList()).stream()) .filter(o -> Objects.nonNull(o.getUpgradeGoodsId())) .map(GoodsListingProductDetailDTO::getUpgradeGoodsId) .distinct() .collect(Collectors.toList());

    二、评分明细

    1. 功能实现的正确性与健壮性(25/40分)
      • 理由:本次重构将循环 RPC 调用改为批量查询,方向非常正确。但使用分页接口进行批量查询且未设置页大小,存在极高的数据截断风险(严重 Bug);此外 Stream 操作缺乏对集合的防空保护。扣 15 分。
    2. 安全性与潜在风险(30/30分)
      • 理由:代码中未发现 SQL 注入、越权、敏感信息泄露等安全隐患。未发现违规修改 Dubbo Remote 接口已有参数的情况(仅使用了 DTO 作为查询条件)。
    3. 最佳实践与可维护性(18/20分)
      • 理由:消除了典型的 N+1 查询问题,将 forEach 替换为常规 for 循环以便于异常抛出,符合工程最佳实践。代码结构清晰,但防御性编程意识(防空指针)有待加强。扣 2 分。
    4. 性能与资源利用(4/5分)
      • 理由:批量查询大幅提升了性能,但缺少 ID 去重机制,导致 RPC 负载和 DB 查询存在不必要的资源浪费。扣 1 分。
    5. 提交信息质量(5/5分)
      • 理由:Commit message 格式规范(使用了 refactor 标签),并且清晰地分点列出了优化的目的和具体的改动内容,可追溯性极强。

    总分:82分

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