diff --git a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java index 5a5975d4..9091ce03 100644 --- a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java +++ b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java @@ -1,7 +1,6 @@ package cn.lili.listener; import cn.hutool.json.JSONUtil; -import cn.lili.rocketmq.tags.GoodsTagsEnum; import cn.lili.event.GoodsCommentCompleteEvent; import cn.lili.modules.distribution.entity.dos.DistributionGoods; import cn.lili.modules.distribution.entity.dos.DistributionSelectedGoods; @@ -10,6 +9,9 @@ import cn.lili.modules.distribution.service.DistributionSelectedGoodsService; import cn.lili.modules.goods.entity.dos.Goods; import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.entity.dto.GoodsCompleteMessage; +import cn.lili.modules.goods.entity.dto.GoodsParamsDTO; +import cn.lili.modules.goods.entity.enums.GoodsAuthEnum; +import cn.lili.modules.goods.entity.enums.GoodsStatusEnum; import cn.lili.modules.goods.service.GoodsService; import cn.lili.modules.goods.service.GoodsSkuService; import cn.lili.modules.member.entity.dos.FootPrint; @@ -19,6 +21,7 @@ import cn.lili.modules.member.service.GoodsCollectionService; import cn.lili.modules.search.entity.dos.EsGoodsIndex; import cn.lili.modules.search.service.EsGoodsIndexService; import cn.lili.modules.store.service.StoreService; +import cn.lili.rocketmq.tags.GoodsTagsEnum; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import lombok.extern.slf4j.Slf4j; @@ -98,11 +101,37 @@ public class GoodsMessageListener implements RocketMQListener { break; //生成索引 case GENERATOR_GOODS_INDEX: - String goodsIndexJsonStr = new String(messageExt.getBody()); - List goodsIndices = JSONUtil.toList(JSONUtil.parseArray(goodsIndexJsonStr), EsGoodsIndex.class); - for (EsGoodsIndex goodsIndex : goodsIndices) { - log.info("生成商品索引" + goodsIndex); - this.goodsIndexService.addIndex(goodsIndex); + String goodsJsonStr = new String(messageExt.getBody()); + Goods goods = JSONUtil.toBean(goodsJsonStr, Goods.class); + //如果商品通过审核&&并且已上架 + List goodsSkuList = this.goodsSkuService.list(new LambdaQueryWrapper().eq(GoodsSku::getGoodsId, goods.getId())); + if (goods.getIsAuth().equals(GoodsAuthEnum.PASS.name()) + && goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name()) + && Boolean.FALSE.equals(goods.getDeleteFlag())) { + for (GoodsSku goodsSku : goodsSkuList) { + EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId()); + EsGoodsIndex goodsIndex = new EsGoodsIndex(goodsSku); + if (goods.getParams() != null && !goods.getParams().isEmpty()) { + List goodsParamDTOS = JSONUtil.toList(goods.getParams(), GoodsParamsDTO.class); + goodsIndex = new EsGoodsIndex(goodsSku, goodsParamDTOS); + } + //如果商品库存不为0,并且es中有数据 + if (goodsSku.getQuantity() > 0 && esGoodsOld == null) { + log.info("生成商品索引 {}", goodsIndex); + this.goodsIndexService.addIndex(goodsIndex); + } else if (goodsSku.getQuantity() > 0 && esGoodsOld != null) { + goodsIndexService.updateIndex(goodsIndex); + } + } + } + //如果商品状态值不支持es搜索,那么将商品信息做下架处理 + else { + for (GoodsSku goodsSku : goodsSkuList) { + EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId()); + if (esGoodsOld != null) { + goodsIndexService.deleteIndexById(goodsSku.getId()); + } + } } break; //审核商品 @@ -143,7 +172,7 @@ public class GoodsMessageListener implements RocketMQListener { this.goodsBuyComplete(messageExt); break; default: - log.error("商品执行异常:", new String(messageExt.getBody())); + log.error("商品执行异常:{}", new String(messageExt.getBody())); break; } } 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 d484f1fd..323b11b9 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 @@ -8,14 +8,10 @@ import cn.hutool.json.JSONUtil; import cn.lili.cache.Cache; import cn.lili.common.enums.ResultCode; import cn.lili.common.exception.ServiceException; -import cn.lili.rocketmq.RocketmqSendCallbackBuilder; -import cn.lili.rocketmq.tags.GoodsTagsEnum; -import cn.lili.common.security.context.UserContext; -import cn.lili.mybatis.util.PageUtil; import cn.lili.common.properties.RocketmqCustomProperties; +import cn.lili.common.security.context.UserContext; import cn.lili.modules.goods.entity.dos.Goods; import cn.lili.modules.goods.entity.dos.GoodsSku; -import cn.lili.modules.goods.entity.dto.GoodsParamsDTO; import cn.lili.modules.goods.entity.dto.GoodsSearchParams; import cn.lili.modules.goods.entity.dto.GoodsSkuStockDTO; import cn.lili.modules.goods.entity.enums.GoodsAuthEnum; @@ -36,6 +32,9 @@ import cn.lili.modules.member.service.MemberEvaluationService; import cn.lili.modules.search.entity.dos.EsGoodsAttribute; import cn.lili.modules.search.entity.dos.EsGoodsIndex; import cn.lili.modules.search.service.EsGoodsIndexService; +import cn.lili.mybatis.util.PageUtil; +import cn.lili.rocketmq.RocketmqSendCallbackBuilder; +import cn.lili.rocketmq.tags.GoodsTagsEnum; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -486,41 +485,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl i * @param goods 商品信息 */ private void generateEsCheck(Goods goods) { - //如果商品通过审核&&并且已上架 - List goodsSkuList = this.list(new LambdaQueryWrapper().eq(GoodsSku::getGoodsId, goods.getId())); - if (goods.getIsAuth().equals(GoodsAuthEnum.PASS.name()) - && goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name()) - && Boolean.FALSE.equals(goods.getDeleteFlag())) { - List goodsIndices = new ArrayList<>(); - for (GoodsSku goodsSku : goodsSkuList) { - EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId()); - EsGoodsIndex goodsIndex = new EsGoodsIndex(goodsSku); - if (goods.getParams() != null && !goods.getParams().isEmpty()) { - List goodsParamDTOS = JSONUtil.toList(goods.getParams(), GoodsParamsDTO.class); - goodsIndex = new EsGoodsIndex(goodsSku, goodsParamDTOS); - } - //如果商品库存不为0,并且es中有数据 - if (goodsSku.getQuantity() > 0 && esGoodsOld == null) { - goodsIndices.add(goodsIndex); - } else if (goodsSku.getQuantity() > 0 && esGoodsOld != null) { - goodsIndexService.updateIndex(goodsIndex); - } - } - if (!goodsIndices.isEmpty()) { - String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(); - //发送mq消息 - rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIndices), RocketmqSendCallbackBuilder.commonCallback()); - } - } - //如果商品状态值不支持es搜索,那么将商品信息做下架处理 - else { - for (GoodsSku goodsSku : goodsSkuList) { - EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId()); - if (esGoodsOld != null) { - goodsIndexService.deleteIndexById(goodsSku.getId()); - } - } - } + String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(); + //发送mq消息 + rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goods), RocketmqSendCallbackBuilder.commonCallback()); } /**