package com.jwipc.job.handler;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jwipc.constant.FlagConst;
import com.jwipc.order.entity.DeliveryOrder;
import com.jwipc.order.entity.PurchaseOrderDetail;
import com.jwipc.order.service.IDeliveryOrderService;
import com.jwipc.order.service.IPurchaseOrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class UpdateDeliveryOrderReturnConfirmStatusJob extends QuartzJobBean implements InterruptableJob {
    @Autowired
    private IDeliveryOrderService deliveryOrderService;

    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        //需要查询订单解锁日期 unlockDate 是否是三天前
        final LocalDateTime now = LocalDateTime.now().plusDays(-3);
        final LambdaQueryWrapper<DeliveryOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeliveryOrder::getReturnConfirmStatus, 1);
        queryWrapper.le(DeliveryOrder::getMvPostDate, now);
        final List<DeliveryOrder> list = deliveryOrderService.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            final Set<Long> ids = list.stream().map(DeliveryOrder::getId).collect(Collectors.toSet());
            final LambdaUpdateWrapper<DeliveryOrder> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(DeliveryOrder::getReturnConfirmStatus, 2)
                    .in(DeliveryOrder::getId, ids);
            deliveryOrderService.update(updateWrapper);
        }
    }
}
