package cn.wise.sc.energy.power.plant.business.task;

import cn.wise.sc.energy.power.plant.business.bean.DataPower;
import cn.wise.sc.energy.power.plant.business.domain.EventInfo;
import cn.wise.sc.energy.power.plant.business.domain.Frequency;
import cn.wise.sc.energy.power.plant.business.domain.vo.CharacterParamInfoVo;
import cn.wise.sc.energy.power.plant.business.service.ICharacterParamService;
import cn.wise.sc.energy.power.plant.business.service.IEventInfoService;
import cn.wise.sc.energy.power.plant.business.service.impl.EventInfoServiceImpl;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @description: 定时任务模型
 * @author: qh
 * @create: 2020-05-20 16:00
 */
@Component
public class ScheduledModel {

    final
    WebSocketServer webSocketServer;
    final
    ICharacterParamService iCharacterParamService;
    final
    IEventInfoService iEventInfoService;

    public ScheduledModel(WebSocketServer webSocketServer,
                          ICharacterParamService iCharacterParamService,
                          IEventInfoService iEventInfoService) {
        this.webSocketServer = webSocketServer;
        this.iCharacterParamService = iCharacterParamService;
        this.iEventInfoService = iEventInfoService;
    }

    @Scheduled(cron = "0/1 * * * * ? ")
    public void indexRealTimeData() throws IOException {
        ConcurrentHashMap<String, WebSocketServer> webSocketMap =
                WebSocketServer.webSocketMap;
        ConcurrentHashMap<String, String> plantCodeMap = WebSocketServer.plantCodeMap;
        if (webSocketMap.size() == 0 || plantCodeMap.size() == 0) {
            return;
        }
        //向每个webSocket推送氢系统实时数据
        for (String uuid : webSocketMap.keySet()) {
            final WebSocketServer webSocket = webSocketMap.get(uuid);
            final String plantCode = plantCodeMap.get(uuid);
            List<String> characterNames = new ArrayList<>(14);
            characterNames.add("汽端座振X");
            characterNames.add("汽端轴振X");
            characterNames.add("励端座振X");
            characterNames.add("励端轴振X");
            characterNames.add("汽端轴瓦温度4");
            characterNames.add("汽端轴瓦温度5");
            characterNames.add("励端轴瓦温度1");
            characterNames.add("励端轴瓦温度2");
            characterNames.add("发电机定子A相电流");
            characterNames.add("发电机定子B相电流");
            characterNames.add("发电机定子C相电流");
            characterNames.add("发电机定子AB线电压");
            characterNames.add("发电机定子BC线电压");
            characterNames.add("发电机定子CA线电压");
            List<CharacterParamInfoVo> characterParamInfoVos =
                    iCharacterParamService.getCharacterByName(characterNames, plantCode, "");
            List<String> KKsCodes = characterParamInfoVos.stream()
                    .map(CharacterParamInfoVo::getKksCode).collect(Collectors.toList());
            String start = "1s-ago";
            Map<Long, Map<String, Number>> response = iCharacterParamService
                    .getRealTimeDataByKksCode(KKsCodes, start, "");

            Long time = response.keySet().iterator().next();
            Map<String, Number> realTimeDataAndCpName = response.get(time);
            DataPower dataPower = new DataPower();
            dataPower.getXAxis().add(time);
            DataPower dataPower1 = new DataPower();
            dataPower1.getXAxis().add(time);
            DataPower dataPower2 = new DataPower();
            dataPower2.getXAxis().add(time);
            DataPower dataPower3 = new DataPower();
            dataPower3.getXAxis().add(time);

            for (String key : realTimeDataAndCpName.keySet()) {
                DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
                powerPoints.setName(key);
                powerPoints.getValue().add(realTimeDataAndCpName.get(key).toString());
                if (key.contains("汽端座振X")
                        || key.contains("汽端轴振X")
                        || key.contains("励端座振X")
                        || key.contains("励端轴振X")) {
                    dataPower1.getDataList().add(powerPoints);
                }
                if (key.contains("汽端轴瓦温度")
                        || key.contains("励端轴瓦温度")) {
                    dataPower2.getDataList().add(powerPoints);
                }
                if (key.contains("发电机定子")) {
                    dataPower.getDataList().add(powerPoints);
                }
            }
            List<DataPower> rts = new ArrayList<>(4);
            rts.add(dataPower);
            rts.add(dataPower1);
            rts.add(dataPower2);

            //添加报警点
            DataPower dataPower4 = new DataPower();
            List<EventInfoServiceImpl.AlertCount> alertCounts = iEventInfoService.countAlert();
            DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
            powerPoints.setName("预报警");
            DataPower.PowerPoints powerPoints1 = new DataPower.PowerPoints();
            powerPoints1.setName("高报警");
            DataPower.PowerPoints powerPoints2 = new DataPower.PowerPoints();
            powerPoints2.setName("高高报警");
            dataPower4.getDataList().add(powerPoints);
            dataPower4.getDataList().add(powerPoints1);
            dataPower4.getDataList().add(powerPoints2);
            //获取设备长度
            Set<String> deviceNames = new HashSet<>();
            alertCounts.forEach(arg -> deviceNames.add(arg.getDeviceName()));
            int idx = 0;
            for (String deviceName : deviceNames) {
                dataPower4.getXAxis().add(deviceName);
                int finalIdx = idx;
                alertCounts.forEach(arg -> {
                    //处理名字报警
                    if ("preAlert".equals(arg.getAlertLevel())) {
                        //预报警
                        dataPower4.getDataList().get(0).getValue().add(finalIdx, arg.getCount() + "");
                    } else if ("tallAlert".equals(arg.getAlertLevel())) {
                        //高报
                        dataPower4.getDataList().get(1).getValue().add(finalIdx, arg.getCount() + "");
                    } else {
                        //高高报
                        dataPower4.getDataList().get(2).getValue().add(finalIdx, arg.getCount() + "");
                    }
                });
                idx++;
            }

            rts.add(dataPower4);


            webSocket.sendMessage(JSON.toJSONString(rts));
        }
    }

