package com.gyee.alarm.task.thread;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.alarm.feigns.IAlarmService;
import com.gyee.alarm.model.vo.AlarmTag;
import com.gyee.alarm.model.vo.AlarmVo;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.service.auto.IProEconWtAlysisDayService;
import com.gyee.alarm.util.DateUtils;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import com.gyee.alarm.util.statisticcs.Initial;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

public class AlarmThreadPool implements Callable<String>, Serializable {

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

	private static final long serialVersionUID = 0;

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

	private Object threadPoolTaskData;

	private IEdosUtil edosUtil;

	private IAlarmService alarmService;

	private List<AlarmTag> alarmVoList;
	private String str;

	private Integer interval;
	private String alarmType;
	private Integer readRows;
	public AlarmThreadPool(Object tasks, IEdosUtil edosUtil, IAlarmService alarmService, List<AlarmTag> alarmVoList,
						   Integer interval, String str,Integer readRows, String alarmType
	) {

		this.threadPoolTaskData = tasks;
		this.edosUtil = edosUtil;
		this.alarmService = alarmService;
		this.alarmVoList = alarmVoList;
		this.interval = interval;
		this.str = str;
		this.readRows = readRows;
		this.alarmType=alarmType;
	}

	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(AlarmTag vo:alarmVoList)
				{
					ls.add(vo.getTagId());
					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);
				}

				int alarmNumber=0;
				if(values.size() == alarmVoList.size())
				{
					List<AlarmTag>  alarmTags=new ArrayList<>();

					for(int i=0;i<alarmVoList.size();i++)
					{

						AlarmTag vo=alarmVoList.get(i);

						PointData value=values.get(i);

						if(vo.updateAndCheck(value))
						{
							if(vo.getVal()==1)
							{AlarmTag po=new AlarmTag();

								po.setId(vo.getId());
								po.setTs(vo.getTs());
								po.setVal(vo.getVal());
								po.setOval(vo.getOval());
								po.setTriggerType(vo.getTriggerType());
								po.setTagId(vo.getTagId());
								po.setRank(vo.getRank());
								alarmTags.add(po);
								alarmNumber++;
							}
						}
					}

					switch (alarmType){
						case "wt_alarms":
							alarmService.saveWtTags(alarmTags);
							break;
						case "bt_alarms":
							alarmService.saveBtTags(alarmTags);
							break;

					}


					logger.info(String.valueOf(threadPoolTaskData)+"报警数量：" + alarmNumber);
				}

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

		} catch (Exception e) {

			e.printStackTrace();
		}
		threadPoolTaskData = null;

		return result;

	}



}