package com.gyee.alarm.service;


import com.gyee.alarm.feigns.IAlarmService;

import com.gyee.alarm.model.vo.AlarmSuperTalbeType;
import com.gyee.alarm.model.vo.AlarmTag;
import com.gyee.alarm.task.thread.AlarmThread;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
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;


@Service

public class AlarmScannerService {

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

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

    @Value("${alarmType}")
    private String alarmType;
    @Value("${read-rows}")
    private Integer readRows;
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private IAlarmService alarmService;

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


    /**
     * 初始化
     */
    public void init() {
//#WT("wt_alarms", "设备报警超级表"),
//#BT("bt_alarms", "升压站报警超级表"),

        switch (alarmType){
            case "wt_alarms":
                alarmTags =alarmService.findTagsByWt();
                break;
            case "bt_alarms":
                alarmTags =alarmService.findTagsByBt();
                break;

        }
    }


    /**
     * 分配任务
     */
    public void taskJobs() {


        int len = 1;

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

        int listNumber=alarmTags.size()/taskCount;

        StringBuilder str=new StringBuilder();

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

        for(int i=0;i<alarmTags.size();i++)
        {
            alarmls.add(alarmTags.get(i));
            if(i!=0 && i%listNumber==0)
            {
                new Thread(new AlarmThread(executor, edosUtil, alarmService,  alarmTags,interval,String.valueOf(len),readRows,alarmType)).start();
                alarmls=new ArrayList<>();
                len++;
            }
        }

        if(!alarmls.isEmpty())
        {
            new Thread(new AlarmThread(executor, edosUtil, alarmService,  alarmTags,interval,String.valueOf(len),readRows,alarmType)).start();
            listNumber++;
        }

        str.append("总数：").append(alarmTags.size()).append("，线程数：").append(taskCount).append("，分块大小：").append(listNumber);

    }
}
