Spring Task 实现超时未支付的订单自动取消

1、导入依赖

需要导入spring-context依赖,但这个依赖已经在spring-boot-starter起步依赖中包含了,如下:

2、在启动类上添加@EnableScheduling注解,开启任务调度

less 复制代码
 @SpringBootApplication
 @EnableScheduling // 开启任务调度
 public class SkyApplication {
     public static void main(String[] args) {
         SpringApplication.run(SkyApplication.class, args);
     }
 }

3、自定义定时任务类

以订单的超时未支付时间为15分钟来举例。

订单状态已定义在Orders实体类中,如下:

ruby 复制代码
 @Data
 @Builder
 @NoArgsConstructor
 @AllArgsConstructor
 public class Orders implements Serializable {
 ​
     /**
      * 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
      */
     public static final Integer PENDING_PAYMENT = 1;
     public static final Integer TO_BE_CONFIRMED = 2;
     public static final Integer CONFIRMED = 3;
     public static final Integer DELIVERY_IN_PROGRESS = 4;
     public static final Integer COMPLETED = 5;
     public static final Integer CANCELLED = 6;
     
     /////////////////////////////////////////
     // 略
     /////////////////////////////////////////
 }

自定义定时任务类:

less 复制代码
 @Component
 @Slf4j
 public class OrderTask {
 ​
     @Autowired
     private OrderMapper orderMapper;
 ​
     // 超时未支付订单处理
     // cron表达式,用来定义任务触发的时间,此处表示每分钟触发一次。该表达式怎么写,此处不赘述
     @Scheduled(cron = "0 0/1 * * * ?") 
     public void processPayTimeOut(){
         // 从订单表中查询哪些订单超时了,条件:status = 1 and order_time < 当前时间 - 15mins
         LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
         List<Orders> ordersList = orderMapper.getByStatusAndCreateTimeLT(Orders.PENDING_PAYMENT, time);
 ​
         if (ordersList != null && ordersList.size() > 0){
             for (Orders order : ordersList) {
                 // 设置支付超时订单的状态为 6已取消
                 order.setStatus(Orders.CANCELLED);
                 order.setCancelTime(LocalDateTime.now());
                 order.setCancelReason("订单支付超时");
 ​
                 // 更新数据库中的数据
                 orderMapper.update(order);
             }
         }
     }
 }

OrderMapper:

java 复制代码
 @Mapper
 public interface OrderMapper {
     @Select("select * from orders where status = #{status} and order_time < #{time}")
     List<Orders> getByStatusAndCreateTimeLT(Integer status, LocalDateTime time);
 ​
     void update(Orders orders);
 }

OrderMapper.xml:

xml 复制代码
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper
         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.sky.mapper.OrderMapper">
     
     <update id="update" parameterType="com.sky.entity.Orders">
         update orders
         <set>
             <if test="cancelReason != null and cancelReason!='' ">
                 cancel_reason=#{cancelReason},
             </if>
             <if test="rejectionReason != null and rejectionReason!='' ">
                 rejection_reason=#{rejectionReason},
             </if>
             <if test="cancelTime != null">
                 cancel_time=#{cancelTime},
             </if>
             <if test="payStatus != null">
                 pay_status=#{payStatus},
             </if>
             <if test="payMethod != null">
                 pay_method=#{payMethod},
             </if>
             <if test="checkoutTime != null">
                 checkout_time=#{checkoutTime},
             </if>
             <if test="status != null">
                 status = #{status},
             </if>
             <if test="deliveryTime != null">
                 delivery_time = #{deliveryTime}
             </if>
         </set>
         where id = #{id}
     </update>
 </mapper>
相关推荐
程序员游老板4 分钟前
基于SpringBoot3_vue3_MybatisPlus_Mysql_Maven的社区养老系统/养老院管理系统
java·spring boot·mysql·毕业设计·软件工程·信息与通信·毕设
码事漫谈21 分钟前
VS Code 1.107 更新:多智能体协同与开发体验升级
后端
福尔摩斯张23 分钟前
C++核心特性精讲:从C语言痛点出发,掌握现代C++编程精髓(超详细)
java·linux·c语言·数据结构·c++·驱动开发·算法
码事漫谈35 分钟前
从概念开始开始C++管道编程
后端
@淡 定39 分钟前
Spring中@Autowired注解的实现原理
java·后端·spring
时空无限1 小时前
Java Buildpack Reference
java·开发语言
serendipity_hky1 小时前
【go语言 | 第2篇】Go变量声明 + 常用数据类型的使用
开发语言·后端·golang
疯狂的程序猴2 小时前
App Store上架完整流程与注意事项详解
后端
爱笑的眼睛112 小时前
超越剪枝与量化:下一代AI模型压缩工具的技术演进与实践
java·人工智能·python·ai
开心就好20252 小时前
把 H5 应用上架 App Store,并不是套个壳这么简单
后端