refactor(智能柜): 优化商品列表查询逻辑以避免空列表查询

将商品ID列表提取为独立变量并添加空检查,避免在空列表时执行不必要的数据库查询。同时将商品列表设为final变量以确保线程安全。
This commit is contained in:
dzq 2025-12-09 11:26:41 +08:00
parent 51830d153d
commit bd2cd96163
1 changed files with 16 additions and 7 deletions

View File

@ -182,15 +182,24 @@ public class SmartCabinetApplicationService {
.eq("deleted", false);
List<CabinetCellEntity> cabinetCells = cabinetCellService.list(cabinetCellEntityQueryWrapper);
QueryWrapper<ShopGoodsEntity> shopGoodsEntityQueryWrapper = new QueryWrapper<>();
shopGoodsEntityQueryWrapper.in("goods_id",
cabinetCells.stream()
List<ShopGoodsEntity> goodsList = new ArrayList<>();
List<Long> goodsIds = cabinetCells.stream()
.map(CabinetCellEntity::getGoodsId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList()))
.eq("deleted", false);
List<ShopGoodsEntity> goodsList = shopGoodsService.list(shopGoodsEntityQueryWrapper);
.collect(Collectors.toList());
if (goodsIds != null && !goodsIds.isEmpty()) {
QueryWrapper<ShopGoodsEntity> 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<ShopGoodsEntity> finalGoodsList = goodsList;
List<CabinetDetailDTO> 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) {