From 90185d8c32f75287785a4a2da00d88bec1511144 Mon Sep 17 00:00:00 2001 From: paulGao Date: Wed, 19 Jan 2022 10:18:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BC=96=E8=BE=91=E5=95=86?= =?UTF-8?q?=E5=93=81=E6=97=B6=E7=9A=84=E7=94=9F=E6=88=90=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=E5=92=8C=E6=93=8D=E4=BD=9C=E4=BF=83=E9=94=80=E6=B4=BB=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E6=9B=B4=E6=96=B0=E7=B4=A2=E5=BC=95=E4=BF=83=E9=94=80?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=9C=A8=E4=BA=8B=E5=8A=A1=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E5=90=8E=E5=8F=91=E9=80=81mq=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/GeneratorEsGoodsIndexEvent.java | 19 +++++++++ .../GeneratorEsGoodsIndexListener.java | 38 +++++++++++++++++ .../serviceimpl/GoodsSkuServiceImpl.java | 12 +++--- .../UpdateEsGoodsIndexPromotionsEvent.java | 19 +++++++++ .../UpdateEsGoodsIndexPromotionsListener.java | 41 +++++++++++++++++++ .../AbstractPromotionsServiceImpl.java | 11 ++--- .../CouponActivityItemServiceImpl.java | 2 + .../CouponActivityServiceImpl.java | 5 ++- .../serviceimpl/CouponServiceImpl.java | 7 +++- .../serviceimpl/FullDiscountServiceImpl.java | 2 +- .../serviceimpl/SeckillApplyServiceImpl.java | 1 + .../serviceimpl/SeckillServiceImpl.java | 16 +------- 12 files changed, 144 insertions(+), 29 deletions(-) create mode 100644 framework/src/main/java/cn/lili/modules/goods/event/GeneratorEsGoodsIndexEvent.java create mode 100644 framework/src/main/java/cn/lili/modules/goods/listener/GeneratorEsGoodsIndexListener.java create mode 100644 framework/src/main/java/cn/lili/modules/promotion/event/UpdateEsGoodsIndexPromotionsEvent.java create mode 100644 framework/src/main/java/cn/lili/modules/promotion/listener/UpdateEsGoodsIndexPromotionsListener.java diff --git a/framework/src/main/java/cn/lili/modules/goods/event/GeneratorEsGoodsIndexEvent.java b/framework/src/main/java/cn/lili/modules/goods/event/GeneratorEsGoodsIndexEvent.java new file mode 100644 index 00000000..ff5da90f --- /dev/null +++ b/framework/src/main/java/cn/lili/modules/goods/event/GeneratorEsGoodsIndexEvent.java @@ -0,0 +1,19 @@ +package cn.lili.modules.goods.event; + +import lombok.Data; +import org.springframework.context.ApplicationEvent; + +/** + * @author paulG + * @since 2022/1/19 + **/ +@Data +public class GeneratorEsGoodsIndexEvent extends ApplicationEvent { + + private String goodsId; + + public GeneratorEsGoodsIndexEvent(Object source, String goodsId) { + super(source); + this.goodsId = goodsId; + } +} diff --git a/framework/src/main/java/cn/lili/modules/goods/listener/GeneratorEsGoodsIndexListener.java b/framework/src/main/java/cn/lili/modules/goods/listener/GeneratorEsGoodsIndexListener.java new file mode 100644 index 00000000..7c5b95ba --- /dev/null +++ b/framework/src/main/java/cn/lili/modules/goods/listener/GeneratorEsGoodsIndexListener.java @@ -0,0 +1,38 @@ +package cn.lili.modules.goods.listener; + +import cn.lili.common.properties.RocketmqCustomProperties; +import cn.lili.modules.goods.event.GeneratorEsGoodsIndexEvent; +import cn.lili.rocketmq.RocketmqSendCallbackBuilder; +import cn.lili.rocketmq.tags.GoodsTagsEnum; +import org.apache.rocketmq.spring.core.RocketMQTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +/** + * @author paulG + * @since 2022/1/19 + **/ +@Component +public class GeneratorEsGoodsIndexListener { + + /** + * rocketMq + */ + @Autowired + private RocketMQTemplate rocketMQTemplate; + /** + * rocketMq配置 + */ + @Autowired + private RocketmqCustomProperties rocketmqCustomProperties; + + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + public void generatorEsGoodsIndex(GeneratorEsGoodsIndexEvent esGoodsIndexEvent) { + String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(); + //发送mq消息 + rocketMQTemplate.asyncSend(destination, esGoodsIndexEvent.getGoodsId(), RocketmqSendCallbackBuilder.commonCallback()); + } + +} 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 fe4cec56..1b3e475d 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 @@ -22,6 +22,7 @@ import cn.lili.modules.goods.entity.vos.GoodsSkuSpecVO; import cn.lili.modules.goods.entity.vos.GoodsSkuVO; import cn.lili.modules.goods.entity.vos.GoodsVO; import cn.lili.modules.goods.entity.vos.SpecValueVO; +import cn.lili.modules.goods.event.GeneratorEsGoodsIndexEvent; import cn.lili.modules.goods.mapper.GoodsSkuMapper; import cn.lili.modules.goods.service.CategoryService; import cn.lili.modules.goods.service.GoodsGalleryService; @@ -48,10 +49,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.transaction.event.TransactionPhase; -import org.springframework.transaction.event.TransactionalEventListener; import java.util.*; import java.util.stream.Collectors; @@ -110,6 +110,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl i @Autowired private PromotionGoodsService promotionGoodsService; + @Autowired + private ApplicationEventPublisher applicationEventPublisher; + @Override public void add(List> skuList, Goods goods) { // 检查是否需要生成索引 @@ -544,15 +547,12 @@ public class GoodsSkuServiceImpl extends ServiceImpl i * @param goods 商品信息 */ @Override - @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void generateEs(Goods goods) { // 不生成没有审核通过且没有上架的商品 if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) { return; } - String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(); - //发送mq消息 - rocketMQTemplate.asyncSend(destination, goods.getId(), RocketmqSendCallbackBuilder.commonCallback()); + applicationEventPublisher.publishEvent(new GeneratorEsGoodsIndexEvent("生成商品索引事件", goods.getId())); } /** diff --git a/framework/src/main/java/cn/lili/modules/promotion/event/UpdateEsGoodsIndexPromotionsEvent.java b/framework/src/main/java/cn/lili/modules/promotion/event/UpdateEsGoodsIndexPromotionsEvent.java new file mode 100644 index 00000000..94442bf7 --- /dev/null +++ b/framework/src/main/java/cn/lili/modules/promotion/event/UpdateEsGoodsIndexPromotionsEvent.java @@ -0,0 +1,19 @@ +package cn.lili.modules.promotion.event; + +import lombok.Data; +import org.springframework.context.ApplicationEvent; + +/** + * @author paulG + * @since 2022/1/19 + **/ +@Data +public class UpdateEsGoodsIndexPromotionsEvent extends ApplicationEvent { + + private String promotionsJsonStr; + + public UpdateEsGoodsIndexPromotionsEvent(Object source, String promotionsJsonStr) { + super(source); + this.promotionsJsonStr = promotionsJsonStr; + } +} diff --git a/framework/src/main/java/cn/lili/modules/promotion/listener/UpdateEsGoodsIndexPromotionsListener.java b/framework/src/main/java/cn/lili/modules/promotion/listener/UpdateEsGoodsIndexPromotionsListener.java new file mode 100644 index 00000000..06b20cab --- /dev/null +++ b/framework/src/main/java/cn/lili/modules/promotion/listener/UpdateEsGoodsIndexPromotionsListener.java @@ -0,0 +1,41 @@ +package cn.lili.modules.promotion.listener; + +import cn.lili.common.properties.RocketmqCustomProperties; +import cn.lili.modules.promotion.event.UpdateEsGoodsIndexPromotionsEvent; +import cn.lili.rocketmq.RocketmqSendCallbackBuilder; +import cn.lili.rocketmq.tags.GoodsTagsEnum; +import org.apache.rocketmq.spring.core.RocketMQTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +/** + * @author paulG + * @since 2022/1/19 + **/ +@Component +public class UpdateEsGoodsIndexPromotionsListener { + + /** + * rocketMq + */ + @Autowired + private RocketMQTemplate rocketMQTemplate; + /** + * rocketMq配置 + */ + @Autowired + private RocketmqCustomProperties rocketmqCustomProperties; + + + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + public void updateEsGoodsIndexPromotions(UpdateEsGoodsIndexPromotionsEvent event) { + //更新商品促销消息 + String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_PROMOTIONS.name(); + //发送mq消息 + rocketMQTemplate.asyncSend(destination, event.getPromotionsJsonStr(), RocketmqSendCallbackBuilder.commonCallback()); + } + + +} diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/AbstractPromotionsServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/AbstractPromotionsServiceImpl.java index 768cf01a..1baa838a 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/AbstractPromotionsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/AbstractPromotionsServiceImpl.java @@ -10,6 +10,7 @@ import cn.lili.modules.promotion.entity.dos.BasePromotions; import cn.lili.modules.promotion.entity.dos.PromotionGoods; import cn.lili.modules.promotion.entity.dto.search.BasePromotionsSearchParams; import cn.lili.modules.promotion.entity.enums.PromotionsScopeTypeEnum; +import cn.lili.modules.promotion.event.UpdateEsGoodsIndexPromotionsEvent; import cn.lili.modules.promotion.service.AbstractPromotionsService; import cn.lili.modules.promotion.service.PromotionGoodsService; import cn.lili.modules.promotion.tools.PromotionTools; @@ -23,6 +24,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -31,7 +33,6 @@ import java.util.*; * @author paulG * @since 2021/11/30 **/ -@Transactional(rollbackFor = Exception.class) public abstract class AbstractPromotionsServiceImpl, T extends BasePromotions> extends ServiceImpl implements AbstractPromotionsService { /** @@ -52,6 +53,9 @@ public abstract class AbstractPromotionsServiceImpl, T e @Autowired private RocketMQTemplate rocketMQTemplate; + @Autowired + private ApplicationEventPublisher applicationEventPublisher; + /** * 通用促销保存 * 调用顺序: @@ -264,10 +268,7 @@ public abstract class AbstractPromotionsServiceImpl, T e map.put("promotionsType", promotions.getClass().getName()); // 促销实体 map.put("promotions", promotions); - //更新商品促销消息 - String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_PROMOTIONS.name(); - //发送mq消息 - rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(map), RocketmqSendCallbackBuilder.commonCallback()); + applicationEventPublisher.publishEvent(new UpdateEsGoodsIndexPromotionsEvent("更新商品索引促销事件", JSONUtil.toJsonStr(map))); } } diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponActivityItemServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponActivityItemServiceImpl.java index 03eedfa4..b7fcde45 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponActivityItemServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponActivityItemServiceImpl.java @@ -7,6 +7,7 @@ import cn.lili.modules.promotion.service.CouponActivityItemService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -37,6 +38,7 @@ public class CouponActivityItemServiceImpl extends ServiceImpl couponIds) { this.remove(new LambdaQueryWrapper() .in(CouponActivityItem::getCouponId, couponIds)); diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponActivityServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponActivityServiceImpl.java index c413e6a4..ee56fcdc 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponActivityServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponActivityServiceImpl.java @@ -23,6 +23,7 @@ import cn.lili.modules.promotion.tools.PromotionTools; import groovy.util.logging.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; @@ -140,6 +141,7 @@ public class CouponActivityServiceImpl extends AbstractPromotionsServiceImpl> memberList, List couponActivityItems) { + @Transactional(rollbackFor = {Exception.class}) + void sendCoupon(List> memberList, List couponActivityItems) { for (CouponActivityItem couponActivityItem : couponActivityItems) { //获取优惠券 diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponServiceImpl.java index 7a2f3d90..2c2715e8 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/CouponServiceImpl.java @@ -42,7 +42,6 @@ import java.util.stream.Collectors; * @since 2020/8/21 */ @Service -@Transactional(rollbackFor = Exception.class) public class CouponServiceImpl extends AbstractPromotionsServiceImpl implements CouponService { /** @@ -78,6 +77,7 @@ public class CouponServiceImpl extends AbstractPromotionsServiceImpl ids) { //删除优惠券信息 this.memberCouponService.closeMemberCoupon(ids); @@ -104,6 +105,7 @@ public class CouponServiceImpl extends AbstractPromotionsServiceImpl ids, Long startTime, Long endTime) { List list = this.list(new LambdaQueryWrapper().in(Coupon::getId, ids).eq(Coupon::getRangeDayType, CouponRangeDayEnum.DYNAMICTIME.name())); if (!list.isEmpty()) { @@ -221,6 +224,7 @@ public class CouponServiceImpl extends AbstractPromotionsServiceImpl implements FullDiscountService { /** @@ -110,6 +109,7 @@ public class FullDiscountServiceImpl extends AbstractPromotionsServiceImpl seckillApplyList) { Seckill seckill = this.seckillService.getById(seckillId); if (seckill == null) { 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 bf0b00a5..66142a56 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 @@ -39,9 +39,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * 秒杀活动业务层实现 @@ -154,19 +152,7 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl map = new HashMap<>(); - // es促销key - map.put("esPromotionKey", promotionKey); - // 促销类型全路径名 - map.put("promotionsType", Seckill.class.getName()); - // 促销实体 - map.put("promotions", seckill); - //更新商品促销消息 - String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_PROMOTIONS.name(); - //发送mq消息 - rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(map), RocketmqSendCallbackBuilder.commonCallback()); + this.updateEsGoodsIndex(seckill); } } }