    /**
     * 首页实时数据  整个电厂的
     *
     * @throws IOException
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void indexRealTimeDataPower() throws IOException {
        ConcurrentHashMap<String, WebSocketServer> webSocketMap =
                WebSocketServer.webSocketMap;
        ConcurrentHashMap<String, String> plantCodeMap = WebSocketServer.plantCodeMap;
        if (webSocketMap.size() == 0 || plantCodeMap.size() == 0) {
            return;
        }
        //向每个webSocket推送氢系统实时数据
        for (String uuid : webSocketMap.keySet()) {
            final WebSocketServer webSocket = webSocketMap.get(uuid);
            final String plantCode = plantCodeMap.get(uuid);
            List<String> characterNames = new ArrayList<>(1);
            characterNames.add("燃机发电机有功功率");
            List<CharacterParamInfoVo> characterParamInfoVos =
                    iCharacterParamService.getCharacterByName(characterNames, plantCode, "");
            List<String> KKsCodes = characterParamInfoVos.stream()
                    .map(CharacterParamInfoVo::getKksCode).collect(Collectors.toList());
            String start = "1s-ago";
            Map<Long, Map<String, Number>> response = iCharacterParamService
                    .getRealTimeDataByKksCode(KKsCodes, start, "2h-avg-zero");

            Long time = response.keySet().iterator().next();
            Map<String, Number> realTimeDataAndCpName = response.get(time);
            DataPower dataPower = new DataPower();
            dataPower.getXAxis().add(time);
            Double power1F = 0D;
            Double power2F = 0D;
            for (String key : realTimeDataAndCpName.keySet()) {
                if (key.contains("1F")) {
                    power1F += Double.parseDouble(realTimeDataAndCpName.get(key) + "");
                }
                if (key.contains("2F")) {
                    power2F += Double.parseDouble(realTimeDataAndCpName.get(key) + "");
                }
            }
            DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
            powerPoints.setName("1号机组功率");
            powerPoints.getValue().add(power1F.toString());
            dataPower.getDataList().add(powerPoints);
            DataPower.PowerPoints powerPoints1 = new DataPower.PowerPoints();
            powerPoints1.setName("2号机组功率");
            powerPoints1.getValue().add(power2F.toString());
            dataPower.getDataList().add(powerPoints1);
            webSocket.sendMessage(JSON.toJSONString(dataPower));
        }
    }

    /**
     * 首页数据  部件的  秒级
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void indexRealDataByDevice() throws IOException {
        ConcurrentHashMap<String, IndexSystemSocket> webSocketMap =
                IndexSystemSocket.webSocketMap;
        ConcurrentHashMap<String, String> deviceMap = IndexSystemSocket.deviceMap;
        if (webSocketMap.size() == 0 || deviceMap.size() == 0) {
            return;
        }
        //向每个webSocket推送氢系统实时数据
        for (String uuid : webSocketMap.keySet()) {
            final IndexSystemSocket webSocket = webSocketMap.get(uuid);
            final String deviceId = deviceMap.get(uuid);
            List<String> characterNames = new ArrayList<>(14);
            characterNames.add("发电机转速");
            characterNames.add("汽端座振X");
            characterNames.add("汽端轴振X");
            characterNames.add("励端座振X");
            characterNames.add("励端轴振X");
            characterNames.add("发电机定子A相电流");
            characterNames.add("发电机定子B相电流");
            characterNames.add("发电机定子C相电流");
            characterNames.add("发电机定子AB线电压");
            characterNames.add("发电机定子BC线电压");
            characterNames.add("发电机定子CA线电压");
            characterNames.add("励端轴承排油温度");
            characterNames.add("汽端轴承排油温度");
            characterNames.add("定子上下线棒层间温度");
            characterNames.add("定子上层线棒出水温度");
            characterNames.add("定子下层线棒出水温度");
            List<CharacterParamInfoVo> characterParamInfoVos =
                    iCharacterParamService.getCharacterByName(characterNames, "", deviceId);
            List<String> KKsCodes = characterParamInfoVos.stream()
                    .map(CharacterParamInfoVo::getKksCode).collect(Collectors.toList());
            String start = "1s-ago";

            Map<Long, Map<String, Number>> response = iCharacterParamService
                    .getRealTimeDataByKksCode(KKsCodes, start, "");
            Long time = response.keySet().iterator().next();
            Map<String, Number> realTimeDataAndCpName = response.get(time);

            //组装数据结构
            //首页数据  --->  机组
            DataPower dataPower = new DataPower();
            dataPower.getXAxis().add(time);
            DataPower dataPower1 = new DataPower();
            dataPower1.getXAxis().add(time);
            DataPower dataPower2 = new DataPower();
            dataPower2.getXAxis().add(time);
            DataPower dataPower3 = new DataPower();
            dataPower3.getXAxis().add(time);
            DataPower dataPower4 = new DataPower();
            dataPower4.getXAxis().add(time);
            for (String key : realTimeDataAndCpName.keySet()) {
                DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
                powerPoints.setName(key);
                powerPoints.getValue().add(realTimeDataAndCpName.get(key).toString());
                if (key.contains("燃机发电机有功功率")) {
                    dataPower.getDataList().add(powerPoints);
                }
                if (key.contains("发电机转速")) {
                    dataPower1.getDataList().add(powerPoints);
                }
                if (key.contains("汽端座振X")
                        || key.contains("汽端轴振X")
                        || key.contains("励端座振X")
                        || key.contains("励端轴振X")) {
                    dataPower3.getDataList().add(powerPoints);
                }
                if (key.contains("发电机定子A相电流")
                        || key.contains("发电机定子B相电流")
                        || key.contains("发电机定子C相电流")
                        || key.contains("发电机定子AB线电压")
                        || key.contains("发电机定子BC线电压")
                        || key.contains("发电机定子CA线电压")
                ) {
                    dataPower2.getDataList().add(powerPoints);
                }
                if (key.contains("励端轴承排油温度")
                        || key.contains("汽端轴承排油温度")) {
                    dataPower4.getDataList().add(powerPoints);
                }
            }
            //处理首页雷达图 超麻烦
            DataPower dataPower5 = new DataPower();
            DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
            powerPoints.setName("层间温度");
            powerPoints.setValue(initList());
            DataPower.PowerPoints powerPoints1 = new DataPower.PowerPoints();
            powerPoints1.setName("上层线棒出水");
            powerPoints1.setValue(initList());
            DataPower.PowerPoints powerPoints2 = new DataPower.PowerPoints();
            powerPoints2.setName("下层线棒出水");
            powerPoints2.setValue(initList());
            dataPower5.getDataList().add(powerPoints);
            dataPower5.getDataList().add(powerPoints1);
            dataPower5.getDataList().add(powerPoints2);
            for (String key : realTimeDataAndCpName.keySet()) {
                //只取A结尾的数据
                if (key.contains("定子上下线棒层间温度") && !key.contains("B")) {
                    //解析末尾数字
                    String indexStr = key.split("定子上下线棒层间温度")[1]
                            .replace("A", "");

                    int index;
                    try {
                        index = Integer.parseInt(indexStr);
                    } catch (NumberFormatException ex) {
                        throw new RuntimeException("测定名字不符合命名规范!");
                    }
                    powerPoints.getValue().set(index - 1, realTimeDataAndCpName.get(key).toString());
                }
                if (key.contains("定子上层线棒出水温度")) {
                    //解析末尾数字
                    String indexStr = key.split("上层线棒出水温度")[1];
                    int index;
                    try {
                        index = Integer.parseInt(indexStr);
                    } catch (NumberFormatException ex) {
                        throw new RuntimeException("测定名字不符合命名规范!");
                    }
                    powerPoints1.getValue().set(index - 1, realTimeDataAndCpName.get(key).toString());
                }
                if (key.contains("定子下层线棒出水温度")) {
                    //解析末尾数字
                    String indexStr = key.split("下层线棒出水温度")[1];
                    int index;
                    try {
                        index = Integer.parseInt(indexStr);
                    } catch (NumberFormatException ex) {
                        throw new RuntimeException("测定名字不符合命名规范!");
                    }
                    powerPoints2.getValue().set(index - 1, realTimeDataAndCpName.get(key).toString());
                }
            }

            List<DataPower> rts = new ArrayList<>(6);
            rts.add(dataPower);
            rts.add(dataPower1);
            rts.add(dataPower2);
            rts.add(dataPower3);
            rts.add(dataPower4);
            rts.add(dataPower5);
            webSocket.sendMessage(JSON.toJSONString(rts));
        }
    }

    private List<String> initList() {
        List<String> rts = new ArrayList<>(54);
        for (int i = 0; i < 54; i++) {
            rts.add("");
        }
        return rts;
    }

    /**
     * 首页实时数据：功率趋势图-->单个机组  小时级
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void indexRealDataPowerByDevice() throws IOException {
        ConcurrentHashMap<String, IndexSystemSocket> webSocketMap =
                IndexSystemSocket.webSocketMap;
        ConcurrentHashMap<String, String> deviceMap = IndexSystemSocket.deviceMap;
        if (webSocketMap.size() == 0 || deviceMap.size() == 0) {
            return;
        }
        //向每个webSocket推送氢系统实时数据
        for (String uuid : webSocketMap.keySet()) {
            final IndexSystemSocket webSocket = webSocketMap.get(uuid);
            final String deviceId = deviceMap.get(uuid);
            List<String> characterNames = new ArrayList<>(4);
            characterNames.add("燃机发电机有功功率");
            List<CharacterParamInfoVo> characterParamInfoVos =
                    iCharacterParamService.getCharacterByName(characterNames, "", deviceId);
            List<String> KKsCodes = characterParamInfoVos.stream()
                    .map(CharacterParamInfoVo::getKksCode).collect(Collectors.toList());
            String start = "1s-ago";
            Map<Long, Map<String, Number>> response = iCharacterParamService
                    .getRealTimeDataByKksCode(KKsCodes, start, "2h-avg-zero");
            Long time = response.keySet().iterator().next();
            Map<String, Number> realTimeDataAndCpName = response.get(time);
            //组装数据结构
            //漏氢统计
            DataPower dataPower = new DataPower();
            dataPower.getXAxis().add(time);
            for (String key : realTimeDataAndCpName.keySet()) {
                DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
                powerPoints.setName(key);
                powerPoints.getValue().add(realTimeDataAndCpName.get(key).toString());
                dataPower.getDataList().add(powerPoints);
            }

            webSocket.sendMessage(JSON.toJSONString(dataPower));
        }
    }

    /**
     * 氢系统实时数据
     */
    @Scheduled(cron = "0/1 * * * * ? ")
    public void hydrogenSystem() throws IOException {
        ConcurrentHashMap<String, HydrogenSystemWebSocketServer> webSocketMap =
                HydrogenSystemWebSocketServer.webSocketMap;
        ConcurrentHashMap<String, String> deviceMap = HydrogenSystemWebSocketServer.deviceMap;
        if (webSocketMap.size() == 0 || deviceMap.size() == 0) {
            return;
        }
        //向每个webSocket推送氢系统实时数据
        for (String uuid : webSocketMap.keySet()) {
            final HydrogenSystemWebSocketServer webSocket = webSocketMap.get(uuid);
            final String deviceId = deviceMap.get(uuid);
            List<String> characterNames = new ArrayList<>(4);
            characterNames.add("发电机机内氢压");
            characterNames.add("机内氢气湿度");
            characterNames.add("漏氢含量");
            characterNames.add("汽端发电机冷氢温度");
            List<CharacterParamInfoVo> characterParamInfoVos =
                    iCharacterParamService.getCharacterByName(characterNames, "", deviceId);
            List<String> KKsCodes = characterParamInfoVos.stream()
                    .map(CharacterParamInfoVo::getKksCode).collect(Collectors.toList());
            String start = "1s-ago";

            Map<Long, Map<String, Number>> response = iCharacterParamService
                    .getRealTimeDataByKksCode(KKsCodes, start, "");
            Long time = response.keySet().iterator().next();
            Map<String, Number> realTimeDataAndCpName = response.get(time);

            //组装数据结构
            //漏氢统计
            DataPower dataPower = new DataPower();
            dataPower.getXAxis().add(time);
            DataPower dataPower1 = new DataPower();
            dataPower1.getXAxis().add(time);
            DataPower dataPower2 = new DataPower();
            dataPower2.getXAxis().add(time);
            DataPower dataPower3 = new DataPower();
            dataPower3.getXAxis().add(time);
            for (String key : realTimeDataAndCpName.keySet()) {
                DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
                powerPoints.setName(key);
                powerPoints.getValue().add(realTimeDataAndCpName.get(key).toString());
                if (key.contains("发电机机内氢压")) {
                    dataPower.getDataList().add(powerPoints);
                }
                if (key.contains("机内氢气湿度")) {
                    dataPower1.getDataList().add(powerPoints);
                }
                if (key.contains("漏氢含量")) {
                    dataPower2.getDataList().add(powerPoints);
                }
                if (key.contains("汽端发电机冷氢温度")) {
                    dataPower3.getDataList().add(powerPoints);
                }
            }

            List<DataPower> rts = new ArrayList<>(4);
            rts.add(dataPower);
            rts.add(dataPower1);
            rts.add(dataPower2);
            rts.add(dataPower3);
            webSocket.sendMessage(JSON.toJSONString(rts));
        }

    }

