苍穹外卖day10
- Spring Task
 - 订单状态定时处理
 - WebSocket
 - 应用(弹幕,网页聊天,体育实况更新,股票基金实时更新)
 
- 来单题型
 - 代码实现
 - 需求分析
 
- 客户催单
 
Spring Task

 

链接: 在线生成器
在线生成器
 


订单状态定时处理
每分钟检查一下,是否有订单超过十五分钟,如果有的话就取消
	@Scheduled(cron = "0 * * * * ?  ")
    public void processTimeoutOrder(){
        log.info("定时处理超时订单:{}", LocalDateTime.now());
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-15);
        //查询超时的订单 待付款
        List<Orders> list = orderMapper.getByStatusAndOrderTime(Orders.PENDING_PAYMENT,localDateTime);
        if(list != null && list.size() > 0){
            for(Orders orders : list){
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelReason("订单超时,自动取消");
                orders.setCancelTime(LocalDateTime.now());
            }
        }
    }
 
@Select("select * from orders where status status = #{status} and order_time<#{orderTime}")
    List<Orders> getByStatusAndOrderTime(Integer status,LocalDateTime orderTime);
 
//处理一直在派送的订单
    @Scheduled(cron = "0 0 1 * * ?")
    public void processDeliverOrder(){
        log.info("定时处理派送中的订单L{}",LocalDateTime.now());
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-60);
        List<Orders> list = orderMapper.getByStatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS,localDateTime);
        if(list != null && list.size() > 0){
            for(Orders orders : list){
                orders.setStatus(Orders.COMPLETED);
                orderMapper.update(orders);
            }
        }
    }
 
WebSocket
http是短链接,使用请求到回复的那一小段时间是连接的

应用(弹幕,网页聊天,体育实况更新,股票基金实时更新)
来单题型

 需要WebSocket保持长连接
 支付成功之后,调用WebSocket的相关API实现服务端向客户端推送消息
 约定服务端发送给客户端浏览器的数据格式为JSON
代码实现
public void paySuccess(String outTradeNo) {
        // 根据订单号查询订单
        Orders ordersDB = orderMapper.getByNumber(outTradeNo);
        // 根据订单id更新订单的状态、支付方式、支付状态、结账时间
        Orders orders = Orders.builder()
                .id(ordersDB.getId())
                .status(Orders.TO_BE_CONFIRMED)
                .payStatus(Orders.PAID)
                .checkoutTime(LocalDateTime.now())
                .build();
        orderMapper.update(orders);
        //通过websocket向客户端浏览器推送消息
        Map map = new HashMap();
        map.put("type",1);//表示1来单提醒 2客户催单
        map.put("orderId",ordersDB.getId());
        map.put("content","订单号:"+outTradeNo);
        String jsonString = JSON.toJSONString(map);
        webSocketServer.sendToAllClient(jsonString);
    }
 
使用webSocketServer的函数进行websocket的消息传输
public void sendToAllClient(String message) {
        Collection<Session> sessions = sessionMap.values();
        for (Session session : sessions) {
            try {
                //服务器向客户端发送消息
                session.getBasicRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
使用session去推送数据
需求分析
客户催单




















