package com.mcwl.pay.task;

import com.mcwl.pay.domain.OrderTrade;
import com.mcwl.pay.service.OrderTradeService;
import lombok.RequiredArgsConstructor;
import org.joda.time.LocalDateTime;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component("payTask")
@RequiredArgsConstructor
public class PayTask {

    private final OrderTradeService orderTradeService;


    /**
     * 删除失效订单 间隔1h检查 0 0 0/1 * * ?
     */
    public void deleteInvalidOrderTask() {

        Date date = LocalDateTime.now().plusMinutes(-15).toDate();

        List<OrderTrade> orderTradeList = orderTradeService.lambdaQuery()
                .le(OrderTrade::getCreateTime, date)
                .and(wrapper -> wrapper
                        .eq(OrderTrade::getOrderStatus, 1)
                        .or()
                        .eq(OrderTrade::getOrderStatus, 4)
                        .or()
                        .eq(OrderTrade::getPayStatus, 1)
                )
                .list();

        orderTradeService.removeBatchByIds(orderTradeList);

    }


}
