diff --git a/framework/src/main/java/cn/lili/modules/goods/entity/dos/Goods.java b/framework/src/main/java/cn/lili/modules/goods/entity/dos/Goods.java index 7a079046..6e9aafad 100644 --- a/framework/src/main/java/cn/lili/modules/goods/entity/dos/Goods.java +++ b/framework/src/main/java/cn/lili/modules/goods/entity/dos/Goods.java @@ -13,7 +13,6 @@ import cn.lili.modules.goods.entity.enums.GoodsStatusEnum; import cn.lili.modules.goods.entity.enums.GoodsTypeEnum; import cn.lili.mybatis.BaseEntity; import com.baomidou.mybatisplus.annotation.TableName; -import com.fasterxml.jackson.annotation.JsonIgnore; import com.xkcoding.http.util.StringUtil; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -148,7 +147,6 @@ public class Goods extends BaseEntity { private String goodsType; @ApiModelProperty(value = "商品参数json", hidden = true) - @JsonIgnore private String params; 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 a78f9bb0..2d52036d 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 @@ -181,13 +181,6 @@ public interface GoodsSkuService extends IService { */ void updateGoodsSkuStatusByStoreId(String storeId, String marketEnable, String authFlag); - /** - * 发送生成ES商品索引 - * - * @param goods 商品信息 - */ - void generateEs(Goods goods); - /** * 更新SKU库存 * 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 230b916e..b44250b4 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 @@ -7,6 +7,7 @@ import cn.hutool.json.JSONUtil; import cn.lili.cache.Cache; import cn.lili.cache.CachePrefix; import cn.lili.common.enums.ResultCode; +import cn.lili.common.event.TransactionCommitSendMQEvent; import cn.lili.common.exception.ServiceException; import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.common.security.AuthUser; @@ -50,6 +51,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -106,6 +108,9 @@ public class GoodsServiceImpl extends ServiceImpl implements @Autowired private RocketmqCustomProperties rocketmqCustomProperties; + + @Autowired + private ApplicationEventPublisher applicationEventPublisher; @Autowired private FreightTemplateService freightTemplateService; @@ -175,6 +180,7 @@ public class GoodsServiceImpl extends ServiceImpl implements if (goodsOperationDTO.getGoodsGalleryList() != null && !goodsOperationDTO.getGoodsGalleryList().isEmpty()) { this.goodsGalleryService.add(goodsOperationDTO.getGoodsGalleryList(), goods.getId()); } + this.generateEs(goods); } @@ -203,6 +209,7 @@ public class GoodsServiceImpl extends ServiceImpl implements this.deleteEsGoods(Collections.singletonList(goodsId)); } cache.remove(CachePrefix.GOODS.getPrefix() + goodsId); + this.generateEs(goods); } @Override @@ -478,17 +485,6 @@ public class GoodsServiceImpl extends ServiceImpl implements } - - /** - * 更新店铺商品数量 - * - * @param storeId 信息体 - */ - void updateGoodsNum(String storeId) { - Long num = goodsSkuService.countSkuNum(storeId); - storeService.updateStoreGoodsNum(storeId, num); - } - /** * 更新商品状态 * @@ -506,19 +502,43 @@ public class GoodsServiceImpl extends ServiceImpl implements if (GoodsStatusEnum.DOWN.equals(goodsStatusEnum)) { this.deleteEsGoods(goodsIds); + } else { + this.updateEsGoods(goodsIds); } } + /** + * 发送生成ES商品索引 + * + * @param goods 商品信息 + */ + @Transactional + public void generateEs(Goods goods) { + // 不生成没有审核通过且没有上架的商品 + if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) { + return; + } + applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("生成商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(), goods.getId())); + } + + /** + * 发送生成ES商品索引 + * + * @param goodsIds 商品id + */ + @Transactional + public void updateEsGoods(List goodsIds) { + applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("更新商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.UPDATE_GOODS_INDEX.name(), goodsIds)); + } + /** * 发送删除es索引的信息 * * @param goodsIds 商品id */ - private void deleteEsGoods(List goodsIds) { - //商品删除消息 - String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name(); - //发送mq消息 - rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIds), RocketmqSendCallbackBuilder.commonCallback()); + @Transactional + public void deleteEsGoods(List goodsIds) { + applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("删除商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.GOODS_DELETE.name(), JSONUtil.toJsonStr(goodsIds))); } /** 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 5438ae95..bea097f8 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 @@ -43,6 +43,7 @@ import cn.lili.modules.promotion.service.PromotionGoodsService; import cn.lili.modules.search.entity.dos.EsGoodsIndex; import cn.lili.modules.search.service.EsGoodsIndexService; import cn.lili.modules.search.utils.EsIndexUtil; +import cn.lili.mybatis.BaseEntity; import cn.lili.mybatis.util.PageUtil; import cn.lili.rocketmq.RocketmqSendCallbackBuilder; import cn.lili.rocketmq.tags.GoodsTagsEnum; @@ -137,7 +138,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl i if (!goodsSkus.isEmpty()) { this.saveOrUpdateBatch(goodsSkus); this.updateStock(goodsSkus); - this.generateEs(goods); } } @@ -161,7 +161,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl i cache.remove(GoodsSkuService.getCacheKeys(goodsSkuVO.getId())); } - this.remove(new LambdaQueryWrapper().eq(GoodsSku::getGoodsId, goods.getId())); //删除sku相册 goodsGalleryService.removeByGoodsId(goods.getId()); @@ -182,10 +181,12 @@ public class GoodsSkuServiceImpl extends ServiceImpl i } } if (!skuList.isEmpty()) { - this.remove(new LambdaQueryWrapper().eq(GoodsSku::getGoodsId, goods.getId())); + LambdaQueryWrapper unnecessarySkuIdsQuery = new LambdaQueryWrapper<>(); + unnecessarySkuIdsQuery.eq(GoodsSku::getGoodsId, goods.getId()); + unnecessarySkuIdsQuery.notIn(GoodsSku::getId, skuList.stream().map(BaseEntity::getId).collect(Collectors.toList())); + this.remove(unnecessarySkuIdsQuery); this.saveOrUpdateBatch(skuList); this.updateStock(skuList); - this.generateEs(goods); } } @@ -354,9 +355,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl i cache.remove(GoodsSkuService.getCacheKeys(sku.getId())); cache.put(GoodsSkuService.getCacheKeys(sku.getId()), sku); } - if (!goodsSkus.isEmpty()) { - this.generateEs(goods); - } } } @@ -593,21 +591,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl i return this.baseMapper.getGoodsSkuIdByGoodsId(goodsId); } - /** - * 发送生成ES商品索引 - * - * @param goods 商品信息 - */ - @Override - @Transactional(rollbackFor = Exception.class) - public void generateEs(Goods goods) { - // 不生成没有审核通过且没有上架的商品 - if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) { - return; - } - applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("生成商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(), goods.getId())); - } - @Override @Transactional(rollbackFor = Exception.class) public boolean deleteAndInsertGoodsSkus(List goodsSkus) {