diff --git a/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java b/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java index 7f3c3536d..ee92e1d51 100644 --- a/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java +++ b/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java @@ -153,9 +153,8 @@ public interface GoodsService extends IService { * 修改商品库存数量 * * @param goodsId 商品ID - * @param quantity 库存数量 */ - void updateStock(String goodsId, Integer quantity); + void updateStock(String goodsId); /** * 更新商品评价数量 diff --git a/framework/src/main/java/cn/lili/modules/goods/service/GoodsSkuService.java b/framework/src/main/java/cn/lili/modules/goods/service/GoodsSkuService.java index ed84429a6..ad64f849f 100644 --- a/framework/src/main/java/cn/lili/modules/goods/service/GoodsSkuService.java +++ b/framework/src/main/java/cn/lili/modules/goods/service/GoodsSkuService.java @@ -271,4 +271,12 @@ public interface GoodsSkuService extends IService { * @param commentNum 评论数量 */ void updateGoodsSkuGrade(String goodsId, double grade,int commentNum); + + /** + * 获取最新商品库存 + * + * @param goodsId 商品ID + * @return 库存数量 + */ + Integer getGoodsStock(String goodsId); } \ No newline at end of file diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java index 310bcba5b..b3c9e18e7 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java @@ -441,10 +441,11 @@ public class GoodsServiceImpl extends ServiceImpl implements } @Override - @SystemLogPoint(description = "修改商品库存", customerLog = "'操作的商品ID:['+#goodsId+'],修改后的库存:['+#quantity+']'") - public void updateStock(String goodsId, Integer quantity) { + @SystemLogPoint(description = "同步商品库存", customerLog = "'同步商品商品ID的库存:['+#goodsId+']'") + public void updateStock(String goodsId) { LambdaUpdateWrapper lambdaUpdateWrapper = Wrappers.lambdaUpdate(); - lambdaUpdateWrapper.set(Goods::getQuantity, quantity); + Integer stock = goodsSkuService.getGoodsStock(goodsId); + lambdaUpdateWrapper.set(Goods::getQuantity, stock); lambdaUpdateWrapper.eq(Goods::getId, goodsId); cache.remove(CachePrefix.GOODS.getPrefix() + goodsId); this.update(lambdaUpdateWrapper); diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java index 0f6cefe31..b1851d17b 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java @@ -519,16 +519,13 @@ public class GoodsSkuServiceImpl extends ServiceImpl i //统计每个商品的库存 for (Map.Entry> entry : groupByGoodsIds.entrySet()) { //库存 - Integer quantity = 0; for (GoodsSkuStockDTO goodsSku : entry.getValue()) { goodsSkuStockDTOS.stream().filter(i -> i.getSkuId().equals(goodsSku.getSkuId())).findFirst().ifPresent(i -> goodsSku.setQuantity(i.getQuantity())); - if (entry.getKey().equals(goodsSku.getGoodsId())) { - quantity += goodsSku.getQuantity(); - } + this.updateStock(goodsSku.getSkuId(), goodsSku.getQuantity()); } //保存商品库存结果 - goodsService.updateStock(entry.getKey(), quantity); + goodsService.updateStock(entry.getKey()); } } @@ -588,7 +585,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl i if (isFlag && CharSequenceUtil.equals(goodsSku.getMarketEnable(), GoodsStatusEnum.UPPER.name())) { List goodsIds = new ArrayList<>(); goodsIds.add(goodsSku.getGoodsId()); - applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("更新商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.UPDATE_GOODS_INDEX.name(), goodsIds)); + applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("更新商品", rocketmqCustomProperties.getGoodsTopic(), + GoodsTagsEnum.UPDATE_GOODS_INDEX.name(), goodsIds)); } } } @@ -622,7 +620,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i this.updateStock(goodsSku.getId(), goodsSku.getQuantity()); } //保存商品库存结果 - goodsService.updateStock(goodsId, quantity); + goodsService.updateStock(goodsId); } @@ -656,7 +654,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl i public Long countSkuNum(String storeId) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(GoodsSku::getStoreId, storeId).eq(GoodsSku::getDeleteFlag, Boolean.FALSE).eq(GoodsSku::getAuthFlag, GoodsAuthEnum.PASS.name()).eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name()); + queryWrapper.eq(GoodsSku::getStoreId, storeId).eq(GoodsSku::getDeleteFlag, Boolean.FALSE).eq(GoodsSku::getAuthFlag, + GoodsAuthEnum.PASS.name()).eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name()); return this.count(queryWrapper); } @@ -695,6 +694,19 @@ public class GoodsSkuServiceImpl extends ServiceImpl i this.getSkuIdsByGoodsId(goodsId).forEach(this::clearCache); } + @Override + public Integer getGoodsStock(String goodsId) { + List skuIds = this.getSkuIdsByGoodsId(goodsId); + + Integer stock = 0; + + for (String skuId : skuIds) { + stock += this.getStock(skuId); + } + return stock; + + } + /** * 渲染商品sku * diff --git a/framework/src/main/java/cn/lili/modules/member/serviceimpl/FootprintServiceImpl.java b/framework/src/main/java/cn/lili/modules/member/serviceimpl/FootprintServiceImpl.java index a0eb0bded..714157a0a 100644 --- a/framework/src/main/java/cn/lili/modules/member/serviceimpl/FootprintServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/member/serviceimpl/FootprintServiceImpl.java @@ -85,17 +85,16 @@ public class FootprintServiceImpl extends ServiceImpl collect = IntStream.range(0, goodsSkuByIdFromCache.size()) .mapToObj(i -> { if (goodsSkuByIdFromCache.get(i) == null) { - EsGoodsIndex esGoodsIndex = new EsGoodsIndex(); - FootPrint footPrint = footPrintPages.getRecords().get(i); - esGoodsIndex.setGoodsId(footPrint.getGoodsId()); - esGoodsIndex.setId(footPrint.getSkuId()); - esGoodsIndex.setReleaseTime(footPrintPages.getRecords().get(i).getCreateTime().getTime()); - return esGoodsIndex; + return null; } - Optional first = footPrintPages.getRecords().stream().filter(j -> j.getSkuId().equals(goodsSkuByIdFromCache.get(i).getId())).findFirst(); + Optional first = + footPrintPages.getRecords().stream().filter(j -> j.getSkuId().equals(goodsSkuByIdFromCache.get(i).getId())).findFirst(); return first.map(footPrint -> new EsGoodsIndex(goodsSkuByIdFromCache.get(i), footPrint.getCreateTime())).orElseGet(() -> new EsGoodsIndex(goodsSkuByIdFromCache.get(i))); }) .collect(Collectors.toList()); + + collect.removeIf(Objects::isNull); + esGoodsIndexIPage.setPages(footPrintPages.getPages()); esGoodsIndexIPage.setRecords(collect); esGoodsIndexIPage.setTotal(footPrintPages.getTotal());