package com.gyee.generation.task.thread;

import com.gyee.common.model.PointData;
import com.gyee.generation.model.auto.ProEconInterruption;
import com.gyee.generation.model.auto.ProEconShutdownEvent;
import com.gyee.generation.model.auto.ProEconShutdownEvent2;
import com.gyee.generation.model.auto.ProEconWindturbineStatus;
import com.gyee.generation.service.auto.IProEconInterruptionService;
import com.gyee.generation.service.auto.IProEconShutdownEvent2Service;
import com.gyee.generation.service.auto.IProEconShutdownEventService;
import com.gyee.generation.service.auto.IProEconWindturbineStatusService;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

public class ShutdownnewThread implements Runnable {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private Executor executor;


    private Map<String, ProEconWindturbineStatus> oldStatusMap;
    private Map<String, List<PointData>> statusMap;

    private String windturbineId;
    private Map<Integer, String> statusDescriptionMap;

    private Map<String, ProEconShutdownEvent> oldShutdownEvent;
    private Map<String, ProEconInterruption> oldInterrup;
    private Map<String, ProEconShutdownEvent2> oldShutdownEvent2;

    private Date begin;
    private Date end;

    private IEdosUtil edosUtil;
    private IProEconWindturbineStatusService proEconWindturbineStatusService;
    private IProEconShutdownEventService proEconShutdownEventService;
    private IProEconShutdownEvent2Service proEconShutdownEvent2Service;
    private IProEconInterruptionService proEconInterruptionService;

    public ShutdownnewThread(Executor executor, Map<String, ProEconWindturbineStatus> oldStatusMap, Map<String, List<PointData>> statusMap, String windturbineId,
                             Map<Integer, String> statusDescriptionMap, Map<String, ProEconShutdownEvent> oldShutdownEvent,
                             Map<String, ProEconInterruption> oldInterrup, Map<String, ProEconShutdownEvent2> oldShutdownEvent2,
                             Date begin, Date end,
                             IEdosUtil edosUtil, IProEconWindturbineStatusService proEconWindturbineStatusService,
                             IProEconShutdownEventService proEconShutdownEventService, IProEconShutdownEvent2Service proEconShutdownEvent2Service,
                             IProEconInterruptionService proEconInterruptionService) {
        this.executor = executor;
        this.oldStatusMap = oldStatusMap;
        this.statusMap = statusMap;
        this.windturbineId = windturbineId;
        this.statusDescriptionMap = statusDescriptionMap;
        this.oldShutdownEvent = oldShutdownEvent;
        this.oldInterrup = oldInterrup;
        this.oldShutdownEvent2 = oldShutdownEvent2;
        this.begin = begin;
        this.end = end;

        this.edosUtil = edosUtil;
        this.proEconWindturbineStatusService = proEconWindturbineStatusService;
        this.proEconShutdownEventService = proEconShutdownEventService;
        this.proEconShutdownEvent2Service = proEconShutdownEvent2Service;
        this.proEconInterruptionService = proEconInterruptionService;
    }

    @Override
    public synchronized void run() {

        String task = "task@ " + windturbineId;
        logger.info("停机数据创建任务并提交到线程池中：" + windturbineId);
        FutureTask<String> futureTask = new FutureTask<String>(new ShutdownnewThreadPool(task, oldStatusMap, statusMap, windturbineId,
                statusDescriptionMap, oldShutdownEvent,
                oldInterrup, oldShutdownEvent2, begin, end, edosUtil, proEconWindturbineStatusService,
                proEconShutdownEventService, proEconShutdownEvent2Service,
                proEconInterruptionService));
        executor.execute(futureTask);
        // 在这里可以做别的任何事情
        String result = null;
        try {
            // 取得结果，同时设置超时执行时间为1秒。同样可以用future.get()，不设置执行超时时间取得结果
            result = futureTask.get();
        } catch (InterruptedException e) {
            futureTask.cancel(true);
        } catch (ExecutionException e) {
            futureTask.cancel(true);
        } catch (Exception e) {
            futureTask.cancel(true);
            // 超时后，进行相应处理
        }
    }
}