package com.gyee.alarm.task.thread;

import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.model.auto.ProEconEquipmentInfoDay1;
import com.gyee.alarm.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 EquipmentInfo1Thread implements Runnable {

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

	private ProBasicEquipment wt;
	private   Map<String,ProEconEquipmentInfoDay1> pep1map;

	private Date recordDate;
	private Date end;

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

	private CountDownLatch latch;

	public EquipmentInfo1Thread(Executor executor, ProBasicEquipment wt, Map<String,ProEconEquipmentInfoDay1> pep1map,
								Date recordDate, Date end, Date begin,List<ProEconEquipmentInfoDay1> dayls,
								IEdosUtil edosUtil,
                                CountDownLatch latch	) {
		this.executor = executor;
		this.wt = wt;
		this.pep1map = pep1map;
		this.recordDate = recordDate;
		this.end = end;
		this.begin = begin;
		this.dayls = dayls;
		this.edosUtil = edosUtil;
		this.latch = latch;
	}

	@Override
	public synchronized void run() {

		String task = "task@ " + wt.getId();
		logger.info("设备1计算服务创建任务并提交到线程池中：" + wt.getId());
		FutureTask<String> futureTask = new FutureTask<String>(new EquipmentInfo1ThreadPool(task,
				 wt,  pep1map,
				 recordDate,  end,  begin,dayls,
				 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);
			// 超时后，进行相应处理
		}finally {
			latch.countDown();
		}
	}
}