    /**
     * 油系统检测
     */
    @Scheduled(cron = "0/1 * * * * ? ")
    public void oilSystem() throws IOException {
        ConcurrentHashMap<String, OilSystemWebSocketServer> webSocketMap =
                OilSystemWebSocketServer.webSocketMap;
        ConcurrentHashMap<String, String> deviceMap = OilSystemWebSocketServer.deviceMap;
        if (webSocketMap.size() == 0 || deviceMap.size() == 0) {
            return;
        }
        //向每个webSocket推送氢系统实时数据
        for (String uuid : webSocketMap.keySet()) {
            final OilSystemWebSocketServer webSocket = webSocketMap.get(uuid);
            final String deviceId = deviceMap.get(uuid);
            List<String> characterNames = new ArrayList<>(4);
            characterNames.add("励端轴承氢侧密封油温度");
            characterNames.add("汽端轴承氢侧密封油温度");
            characterNames.add("空侧冷却器密封油出口温度");
            characterNames.add("氢侧冷却器密封油出口温度");
            characterNames.add("空侧交流密封油泵进出口压差");
            characterNames.add("空侧直流密封油泵进出口压差");
            characterNames.add("密封油油氢压差");
            characterNames.add("密封油油氢压差");
            characterNames.add("氢侧交流油泵进出口压差");
            characterNames.add("氢侧直流油泵进出口压差");
            characterNames.add("汽轮机备用油差压阀进口压力");
            characterNames.add("排油烟机#1进口压力");
            characterNames.add("排油烟机#2进口压力");

            List<CharacterParamInfoVo> characterParamInfoVos =
                    iCharacterParamService.getCharacterByName(characterNames, "", deviceId);
            List<String> KKsCodes = characterParamInfoVos.stream()
                    .map(CharacterParamInfoVo::getKksCode).collect(Collectors.toList());
            String start = "1s-ago";

            Map<Long, Map<String, Number>> response = iCharacterParamService
                    .getRealTimeDataByKksCode(KKsCodes, start, "");
            Long time = response.keySet().iterator().next();
            Map<String, Number> realTimeDataAndCpName = response.get(time);

            //组装数据结构
            //漏氢统计
            DataPower dataPower = new DataPower();
            dataPower.getXAxis().add(time);
            DataPower dataPower1 = new DataPower();
            dataPower1.getXAxis().add(time);
            DataPower dataPower2 = new DataPower();
            dataPower2.getXAxis().add(time);
            DataPower dataPower3 = new DataPower();
            dataPower3.getXAxis().add(time);
            DataPower dataPower4 = new DataPower();
            dataPower4.getXAxis().add(time);
            DataPower dataPower5 = new DataPower();
            dataPower5.getXAxis().add(time);

            //todo  流量需要计算待确定
            //todo 报警点数==>查有预警值的测点,再去查询报警点数
            for (String key : realTimeDataAndCpName.keySet()) {
                DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
                powerPoints.setName(key);
                powerPoints.getValue().add(realTimeDataAndCpName.get(key).toString());
                if (key.contains("空侧交流密封油泵进出口压差")
                        || key.contains("空侧直流密封油泵进出口压差")
                        || key.contains("密封油油氢压差")
                        || key.contains("密封油油氢压差")
                        || key.contains("氢侧交流油泵进出口压差")
                        || key.contains("氢侧直流油泵进出口压差")) {
                    dataPower.getDataList().add(powerPoints);
                }
                if (key.contains("励端轴承氢侧密封油温度")
                        || key.contains("汽端轴承氢侧密封油温度")
                        || key.contains("空侧冷却器密封油出口温度")
                        || key.contains("氢侧冷却器密封油出口温度")) {
                    dataPower1.getDataList().add(powerPoints);
                }
                if (key.contains("漏氢含量")) {
                    dataPower2.getDataList().add(powerPoints);
                }
                if (key.contains("汽端发电机冷氢温度")) {
                    dataPower3.getDataList().add(powerPoints);
                }
                if (key.contains("压力")) {
                    dataPower4.getDataList().add(powerPoints);
                }
            }

            //添加报警记录
            List<String> alertCharacterNames = new ArrayList<>(4);
            alertCharacterNames.add("励端轴承氢侧密封油温度");
            alertCharacterNames.add("汽端轴承氢侧密封油温度");
            alertCharacterNames.add("空侧冷却器密封油出口温度");
            alertCharacterNames.add("氢侧冷却器密封油出口温度");

            List<CharacterParamInfoVo> alertCharacterParamInfoVos =
                    iCharacterParamService.getCharacterByName(alertCharacterNames, "", deviceId);
            List<String> alertKKsCodes = alertCharacterParamInfoVos.stream()
                    .map(CharacterParamInfoVo::getKksCode).collect(Collectors.toList());
            List<EventInfo> list = iEventInfoService.getList(alertKKsCodes);

            alertCharacterParamInfoVos.forEach(arg -> {
                DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
                powerPoints.setName(arg.getCpName());
                //获取报警的条数
                long count = list.stream()
                        .filter(opt -> opt.getKKSCode().equals(arg.getKksCode()))
                        .filter(opt -> {
                            boolean flag = false;
                            if (arg.getAlertRuleMinValue() != 0) {
                                //小于报警下限时  报警
                                if (opt.getValue() < arg.getAlertRuleMinValue()) {
                                    flag = true;
                                }
                            }
                            if (arg.getAlertRuleMaxValue() != 0) {
                                //高于报警上限时  报警
                                if (opt.getValue() > arg.getAlertRuleMaxValue()) {
                                    flag = true;
                                }
                            }
                            return flag;
                        }).count();
                powerPoints.getValue().add(count + "");
            });
            List<DataPower> rts = new ArrayList<>(6);
            rts.add(dataPower);
            rts.add(dataPower1);
            rts.add(dataPower2);
            rts.add(dataPower3);
            rts.add(dataPower4);
            rts.add(dataPower5);

            webSocket.sendMessage(JSON.toJSONString(rts));
        }
    }

