mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	优惠券:新人券使用 MQ 发放
This commit is contained in:
		| @@ -12,6 +12,7 @@ import org.apache.ibatis.annotations.Mapper; | |||||||
| import org.apache.ibatis.annotations.Param; | import org.apache.ibatis.annotations.Param; | ||||||
|  |  | ||||||
| import java.time.LocalDateTime; | import java.time.LocalDateTime; | ||||||
|  | import java.util.List; | ||||||
| import java.util.function.Consumer; | import java.util.function.Consumer; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -48,4 +49,7 @@ public interface CouponTemplateMapper extends BaseMapperX<CouponTemplateDO> { | |||||||
|  |  | ||||||
|     void updateTakeCount(@Param("id") Long id, @Param("incrCount") Integer incrCount); |     void updateTakeCount(@Param("id") Long id, @Param("incrCount") Integer incrCount); | ||||||
|  |  | ||||||
|  |     default List<CouponTemplateDO> selectListByTakeType(Integer takeType) { | ||||||
|  |         return selectList(CouponTemplateDO::getTakeType, takeType); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -0,0 +1,29 @@ | |||||||
|  | package cn.iocoder.yudao.module.promotion.mq.consumer.coupon; | ||||||
|  |  | ||||||
|  | import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener; | ||||||
|  | import cn.iocoder.yudao.module.member.mq.message.user.RegisterCouponSendMessage; | ||||||
|  | import cn.iocoder.yudao.module.promotion.service.coupon.CouponService; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  |  | ||||||
|  | import javax.annotation.Resource; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * 针对 {@link RegisterCouponSendMessage} 的消费者 | ||||||
|  |  * | ||||||
|  |  * @author owen | ||||||
|  |  */ | ||||||
|  | @Component | ||||||
|  | @Slf4j | ||||||
|  | public class RegisterCouponSendConsumer extends AbstractStreamMessageListener<RegisterCouponSendMessage> { | ||||||
|  |  | ||||||
|  |     @Resource | ||||||
|  |     private CouponService couponService; | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void onMessage(RegisterCouponSendMessage message) { | ||||||
|  |         log.info("[onMessage][消息内容({})]", message); | ||||||
|  |         couponService.takeCouponByRegister(message.getUserId()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -119,12 +119,9 @@ public interface CouponService { | |||||||
|     /** |     /** | ||||||
|      * 【系统】给用户发送新人券 |      * 【系统】给用户发送新人券 | ||||||
|      * |      * | ||||||
|      * @param templateId 优惠券模板编号 |      * @param userId 用户编号 | ||||||
|      * @param userId     用户编号列表 |  | ||||||
|      */ |      */ | ||||||
|     default void takeCouponByRegister(Long templateId, Long userId) { |     void takeCouponByRegister(Long userId); | ||||||
|         takeCoupon(templateId, CollUtil.newHashSet(userId), CouponTakeTypeEnum.REGISTER); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 获取会员领取指定优惠券的数量 |      * 获取会员领取指定优惠券的数量 | ||||||
|   | |||||||
| @@ -40,7 +40,6 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. | |||||||
| import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; | import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; | ||||||
| import static java.util.Arrays.asList; | import static java.util.Arrays.asList; | ||||||
|  |  | ||||||
| // TODO @疯狂:注册时,赠送用户优惠劵;为了解耦,可以考虑注册时发个 MQ 消息;然后营销这里监听后消费; |  | ||||||
| /** | /** | ||||||
|  * 优惠劵 Service 实现类 |  * 优惠劵 Service 实现类 | ||||||
|  * |  * | ||||||
| @@ -183,6 +182,14 @@ public class CouponServiceImpl implements CouponService { | |||||||
|         couponTemplateService.updateCouponTemplateTakeCount(templateId, userIds.size()); |         couponTemplateService.updateCouponTemplateTakeCount(templateId, userIds.size()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void takeCouponByRegister(Long userId) { | ||||||
|  |         List<CouponTemplateDO> templates = couponTemplateService.getCouponTemplateByTakeType(CouponTakeTypeEnum.REGISTER); | ||||||
|  |         for (CouponTemplateDO template : templates) { | ||||||
|  |             takeCoupon(template.getId(), CollUtil.newHashSet(userId), CouponTakeTypeEnum.REGISTER); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public List<CouponTakeCountBO> getTakeCountListByTemplateIds(Collection<Long> templateIds, Long userId) { |     public List<CouponTakeCountBO> getTakeCountListByTemplateIds(Collection<Long> templateIds, Long userId) { | ||||||
|         if (CollUtil.isEmpty(templateIds)) { |         if (CollUtil.isEmpty(templateIds)) { | ||||||
|   | |||||||
| @@ -5,8 +5,10 @@ import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.template.Cou | |||||||
| import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.template.CouponTemplatePageReqVO; | import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.template.CouponTemplatePageReqVO; | ||||||
| import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.template.CouponTemplateUpdateReqVO; | import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.template.CouponTemplateUpdateReqVO; | ||||||
| import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO; | import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO; | ||||||
|  | import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; | ||||||
|  |  | ||||||
| import javax.validation.Valid; | import javax.validation.Valid; | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 优惠劵模板 Service 接口 |  * 优惠劵模板 Service 接口 | ||||||
| @@ -69,4 +71,11 @@ public interface CouponTemplateService { | |||||||
|      */ |      */ | ||||||
|     void updateCouponTemplateTakeCount(Long id, int incrCount); |     void updateCouponTemplateTakeCount(Long id, int incrCount); | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 获得指定领取方式的优惠券模板 | ||||||
|  |      * | ||||||
|  |      * @param takeType 领取方式 | ||||||
|  |      * @return 优惠券模板列表 | ||||||
|  |      */ | ||||||
|  |     List<CouponTemplateDO> getCouponTemplateByTakeType(CouponTakeTypeEnum takeType); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -8,11 +8,14 @@ import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.template.Cou | |||||||
| import cn.iocoder.yudao.module.promotion.convert.coupon.CouponTemplateConvert; | import cn.iocoder.yudao.module.promotion.convert.coupon.CouponTemplateConvert; | ||||||
| import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO; | import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO; | ||||||
| import cn.iocoder.yudao.module.promotion.dal.mysql.coupon.CouponTemplateMapper; | import cn.iocoder.yudao.module.promotion.dal.mysql.coupon.CouponTemplateMapper; | ||||||
|  | import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; | ||||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||||
| import org.springframework.validation.annotation.Validated; | import org.springframework.validation.annotation.Validated; | ||||||
|  |  | ||||||
| import javax.annotation.Resource; | import javax.annotation.Resource; | ||||||
|  |  | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
| import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||||||
| import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_NOT_EXISTS; | import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_NOT_EXISTS; | ||||||
| import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_TOTAL_COUNT_TOO_SMALL; | import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_TOTAL_COUNT_TOO_SMALL; | ||||||
| @@ -93,4 +96,9 @@ public class CouponTemplateServiceImpl implements CouponTemplateService { | |||||||
|         couponTemplateMapper.updateTakeCount(id, incrCount); |         couponTemplateMapper.updateTakeCount(id, incrCount); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public List<CouponTemplateDO> getCouponTemplateByTakeType(CouponTakeTypeEnum takeType) { | ||||||
|  |         return couponTemplateMapper.selectListByTakeType(takeType.getValue()); | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -22,6 +22,11 @@ | |||||||
|             <artifactId>yudao-common</artifactId> |             <artifactId>yudao-common</artifactId> | ||||||
|         </dependency> |         </dependency> | ||||||
|  |  | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>cn.iocoder.boot</groupId> | ||||||
|  |             <artifactId>yudao-spring-boot-starter-mq</artifactId> | ||||||
|  |             <scope>compile</scope> | ||||||
|  |         </dependency> | ||||||
|         <!-- 参数校验 --> |         <!-- 参数校验 --> | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.springframework.boot</groupId> |             <groupId>org.springframework.boot</groupId> | ||||||
|   | |||||||
| @@ -0,0 +1,29 @@ | |||||||
|  | package cn.iocoder.yudao.module.member.mq.message.user; | ||||||
|  |  | ||||||
|  | import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage; | ||||||
|  | import lombok.Data; | ||||||
|  | import lombok.EqualsAndHashCode; | ||||||
|  |  | ||||||
|  | import javax.validation.constraints.NotNull; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * 新人券发放消息 | ||||||
|  |  * | ||||||
|  |  * @author owen | ||||||
|  |  */ | ||||||
|  | @Data | ||||||
|  | @EqualsAndHashCode(callSuper = true) | ||||||
|  | public class RegisterCouponSendMessage extends AbstractStreamMessage { | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 用户编号 | ||||||
|  |      */ | ||||||
|  |     @NotNull(message = "用户编号不能为空") | ||||||
|  |     private Long userId; | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public String getStreamKey() { | ||||||
|  |         return "member.register-coupon.send"; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -0,0 +1,31 @@ | |||||||
|  | package cn.iocoder.yudao.module.member.mq.producer.user; | ||||||
|  |  | ||||||
|  | import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; | ||||||
|  | import cn.iocoder.yudao.module.member.mq.message.user.RegisterCouponSendMessage; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  |  | ||||||
|  | import javax.annotation.Resource; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * 新人券发放 Producer | ||||||
|  |  * | ||||||
|  |  * @author owen | ||||||
|  |  */ | ||||||
|  | @Slf4j | ||||||
|  | @Component | ||||||
|  | public class RegisterCouponProducer { | ||||||
|  |  | ||||||
|  |     @Resource | ||||||
|  |     private RedisMQTemplate redisMQTemplate; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 发送 {@link RegisterCouponSendMessage} 消息 | ||||||
|  |      * | ||||||
|  |      * @param userId 用户编号 | ||||||
|  |      */ | ||||||
|  |     public void sendMailSendMessage(Long userId) { | ||||||
|  |         redisMQTemplate.send(new RegisterCouponSendMessage().setUserId(userId)); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -18,6 +18,7 @@ import cn.iocoder.yudao.module.member.convert.auth.AuthConvert; | |||||||
| import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert; | import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert; | ||||||
| import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; | import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; | ||||||
| import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper; | import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper; | ||||||
|  | import cn.iocoder.yudao.module.member.mq.producer.user.RegisterCouponProducer; | ||||||
| import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi; | import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi; | ||||||
| import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; | ||||||
| import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; | import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; | ||||||
| @@ -58,6 +59,9 @@ public class MemberUserServiceImpl implements MemberUserService { | |||||||
|     @Resource |     @Resource | ||||||
|     private PasswordEncoder passwordEncoder; |     private PasswordEncoder passwordEncoder; | ||||||
|  |  | ||||||
|  |     @Resource | ||||||
|  |     private RegisterCouponProducer registerCouponProducer; | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public MemberUserDO getUserByMobile(String mobile) { |     public MemberUserDO getUserByMobile(String mobile) { | ||||||
|         return memberUserMapper.selectByMobile(mobile); |         return memberUserMapper.selectByMobile(mobile); | ||||||
| @@ -89,6 +93,9 @@ public class MemberUserServiceImpl implements MemberUserService { | |||||||
|         user.setPassword(encodePassword(password)); // 加密密码 |         user.setPassword(encodePassword(password)); // 加密密码 | ||||||
|         user.setRegisterIp(registerIp); |         user.setRegisterIp(registerIp); | ||||||
|         memberUserMapper.insert(user); |         memberUserMapper.insert(user); | ||||||
|  |  | ||||||
|  |         // 发送 MQ 消息,发放新人券 | ||||||
|  |         registerCouponProducer.sendMailSendMessage(user.getId()); | ||||||
|         return user; |         return user; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 owen
					owen