package com.ruoyi.web.modules.order.job;

import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.web.modules.mall.facade.IMallConfigFacade;
import com.ruoyi.web.modules.order.domain.BizOrder;
import com.ruoyi.web.modules.order.mapper.BizOrderMapper;
import com.ruoyi.web.modules.order.service.IOrderCompletionService;

/**
 * 已发货超时自动确认收货
 */
@Component("orderAutoConfirmReceiveJob")
public class OrderAutoConfirmReceiveJob
{
    private static final Logger log = LoggerFactory.getLogger(OrderAutoConfirmReceiveJob.class);

    private static final int BATCH_SIZE = 500;

    @Autowired
    private BizOrderMapper orderMapper;

    @Autowired
    private IMallConfigFacade mallConfigFacade;

    @Autowired
    private IOrderCompletionService orderCompletionService;

    public void autoConfirmReceive()
    {
        log.info("--------------------OrderAutoConfirmReceiveJob.autoConfirmReceive() enter--------------------------");
        int days = mallConfigFacade.getAutoConfirmDays();
        List<BizOrder> orders = orderMapper.selectShippedForAutoConfirm(days, BATCH_SIZE);
        if (orders == null || orders.isEmpty())
        {
            return;
        }
        Date now = new Date();
        for (BizOrder order : orders)
        {
            try
            {
                orderCompletionService.completeOrder(order, now, "system", "系统自动确认收货");
            }
            catch (Exception ex)
            {
                log.warn("auto confirm order failed, orderId={}", order.getOrderId(), ex);
            }
        }
    }
}
