package com.gyee.alarm.task.thread;

import com.gyee.alarm.feigns.IAlarmService;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProBasicEquipmentPoint;
import com.gyee.alarm.model.vo.AlarmCustomTag;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import com.gyee.common.model.PointData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
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 ReadWtDataThread implements Runnable {

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

    private IEdosUtil edosUtil;

    private Integer readRows;

    private List<ProBasicEquipmentPoint> pointList;
    private StringBuilder str;

    private Integer interval;

    private boolean serverStarted=true;
    private CountDownLatch countDownLatch;
    public ReadWtDataThread(Executor executor, IEdosUtil edosUtil, Integer readRows, List<ProBasicEquipmentPoint> pointList, Integer interval, StringBuilder str, CountDownLatch countDownLatch) {
        this.executor = executor;
        this.edosUtil = edosUtil;
        this.readRows = readRows;
        this.pointList = pointList;
        this.interval = interval;
        this.str = str;
        this.countDownLatch = countDownLatch;
    }


    @Override
    public synchronized void run() {


        String task = "task@ " + String.valueOf(str);
        logger.info("创建任务并提交到线程池中：" + task);
        Integer result = 1;

        try {

//			while (runStarted)
//			{
            List<String>  ls=new ArrayList<>();
            List<PointData> values=new ArrayList<>();

            int times=0;
            for(ProBasicEquipmentPoint vo:pointList)
            {
                ls.add(vo.getNemCode());
                times++;
                if(times==readRows)
                {
                    List<PointData> templs=edosUtil.getRealData(ls);
                    values.addAll(templs);
                    ls=new ArrayList<>();
                    times=0;
                }
            }
            if(!ls.isEmpty())
            {
                List<PointData> templs=edosUtil.getRealData(ls);
                values.addAll(templs);
            }

            if(values.size() == pointList.size())
            {

                for(int i=0;i<pointList.size();i++)
                {
                    ProBasicEquipmentPoint point=pointList.get(i);
                    PointData value=values.get(i);
                    if(CacheContext.wtDataValueMap.containsKey(point.getWindturbineId()))
                    {
                        Map<String, PointData> map=CacheContext.wtDataValueMap.get(point.getWindturbineId());
                        map.put(point.getUniformCode(),value);
                    }else
                    {
                        Map<String, PointData> map=new HashMap<>();

                        map.put(point.getUniformCode(),value);
                        CacheContext.wtDataValueMap.put(point.getWindturbineId(),map);
                    }
                }
            }

//				try {
//					Thread.sleep((interval * 1000));
//				} catch (InterruptedException e) {
//					throw new RuntimeException(e);
//				}
//			}

        } catch (Exception e) {

            e.printStackTrace();
            result=0;
        } finally {
            countDownLatch.countDown();
            logger.info("完成任务并提交到线程池中：" + task);
        }
    }
}