订单超时、订单派送超时
This commit is contained in:
parent
c6cae41822
commit
acadf1b99b
|
@ -4,9 +4,11 @@ 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;
|
||||||
|
|
||||||
@EnableCaching// 开启缓存注解
|
@EnableCaching// 开启缓存注解
|
||||||
|
@EnableScheduling// 开启task
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableTransactionManagement // 开启注解方式的事务管理
|
@EnableTransactionManagement // 开启注解方式的事务管理
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
|
|
@ -2,25 +2,39 @@ package com.sky.mapper;
|
||||||
|
|
||||||
import com.sky.entity.Orders;
|
import com.sky.entity.Orders;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Select;
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface OrderMapper {
|
public interface OrderMapper {
|
||||||
// 向订单中插入1条数据
|
/**
|
||||||
|
* 向订单中插入1条数据
|
||||||
|
*
|
||||||
|
* @param orders Orders
|
||||||
|
*/
|
||||||
void insert(Orders orders);
|
void insert(Orders orders);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据订单号查询订单
|
* 根据订单号查询订单
|
||||||
*
|
*
|
||||||
* @param orderNumber
|
* @param orderNumber String
|
||||||
*/
|
*/
|
||||||
@Select("select * from orders where number = #{orderNumber}")
|
|
||||||
Orders getByNumber(String orderNumber);
|
Orders getByNumber(String orderNumber);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改订单信息
|
* 修改订单信息
|
||||||
*
|
*
|
||||||
* @param orders
|
* @param orders Orders
|
||||||
*/
|
*/
|
||||||
void update(Orders orders);
|
void update(Orders orders);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据订单状态和下单时间查询订单状态
|
||||||
|
*
|
||||||
|
* @param status Integer
|
||||||
|
* @param orderTime LocalDateTime
|
||||||
|
* @return List<Orders>
|
||||||
|
*/
|
||||||
|
List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime orderTime);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.sky.task;
|
||||||
|
|
||||||
|
import com.sky.entity.Orders;
|
||||||
|
import com.sky.mapper.OrderMapper;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class OrderTask {
|
||||||
|
@Resource
|
||||||
|
private OrderMapper orderMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理超时订单
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "0 * * * * ?")
|
||||||
|
// @Scheduled(cron = "0/5 * * * * ?")
|
||||||
|
public void processTImeOrder() {
|
||||||
|
log.info("定时处理超时订单:{}", LocalDateTime.now());
|
||||||
|
LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
|
||||||
|
List<Orders> list = orderMapper.getByStatusAndOrderTime(Orders.PENDING_PAYMENT, time);
|
||||||
|
if (list != null && !list.isEmpty()) {
|
||||||
|
for (Orders orders : list) {
|
||||||
|
orders.setStatus(Orders.CANCELLED);
|
||||||
|
orders.setCancelReason("订单超时,自动取消");
|
||||||
|
orders.setCancelTime(LocalDateTime.now());
|
||||||
|
orderMapper.update(orders);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理一直派送中的订单
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "0 0 1 * * ?")
|
||||||
|
// @Scheduled(cron = "0/5 * * * * ?")
|
||||||
|
public void processDeliveryOrder() {
|
||||||
|
log.info("定时处理处于派送中的订单:{}", LocalDateTime.now());
|
||||||
|
LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
|
||||||
|
List<Orders> list = orderMapper.getByStatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS, time);
|
||||||
|
if (list != null && !list.isEmpty()) {
|
||||||
|
for (Orders orders : list) {
|
||||||
|
orders.setStatus(Orders.COMPLETED);
|
||||||
|
orderMapper.update(orders);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -46,4 +46,19 @@
|
||||||
where id = #{id}
|
where id = #{id}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
<!-- 根据订单号查询订单 -->
|
||||||
|
<select id="getByNumber" resultType="com.sky.entity.Orders">
|
||||||
|
select *
|
||||||
|
from orders
|
||||||
|
where number = #{orderNumber}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<!-- 根据订单状态和下单时间查询订单状态 -->
|
||||||
|
<select id="getByStatusAndOrderTime" resultType="com.sky.entity.Orders">
|
||||||
|
select *
|
||||||
|
from orders
|
||||||
|
where status = #{status}
|
||||||
|
and order_time < #{orderTime}
|
||||||
|
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
Loading…
Reference in New Issue