package com.gyee.generation.task.thread;

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

import java.util.Calendar;
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 EconWtAlysisDayThread implements Runnable {

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

    private ProBasicEquipment wt;
    private Map<String, ProEconEquipmentInfoDay1> eqday1map;
    private Map<String, ProEconEquipmentInfoDay2> eqday2map;
    private Map<String, ProEconEquipmentInfoDay3> eqday3map;
    private Map<String, ProEconEquipmentInfoDay4> eqday4map;
    private Date recordDate;
    private Date end;

    private Date begin;
    private List<ProEconWtAlysisDay> dayls;
    private IEdosUtil edosUtil;
    private Calendar c;

    private IProEconWtAlysisDayService proEconWtAlysisDayService;

    private CountDownLatch latch;

    public EconWtAlysisDayThread(Executor executor, ProBasicEquipment wt, Map<String, ProEconEquipmentInfoDay1> eqday1map,
                                 Map<String, ProEconEquipmentInfoDay2> eqday2map,
                                 Map<String, ProEconEquipmentInfoDay3> eqday3map,
                                 Map<String, ProEconEquipmentInfoDay4> eqday4map,
                                 Date recordDate, Date end, Date begin, List<ProEconWtAlysisDay> dayls,
                                 IEdosUtil edosUtil, Calendar c, IProEconWtAlysisDayService proEconWtAlysisDayService,
                                 CountDownLatch latch) {
        this.executor = executor;
        this.wt = wt;
        this.eqday1map = eqday1map;
        this.eqday2map = eqday2map;
        this.eqday3map = eqday3map;
        this.eqday4map = eqday4map;
        this.recordDate = recordDate;
        this.end = end;
        this.begin = begin;
        this.dayls = dayls;
        this.edosUtil = edosUtil;
        this.c = c;
        this.proEconWtAlysisDayService = proEconWtAlysisDayService;
        this.latch = latch;
    }

    @Override
    public synchronized void run() {

        String task = "task@ " + wt.getId();
        logger.info("分析数据统计务创建任务并提交到线程池中：" + wt.getId());
        FutureTask<String> futureTask = new FutureTask<String>(new EconWtAlysisDayThreadPool(task,
                wt, eqday1map,
                eqday2map,
                eqday3map,
                eqday4map,
                recordDate, end, begin, dayls,
                edosUtil, c, proEconWtAlysisDayService));

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

        }
    }
}