refactor(智能柜): 优化商品列表查询逻辑以避免空列表查询
将商品ID列表提取为独立变量并添加空检查,避免在空列表时执行不必要的数据库查询。同时将商品列表设为final变量以确保线程安全。
This commit is contained in:
parent
51830d153d
commit
bd2cd96163
|
|
@ -182,6 +182,13 @@ public class SmartCabinetApplicationService {
|
||||||
.eq("deleted", false);
|
.eq("deleted", false);
|
||||||
List<CabinetCellEntity> cabinetCells = cabinetCellService.list(cabinetCellEntityQueryWrapper);
|
List<CabinetCellEntity> cabinetCells = cabinetCellService.list(cabinetCellEntityQueryWrapper);
|
||||||
|
|
||||||
|
List<ShopGoodsEntity> goodsList = new ArrayList<>();
|
||||||
|
List<Long> goodsIds = cabinetCells.stream()
|
||||||
|
.map(CabinetCellEntity::getGoodsId)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
if (goodsIds != null && !goodsIds.isEmpty()) {
|
||||||
QueryWrapper<ShopGoodsEntity> shopGoodsEntityQueryWrapper = new QueryWrapper<>();
|
QueryWrapper<ShopGoodsEntity> shopGoodsEntityQueryWrapper = new QueryWrapper<>();
|
||||||
shopGoodsEntityQueryWrapper.in("goods_id",
|
shopGoodsEntityQueryWrapper.in("goods_id",
|
||||||
cabinetCells.stream()
|
cabinetCells.stream()
|
||||||
|
|
@ -190,7 +197,9 @@ public class SmartCabinetApplicationService {
|
||||||
.distinct()
|
.distinct()
|
||||||
.collect(Collectors.toList()))
|
.collect(Collectors.toList()))
|
||||||
.eq("deleted", false);
|
.eq("deleted", false);
|
||||||
List<ShopGoodsEntity> goodsList = shopGoodsService.list(shopGoodsEntityQueryWrapper);
|
goodsList = shopGoodsService.list(shopGoodsEntityQueryWrapper);
|
||||||
|
}
|
||||||
|
final List<ShopGoodsEntity> finalGoodsList = goodsList;
|
||||||
|
|
||||||
List<CabinetDetailDTO> result = new ArrayList<>();
|
List<CabinetDetailDTO> result = new ArrayList<>();
|
||||||
// 遍历每个智能柜构建详细信息
|
// 遍历每个智能柜构建详细信息
|
||||||
|
|
@ -214,7 +223,7 @@ public class SmartCabinetApplicationService {
|
||||||
|
|
||||||
// 处理单元格关联的商品信息
|
// 处理单元格关联的商品信息
|
||||||
if (cell.getGoodsId() != null) {
|
if (cell.getGoodsId() != null) {
|
||||||
ShopGoodsEntity goods = goodsList.stream()
|
ShopGoodsEntity goods = finalGoodsList.stream()
|
||||||
.filter(g -> g.getGoodsId().equals(cell.getGoodsId()))
|
.filter(g -> g.getGoodsId().equals(cell.getGoodsId()))
|
||||||
.findFirst().orElse(null);
|
.findFirst().orElse(null);
|
||||||
if (goods != null) {
|
if (goods != null) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue