package com.gyee.generation.task.thread;

import com.gyee.generation.model.auto.ProEconEquipmentInfoDay1;
import com.gyee.generation.model.auto.ProEconWindturbineGoodness;
import com.gyee.generation.service.CoefficientService;
import com.gyee.generation.service.GoodnessOfFitService;
import com.gyee.generation.service.auto.ITurbineInfoMinService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class GoodnessOfFitThread implements Runnable {

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


    private int i;
    private Date recordDate;

    private CoefficientService coefficientService;
    private GoodnessOfFitService goodnessOfFitService;

    private Map<String, ProEconEquipmentInfoDay1> equipmentInfoDay1Map;
    private List<ProEconWindturbineGoodness> vos;

    private CountDownLatch latch;
    private ITurbineInfoMinService turbineInfoMinService;

    public GoodnessOfFitThread(Executor executor, int i, Date recordDate, CoefficientService coefficientService,
                               GoodnessOfFitService goodnessOfFitService,
                               ITurbineInfoMinService turbineInfoMinService, Map<String, ProEconEquipmentInfoDay1> equipmentInfoDay1Map, List<ProEconWindturbineGoodness> vos,
                               CountDownLatch latch) {
        this.executor = executor;
        this.i = i;
        this.recordDate = recordDate;
        this.coefficientService = coefficientService;
        this.goodnessOfFitService = goodnessOfFitService;
        this.equipmentInfoDay1Map = equipmentInfoDay1Map;
        this.turbineInfoMinService = turbineInfoMinService;
        this.vos = vos;
        this.latch = latch;
    }

    @Override
    public synchronized void run() {

        String task = "task@ " + i;
        logger.info("功率一致性系数创建任务并提交到线程池中：" + i);
        FutureTask<String> futureTask = new FutureTask<String>(new GoodnessOfFitThreadPool(task, i, recordDate, coefficientService,
                goodnessOfFitService,turbineInfoMinService,
                equipmentInfoDay1Map, vos));

        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);
            // 超时后，进行相应处理
        } finally {
            latch.countDown();
        }
    }
}