package com.gyee.generation.task.thread;

import com.gyee.common.model.PointData;
import com.gyee.generation.model.auto.ProEconBrownoutsEvent;
import com.gyee.generation.model.auto.ProEconBrownoutsEvent2;
import com.gyee.generation.model.auto.ProEconMainBrownouts;
import com.gyee.generation.model.auto.ProEconMainBrownouts2;
import com.gyee.generation.service.auto.IProEconBrownoutsEvent2Service;
import com.gyee.generation.service.auto.IProEconBrownoutsEventService;
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.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

public class BrownoutsThread implements Runnable {

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

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

    private String wtId;
    private Map<String, ProEconBrownoutsEvent> oldBrownoutsStatusMap;

    private Map<String, ProEconBrownoutsEvent2> oldBrownoutsStatus2Map;

    private IProEconBrownoutsEventService proEconBrownoutsEventService;

    private IProEconBrownoutsEvent2Service proEconBrownoutsEvent2Service;

    private Map<Integer, String> statusDescriptionMap;

    private Map<String, Map<String, ProEconMainBrownouts2>> newMainBrownouts2Map;
    private Map<String, Map<String, ProEconMainBrownouts>> newMainBrownoutsMap;
    private IEdosUtil edosUtil;
    private Date begin;
    private Date end;

    public BrownoutsThread(Executor executor,
                           Map<String, List<PointData>> statusMap, String wtId,
                           Map<String, ProEconBrownoutsEvent> oldBrownoutsStatusMap,
                           Map<String, ProEconBrownoutsEvent2> oldBrownoutsStatus2Map,
                           IProEconBrownoutsEventService proEconBrownoutsEventService,
                           IProEconBrownoutsEvent2Service proEconBrownoutsEvent2Service,
                           Map<Integer, String> statusDescriptionMap,
                           Map<String, Map<String, ProEconMainBrownouts2>> newMainBrownouts2Map,
                           Map<String, Map<String, ProEconMainBrownouts>> newMainBrownoutsMap,
                           IEdosUtil edosUtil, Date begin, Date end

    ) {
        this.executor = executor;
        this.statusMap = statusMap;
        this.wtId = wtId;
        this.oldBrownoutsStatusMap = oldBrownoutsStatusMap;
        this.oldBrownoutsStatus2Map = oldBrownoutsStatus2Map;
        this.proEconBrownoutsEventService = proEconBrownoutsEventService;
        this.proEconBrownoutsEvent2Service = proEconBrownoutsEvent2Service;
        this.statusDescriptionMap = statusDescriptionMap;
        this.newMainBrownoutsMap = newMainBrownoutsMap;
        this.newMainBrownouts2Map = newMainBrownouts2Map;
        this.begin = begin;
        this.end = end;
        this.edosUtil = edosUtil;
    }

    @Override
    public synchronized void run() {

        String task = "task@ " + wtId;
        logger.info("限电子表数据创建任务并提交到线程池中：" + wtId);
        FutureTask<String> futureTask = new FutureTask<String>(new BrownoutsThreadPool(task,
                statusMap, wtId, oldBrownoutsStatusMap, oldBrownoutsStatus2Map,
                proEconBrownoutsEventService,
                proEconBrownoutsEvent2Service,
                statusDescriptionMap,
                newMainBrownouts2Map,
                newMainBrownoutsMap,
                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 {
            logger.info("限电子表数据完成任务并提交到线程池中：" + wtId);
        }
    }
}