From 050b026166b36c50e2226e18d6f398d22ce6af6f Mon Sep 17 00:00:00 2001 From: paulGao Date: Thu, 11 Aug 2022 11:57:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B8=85=E9=99=A4=E6=97=A0?= =?UTF-8?q?=E6=95=88=E4=BF=83=E9=94=80=E6=B4=BB=E5=8A=A8=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E4=B8=BA=E6=89=B9=E9=87=8F=E6=B8=85=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serviceimpl/SeckillServiceImpl.java | 3 +- .../serviceimpl/EsGoodsIndexServiceImpl.java | 93 +++++++++++-------- 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillServiceImpl.java index 72b061a3a7..c1d07d752f 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillServiceImpl.java @@ -1,5 +1,6 @@ package cn.lili.modules.promotion.serviceimpl; +import cn.hutool.core.convert.Convert; import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; @@ -249,7 +250,7 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl list) { - HashSet h = new HashSet(list); + HashSet h = new HashSet<>(list); list.clear(); list.addAll(h); } @@ -553,36 +553,37 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements */ @Override public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) { - ThreadUtil.execAsync(() -> { - for (int i = 1; ; i++) { - List skuIds; - //如果storeId不为空,则表示是店铺活动 - if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) { - PageVO pageVO = new PageVO(); - pageVO.setPageNumber(i); - pageVO.setPageSize(1000); - EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO(); - searchDTO.setStoreId(promotion.getStoreId()); - //查询出店铺商品 - SearchPage esGoodsIndices = goodsSearchService.searchGoods(searchDTO, pageVO); - - skuIds = esGoodsIndices.isEmpty() ? new ArrayList<>() : esGoodsIndices.getContent().stream().map(SearchHit::getId).collect(Collectors.toList()); - } else { - //否则是平台活动 - org.springframework.data.domain.Page all = goodsIndexRepository.findAll(PageRequest.of(i, 1000)); - - //查询出全部商品 - skuIds = all.isEmpty() ? new ArrayList<>() : all.toList().stream().map(EsGoodsIndex::getId).collect(Collectors.toList()); - } - if (skuIds.isEmpty()) { - break; - } - this.deleteEsGoodsPromotionByPromotionKey(skuIds, key); - this.updateEsGoodsIndexPromotions(skuIds, promotion, key); - } + ThreadUtil.execAsync(() -> this.executeUpdateEsGoodsIndexAll(promotion, key)); - }); + } + + private void executeUpdateEsGoodsIndexAll(BasePromotions promotion, String key) { + for (int i = 1; ; i++) { + List skuIds; + //如果storeId不为空,则表示是店铺活动 + if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) { + PageVO pageVO = new PageVO(); + pageVO.setPageNumber(i); + pageVO.setPageSize(1000); + EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO(); + searchDTO.setStoreId(promotion.getStoreId()); + //查询出店铺商品 + SearchPage esGoodsIndices = goodsSearchService.searchGoods(searchDTO, pageVO); + + skuIds = esGoodsIndices.isEmpty() ? new ArrayList<>() : esGoodsIndices.getContent().stream().map(SearchHit::getId).collect(Collectors.toList()); + } else { + //否则是平台活动 + org.springframework.data.domain.Page all = goodsIndexRepository.findAll(PageRequest.of(i, 1000)); + //查询出全部商品 + skuIds = all.isEmpty() ? new ArrayList<>() : all.toList().stream().map(EsGoodsIndex::getId).collect(Collectors.toList()); + } + if (skuIds.isEmpty()) { + break; + } + this.deleteEsGoodsPromotionByPromotionKey(skuIds, key); + this.updateEsGoodsIndexPromotions(skuIds, promotion, key); + } } @Override @@ -649,22 +650,32 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements */ @Override public void cleanInvalidPromotion() { - Iterable all = goodsIndexRepository.findAll(); - for (EsGoodsIndex goodsIndex : all) { - Map promotionMap = goodsIndex.getOriginPromotionMap(); - //获取商品索引 - if (promotionMap != null && !promotionMap.isEmpty()) { - //促销不为空则进行清洗 - promotionMap.entrySet().removeIf(i -> { - JSONObject promotionJson = JSONUtil.parseObj(i.getValue()); - BasePromotions promotion = promotionJson.toBean(BasePromotions.class); - return promotion.getEndTime() != null && promotion.getEndTime().getTime() < DateUtil.date().getTime(); - }); + ThreadUtil.execAsync(this::executeCleanInvalidPromotions); + } + + private void executeCleanInvalidPromotions() { + for (int i = 1; ; i++) { + org.springframework.data.domain.Page all = goodsIndexRepository.findAll(PageRequest.of(i, 1000)); + if (all.isEmpty()) { + break; + } + for (EsGoodsIndex goodsIndex : all.toList()) { + Map promotionMap = goodsIndex.getOriginPromotionMap(); + //获取商品索引 + if (promotionMap != null && !promotionMap.isEmpty()) { + //促销不为空则进行清洗 + promotionMap.entrySet().removeIf(j -> { + JSONObject promotionJson = JSONUtil.parseObj(j.getValue()); + BasePromotions promotion = promotionJson.toBean(BasePromotions.class); + return promotion.getEndTime() != null && promotion.getEndTime().getTime() < DateUtil.date().getTime(); + }); + } } + goodsIndexRepository.saveAll(all); } - goodsIndexRepository.saveAll(all); } + @Override public EsGoodsIndex findById(String id) { Optional goodsIndex = goodsIndexRepository.findById(id); -- Gitee