From 58aa981c1c0458855a6360973237970fb222cc30 Mon Sep 17 00:00:00 2001 From: dy-hu Date: Thu, 27 Jun 2024 17:44:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=A0=E9=81=93=E4=BC=98=E6=83=A0=E5=88=B8?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E4=BC=98=E6=83=A0=E5=88=B8?= =?UTF-8?q?=E5=85=B3=E8=81=94=E5=95=86=E5=93=81=E9=87=8D=E5=A4=8D=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/product/impl/CereShopProductServiceImpl.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cereshop-business/src/main/java/com/shop/cereshop/business/service/product/impl/CereShopProductServiceImpl.java b/cereshop-business/src/main/java/com/shop/cereshop/business/service/product/impl/CereShopProductServiceImpl.java index 02f1225..524006f 100644 --- a/cereshop-business/src/main/java/com/shop/cereshop/business/service/product/impl/CereShopProductServiceImpl.java +++ b/cereshop-business/src/main/java/com/shop/cereshop/business/service/product/impl/CereShopProductServiceImpl.java @@ -61,7 +61,9 @@ import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -868,6 +870,9 @@ public class CereShopProductServiceImpl implements CereShopProductService { list=cereShopProductDAO.getProducts(param); } + //可能会存在重复商品元素。去重处理 + list = list.stream().filter(distinctByKey(CanvasProduct::getProductId)).collect(Collectors.toList()); + List productIdList = new ArrayList<>(); productIdList.addAll(list.stream().map(CanvasProduct::getProductId).collect(Collectors.toList())); productIdList = productIdList.stream().filter(item -> item != null && item > 0).distinct().collect(Collectors.toList()); @@ -1355,4 +1360,9 @@ public class CereShopProductServiceImpl implements CereShopProductService { stringRedisService.set(skuTotalStockKey, sku.getTotal()); } } + + public static Predicate distinctByKey(Function keyExtractor) { + Map seen = new ConcurrentHashMap<>(); + return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; + } }