苍穹外卖项目学习日记(13) day10
Spring task
-
cron表达式
-
导入spring task坐标(spring-context已经存在),添加
@EnableScheduling
注解
订单状态定时处理
- 新建task包,并且创建OrderTask定时类,添加处理超时订单和处理一直派送中的订单方法
- OrderTask.java
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 * * * * ?")//每分钟触发一次
public void processTimeoutOrder() {
log.info("定时处理超时订单:{}", LocalDateTime.now());
LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-15);
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.PENDING_PAYMENT, localDateTime);
if (ordersList != null && ordersList.size() > 0) {
for (Orders orders : ordersList) {
orders.setStatus(Orders.CANCELLED);
orders.setCancelReason("订单超时自动取消");
orders.setCancelTime(LocalDateTime.now());
orderMapper.update(orders);
}
}
}
/**
* 处理一直派送中的订单
*/
@Scheduled(cron = "0 0 1 * * ?")//每天凌晨一点
public void processDeliveryOrder(){
log.info("处理一直派送中的订单:{}",LocalDateTime.now());
LocalDateTime localDateTime = LocalDateTime.now().plusHours(-1);
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.DELIVERY_IN_PROGRESS, localDateTime);
if (ordersList != null && ordersList.size() > 0) {
for (Orders orders : ordersList) {
orders.setStatus(Orders.COMPLETED);
orders.setCancelReason("订单超时自动取消");
orders.setCancelTime(LocalDateTime.now());
orderMapper.update(orders);
}
}
}
}
来单提醒
- 因为无法实现支付功能,所以直接在模拟订单支付处修改,而不是支付成功处
- OrderServiceImpl.java
@Override
public void payment(OrdersPaymentDTO ordersPaymentDTO) {
//下单时直接根据用户id更新订单状态为已支付
Long currentId = BaseContext.getCurrentId();
Orders ordersDB = orderMapper.getByNumber(ordersPaymentDTO.getOrderNumber());
// 根据订单id更新订单的状态、支付方式、支付状态、结账时间
Orders orders = Orders.builder()
.id(ordersDB.getId())
.userId(currentId)
.status(Orders.TO_BE_CONFIRMED)
.payStatus(Orders.PAID)
.checkoutTime(LocalDateTime.now())
.build();
orderMapper.updateByUserId(orders);
Map map = new HashMap<>();
map.put("type",1);
map.put("orderId",ordersDB.getId());
map.put("content","订单号" + ordersPaymentDTO.getOrderNumber());
String s = JSON.toJSONString(map);
webSocketServer.sendToAllClient(s);
}
客户催单
- user controller层添加客户催单方法
OrderController.java
@GetMapping("/reminder/{id}")
@ApiOperation("用户催单")
public Result reminder(@PathVariable Long id){
orderService.reminder(id);
log.info("用户催单:{}",id);
return Result.success();
}
- OrderServiceImpl相应添加并且实现
- OrderServiceImpl.java
@Override
public void reminder(Long id) {
// 根据id查询订单
Orders ordersDB = orderMapper.getByOrderId(id);
// 校验订单是否存在,并且状态为4
if (ordersDB == null ) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}
Map map = new HashMap<>();
map.put("type",2);
map.put("orderId",id);
map.put("content","订单号" + ordersDB.getNumber());
webSocketServer.sendToAllClient(JSON.toJSONString(map));
}