package com.qs.mp.quartz.task;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qs.mp.channel.domain.ChannelGoodsOrder;
import com.qs.mp.channel.domain.ChannelOrder;
import com.qs.mp.channel.service.IChannelGoodsOrderService;
import com.qs.mp.common.enums.ChannelGoodsOrderStatusEnum;
import com.qs.mp.common.enums.ChannelOrderStatusEnum;
import com.qs.mp.common.utils.DateUtils;
import com.qs.mp.common.utils.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @author zhangkaikai
 * @create 2023-06-08 9:54 PM
 **/
@Component("channelGoodsOrderTask")
public class ChannelGoodsOrderTask {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

    @Autowired
    private IChannelGoodsOrderService channelGoodsOrderService;

    private final int PAGE_SIZE = 500;


    /**
     * x天前的待收货订单自动确认收货任务
     */
    public void confirm(Integer day) {
        LogUtil.info(logger, "...渠道商采购商品订单自动确认收货任务开始...");
        LocalDateTime now = LocalDateTimeUtil.now();
        LocalDateTime deliveryTime = LocalDateTimeUtil.offset(now, -day, ChronoUnit.DAYS);

        List<ChannelGoodsOrder> channelOrderList = channelGoodsOrderService.list(new LambdaQueryWrapper<ChannelGoodsOrder>()
                .eq(ChannelGoodsOrder::getStatus, ChannelGoodsOrderStatusEnum.NOT_CONFIRM.getValue())
                .le(ChannelGoodsOrder::getDeliveryTime, deliveryTime));

        if (CollectionUtils.isNotEmpty(channelOrderList)) {
            for (ChannelGoodsOrder channelGoodsOrder : channelOrderList) {
                channelGoodsOrderService.confirmOrder(channelGoodsOrder);
            }
        }

        LogUtil.info(logger, "...渠道商采购商品订单自动确认收货任务结束...");
    }



    /**
     * 订单取消到期任务
     */
    public void cancel() {
        LogUtil.info(logger, "...渠道未支付采购商品订单定时取消任务开始...");
        int total = PAGE_SIZE;
        while (total == PAGE_SIZE) {
            // 捞取30分钟前未支付订单
            List<ChannelGoodsOrder> channelGoodsOrderList = channelGoodsOrderService.list(
                    new LambdaQueryWrapper<ChannelGoodsOrder>()
                            .eq(ChannelGoodsOrder::getStatus, ChannelOrderStatusEnum.NOT_PAY)
                            .lt(ChannelGoodsOrder::getCreatedTime, DateUtils.addMinutes(DateUtils.getNowDate(), -30))
                            .last("limit " + PAGE_SIZE));
            total = channelGoodsOrderList.size();
            for (ChannelGoodsOrder channelGoodsOrder : channelGoodsOrderList) {
                try {
                    channelGoodsOrderService.cancelOrder(channelGoodsOrder.getChannelId(), channelGoodsOrder.getOrderId());
                } catch (Exception e) {
                    LogUtil.error(logger, e, "定时取消未支付渠道采购商品订单异常。orderId:{0}", channelGoodsOrder.getOrderId());
                }
            }
        }
        LogUtil.info(logger, "...渠道未支付采购商品订单定时取消任务结束...");
    }
}
