package com.gyee.generation.task.thread;

import com.gyee.generation.model.auto.ProBasicEquipment;
import com.gyee.generation.service.auto.IProEconInputOrOutputSpeedService;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class InputOrOutPutkThread implements Runnable {

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

    private IProEconInputOrOutputSpeedService proEconInputOrOutputSpeedService;
    private IEdosUtil edosUtil;
    private ProBasicEquipment wt;
    private Date begin;
    private Date end;


    public InputOrOutPutkThread(Executor executor, ProBasicEquipment wt, Date begin, Date end,
                                IProEconInputOrOutputSpeedService proEconInputOrOutputSpeedService, IEdosUtil edosUtil) {
        this.executor = executor;
        this.wt = wt;
        this.begin = begin;
        this.end = end;
        this.proEconInputOrOutputSpeedService = proEconInputOrOutputSpeedService;
        this.edosUtil = edosUtil;
    }

    @Override
    public synchronized void run() {

        String task = "task@ " + wt.getId();
        logger.info("风机切入切出数据创建任务并提交到线程池中：" + wt.getId());
        FutureTask<String> futureTask = new FutureTask<String>(new InputOrOutPutThreadPool(task, wt, begin, end, proEconInputOrOutputSpeedService, edosUtil));
        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);
            // 超时后，进行相应处理
        }
    }
}