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; + } }