From bd2cd961638622ae8c17ee1cc1c5678a025a52f7 Mon Sep 17 00:00:00 2001 From: dzq Date: Tue, 9 Dec 2025 11:26:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=99=BA=E8=83=BD=E6=9F=9C):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=95=86=E5=93=81=E5=88=97=E8=A1=A8=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E9=80=BB=E8=BE=91=E4=BB=A5=E9=81=BF=E5=85=8D=E7=A9=BA?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将商品ID列表提取为独立变量并添加空检查,避免在空列表时执行不必要的数据库查询。同时将商品列表设为final变量以确保线程安全。 --- .../SmartCabinetApplicationService.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/cabinet/smartCabinet/SmartCabinetApplicationService.java b/agileboot-domain/src/main/java/com/agileboot/domain/cabinet/smartCabinet/SmartCabinetApplicationService.java index b324454..ba8b687 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/cabinet/smartCabinet/SmartCabinetApplicationService.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/cabinet/smartCabinet/SmartCabinetApplicationService.java @@ -182,15 +182,24 @@ public class SmartCabinetApplicationService { .eq("deleted", false); List cabinetCells = cabinetCellService.list(cabinetCellEntityQueryWrapper); - QueryWrapper shopGoodsEntityQueryWrapper = new QueryWrapper<>(); - shopGoodsEntityQueryWrapper.in("goods_id", - cabinetCells.stream() + List goodsList = new ArrayList<>(); + List goodsIds = cabinetCells.stream() .map(CabinetCellEntity::getGoodsId) .filter(Objects::nonNull) .distinct() - .collect(Collectors.toList())) - .eq("deleted", false); - List goodsList = shopGoodsService.list(shopGoodsEntityQueryWrapper); + .collect(Collectors.toList()); + if (goodsIds != null && !goodsIds.isEmpty()) { + QueryWrapper shopGoodsEntityQueryWrapper = new QueryWrapper<>(); + shopGoodsEntityQueryWrapper.in("goods_id", + cabinetCells.stream() + .map(CabinetCellEntity::getGoodsId) + .filter(Objects::nonNull) + .distinct() + .collect(Collectors.toList())) + .eq("deleted", false); + goodsList = shopGoodsService.list(shopGoodsEntityQueryWrapper); + } + final List finalGoodsList = goodsList; List result = new ArrayList<>(); // 遍历每个智能柜构建详细信息 @@ -214,7 +223,7 @@ public class SmartCabinetApplicationService { // 处理单元格关联的商品信息 if (cell.getGoodsId() != null) { - ShopGoodsEntity goods = goodsList.stream() + ShopGoodsEntity goods = finalGoodsList.stream() .filter(g -> g.getGoodsId().equals(cell.getGoodsId())) .findFirst().orElse(null); if (goods != null) {