package com.gyee.alarm.task.thread;

import com.gyee.common.model.PointData;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.service.auto.*;
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 MainBrownoutsThread implements Runnable {

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

	private Map<String, List<PointData>> wpstatusMap;

	private String wpId;
	private Map<String, ProEconMainBrownouts> oldMainBrownoutsMap;
	private Map<String, ProEconMainBrownouts2> oldMainBrownouts2Map;

	private IProEconMainBrownoutsService proEconMainBrownoutsService;

	private Map<String, Map<String, ProEconMainBrownouts>> newMainBrownoutsMap;
	private Map<String, Map<String, ProEconMainBrownouts2>> newMainBrownouts2Map;

	private IProEconMainBrownouts2Service proEconMainBrownouts2Service;
	private IEdosUtil edosUtil;
	private Date begin;
	private Date end;

	private CountDownLatch latch;

	public MainBrownoutsThread(Executor executor,  Map<String, List<PointData>> wpstatusMap, String wpId, Map<String, ProEconMainBrownouts> oldMainBrownoutsMap, Map<String, ProEconMainBrownouts2> oldMainBrownouts2Map, IProEconMainBrownoutsService proEconMainBrownoutsService, Map<String, Map<String, ProEconMainBrownouts>> newMainBrownoutsMap, Map<String, Map<String, ProEconMainBrownouts2>> newMainBrownouts2Map, IProEconMainBrownouts2Service proEconMainBrownouts2Service, IEdosUtil edosUtil, Date begin, Date end,CountDownLatch latch	) {
		this.executor = executor;
		this.wpstatusMap = wpstatusMap;
		this.wpId = wpId;
		this.oldMainBrownoutsMap = oldMainBrownoutsMap;
		this.oldMainBrownouts2Map = oldMainBrownouts2Map;
		this.proEconMainBrownoutsService = proEconMainBrownoutsService;
		this.newMainBrownoutsMap = newMainBrownoutsMap;
		this.newMainBrownouts2Map = newMainBrownouts2Map;
		this.begin = begin;
		this.end = end;

		this.edosUtil = edosUtil;
		this.proEconMainBrownouts2Service = proEconMainBrownouts2Service;
		this.latch=latch;
	}

	@Override
	public synchronized void run() {

		String task = "task@ " + wpId;
		logger.info("限电主表创建任务并提交到线程池中：" + wpId);
		FutureTask<String> futureTask = new FutureTask<String>(new MainBrownoutsThreadPool(task
				,  wpstatusMap,  wpId, oldMainBrownoutsMap, oldMainBrownouts2Map,  proEconMainBrownoutsService,  newMainBrownoutsMap,  newMainBrownouts2Map,  proEconMainBrownouts2Service,  edosUtil,  begin,  end)
		);

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