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.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

public class ReadWtDataThreadPoolPool implements Callable<String>, Serializable {

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

	private static final long serialVersionUID = 0;

	// 保存任务所需要的数据

	private Object threadPoolTaskData;

	private IEdosUtil edosUtil;

	private Integer readRows;

	private List<ProBasicEquipmentPoint> pointList;
	private StringBuilder str;

	private Integer interval;


	public ReadWtDataThreadPoolPool(Object tasks, IEdosUtil edosUtil, Integer readRows, List<ProBasicEquipmentPoint> pointList,
                                    Integer interval, StringBuilder str
	) {

		this.threadPoolTaskData = tasks;
		this.edosUtil = edosUtil;
		this.readRows = readRows;
		this.pointList = pointList;
		this.interval = interval;
		this.str = str;
	}

	public synchronized String call() throws Exception {

		String result = "";

		try {

			while (true)
			{
				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();
		}
		threadPoolTaskData = null;

		return result;

	}



}