|
|
@@ -10,6 +10,8 @@ import beilv.stadium.domain.Stadium;
|
|
|
import beilv.vipCardLog.domain.VipCardLog;
|
|
|
import beilv.vipCardLog.mapper.VipCardLogMapper;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
@@ -21,123 +23,82 @@ import java.util.Set;
|
|
|
|
|
|
import static beilv.competition.domain.Constant.*;
|
|
|
|
|
|
-/**
|
|
|
- * 轮询Redis有序集合, 处理到期的任务.
|
|
|
- * 将到期的任务发布, 由监听者接收
|
|
|
- * 同时移除其他相同eventId的任务
|
|
|
- */
|
|
|
-
|
|
|
@Component
|
|
|
public class RedisTaskChecker {
|
|
|
|
|
|
+ // 日志记录器
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(RedisTaskChecker.class);
|
|
|
+
|
|
|
+ // 注入 Redis 模板
|
|
|
@Autowired
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
+ // 注入比赛 Mapper
|
|
|
@Resource
|
|
|
private CompetitionMapper competitionMapper;
|
|
|
|
|
|
+ // 注入会员卡日志 Mapper
|
|
|
@Resource
|
|
|
private VipCardLogMapper vipCardLogMapper;
|
|
|
|
|
|
+ // 注入门票 Mapper
|
|
|
@Resource
|
|
|
private AdmissionTicketMapper ticketMapper;
|
|
|
|
|
|
+ // 注入 ObjectMapper 用于 JSON 转换
|
|
|
+ @Autowired
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+
|
|
|
/**
|
|
|
- * 检测到期任务, 并将消息发布
|
|
|
+ * 定时任务,每秒检查一次,处理报名结束的任务
|
|
|
*/
|
|
|
@Scheduled(fixedRate = 1000)
|
|
|
public void checkTasks() {
|
|
|
- long currentTime = System.currentTimeMillis() / 1000;
|
|
|
-
|
|
|
- // 获取所有有序集合的键
|
|
|
- Set<String> keys = stringRedisTemplate.keys(TASK_QUEUE_QUERY_APPLY);
|
|
|
- if (keys != null && !keys.isEmpty()) {
|
|
|
- for (String key : keys) {
|
|
|
- // 获取当前时间之前的所有任务
|
|
|
- Set<String> tasks = stringRedisTemplate.opsForZSet().rangeByScore(key, 0, currentTime);
|
|
|
- if (tasks != null && !tasks.isEmpty()) {
|
|
|
- for (String taskJson : tasks) {
|
|
|
- try {
|
|
|
-// System.out.println("taskJson => " + taskJson);
|
|
|
-
|
|
|
- //获取任务信息
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- RedisTask task = objectMapper.readValue(taskJson, RedisTask.class);
|
|
|
-
|
|
|
- //赛事报名结束: 参赛订单核销, 赛事状态:报名结束
|
|
|
-
|
|
|
- Competition competition = new Competition();
|
|
|
- competition.setId(task.getCompetitionId());
|
|
|
- competition.setCompetitionState(task.getCompetitionState());
|
|
|
- competitionMapper.updateCompetition(competition);
|
|
|
-
|
|
|
-
|
|
|
- //赛事id, 订单状态, 核验时间
|
|
|
- VipCardLog vipCardLog = new VipCardLog(String.valueOf(task.getCompetitionId()), "cansai", task.getPaymentStatus(), DateUtils.getNowDate());
|
|
|
- vipCardLogMapper.updateVipCardLogByBusId(vipCardLog);
|
|
|
-
|
|
|
- // 移除当前已发布的任务
|
|
|
- stringRedisTemplate.opsForZSet().remove(key, taskJson);
|
|
|
- System.out.println("Removed task: " + taskJson);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ processTasks(TASK_QUEUE_QUERY_APPLY, this::handleTaskApply);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 定时任务,每秒检查一次,处理比赛开始的任务
|
|
|
+ */
|
|
|
@Scheduled(fixedRate = 1000)
|
|
|
public void checkTasksStart() {
|
|
|
- long currentTime = System.currentTimeMillis() / 1000;
|
|
|
-
|
|
|
- // 获取所有有序集合的键
|
|
|
- Set<String> keys = stringRedisTemplate.keys(TASK_QUEUE_QUERY_START);
|
|
|
- if (keys != null && !keys.isEmpty()) {
|
|
|
- for (String key : keys) {
|
|
|
- // 获取当前时间之前的所有任务
|
|
|
- Set<String> tasks = stringRedisTemplate.opsForZSet().rangeByScore(key, 0, currentTime);
|
|
|
- if (tasks != null && !tasks.isEmpty()) {
|
|
|
- for (String taskJson : tasks) {
|
|
|
- try {
|
|
|
-// System.out.println("taskJson => " + taskJson);
|
|
|
-
|
|
|
- //获取任务信息
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- RedisTask task = objectMapper.readValue(taskJson, RedisTask.class);
|
|
|
-
|
|
|
- //比赛开始, 门票订单核销, 赛事状态, 比赛开始
|
|
|
-
|
|
|
- Competition competition = new Competition();
|
|
|
- competition.setId(task.getCompetitionId());
|
|
|
- competition.setCompetitionState(task.getCompetitionState());
|
|
|
- competitionMapper.updateCompetition(competition);
|
|
|
-
|
|
|
- //赛事id, 订单状态, 核验时间
|
|
|
- VipCardLog vipCardLog = new VipCardLog(String.valueOf(task.getCompetitionId()), "menpiao", task.getPaymentStatus(), DateUtils.getNowDate());
|
|
|
- vipCardLogMapper.updateVipCardLogByBusId(vipCardLog);
|
|
|
+ processTasks(TASK_QUEUE_QUERY_START, this::handleTaskStart);
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 定时任务,每秒检查一次,处理比赛结束的任务
|
|
|
+ */
|
|
|
+ @Scheduled(fixedRate = 1000)
|
|
|
+ public void checkTasksEnd() {
|
|
|
+ processTasks(TASK_QUEUE_QUERY_END, this::handleTaskEnd);
|
|
|
+ }
|
|
|
|
|
|
- // 移除当前已发布的任务
|
|
|
- stringRedisTemplate.opsForZSet().remove(key, taskJson);
|
|
|
- System.out.println("Removed task: " + taskJson);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 定时任务,每秒检查一次,处理门票相关任务
|
|
|
+ */
|
|
|
+ @Scheduled(fixedRate = 1000)
|
|
|
+ public void checkTicket() {
|
|
|
+ processTasks(TICKET_QUEUE_ALL, this::handleTicketTask);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 定时任务,每秒检查一次,处理订单相关任务
|
|
|
+ */
|
|
|
@Scheduled(fixedRate = 1000)
|
|
|
- public void checkTasksEnd() {
|
|
|
- long currentTime = System.currentTimeMillis() / 1000;
|
|
|
+ public void checkOrder() {
|
|
|
+ processTasks(ORDER_QUEUE_ALL, this::handleOrderTask);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用的任务处理方法
|
|
|
+ * @param keyPattern Redis 键的模式
|
|
|
+ * @param taskHandler 具体的任务处理逻辑
|
|
|
+ */
|
|
|
+ private void processTasks(String keyPattern, TaskHandler taskHandler) {
|
|
|
+ long currentTime = System.currentTimeMillis() / 1000; // 当前时间戳(秒)
|
|
|
|
|
|
- // 获取所有有序集合的键
|
|
|
- Set<String> keys = stringRedisTemplate.keys(TASK_QUEUE_QUERY_END);
|
|
|
+ // 获取所有匹配的 Redis 键
|
|
|
+ Set<String> keys = stringRedisTemplate.keys(keyPattern);
|
|
|
if (keys != null && !keys.isEmpty()) {
|
|
|
for (String key : keys) {
|
|
|
// 获取当前时间之前的所有任务
|
|
|
@@ -145,25 +106,16 @@ public class RedisTaskChecker {
|
|
|
if (tasks != null && !tasks.isEmpty()) {
|
|
|
for (String taskJson : tasks) {
|
|
|
try {
|
|
|
-// System.out.println("taskJson => " + taskJson);
|
|
|
-
|
|
|
- //获取任务信息
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ // 将 JSON 字符串转换为 RedisTask 对象
|
|
|
RedisTask task = objectMapper.readValue(taskJson, RedisTask.class);
|
|
|
-
|
|
|
- //比赛开始, 门票订单核销, 赛事状态, 比赛开始
|
|
|
-
|
|
|
- Competition competition = new Competition();
|
|
|
- competition.setId(task.getCompetitionId());
|
|
|
- competition.setCompetitionState(task.getCompetitionState());
|
|
|
- competitionMapper.updateCompetition(competition);
|
|
|
-
|
|
|
-
|
|
|
- // 移除当前已发布的任务
|
|
|
+ // 调用具体的任务处理逻辑
|
|
|
+ taskHandler.handle(task);
|
|
|
+ // 从 Redis 中移除已处理的任务
|
|
|
stringRedisTemplate.opsForZSet().remove(key, taskJson);
|
|
|
- System.out.println("Removed task: " + taskJson);
|
|
|
+ logger.info("Removed task: {}", taskJson);
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ // 记录异常信息
|
|
|
+ logger.error("Error processing task: {}", taskJson, e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -171,102 +123,125 @@ public class RedisTaskChecker {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理报名结束的任务
|
|
|
+ * @param task RedisTask 对象
|
|
|
+ */
|
|
|
+ private void handleTaskApply(RedisTask task) {
|
|
|
+ // 更新比赛状态
|
|
|
+ updateCompetitionState(task.getCompetitionId(), task.getCompetitionState());
|
|
|
+ // 更新会员卡日志
|
|
|
+ updateVipCardLog(task.getCompetitionId(), "cansai", task.getPaymentStatus());
|
|
|
+ }
|
|
|
|
|
|
- @Scheduled(fixedRate = 1000)
|
|
|
- public void checkTicket() {
|
|
|
- long currentTime = System.currentTimeMillis() / 1000;
|
|
|
-
|
|
|
- // 获取所有有序集合的键
|
|
|
- Set<String> keys = stringRedisTemplate.keys(TICKET_QUEUE_ALL);
|
|
|
- if (keys != null && !keys.isEmpty()) {
|
|
|
- for (String key : keys) {
|
|
|
- // 获取当前时间之前的所有任务
|
|
|
- Set<String> tasks = stringRedisTemplate.opsForZSet().rangeByScore(key, 0, currentTime);
|
|
|
- if (tasks != null && !tasks.isEmpty()) {
|
|
|
- for (String taskJson : tasks) {
|
|
|
- try {
|
|
|
-// System.out.println("taskJson => " + taskJson);
|
|
|
-
|
|
|
- //获取任务信息
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- RedisTask task = objectMapper.readValue(taskJson, RedisTask.class);
|
|
|
+ /**
|
|
|
+ * 处理比赛开始的任务
|
|
|
+ * @param task RedisTask 对象
|
|
|
+ */
|
|
|
+ private void handleTaskStart(RedisTask task) {
|
|
|
+ // 更新比赛状态
|
|
|
+ updateCompetitionState(task.getCompetitionId(), task.getCompetitionState());
|
|
|
+ // 更新会员卡日志
|
|
|
+ updateVipCardLog(task.getCompetitionId(), "menpiao", task.getPaymentStatus());
|
|
|
+ }
|
|
|
|
|
|
- //修改订单状态为核销
|
|
|
- VipCardLog vipCardLog = new VipCardLog();
|
|
|
- vipCardLog.setBusId(String.valueOf(task.getCompetitionId()));
|
|
|
- vipCardLog.setOrderType("yueqiu");
|
|
|
- vipCardLog.setVerifyTime(DateUtils.getNowDate());
|
|
|
- vipCardLog.setPaymentStatus(task.getPaymentStatus());
|
|
|
- vipCardLogMapper.updateVipCardLogByBusId(vipCardLog);
|
|
|
+ /**
|
|
|
+ * 处理比赛结束的任务
|
|
|
+ * @param task RedisTask 对象
|
|
|
+ */
|
|
|
+ private void handleTaskEnd(RedisTask task) {
|
|
|
+ // 更新比赛状态
|
|
|
+ updateCompetitionState(task.getCompetitionId(), task.getCompetitionState());
|
|
|
+ }
|
|
|
|
|
|
- //修改场次状态为核销
|
|
|
- AdmissionTicket admissionTicket = new AdmissionTicket();
|
|
|
- admissionTicket.setId(task.getCompetitionId());
|
|
|
- admissionTicket.setAdmissionTicketStatus(task.getCompetitionState());
|
|
|
- ticketMapper.updateBeilvAdmissionTicket(admissionTicket);
|
|
|
+ /**
|
|
|
+ * 处理门票相关任务
|
|
|
+ * @param task RedisTask 对象
|
|
|
+ */
|
|
|
+ private void handleTicketTask(RedisTask task) {
|
|
|
+ // 更新会员卡日志
|
|
|
+ updateVipCardLog(task.getCompetitionId(), "yueqiu", task.getPaymentStatus());
|
|
|
+ // 更新门票状态
|
|
|
+ updateAdmissionTicketStatus(task.getCompetitionId(), task.getCompetitionState());
|
|
|
+ }
|
|
|
|
|
|
- // 移除当前已发布的任务
|
|
|
- stringRedisTemplate.opsForZSet().remove(key, taskJson);
|
|
|
- System.out.println("Removed task: " + taskJson);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 处理订单相关任务
|
|
|
+ * @param task RedisTask 对象
|
|
|
+ */
|
|
|
+ private void handleOrderTask(RedisTask task) {
|
|
|
+ // 更新会员卡日志
|
|
|
+ updateVipCardLog(task.getCompetitionId(), task.getPaymentStatus());
|
|
|
+ // 如果是约球订单,重置球场信息
|
|
|
+ if ("yueqiu".equals(task.getOrderType())) {
|
|
|
+ resetAdmissionTicketStatus(task.getBusId());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新比赛状态
|
|
|
+ * @param competitionId 比赛 ID
|
|
|
+ * @param competitionState 比赛状态
|
|
|
+ */
|
|
|
+ private void updateCompetitionState(String competitionId, String competitionState) {
|
|
|
+ Competition competition = new Competition();
|
|
|
+ competition.setId(competitionId);
|
|
|
+ competition.setCompetitionState(competitionState);
|
|
|
+ competitionMapper.updateCompetition(competition);
|
|
|
+ }
|
|
|
|
|
|
- @Scheduled(fixedRate = 1000)
|
|
|
- public void checkOrder() {
|
|
|
- long currentTime = System.currentTimeMillis() / 1000;
|
|
|
-
|
|
|
- // 获取所有有序集合的键
|
|
|
- Set<String> keys = stringRedisTemplate.keys(ORDER_QUEUE_ALL);
|
|
|
- if (keys != null && !keys.isEmpty()) {
|
|
|
- for (String key : keys) {
|
|
|
- // 获取当前时间之前的所有任务
|
|
|
- Set<String> tasks = stringRedisTemplate.opsForZSet().rangeByScore(key, 0, currentTime);
|
|
|
- if (tasks != null && !tasks.isEmpty()) {
|
|
|
- for (String taskJson : tasks) {
|
|
|
- try {
|
|
|
-// System.out.println("taskJson => " + taskJson);
|
|
|
-
|
|
|
- //获取任务信息
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- RedisTask task = objectMapper.readValue(taskJson, RedisTask.class);
|
|
|
+ /**
|
|
|
+ * 更新会员卡日志
|
|
|
+ * @param competitionId 比赛 ID
|
|
|
+ * @param orderType 订单类型
|
|
|
+ * @param paymentStatus 支付状态
|
|
|
+ */
|
|
|
+ private void updateVipCardLog(String competitionId, String orderType, String paymentStatus) {
|
|
|
+ VipCardLog vipCardLog = new VipCardLog(String.valueOf(competitionId), orderType, paymentStatus, DateUtils.getNowDate());
|
|
|
+ vipCardLogMapper.updateVipCardLogByBusId(vipCardLog);
|
|
|
+ }
|
|
|
|
|
|
- //根据订单id, 修改订单为已取消
|
|
|
- VipCardLog vipCardLog = new VipCardLog();
|
|
|
- vipCardLog.setId(task.getCompetitionId());
|
|
|
- vipCardLog.setPaymentStatus(task.getPaymentStatus());
|
|
|
- vipCardLog.setCancellationTime(DateUtils.getNowDate());
|
|
|
- vipCardLogMapper.updateVipCardLog(vipCardLog);
|
|
|
+ /**
|
|
|
+ * 更新会员卡日志(仅支付状态)
|
|
|
+ * @param competitionId 比赛 ID
|
|
|
+ * @param paymentStatus 支付状态
|
|
|
+ */
|
|
|
+ private void updateVipCardLog(String competitionId, String paymentStatus) {
|
|
|
+ VipCardLog vipCardLog = new VipCardLog();
|
|
|
+ vipCardLog.setId(competitionId);
|
|
|
+ vipCardLog.setPaymentStatus(paymentStatus);
|
|
|
+ vipCardLog.setCancellationTime(DateUtils.getNowDate());
|
|
|
+ vipCardLogMapper.updateVipCardLog(vipCardLog);
|
|
|
+ }
|
|
|
|
|
|
- //判断订单类型, 如果是约球, 要重置球场信息
|
|
|
- if("yueqiu".equals(task.getOrderType())){
|
|
|
- AdmissionTicket admissionTicket = new AdmissionTicket();
|
|
|
- admissionTicket.setId(task.getBusId());
|
|
|
- admissionTicket.setAdmissionTicketStatus("0");
|
|
|
- ticketMapper.updateBeilvAdmissionTicket(admissionTicket);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 更新门票状态
|
|
|
+ * @param ticketId 门票 ID
|
|
|
+ * @param status 状态
|
|
|
+ */
|
|
|
+ private void updateAdmissionTicketStatus(String ticketId, String status) {
|
|
|
+ AdmissionTicket admissionTicket = new AdmissionTicket();
|
|
|
+ admissionTicket.setId(ticketId);
|
|
|
+ admissionTicket.setAdmissionTicketStatus(status);
|
|
|
+ ticketMapper.updateBeilvAdmissionTicket(admissionTicket);
|
|
|
+ }
|
|
|
|
|
|
- // 移除当前已发布的任务
|
|
|
- stringRedisTemplate.opsForZSet().remove(key, taskJson);
|
|
|
- System.out.println("Removed task: " + taskJson);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 重置门票状态
|
|
|
+ * @param ticketId 门票 ID
|
|
|
+ */
|
|
|
+ private void resetAdmissionTicketStatus(String ticketId) {
|
|
|
+ AdmissionTicket admissionTicket = new AdmissionTicket();
|
|
|
+ admissionTicket.setId(ticketId);
|
|
|
+ admissionTicket.setAdmissionTicketStatus("0");
|
|
|
+ ticketMapper.updateBeilvAdmissionTicket(admissionTicket);
|
|
|
}
|
|
|
|
|
|
- public void removeTaskByKey(String zKey){
|
|
|
- stringRedisTemplate.delete(zKey);
|
|
|
+ /**
|
|
|
+ * 定义任务处理逻辑的函数式接口
|
|
|
+ */
|
|
|
+ @FunctionalInterface
|
|
|
+ interface TaskHandler {
|
|
|
+ void handle(RedisTask task);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|