Skip to content

Commit

Permalink
自定义定时任务,处理订单状态
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-1515 committed Jul 25, 2022
1 parent ca35364 commit 72bb5e8
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
2 changes: 2 additions & 0 deletions reggie_server/src/main/java/com/reggie/ReggieApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
@EnableCaching //开启注解缓存
@EnableScheduling//开启任务调度
@Slf4j
public class ReggieApplication {
public static void main(String[] args) {
Expand Down
15 changes: 15 additions & 0 deletions reggie_server/src/main/java/com/reggie/mapper/OrderMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import com.reggie.entity.Orders;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
/**
* 插入订单数据
*
* @param order
*/
void insert(Orders order);

/**
* 根据订单号和用户id查询订单
*
* @param orderNumber
* @param userId
* @return
Expand All @@ -24,6 +28,7 @@ public interface OrderMapper {

/**
* 修改订单信息
*
* @param orders
*/
void updateOrdersById(Orders orders);
Expand All @@ -38,9 +43,19 @@ public interface OrderMapper {

/**
* 根据id查询订单
*
* @param id
* @return
*/
Orders getById(Long id);

/**
* 根据状态和下单时间查询订单
*
* @param status 状态
* @param orderTime 下单时间
* @return 订单
*/
List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

}
77 changes: 77 additions & 0 deletions reggie_server/src/main/java/com/reggie/task/OrderTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.reggie.task;

import com.reggie.entity.Orders;
import com.reggie.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;

/**
* 支付超时订单处理
* 对于下单后超过15分钟仍未支付的订单自动修改状态为 [已取消]
*/
@Scheduled(cron = "0 0/1 * * * ?")//每分钟执行一次
public void processTimeoutOrder() {
log.info("开始进行支付超时订单处理:{}", LocalDateTime.now());

//当前时间-15分钟
LocalDateTime orderTime = LocalDateTime.now().plusMinutes(-15);
//查询超出15分钟并且未付款的状态
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.PENDING_PAYMENT, orderTime);

if (ordersList != null || ordersList.size() > 0) {
//循环更新超时的订单
ordersList.forEach(item -> {
Orders order = Orders.builder()
.id(item.getId()) //订单id
.status(Orders.CANCELLED) //订单状态 6已取消
.cancelReason("订单支付超时,自动取消") //订单取消原因
.cancelTime(LocalDateTime.now()) //订单取消时间
.build();

//更新订单信息
orderMapper.updateOrdersById(order);
});
}
}

/**
* 派送中状态的订单处理
* 对于一直处于派送中状态的订单,自动修改状态为 [已完成]
*/
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行一次
public void processDeliveryOrder() {
log.info("开始进行未完成订单状态处理:{}", LocalDateTime.now());

//前一天中所有派送中的订单自动完成
LocalDateTime orderTime = LocalDateTime.now().plusMinutes(-60);
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.DELIVERY_IN_PROGRESS, orderTime);
if (ordersList != null || ordersList.size() > 0) {
//循环更新超时的订单
ordersList.forEach(item -> {
Orders order = Orders.builder()
.id(item.getId()) //订单id
.status(Orders.COMPLETED) //订单状态 5已完成
.build();

//更新订单信息
orderMapper.updateOrdersById(order);
});
}

}

}
16 changes: 14 additions & 2 deletions reggie_server/src/main/resources/mapper/OrderMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
</insert>

<select id="getByNumber" resultType="Orders">
select * from orders where number = #{orderNumber} and user_id= #{userId}
select *
from orders
where number = #{orderNumber}
and user_id = #{userId}
</select>

<update id="updateOrdersById" parameterType="com.reggie.entity.Orders">
Expand Down Expand Up @@ -76,7 +79,16 @@
</select>

<select id="getById" resultType="Orders">
select * from orders where id=#{id}
select *
from orders
where id = #{id}
</select>

<!--根据状态和下单时间查询订单-->
<select id="getByStatusAndOrderTimeLT" resultType="com.reggie.entity.Orders">
select *
from orders
where status = #{status} and order_Time &lt; #{orderTime}
</select>

</mapper>

0 comments on commit 72bb5e8

Please sign in to comment.