key 过期

订单 超时 取消 定时任务
历史订单
订单取消 队列
redis 序列化
This commit is contained in:
2025-02-26 16:14:31 +08:00
parent 0674936901
commit b056d53e91
13 changed files with 191 additions and 44 deletions

View File

@@ -0,0 +1,35 @@
package com.czg.config;
import cn.hutool.core.date.DateUtil;
import com.czg.order.entity.OrderInfo;
import com.czg.order.service.OrderInfoService;
import com.czg.service.order.enums.OrderStatusEnums;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 订单定时任务
*
* @author ww
* @description
*/
@Slf4j
@Component
public class OTimeTask {
@Resource
private OrderInfoService orderInfoService;
/**
* order 过期
*/
@Scheduled(cron = "0 0 1 * * ? ")
public void run() {
orderInfoService.updateChain()
.set(OrderInfo::getStatus, OrderStatusEnums.CANCELLED.getCode())
.eq(OrderInfo::getStatus, OrderStatusEnums.UNPAID.getCode())
.lt(OrderInfo::getTradeDay, DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"))
.update();
}
}

View File

@@ -0,0 +1,51 @@
package com.czg.config;
import com.czg.order.service.OrderInfoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
* redis key过期监听
*
* @author ww
*/
@Slf4j
@Configuration
public class RedisKeyExpirationListener implements MessageListener {
@Value("${spring.data.redis.database}")
private String database;
@Resource
private OrderInfoService tbOrderInfoService;
//redis key失效监听
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// 监听特定键的过期事件
container.addMessageListener(this, new PatternTopic("__keyevent@" + database + "__:expired"));
return container;
}
@Override
public void onMessage(Message message, byte[] pattern) {
String expiredKey = new String(message.getBody());
// 检查过期的键是否以特定前缀开头
if (expiredKey.startsWith(RedisCst.classKeyExpired.EXPIRED_ORDER)) {
log.info("监听到订单过期,订单Id: {}", expiredKey);
String orderId = expiredKey.substring(RedisCst.classKeyExpired.EXPIRED_ORDER.length());
tbOrderInfoService.expired(Long.parseLong(orderId));
}
}
}

View File

@@ -42,8 +42,10 @@ public class AdminOrderController {
}
@GetMapping("/historyOrder")
public CzgResult<HistoryOrderVo> historyOrder(Long orderId) {
return CzgResult.success(orderInfoService.historyOrder(orderId));
public CzgResult<HistoryOrderVo> historyOrder(
@RequestParam(required = false) Long orderId,
@RequestParam(required = false) String tableCode) {
return CzgResult.success(orderInfoService.historyOrder(orderId, tableCode));
}
@PostMapping("/createOrder")
@@ -58,7 +60,6 @@ public class AdminOrderController {
/**
* 订单全额退款 只传订单id
* 部分退款 传参refundDetailMap {"详情id":"数量","详情id":"数量"}
*
*/
@PostMapping("/refundOrder")
public CzgResult<Object> refundOrder(@Validated @RequestBody OrderInfoRefundDTO refundDTO) {

View File

@@ -39,8 +39,10 @@ public class UserOrderController {
}
@GetMapping("/historyOrder")
public CzgResult<HistoryOrderVo> historyOrder(Long orderId) {
return CzgResult.success(orderInfoService.historyOrder(orderId));
public CzgResult<HistoryOrderVo> historyOrder(
@RequestParam(required = false) Long orderId,
@RequestParam(required = false) String tableCode) {
return CzgResult.success(orderInfoService.historyOrder(orderId, tableCode));
}
/**