完成订单状态定时处理功能
This commit is contained in:
parent
0acf4a8cb0
commit
02c6a06dc8
@ -4,12 +4,14 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableTransactionManagement //开启注解方式的事务管理
|
@EnableTransactionManagement //开启注解方式的事务管理
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@EnableCaching //开启注解方式的缓存管理
|
@EnableCaching //开启注解方式的缓存管理
|
||||||
|
@EnableScheduling //开启注解方式的定时任务
|
||||||
public class SkyApplication {
|
public class SkyApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(SkyApplication.class, args);
|
SpringApplication.run(SkyApplication.class, args);
|
||||||
|
@ -6,6 +6,9 @@ import com.sky.entity.Orders;
|
|||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface OrderMapper {
|
public interface OrderMapper {
|
||||||
/**
|
/**
|
||||||
@ -49,4 +52,12 @@ public interface OrderMapper {
|
|||||||
*/
|
*/
|
||||||
@Select("select count(*) from orders where status = #{status}")
|
@Select("select count(*) from orders where status = #{status}")
|
||||||
Integer countStatus(Integer status);
|
Integer countStatus(Integer status);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据状态和下单时间查询订单
|
||||||
|
* @param status
|
||||||
|
* @param orderTime
|
||||||
|
*/
|
||||||
|
@Select("select * from orders where status = #{status} and order_time < #{orderTime}")
|
||||||
|
List<Orders> getByStatusAndOrdertimeLT(Integer status, LocalDateTime orderTime);
|
||||||
}
|
}
|
||||||
|
62
sky-server/src/main/java/com/sky/task/OrderTask.java
Normal file
62
sky-server/src/main/java/com/sky/task/OrderTask.java
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package com.sky.task;
|
||||||
|
|
||||||
|
import com.sky.entity.Orders;
|
||||||
|
import com.sky.mapper.OrderMapper;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定时任务类,定时处理订单状态
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class OrderTask {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OrderMapper orderMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理超时订单的方法
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "0 * * * * *") // 每分钟0秒执行一次
|
||||||
|
public void processTimeoutOrder() {
|
||||||
|
log.info("定时处理超时订单,{}", LocalDateTime.now());
|
||||||
|
|
||||||
|
LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
|
||||||
|
|
||||||
|
// select * from orders where status = 1 and order_time < 当前时间-15分钟
|
||||||
|
List<Orders> ordersList = orderMapper.getByStatusAndOrdertimeLT(Orders.PENDING_PAYMENT, time);
|
||||||
|
if(ordersList != null && ordersList.size() > 0){
|
||||||
|
ordersList.forEach(order -> {
|
||||||
|
order.setStatus(Orders.CANCELLED);
|
||||||
|
order.setCancelReason("支付超时,自动取消");
|
||||||
|
order.setCancelTime(LocalDateTime.now());
|
||||||
|
orderMapper.update(order);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理“派送中”状态的订单
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "0 0 1 * * ?")
|
||||||
|
public void processDeliveryOrder(){
|
||||||
|
log.info("处理派送中订单:{}", LocalDateTime.now());
|
||||||
|
|
||||||
|
// select * from orders where status = 4 and order_time < 当前时间-1小时
|
||||||
|
LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
|
||||||
|
List<Orders> ordersList = orderMapper.getByStatusAndOrdertimeLT(Orders.DELIVERY_IN_PROGRESS, time);
|
||||||
|
|
||||||
|
if(ordersList != null && ordersList.size() > 0){
|
||||||
|
ordersList.forEach(order -> {
|
||||||
|
order.setStatus(Orders.COMPLETED);
|
||||||
|
orderMapper.update(order);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user