package com.ygtx.gxt.task;

import com.ygtx.common.utils.DateUtils;
import com.ygtx.gxt.domain.GxtRepairOrder;
import com.ygtx.gxt.domain.GxtRepairOrderFlow;
import com.ygtx.gxt.domain.GxtWorkOrder;
import com.ygtx.gxt.domain.GxtWorkOrderFlow;
import com.ygtx.gxt.mapper.GxtRepairOrderFlowMapper;
import com.ygtx.gxt.mapper.GxtRepairOrderMapper;
import com.ygtx.gxt.mapper.GxtWorkOrderFlowMapper;
import com.ygtx.gxt.mapper.GxtWorkOrderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 工单自动恢复任务
 * @author lingma
 */
@EnableScheduling
@Component("autoResumeTask")
public class OrderAutoResumeTask {

    private static final Logger log = LoggerFactory.getLogger(OrderAutoResumeTask.class);

    @Autowired
    private GxtRepairOrderMapper gxtRepairOrderMapper;

    @Autowired
    private GxtWorkOrderMapper gxtWorkOrderMapper;

    @Autowired
    private GxtWorkOrderFlowMapper gxtWorkOrderFlowMapper;

    @Autowired
    private GxtRepairOrderFlowMapper gxtRepairOrderFlowMapper;

    /**
     * 每小时执行定时任务
     * 查找已完成复运操作且评分为非空的工单，并进行归档
     */
    public void resumeOrders() {
        log.info("开始执行工单自动恢复任务");
        GxtRepairOrder gxtRepairOrder = new GxtRepairOrder();
        gxtRepairOrder.setWorkOrderStatus("auto_suspend");
        List<GxtRepairOrder> repairOrderList = gxtRepairOrderMapper.selectGxtRepairOrderList(gxtRepairOrder);
        gxtRepairOrder.setWorkOrderStatus("assigned");
        gxtRepairOrder.setUpdateTime(DateUtils.getNowDate());
        gxtRepairOrder.setUpdateBy("admin");

        for (GxtRepairOrder repairOrder : repairOrderList) {
            gxtRepairOrder.setId(repairOrder.getId());
            gxtRepairOrderMapper.updateGxtRepairOrder(gxtRepairOrder);

            GxtRepairOrderFlow flow = new GxtRepairOrderFlow();
            flow.setOrderId(repairOrder.getId());
            flow.setOrderCode(repairOrder.getWorkOrderProjectNo());
            flow.setActionType("auto_resume");
            flow.setFromStatus(repairOrder.getWorkOrderStatus());
            flow.setToStatus(gxtRepairOrder.getWorkOrderStatus());
            flow.setOperatorId(1L);
            flow.setOperatorName("系统");
            flow.setActionTime(DateUtils.getNowDate());
            flow.setCreateBy("admin");
            flow.setCreateTime(DateUtils.getNowDate());
            gxtRepairOrderFlowMapper.insertGxtRepairOrderFlow(flow);
        }

        GxtWorkOrder gxtWorkOrder = new GxtWorkOrder();
        gxtWorkOrder.setWorkOrderStatus("auto_suspend");
        List<GxtWorkOrder> workOrderList = gxtWorkOrderMapper.selectGxtWorkOrderList(gxtWorkOrder);
        gxtWorkOrder.setUpdateTime(DateUtils.getNowDate());
        gxtWorkOrder.setUpdateBy("admin");
        gxtWorkOrder.setWorkOrderStatus("assigned");

        for (GxtWorkOrder workOrder : workOrderList) {
            gxtWorkOrder.setId(workOrder.getId());
            gxtWorkOrderMapper.updateGxtWorkOrder(gxtWorkOrder);

            GxtWorkOrderFlow flow = new GxtWorkOrderFlow();
            flow.setOrderId(workOrder.getId());
            flow.setOrderCode(workOrder.getWorkOrderProjectNo());
            flow.setActionType("auto_resume");
            flow.setFromStatus(workOrder.getWorkOrderStatus());
            flow.setToStatus(gxtWorkOrder.getWorkOrderStatus());
            flow.setOperatorId(1L);
            flow.setOperatorName("系统");
            flow.setActionTime(DateUtils.getNowDate());
            flow.setCreateBy("admin");
            flow.setCreateTime(DateUtils.getNowDate());
            gxtWorkOrderFlowMapper.insertGxtWorkOrderFlow(flow);
        }
    }
}
