苍穹外卖--订单状态自动更新:超时未支付&派送中修改为已完成
This commit is contained in:
@@ -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,7 @@ import com.sky.entity.Orders;
|
|||||||
import org.apache.ibatis.annotations.*;
|
import org.apache.ibatis.annotations.*;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface OrderMapper {
|
public interface OrderMapper {
|
||||||
@@ -47,4 +48,13 @@ public interface OrderMapper {
|
|||||||
*/
|
*/
|
||||||
@Select("select count(id) from orders where status = #{status}")
|
@Select("select count(id) from orders where status = #{status}")
|
||||||
Integer countStatus(Integer status);
|
Integer countStatus(Integer status);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据支付状态和时间获取订单的查询操作
|
||||||
|
* @param status
|
||||||
|
* @param time
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Select("select * from orders where status = #{status} and order_time < #{time}")
|
||||||
|
List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime time);
|
||||||
}
|
}
|
||||||
|
|||||||
17
sky-server/src/main/java/com/sky/task/MyTask.java
Normal file
17
sky-server/src/main/java/com/sky/task/MyTask.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.sky.task;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class MyTask {
|
||||||
|
|
||||||
|
// @Scheduled(cron = "0/5 * * * * ?")
|
||||||
|
public void executeTask(){
|
||||||
|
log.info("定时任务开始执行:{}",new Date());
|
||||||
|
}
|
||||||
|
}
|
||||||
65
sky-server/src/main/java/com/sky/task/OrderTask.java
Normal file
65
sky-server/src/main/java/com/sky/task/OrderTask.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class OrderTask {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OrderMapper orderMapper;
|
||||||
|
|
||||||
|
//处理订单超时未支付状态
|
||||||
|
// @Scheduled(cron = "0 * * * * ? ")
|
||||||
|
@Scheduled(cron = "0/5 * * * * ? ")
|
||||||
|
public void procesTimeOutOrder(){
|
||||||
|
log.info("定时任务自动执行,处理支付超时订单:"+ LocalDateTime.now());
|
||||||
|
//每分中查询一下订单,是否有超过15分(下单时间+15分钟 < 当前时间)
|
||||||
|
//select * from orders where status = 未支付 and order_time < 当前时间 - 15分钟;
|
||||||
|
LocalDateTime timeout = LocalDateTime.now().minusMinutes(15);
|
||||||
|
//根据支付状态和时间获取订单的查询操作
|
||||||
|
List<Orders> orders = orderMapper.getByStatusAndOrderTime(Orders.PENDING_PAYMENT,timeout);
|
||||||
|
//全部设置订单的状态为已取消,并设置取消原因
|
||||||
|
if (orders != null && orders.size() > 0) {
|
||||||
|
orders.stream().forEach(order->{
|
||||||
|
order.setStatus(Orders.CANCELLED);
|
||||||
|
order.setCancelReason("支付超时,自动取消");
|
||||||
|
order.setCancelTime(LocalDateTime.now());
|
||||||
|
orderMapper.update(order);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//每日处理“派送中”的订单为已完成,每天凌晨1点执行一次
|
||||||
|
// @Scheduled(cron = "0 0 1 * * ? ")
|
||||||
|
@Scheduled(cron = "0/5 * * * * ? ")
|
||||||
|
public void processPsOrder(){
|
||||||
|
log.info("定时任务自动执行,处理派送中,未完成的订单:"+ LocalDateTime.now());
|
||||||
|
//每天上午1点查询一下,前一天是否有派送中,未完成的订单(下单时间< 当前时间 - 1小时)
|
||||||
|
//select * from orders where status = 派送中 and order_time < 当前时间 - 1小时;
|
||||||
|
LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
|
||||||
|
|
||||||
|
|
||||||
|
//根据支付状态和时间获取订单的查询操作
|
||||||
|
List<Orders> orders = orderMapper.getByStatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS,time);
|
||||||
|
//全部设置订单的状态为已完成
|
||||||
|
if (orders != null && orders.size() > 0) {
|
||||||
|
orders.stream().forEach(order->{
|
||||||
|
order.setStatus(Orders.COMPLETED);
|
||||||
|
order.setDeliveryTime(LocalDateTime.now());
|
||||||
|
orderMapper.update(order);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user