From 31fb55635ef0a62706ef63774040bed790469163 Mon Sep 17 00:00:00 2001 From: paulGao Date: Fri, 13 May 2022 18:18:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=9F=E6=88=90=E5=95=86?= =?UTF-8?q?=E5=93=81=E7=B4=A2=E5=BC=95=EF=BC=8C=E5=88=86=E6=89=B9=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serviceimpl/EsGoodsIndexServiceImpl.java | 51 ++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index f89582319..6c4626d2b 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -1,5 +1,6 @@ package cn.lili.modules.search.serviceimpl; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.thread.ThreadUtil; @@ -37,6 +38,8 @@ import cn.lili.rocketmq.RocketmqSendCallbackBuilder; import cn.lili.rocketmq.tags.GoodsTagsEnum; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.assertj.core.util.IterableUtil; @@ -137,34 +140,46 @@ public void init() { ThreadUtil.execAsync(() -> { try { - List esGoodsIndices = new ArrayList<>(); LambdaQueryWrapper goodsQueryWrapper = new LambdaQueryWrapper<>(); goodsQueryWrapper.eq(Goods::getAuthFlag, GoodsAuthEnum.PASS.name()); goodsQueryWrapper.eq(Goods::getMarketEnable, GoodsStatusEnum.UPPER.name()); goodsQueryWrapper.eq(Goods::getDeleteFlag, false); - for (Goods goods : goodsService.list(goodsQueryWrapper)) { - LambdaQueryWrapper skuQueryWrapper = new LambdaQueryWrapper<>(); - skuQueryWrapper.eq(GoodsSku::getGoodsId, goods.getId()); - skuQueryWrapper.eq(GoodsSku::getAuthFlag, GoodsAuthEnum.PASS.name()); - skuQueryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name()); - skuQueryWrapper.eq(GoodsSku::getDeleteFlag, false); - - List goodsSkuList = goodsSkuService.list(skuQueryWrapper); - int skuSource = 100; - for (GoodsSku goodsSku : goodsSkuList) { - EsGoodsIndex esGoodsIndex = wrapperEsGoodsIndex(goodsSku, goods); - esGoodsIndex.setSkuSource(skuSource--); - esGoodsIndices.add(esGoodsIndex); - //库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量 - cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity()); + for (int i = 1; ; i++) { + List esGoodsIndices = new ArrayList<>(); + IPage page = new Page<>(i, 1000); + IPage goodsIPage = goodsService.page(page, goodsQueryWrapper); + if (goodsIPage == null || CollUtil.isEmpty(goodsIPage.getRecords())) { + break; } - + for (Goods goods : goodsIPage.getRecords()) { + LambdaQueryWrapper skuQueryWrapper = new LambdaQueryWrapper<>(); + skuQueryWrapper.eq(GoodsSku::getGoodsId, goods.getId()); + skuQueryWrapper.eq(GoodsSku::getAuthFlag, GoodsAuthEnum.PASS.name()); + skuQueryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name()); + skuQueryWrapper.eq(GoodsSku::getDeleteFlag, false); + for (int j = 1; ; j++) { + IPage skuPage = new Page<>(j, 100); + IPage skuIPage = goodsSkuService.page(skuPage, skuQueryWrapper); + if (skuIPage == null || CollUtil.isEmpty(skuIPage.getRecords())) { + break; + } + int skuSource = 100; + for (GoodsSku goodsSku : skuIPage.getRecords()) { + EsGoodsIndex esGoodsIndex = wrapperEsGoodsIndex(goodsSku, goods); + esGoodsIndex.setSkuSource(skuSource--); + esGoodsIndices.add(esGoodsIndex); + //库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量 + cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity()); + } + } + } + this.initIndex(esGoodsIndices); } + //初始化商品索引 - this.initIndex(esGoodsIndices); } catch (Exception e) { log.error("商品索引生成异常:", e); //如果出现异常,则将进行中的任务标识取消掉,打印日志