更新了优惠券模块
This commit is contained in:
parent
c58606137f
commit
c21e809108
|
@ -12,6 +12,7 @@ import org.springframework.amqp.rabbit.annotation.Exchange;
|
||||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||||
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@ -22,6 +23,10 @@ public class CouponStatusListener {
|
||||||
private CouponService couponService;
|
private CouponService couponService;
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@RabbitListener(bindings = @QueueBinding(
|
@RabbitListener(bindings = @QueueBinding(
|
||||||
value = @Queue(MqConstant.DELAY_COUPON_QUEUE),
|
value = @Queue(MqConstant.DELAY_COUPON_QUEUE),
|
||||||
|
@ -43,6 +48,19 @@ public class CouponStatusListener {
|
||||||
if (!sourceCoupon.getEndTime().equals(targetCoupon.getEndTime())) {
|
if (!sourceCoupon.getEndTime().equals(targetCoupon.getEndTime())) {
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
// 3.优惠券未到截止日期,判断是否还有剩余延时时间
|
||||||
|
if (msg.hasNextDelay()) {
|
||||||
|
// 有延迟时间,需要重发延迟消息,先获取延迟时间的int值
|
||||||
|
// 发送延时消息
|
||||||
|
int delayValue = msg.removeNextDelay().intValue();
|
||||||
|
rabbitTemplate.convertAndSend(MqConstant.DELAY_EXCHANGE,
|
||||||
|
MqConstant.DELAY_ORDER_ROUTING_KEY, msg, message -> {
|
||||||
|
// 添加延迟消息属性
|
||||||
|
message.getMessageProperties().setDelay(delayValue * 1000);
|
||||||
|
return message;
|
||||||
|
});
|
||||||
|
return ;
|
||||||
|
}
|
||||||
// 4.更新优惠券状态
|
// 4.更新优惠券状态
|
||||||
UpdateWrapper<Coupon> updateWrapper = new UpdateWrapper<>();
|
UpdateWrapper<Coupon> updateWrapper = new UpdateWrapper<>();
|
||||||
updateWrapper.eq("id", couponId).set("status", "已过期");
|
updateWrapper.eq("id", couponId).set("status", "已过期");
|
||||||
|
|
|
@ -14,6 +14,7 @@ import org.springframework.amqp.rabbit.annotation.Exchange;
|
||||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||||
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,6 +25,10 @@ public class UserCouponStatusListener {
|
||||||
private UserCouponService userCouponService;
|
private UserCouponService userCouponService;
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
|
||||||
@RabbitListener(bindings = @QueueBinding(
|
@RabbitListener(bindings = @QueueBinding(
|
||||||
value = @Queue(MqConstant.DELAY_USER_COUPON_QUEUE),
|
value = @Queue(MqConstant.DELAY_USER_COUPON_QUEUE),
|
||||||
exchange = @Exchange(name = MqConstant.DELAY_EXCHANGE, delayed = "true"),
|
exchange = @Exchange(name = MqConstant.DELAY_EXCHANGE, delayed = "true"),
|
||||||
|
@ -32,7 +37,21 @@ public class UserCouponStatusListener {
|
||||||
public void listenDelayMessage(MultiDelayMessage<Long> msg) {
|
public void listenDelayMessage(MultiDelayMessage<Long> msg) {
|
||||||
System.out.println("\n\n\n\n\nUserCouponStatusListener.listenerDelayMessage msg-------------------------------->:" + msg);
|
System.out.println("\n\n\n\n\nUserCouponStatusListener.listenerDelayMessage msg-------------------------------->:" + msg);
|
||||||
|
|
||||||
// 1.获取消用户优惠券id
|
// 3.我的优惠券未到截止日期,判断是否还有剩余延时时间
|
||||||
|
if (msg.hasNextDelay()) {
|
||||||
|
// 有延迟时间,需要重发延迟消息,先获取延迟时间的int值
|
||||||
|
// 发送延时消息
|
||||||
|
int delayValue = msg.removeNextDelay().intValue();
|
||||||
|
rabbitTemplate.convertAndSend(MqConstant.DELAY_EXCHANGE,
|
||||||
|
MqConstant.DELAY_ORDER_ROUTING_KEY, msg, message -> {
|
||||||
|
// 添加延迟消息属性
|
||||||
|
message.getMessageProperties().setDelay(delayValue * 1000);
|
||||||
|
return message;
|
||||||
|
});
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1.获取用户优惠券id
|
||||||
Long userCouponId = msg.getData();
|
Long userCouponId = msg.getData();
|
||||||
// 2.获取优惠券信息
|
// 2.获取优惠券信息
|
||||||
UserCoupon userCoupon = userCouponService.getById(userCouponId);
|
UserCoupon userCoupon = userCouponService.getById(userCouponId);
|
||||||
|
|
|
@ -16,6 +16,7 @@ import com.cultural.heritage.model.entity.UserCoupon;
|
||||||
import com.cultural.heritage.service.good.CouponService;
|
import com.cultural.heritage.service.good.CouponService;
|
||||||
import com.cultural.heritage.utils.MultiDelayMessage;
|
import com.cultural.heritage.utils.MultiDelayMessage;
|
||||||
import com.cultural.heritage.utils.SqlUtils;
|
import com.cultural.heritage.utils.SqlUtils;
|
||||||
|
import com.cultural.heritage.utils.TimeSplitterUtils;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -24,6 +25,7 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {
|
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {
|
||||||
|
@ -110,16 +112,18 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sendCouponCreateMessage(Coupon coupon) {
|
public void sendCouponCreateMessage(Coupon coupon) {
|
||||||
// 延迟检查优惠券是否过期
|
|
||||||
MultiDelayMessage<Coupon> msg = new MultiDelayMessage<>(coupon);
|
|
||||||
rabbitTemplate.convertAndSend(MqConstant.DELAY_EXCHANGE,
|
|
||||||
MqConstant.DELAY_COUPON_ROUTING_KEY, msg, message -> {
|
|
||||||
//计算优惠券截止日期和当前的时间差
|
//计算优惠券截止日期和当前的时间差
|
||||||
DateTime now = new DateTime();
|
DateTime now = new DateTime();
|
||||||
DateTime end = DateUtil.parse(coupon.getEndTime());
|
DateTime end = DateUtil.parse(coupon.getEndTime());
|
||||||
long seconds = DateUtil.between(now, end, DateUnit.SECOND);
|
long seconds = DateUtil.between(now, end, DateUnit.SECOND);
|
||||||
|
List<Long> splitTimeList = TimeSplitterUtils.splitTime(seconds);
|
||||||
|
// 延迟检查优惠券是否过期
|
||||||
|
MultiDelayMessage<Coupon> msg = new MultiDelayMessage<>(coupon, splitTimeList);
|
||||||
|
int delayValue = msg.removeNextDelay().intValue();
|
||||||
|
rabbitTemplate.convertAndSend(MqConstant.DELAY_EXCHANGE,
|
||||||
|
MqConstant.DELAY_COUPON_ROUTING_KEY, msg, message -> {
|
||||||
// 添加延迟消息属性
|
// 添加延迟消息属性
|
||||||
message.getMessageProperties().setDelay((int) (seconds * 1000));
|
message.getMessageProperties().setDelay(delayValue * 1000);
|
||||||
return message;
|
return message;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -132,16 +136,18 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sendUserCouponCreateMessage(UserCoupon userCoupon, Coupon coupon) {
|
public void sendUserCouponCreateMessage(UserCoupon userCoupon, Coupon coupon) {
|
||||||
// 延迟检查优惠券是否过期
|
|
||||||
MultiDelayMessage<Long> msg = new MultiDelayMessage<>(userCoupon.getId());
|
|
||||||
rabbitTemplate.convertAndSend(MqConstant.DELAY_EXCHANGE,
|
|
||||||
MqConstant.DELAY_USER_COUPON_ROUTING_KEY, msg, message -> {
|
|
||||||
//计算优惠券截止日期和当前的时间差
|
//计算优惠券截止日期和当前的时间差
|
||||||
DateTime now = new DateTime();
|
DateTime now = new DateTime();
|
||||||
DateTime end = DateUtil.parse(coupon.getEndTime());
|
DateTime end = DateUtil.parse(coupon.getEndTime());
|
||||||
long seconds = DateUtil.between(now, end, DateUnit.SECOND);
|
long seconds = DateUtil.between(now, end, DateUnit.SECOND);
|
||||||
|
List<Long> splitTimeList = TimeSplitterUtils.splitTime(seconds);
|
||||||
|
// 延迟检查优惠券是否过期
|
||||||
|
MultiDelayMessage<Long> msg = new MultiDelayMessage<>(userCoupon.getId(), splitTimeList);
|
||||||
|
int delayValue = msg.removeNextDelay().intValue();
|
||||||
|
rabbitTemplate.convertAndSend(MqConstant.DELAY_EXCHANGE,
|
||||||
|
MqConstant.DELAY_USER_COUPON_ROUTING_KEY, msg, message -> {
|
||||||
// 添加延迟消息属性
|
// 添加延迟消息属性
|
||||||
message.getMessageProperties().setDelay((int) (seconds * 1000));
|
message.getMessageProperties().setDelay(delayValue * 1000);
|
||||||
return message;
|
return message;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.cultural.heritage.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TimeSplitterUtils {
|
||||||
|
|
||||||
|
public static List<Long> splitTime(long totalSeconds) {
|
||||||
|
List<Long> timeIntervals = new ArrayList<>();
|
||||||
|
long oneDayInSeconds = 86400; // 1天的秒数
|
||||||
|
|
||||||
|
// 拆分完整的天数
|
||||||
|
while (totalSeconds >= oneDayInSeconds) {
|
||||||
|
timeIntervals.add(oneDayInSeconds); // 每天加一个完整的时间段
|
||||||
|
totalSeconds -= oneDayInSeconds; // 减去已拆分的天数
|
||||||
|
}
|
||||||
|
// 如果剩余时间大于0,则加上剩余的时间
|
||||||
|
if (totalSeconds > 0) {
|
||||||
|
timeIntervals.add(totalSeconds); // 剩余时间
|
||||||
|
}
|
||||||
|
return timeIntervals;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user