package com.gyee.alarm.service;


import com.gyee.alarm.feigns.IAlarmService;

import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProBasicEquipmentPoint;
import com.gyee.alarm.model.auto.ProBasicPowerstationPoint;
import com.gyee.alarm.model.vo.AlarmCustomTag;
import com.gyee.alarm.model.vo.AlarmTag;
import com.gyee.alarm.task.thread.AlarmThread;
import com.gyee.alarm.task.thread.ReadWpDataThread;
import com.gyee.alarm.task.thread.ReadWsDataThread;
import com.gyee.alarm.task.thread.ReadWtDataThread;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;


@Service

public class AlarmCustomService {

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

    @Value("${task-count}")
    private Integer taskCount;

    @Value("${interval}")
    private Integer interval;

    @Value("${read-count}")
    private Integer readCount;

    @Value("${read-rows}")
    private Integer readRows;

    @Value("${alarmType}")
    private String alarmType;
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private IAlarmService alarmService;

    private List<AlarmCustomTag>  alarmTags=new ArrayList<>();
    @Resource
    private Executor executor;

    private  StringBuilder str=new StringBuilder();
    /**
     * 初始化
     */
    public void init() {

        alarmTags =alarmService.findTagsByCt();
    }

    public void taskJobs() throws InterruptedException {
        readTaskJobs();
        Thread.sleep(interval*3);
        calTaskJobs();
    }

    /**
     * 读取测点数据，缓存到内存
     */
    public void readTaskJobs() {


        int wtlen = 1;

        List<ProBasicEquipmentPoint> wtPointList = CacheContext.wtPointList;// 设备测点集合
         List<ProBasicPowerstationPoint> wpPointList =CacheContext.wpPointList; //场站测点集合
        List<ProBasicPowerstationPoint> wsPointList =CacheContext.wsPointList; //升压站测点集合

        //读取设备测点
        List<ProBasicEquipmentPoint> wtpols=new ArrayList<>();

        int listNumber=wtPointList.size()/readCount;

        for(int i=0;i<wtPointList.size();i++)
        {
            wtpols.add(wtPointList.get(i));
            if(i!=0 && i%listNumber==0)
            {
                str.setLength(0);
                str.append("设备读取线程号：").append(wtlen);
                new Thread(new ReadWtDataThread(executor, edosUtil, readRows,  wtpols,interval,str)).start();
                wtpols=new ArrayList<>();
                wtlen++;
            }
        }

        if(!wtpols.isEmpty())
        {
            str.setLength(0);
            str.append("设备读取线程号：").append(wtlen);
            new Thread(new ReadWtDataThread(executor, edosUtil, readRows,  wtpols,interval,str)).start();
            listNumber++;
        }

        StringBuilder str=new StringBuilder();
        str.append("设备读取总数：").append(alarmTags.size()).append("，线程数：").append(taskCount).append("，分块大小：").append(listNumber);

        System.out.println(str.toString());
        //读取场站测点
        int wplen = 1;
        List<ProBasicPowerstationPoint> wppols=new ArrayList<>();
        listNumber=wpPointList.size()/readCount;
        for(int i=0;i<wpPointList.size();i++)
        {
            wppols.add(wpPointList.get(i));
            if(i!=0 && i%listNumber==0)
            {
                str.setLength(0);
                str.append("场站读取线程号：").append(wplen);
                new Thread(new ReadWpDataThread(executor, edosUtil, readRows,  wppols,interval,str)).start();
                wppols=new ArrayList<>();
                wplen++;
            }
        }

        if(!wppols.isEmpty())
        {
            str.setLength(0);
            str.append("场站读取线程号：").append(wplen);
            new Thread(new ReadWpDataThread(executor, edosUtil, readRows,  wppols,interval,str)).start();
            listNumber++;
        }

        str=new StringBuilder();
        str.append("场站读取总数：").append(alarmTags.size()).append("，线程数：").append(taskCount).append("，分块大小：").append(listNumber);

        System.out.println(str.toString());

//        //读取升压站测点
//        int wslen = 1;
//        List<ProBasicPowerstationPoint> wspols=new ArrayList<>();
//        for(int i=0;i<wsPointList.size();i++)
//        {
//            wspols.add(wsPointList.get(i));
//            if(i!=0 && i%readCount==0)
//            {
//                str.setLength(0);
//                str.append("总数：").append(wspols.size()).append("，线程数：").append(readCount).append("，分块大小：").append(wslen);
//
//                new Thread(new ReadWsDataThread(executor, edosUtil, readRows,  wspols,interval,str)).start();
//                wspols=new ArrayList<>();
//                wslen++;
//            }
//        }
//
//        if(!wspols.isEmpty())
//        {
//            str.setLength(0);
//            str.append("总数：").append(wspols.size()).append("，线程数：").append(readCount).append("，分块大小：").append(wslen);
//
//            new Thread(new ReadWsDataThread(executor, edosUtil, readRows,  wspols,interval,str)).start();
//
//        }


    }


    public void calTaskJobs() {


        int len = 1;

        List<AlarmCustomTag> alarmls=new ArrayList<>();

        for(int i=0;i<alarmTags.size();i++)
        {
            alarmls.add(alarmTags.get(i));
            if(i!=0 && i%taskCount==0)
            {
                str.setLength(0);
                str.append("总数：").append(alarmTags.size()).append("，线程数：").append(taskCount).append("，分块大小：").append(len);

                new Thread(new AlarmThread(executor,  alarmService,  alarmls,interval,str,readRows)).start();
                alarmls=new ArrayList<>();
                len++;
            }
        }

        if(!alarmls.isEmpty())
        {
            str.setLength(0);
            str.append("总数：").append(alarmTags.size()).append("，线程数：").append(taskCount).append("，分块大小：").append(len);

            new Thread(new AlarmThread(executor,  alarmService,  alarmls,interval,str,readRows)).start();

        }

    }
}