    @Scheduled(cron = "0/1 * * * * ? ")
    public void waterSystemRealData() throws IOException {
        ConcurrentHashMap<String, WaterSystemWebSocketServer> webSocketMap =
                WaterSystemWebSocketServer.webSocketMap;
        ConcurrentHashMap<String, String> deviceMap = WaterSystemWebSocketServer.deviceMap;
        if (webSocketMap.size() == 0 || deviceMap.size() == 0) {
            return;
        }
        //向每个webSocket推送氢系统实时数据
        for (String uuid : webSocketMap.keySet()) {
            final WaterSystemWebSocketServer webSocket = webSocketMap.get(uuid);
            final String deviceId = deviceMap.get(uuid);
            List<String> characterNames = new ArrayList<>(4);
            characterNames.add("转子冷却水泵A出口压力");
            characterNames.add("转子冷却水泵B出口压力");
            characterNames.add("转子冷却水流量");
            characterNames.add("定子冷却水流量");
            characterNames.add("定子线圈进水电导率");
            characterNames.add("定子线圈进水pH值");
            characterNames.add("转子线圈进水电导率");
            characterNames.add("转子线圈进水pH值");
            characterNames.add("定子线圈进水温度");
            characterNames.add("定子线圈出水温度");
            characterNames.add("转子线圈进水温度");
            characterNames.add("转子线圈出水温度");
            List<CharacterParamInfoVo> characterParamInfoVos =
                    iCharacterParamService.getCharacterByName(characterNames, "", deviceId);
            List<String> KKsCodes = characterParamInfoVos.stream()
                    .map(CharacterParamInfoVo::getKksCode).collect(Collectors.toList());
            String start = "1s-ago";

            Map<Long, Map<String, Number>> response = iCharacterParamService
                    .getRealTimeDataByKksCode(KKsCodes, start, "");
            Long time = response.keySet().iterator().next();
            Map<String, Number> realTimeDataAndCpName = response.get(time);

            //组装数据结构
            //漏氢统计
            DataPower dataPower = new DataPower();
            dataPower.getXAxis().add(time);
            DataPower dataPower1 = new DataPower();
            dataPower1.getXAxis().add(time);
            DataPower dataPower2 = new DataPower();
            dataPower2.getXAxis().add(time);
            DataPower dataPower3 = new DataPower();
            dataPower3.getXAxis().add(time);
            DataPower dataPower4 = new DataPower();
            dataPower3.getXAxis().add(time);
            DataPower dataPower5 = new DataPower();
            dataPower3.getXAxis().add(time);
            DataPower dataPower6 = new DataPower();
            dataPower3.getXAxis().add(time);

            for (String key : realTimeDataAndCpName.keySet()) {
                DataPower.PowerPoints powerPoints = new DataPower.PowerPoints();
                powerPoints.setName(key);
                powerPoints.getValue().add(realTimeDataAndCpName.get(key).toString());
                if (key.contains("转子冷却水泵A出口压力")
                        || key.contains("转子冷却水泵B出口压力")) {
                    dataPower.getDataList().add(powerPoints);
                }
                if (key.contains("转子冷却水流量")
                        || key.contains("定子冷却水流量")) {
                    dataPower1.getDataList().add(powerPoints);
                }
                if (key.contains("定子线圈进水温度")
                        || key.contains("定子线圈出水温度")
                        || key.contains("转子线圈进水温度")
                        || key.contains("转子线圈出水温度")) {
                    dataPower2.getDataList().add(powerPoints);
                }
                if (key.contains("定子线圈进水电导率1")
                        || key.contains("定子线圈进水pH值1")) {
                    dataPower3.getDataList().add(powerPoints);
                }
                if (key.contains("定子线圈进水电导率2")
                        || key.contains("定子线圈进水pH值2")) {
                    dataPower4.getDataList().add(powerPoints);
                }
                if (key.contains("转子线圈进水pH值1")
                        || key.contains("转子线圈进水电导率1")) {
                    dataPower5.getDataList().add(powerPoints);
                }
                if (key.contains("转子线圈进水电导率2")
                        || key.contains("转子线圈进水pH值2")) {
                    dataPower6.getDataList().add(powerPoints);
                }
            }
            List<DataPower> rts = new ArrayList<>(7);
            rts.add(dataPower);
            rts.add(dataPower1);
            rts.add(dataPower2);
            rts.add(dataPower3);
            rts.add(dataPower4);
            rts.add(dataPower5);
            rts.add(dataPower6);
            webSocket.sendMessage(JSON.toJSONString(rts));
        }
    }

}
