Commit fa5a34ab by 吕明尚

重写自动删除优惠卷

parent 4ab7e743
...@@ -5,7 +5,7 @@ package share.common.enums; ...@@ -5,7 +5,7 @@ package share.common.enums;
* @Date 2023/10/19 16:13 * @Date 2023/10/19 16:13
*/ */
public enum CouponStatusEnum { public enum CouponStatusEnum {
NORMAL(0,"正常"), NORMAL(0, "待使用"),
USED(1,"已使用"), USED(1,"已使用"),
EXPIRED(2,"已过期"); EXPIRED(2,"已过期");
......
...@@ -3,11 +3,14 @@ package share.quartz.task; ...@@ -3,11 +3,14 @@ package share.quartz.task;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianping.openapi.sdk.api.oauth.entity.CustomerRefreshTokenResponse; import com.dianping.openapi.sdk.api.oauth.entity.CustomerRefreshTokenResponse;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -16,12 +19,10 @@ import org.springframework.util.ObjectUtils; ...@@ -16,12 +19,10 @@ import org.springframework.util.ObjectUtils;
import share.common.core.redis.RedisUtil; import share.common.core.redis.RedisUtil;
import share.common.enums.*; import share.common.enums.*;
import share.common.exception.base.BaseException; import share.common.exception.base.BaseException;
import share.system.domain.Device; import share.system.domain.*;
import share.system.domain.SOrder;
import share.system.domain.SRoom;
import share.system.domain.SStore;
import share.system.mapper.SConsumerMapper; import share.system.mapper.SConsumerMapper;
import share.system.service.*; import share.system.service.*;
import share.system.service.impl.SOrderServiceImpl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
...@@ -32,6 +33,7 @@ import java.util.stream.Collectors; ...@@ -32,6 +33,7 @@ import java.util.stream.Collectors;
@Component("redisTask") @Component("redisTask")
public class RedisTask { public class RedisTask {
private static final Logger logger = LoggerFactory.getLogger(RedisTask.class);
@Autowired @Autowired
private ISConsumerCouponService isConsumerCouponService; private ISConsumerCouponService isConsumerCouponService;
...@@ -89,6 +91,10 @@ public class RedisTask { ...@@ -89,6 +91,10 @@ public class RedisTask {
if (keys.size() == 0) { if (keys.size() == 0) {
return; return;
} }
//获取所有待使用的优惠卷
LambdaQueryWrapper<SConsumerCoupon> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SConsumerCoupon::getUseStatus, CouponStatusEnum.NORMAL.getValue());
List<SConsumerCoupon> list = isConsumerCouponService.list(queryWrapper);
//遍历key //遍历key
keys.stream().forEach(o -> { keys.stream().forEach(o -> {
//获取key对应的value //获取key对应的value
...@@ -99,14 +105,74 @@ public class RedisTask { ...@@ -99,14 +105,74 @@ public class RedisTask {
if (expirationTime.getTime() < new Date().getTime()) { if (expirationTime.getTime() < new Date().getTime()) {
//获取redis中的券码id //获取redis中的券码id
Long consumerCouponId = jsonObject.getLong("consumerCouponId"); Long consumerCouponId = jsonObject.getLong("consumerCouponId");
if (CollectionUtils.isEmpty(list)) {
//删除redis中的值 //删除redis中的值
redisUtil.delete(o); redisUtil.delete(o);
//删除数据库中的值 //删除数据库中的值
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId); isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
//跳过循环
return;
}
if (ObjectUtil.isEmpty(list.stream().filter(item -> item.getId().equals(consumerCouponId)).findFirst().orElse(null))) {
//删除redis中的值
redisUtil.delete(o);
//删除数据库中的值
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
} else {
//删除redis中的值
redisUtil.delete(o);
}
}
});
}
public void processAutoReceiptCode() {
// 获取redis中所有以tuangou.receipt.prepare开头的key
Set<String> keys = redisTemplate.keys(ReceiptRdeisEnum.PREPARE.getValue() + "*");
if (keys.isEmpty()) {
return;
}
// 获取所有待使用的优惠卷
List<SConsumerCoupon> availableCoupons = isConsumerCouponService.list(
new LambdaQueryWrapper<SConsumerCoupon>().eq(SConsumerCoupon::getUseStatus, CouponStatusEnum.NORMAL.getValue())
);
Set<Long> availableCouponIds = availableCoupons.stream().map(SConsumerCoupon::getId).collect(Collectors.toSet());
keys.forEach(key -> {
try {
String value = redisUtil.get(key);
if (value == null) {
return;
}
JSONObject jsonObject = new JSONObject(value);
Date expirationTime = jsonObject.getDate("expirationTime");
if (expirationTime == null) {
return;
}
// 判断是否过期
if (expirationTime.getTime() < new Date().getTime()) {
Long consumerCouponId = jsonObject.getLong("consumerCouponId");
// 如果优惠券已过期且不在可用的优惠券列表中,则删除Redis中的值和数据库中的记录
if (!availableCouponIds.contains(consumerCouponId)) {
redisUtil.delete(key);
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
} else {
// 否则只删除Redis中的值
redisUtil.delete(key);
}
}
} catch (JSONException e) {
// 记录或处理JSON解析异常
logger.error("Error parsing JSON for key: {}", key, e);
} catch (Exception e) {
// 记录或处理其他异常
logger.error("Error processing key: {}", key, e);
} }
}); });
} }
public void AutomaticMtSessionKey() { public void AutomaticMtSessionKey() {
Boolean b = redisTemplate.hasKey(ReceiptRdeisEnum.MT_SESSION_KEY.getValue()); Boolean b = redisTemplate.hasKey(ReceiptRdeisEnum.MT_SESSION_KEY.getValue());
if (!b) { if (!b) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment