package com.gyee.generation.task.thread;

import com.gyee.generation.model.auto.*;
import com.gyee.generation.service.auto.*;
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.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

public class EquipmentInfo4Thread implements Runnable {

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

    private ProBasicEquipment wt;
    private Map<String, ProEconEquipmentInfoDay2> pep2map;
    private Map<String, ProEconEquipmentInfoDay3> pep3map;
    private Map<String, ProEconEquipmentInfoDay4> pep4map;
    private Date recordDate;
    private Date end;

    private Date begin;
    private List<ProEconEquipmentInfoDay4> dayls;
    private IEdosUtil edosUtil;

    private Map<String, ProEconInOrOutSpeedTotal> peioomap;

    private Map<String, ProEconWindturbineGoodness> wgmap;

    private IProEconCurveFittingMainService proEconCurveFittingMainService;

    private IProEconEquipmentInfoDay5Service proEconEquipmentInfoDay5Service;

    private IProEconFaultLiminatedefectsService proEconFaultLiminatedefectsService;

    private IProEconCurveFittingSubService proEconCurveFittingSubService;

    private IProEconCurveFittMonthMainService proEconCurveFittMonthMainService;

    private IProEconCurveFittMonthSubService proEconCurveFittMonthSubService;

    private IProEconCurveFittYearMainService proEconCurveFittYearMainService;


    private IProEconCurveFittYearSubService proEconCurveFittYearSubService;

    private CountDownLatch latch;

    public EquipmentInfo4Thread(Executor executor, ProBasicEquipment wt, Map<String, ProEconEquipmentInfoDay2> pep2map,
                                Map<String, ProEconEquipmentInfoDay3> pep3map,
                                Map<String, ProEconEquipmentInfoDay4> pep4map,
                                Date recordDate, Date end, Date begin, List<ProEconEquipmentInfoDay4> dayls,
                                IEdosUtil edosUtil, Map<String, ProEconInOrOutSpeedTotal> peioomap,
                                Map<String, ProEconWindturbineGoodness> wgmap,
                                IProEconCurveFittingMainService proEconCurveFittingMainService,
                                IProEconEquipmentInfoDay5Service proEconEquipmentInfoDay5Service,
                                IProEconFaultLiminatedefectsService proEconFaultLiminatedefectsService,
                                IProEconCurveFittingSubService proEconCurveFittingSubService,
                                IProEconCurveFittMonthMainService proEconCurveFittMonthMainService,
                                IProEconCurveFittMonthSubService proEconCurveFittMonthSubService,
                                IProEconCurveFittYearMainService proEconCurveFittYearMainService,
                                IProEconCurveFittYearSubService proEconCurveFittYearSubService,
                                CountDownLatch latch) {
        this.executor = executor;
        this.wt = wt;
        this.pep2map = pep2map;
        this.pep3map = pep3map;
        this.pep4map = pep4map;
        this.recordDate = recordDate;
        this.end = end;
        this.begin = begin;
        this.dayls = dayls;
        this.edosUtil = edosUtil;
        this.peioomap = peioomap;
        this.wgmap = wgmap;
        this.proEconCurveFittingMainService = proEconCurveFittingMainService;
        this.proEconEquipmentInfoDay5Service = proEconEquipmentInfoDay5Service;
        this.proEconFaultLiminatedefectsService = proEconFaultLiminatedefectsService;
        this.proEconCurveFittingSubService = proEconCurveFittingSubService;
        this.proEconCurveFittMonthMainService = proEconCurveFittMonthMainService;
        this.proEconCurveFittMonthSubService = proEconCurveFittMonthSubService;
        this.proEconCurveFittYearMainService = proEconCurveFittYearMainService;
        this.proEconCurveFittYearSubService = proEconCurveFittYearSubService;
        this.latch = latch;
    }

    @Override
    public synchronized void run() {

        String task = "task@ " + wt.getId();
        logger.info("设备4计算服务创建任务并提交到线程池中：" + wt.getId());
        FutureTask<String> futureTask = new FutureTask<String>(new EquipmentInfo4ThreadPool(task,
                wt, pep2map,
                pep3map,
                pep4map,
                recordDate, end, begin, dayls,
                edosUtil, peioomap,
                wgmap,
                proEconCurveFittingMainService,
                proEconEquipmentInfoDay5Service,
                proEconFaultLiminatedefectsService,
                proEconCurveFittingSubService,
                proEconCurveFittMonthMainService,
                proEconCurveFittMonthSubService,
                proEconCurveFittYearMainService,
                proEconCurveFittYearSubService));

        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();
        }
    }
}