优惠券:新人券使用 MQ 发放

This commit is contained in:
owen 2023-09-30 17:29:32 +08:00
parent 497ea2eee8
commit fc432ba073
10 changed files with 132 additions and 6 deletions

View File

@ -12,6 +12,7 @@ import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
import java.util.List;
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);
default List<CouponTemplateDO> selectListByTakeType(Integer takeType) {
return selectList(CouponTemplateDO::getTakeType, takeType);
}
}

View File

@ -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());
}
}

View File

@ -119,12 +119,9 @@ public interface CouponService {
/**
* 系统给用户发送新人券
*
* @param templateId 优惠券模板编号
* @param userId 用户编号列表
* @param userId 用户编号
*/
default void takeCouponByRegister(Long templateId, Long userId) {
takeCoupon(templateId, CollUtil.newHashSet(userId), CouponTakeTypeEnum.REGISTER);
}
void takeCouponByRegister(Long userId);
/**
* 获取会员领取指定优惠券的数量

View File

@ -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 java.util.Arrays.asList;
// TODO @疯狂注册时赠送用户优惠劵为了解耦可以考虑注册时发个 MQ 消息然后营销这里监听后消费
/**
* 优惠劵 Service 实现类
*
@ -183,6 +182,14 @@ public class CouponServiceImpl implements CouponService {
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
public List<CouponTakeCountBO> getTakeCountListByTemplateIds(Collection<Long> templateIds, Long userId) {
if (CollUtil.isEmpty(templateIds)) {

View File

@ -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.CouponTemplateUpdateReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO;
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum;
import javax.validation.Valid;
import java.util.List;
/**
* 优惠劵模板 Service 接口
@ -69,4 +71,11 @@ public interface CouponTemplateService {
*/
void updateCouponTemplateTakeCount(Long id, int incrCount);
/**
* 获得指定领取方式的优惠券模板
*
* @param takeType 领取方式
* @return 优惠券模板列表
*/
List<CouponTemplateDO> getCouponTemplateByTakeType(CouponTakeTypeEnum takeType);
}

View File

@ -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.dal.dataobject.coupon.CouponTemplateDO;
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.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.List;
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_TOTAL_COUNT_TOO_SMALL;
@ -93,4 +96,9 @@ public class CouponTemplateServiceImpl implements CouponTemplateService {
couponTemplateMapper.updateTakeCount(id, incrCount);
}
@Override
public List<CouponTemplateDO> getCouponTemplateByTakeType(CouponTakeTypeEnum takeType) {
return couponTemplateMapper.selectListByTakeType(takeType.getValue());
}
}

View File

@ -22,6 +22,11 @@
<artifactId>yudao-common</artifactId>
</dependency>
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-mq</artifactId>
<scope>compile</scope>
</dependency>
<!-- 参数校验 -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -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";
}
}

View File

@ -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));
}
}

View File

@ -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.dal.dataobject.user.MemberUserDO;
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.dto.code.SmsCodeUseReqDTO;
import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum;
@ -58,6 +59,9 @@ public class MemberUserServiceImpl implements MemberUserService {
@Resource
private PasswordEncoder passwordEncoder;
@Resource
private RegisterCouponProducer registerCouponProducer;
@Override
public MemberUserDO getUserByMobile(String mobile) {
return memberUserMapper.selectByMobile(mobile);
@ -89,6 +93,9 @@ public class MemberUserServiceImpl implements MemberUserService {
user.setPassword(encodePassword(password)); // 加密密码
user.setRegisterIp(registerIp);
memberUserMapper.insert(user);
// 发送 MQ 消息发放新人券
registerCouponProducer.sendMailSendMessage(user.getId());
return user;
}