package com.gyee.wisdom.alarm.sharding.controller;

import com.gyee.wisdom.alarm.sharding.cache.AlarmInfoCache;
import com.gyee.wisdom.alarm.sharding.config.AjaxResult;
import com.gyee.wisdom.alarm.sharding.entity.*;
import com.gyee.wisdom.alarm.sharding.service.AlarmReportService;
import com.gyee.wisdom.alarm.sharding.model.CodeValue;
import com.gyee.wisdom.alarm.sharding.service.*;
import com.gyee.wisdom.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/alarm")
@CrossOrigin
public class AlarmCountController {


    Logger logger = LogManager.getLogger(AlarmCountController.class);
    @Autowired
    private AlarmCountService alarmCountService;
    @Autowired
    private WindturbineService windturbineService;
    @Autowired
    private FeatureStatService featureStatService;
    @Autowired
    private AlarmInfoCache alarmInfoCache;

    @Autowired
    private AlarmReportService alarmReportService;


    @GetMapping(value = "/count/start")
    public void startCount(
            @RequestParam(value = "date", required = false) String startDate
    ) {
        //alarmCountService.startCount();
        alarmReportService.startCount();
    }

    @GetMapping(value = "/count/query")
    public List<AlarmStat1> query(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "startdate", required = true) String startDate,
            @RequestParam(value = "enddate", required = true) String endDate
    ) throws ParseException {

        List<AlarmStat1> result = new ArrayList<>();
        result = alarmCountService.statAlarmCount1(stId, startDate, endDate);
        //添加拦截,查询液压泵运行统计,并将结果合并为 List<AlarmStat1>
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dtStart = sdf.parse(startDate);
        Date dtEnd = sdf.parse(endDate);
        List<AlarmStat1> pressureList = featureStatService.findByCondition(stId, dtStart, dtEnd);

        result.addAll(pressureList);
        return result;
    }

    @GetMapping(value = "/count/query/new")
    public List<Map.Entry<String, List<AlarmStat1>>> querynew(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "startdate", required = true) String startDate,
            @RequestParam(value = "enddate", required = true) String endDate
    ) throws ParseException {

        List<AlarmStat1> result = new ArrayList<>();
        result = alarmCountService.statAlarmCount1(stId, startDate, endDate);
        //添加拦截,查询液压泵运行统计,并将结果合并为 List<AlarmStat1>
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dtStart = sdf.parse(startDate);
        Date dtEnd = sdf.parse(endDate);
        List<AlarmStat1> pressureList = featureStatService.findByCondition(stId, dtStart, dtEnd);

        result.addAll(pressureList);

        for (int i = 0; i < result.size(); i++) {
            AlarmStat1 data = result.get(i);
            //根据预警内容获取所属部件code
            if (alarmInfoCache.getAlertTextToPartMap().containsKey(data.getAlertText())) {
                String relatedParts = alarmInfoCache.getAlertTextToPartMap().get(data.getAlertText());
                data.setRelateParts(relatedParts);
                //根据预警所属部件code获取所属部件名称
                if (alarmInfoCache.getRelatePartsMap().containsKey(relatedParts)) {
                    String releatedPartsText = alarmInfoCache.getRelatePartsMap().get(relatedParts);
                    data.setRelatePartsText(releatedPartsText);
                }
            }
        }


  /*      String[] arr = new String[]{
                "大风引起震动", "机舱震动幅度超限", "断轴或联轴器打滑", "风向突变", "风速突变", "单机风向标故障", "单机风速仪故障", "机组逆功率", "发电机轴A损坏或碳刷室温度过高", "主轴温度温差大于8度", "主轴温度高", "主轴温升大于40度", "齿轮箱油温温度传感器异常值", "齿轮箱温控阀故障或换热器效率低", "齿轮箱轴承温升超过40度", "齿轮箱轴承温升速度超过1.5", "齿轮箱轴承温差大于8度", "发电机轴承温度高", "发电机轴承温升速度大于1.5", "齿轮箱轴承温度高", "机端低电压", "温度测点开路", "停机收浆超时", "液压泵运行", "滤网压差", "偏航", "解缆动作", "变桨通信不良", "叶轮转速超速", "发电机转速超速", "三相电流不平衡"
        };*/

        //String str = "大风引起震动,机舱震动幅度超限,断轴或联轴器打滑,风向突变,风速突变,单机风向标故障,单机风速仪故障,机组逆功率,发电机轴A损坏或碳刷室温度过高,主轴温度温差大于8度,主轴温度高,主轴温升大于40度,齿轮箱油温温度传感器异常值,齿轮箱温控阀故障或换热器效率低,齿轮箱轴承温升超过40度,齿轮箱轴承温升速度超过1.5,齿轮箱轴承温差大于8度,发电机轴承温度高,发电机轴承温升速度大于1.5,齿轮箱轴承温度高,机端低电压,温度测点开路,停机收浆超时,液压泵运行,滤网压差,偏航,解缆动作,变桨通信不良,叶轮转速超速,发电机转速超速,三相电流不平衡";
        String str  = "发电机轴承温升速度大于1.5,断轴或联轴器打滑,齿轮箱轴承温升超过40度,三相电流不平衡,发电机轴A损坏或碳刷室温度过高,风向突变,发电机转速超速,单机风向标故障,变桨通信不良,机舱震动幅度超限,齿轮箱轴承温升速度超过1.5,发电机轴承温升大于40度,齿轮箱温控阀故障或换热器效率低,滤网压差,解缆动作,液压泵运行,偏航,主轴温度温差大于8度,风速突变,风机偏航过程震动,齿轮箱轴承温差大于8度,叶轮转速超速,单机发电机W2绕组超限报警,单机发电机W2绕组温度传感器异常值,单机发电机W2绕组温度预警,单机发电机U2绕组温度预警,单机发电机U2绕组超限报警,停机收浆超时,单机发电机V2绕组超限报警,单机发电机V2绕组温度预警,单机发电机V2绕组温度传感器异常值,机组逆功率,风机启动过程震动,桨叶角过小,单机风速仪故障,AGC 负荷调节延迟预警,主轴叶轮侧温度温度传感器异常值,主轴温升大于40度,主轴温升速度大于1.5,主轴温度高,主轴齿轮箱侧温度传感器异常值,单机发电机U1绕组温度传感器异常值,单机发电机U1绕组温度预警,单机发电机U1绕组超限报警,单机发电机U2绕组温度传感器异常值,单机发电机V1绕组温度传感器异常值,单机发电机V1绕组温度预警,单机发电机V1绕组超限报警,单机发电机W1绕组温度传感器异常值,单机发电机W1绕组温度预警,单机发电机W1绕组超限报警,单机发电机轴a温度传感器异常值,单机发电机轴a温度超限报警,单机发电机轴a温度预警,单机发电机轴b温度传感器异常值,单机发电机轴b温度超限报警,单机发电机轴b温度预警,单机变桨电池柜温度1温度传感器异常值,单机变桨电池柜温度2温度传感器异常值,单机变桨电池柜温度2超限报警,单机变桨电池柜温度2预警,单机变桨电池柜温度3温度传感器异常值,单机变桨电池柜温度3超限报警,单机变桨电池柜温度3预警,单机齿轮箱油温超限报警,发电机轴a每分钟温升超过1.5,发电机轴承温度高,变桨电容电压低,变桨电容电压高,塔底柜温度传感器异常值,夏季单机齿轮箱油温温度传感器异常值,大风引起震动,暴风天气,机端低电压,机端过电压,机组过功率,机舱温度温度传感器异常值,机舱温度高,桨叶角度差,温度测点开路,石板泉发电机轴承温度超过70度,风机停机过程震动,风机轴a轴b温差大,风机轴a轴b温差大_UP82,齿轮箱油温度高,齿轮箱油温温度传感器异常值,齿轮箱轴承温度高,齿轮箱输入轴1温度传感器异常值,齿轮箱输出轴2温度传感器异常值,齿轮箱高速轴轴温大于90度";
        List<String> mustCloumList = Arrays.asList(str.split(","));
        //去重获取所有列
        List<String> cloumList = result.stream().map(AlarmStat1::getAlertText).collect(Collectors.toList());
        cloumList = cloumList.stream().distinct().collect(Collectors.toList());
        //去重复并集
        cloumList.addAll(mustCloumList);
        List<String> allCloumList = cloumList.stream().distinct().collect(Collectors.toList());


        //根据风机编号分组
        Map<String, List<AlarmStat1>> alarmStatMapGroupbyWindturbineId = result.stream().collect(Collectors.groupingBy(AlarmStat1::getWindturbineId));

        Map<String, List<AlarmStat1>> resultMap = new TreeMap<>();

        for (Map.Entry<String, List<AlarmStat1>> entry : alarmStatMapGroupbyWindturbineId.entrySet()) {
            String windturbineId = entry.getKey();
            String stationId = entry.getValue().get(0).getStationId();

            List<String> lst = entry.getValue().stream().map(AlarmStat1::getAlertText).collect(Collectors.toList());
            //与总列的差集
            List<String> subList = allCloumList.stream().filter(item -> !lst.contains(item)).collect(Collectors.toList());
            //将差集(缺失数据添加到原数据中)

            List<AlarmStat1> dataList = entry.getValue();

            for (String txt : subList) {

                AlarmStat1 stat = new AlarmStat1();
                stat.setAlertText(txt);
                stat.setCount(0);
                stat.setTime(0);
                stat.setWindturbineId(windturbineId);
                stat.setStationId(stationId);

                if (alarmInfoCache.getAlertTextToPartMap().containsKey(txt)) {
                    String relatedParts = alarmInfoCache.getAlertTextToPartMap().get(txt);
                    stat.setRelateParts(relatedParts);
                    //根据预警所属部件code获取所属部件名称
                    if (alarmInfoCache.getRelatePartsMap().containsKey(relatedParts)) {
                        String releatedPartsText = alarmInfoCache.getRelatePartsMap().get(relatedParts);
                        stat.setRelatePartsText(releatedPartsText);
                    }
                }

                dataList.add(stat);
            }
            if (!resultMap.containsKey(windturbineId)) {
                resultMap.put(windturbineId, dataList);
            }
        }

        //根据风机编号进行排序
        List<Map.Entry<String, List<AlarmStat1>>> list = new ArrayList<Map.Entry<String, List<AlarmStat1>>>(resultMap.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, List<AlarmStat1>>>() {
            @Override
            public int compare(Map.Entry<String, List<AlarmStat1>> o1, Map.Entry<String, List<AlarmStat1>> o2) {
                int num1 = Integer.valueOf(o1.getKey().substring(5));
                int num2 = Integer.valueOf(o2.getKey().substring(5));
                int diff = num1 - num2;
                if (diff > 0) {
                    return 1;
                } else if (diff < 0) {
                    return -1;
                }
                return 0; //相等为0
            }
        });

        return list;


    }

    @GetMapping(value = "/count/query/new3")
    public AjaxResult querynew3(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "windturbineid", required = true) String windturbineid,
            @RequestParam(value = "startdate", required = true) String startDate,
            @RequestParam(value = "enddate", required = true) String endDate
    ) throws ParseException {

        try {
            Map<Long,AlarmStat1> result = new HashMap<>();
            result = alarmCountService.statAlarmCount5(stId, windturbineid, startDate, endDate);
            return AjaxResult.successData(result);
        } catch (Exception e) {
            return AjaxResult.error();

        }
    }

    @GetMapping(value = "/count/query/new2")
    public AjaxResult querynew2(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "startdate", required = true) String startDate,
            @RequestParam(value = "enddate", required = true) String endDate
    ) throws ParseException {

        try {
            List<AlarmStat1> result = new ArrayList<>();
            List<AlarmStat1> alarmStat1s = alarmCountService.statAlarmCount1(stId, startDate, endDate);
            if(alarmStat1s!=null)
                result.addAll(alarmStat1s);

            //添加拦截,查询液压泵运行统计,并将结果合并为 List<AlarmStat1>
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dtStart = sdf.parse(startDate);
            Date dtEnd = sdf.parse(endDate);
            List<AlarmStat1> pressureList = featureStatService.findByCondition(stId, dtStart, dtEnd);

            result.addAll(pressureList);

            for (int i = 0; i < result.size(); i++) {
                AlarmStat1 data = result.get(i);
                //根据预警内容获取所属部件code
                if (alarmInfoCache.getAlertTextToPartMap().containsKey(data.getAlertText())) {
                    String relatedParts = alarmInfoCache.getAlertTextToPartMap().get(data.getAlertText());
                    data.setRelateParts(relatedParts);
                    //根据预警所属部件code获取所属部件名称
                    if (alarmInfoCache.getRelatePartsMap().containsKey(relatedParts)) {
                        String releatedPartsText = alarmInfoCache.getRelatePartsMap().get(relatedParts);
                        data.setRelatePartsText(releatedPartsText);
                    }
                }
            }


  /*      String[] arr = new String[]{
                "大风引起震动", "机舱震动幅度超限", "断轴或联轴器打滑", "风向突变", "风速突变", "单机风向标故障", "单机风速仪故障", "机组逆功率", "发电机轴A损坏或碳刷室温度过高", "主轴温度温差大于8度", "主轴温度高", "主轴温升大于40度", "齿轮箱油温温度传感器异常值", "齿轮箱温控阀故障或换热器效率低", "齿轮箱轴承温升超过40度", "齿轮箱轴承温升速度超过1.5", "齿轮箱轴承温差大于8度", "发电机轴承温度高", "发电机轴承温升速度大于1.5", "齿轮箱轴承温度高", "机端低电压", "温度测点开路", "停机收浆超时", "液压泵运行", "滤网压差", "偏航", "解缆动作", "变桨通信不良", "叶轮转速超速", "发电机转速超速", "三相电流不平衡"
        };*/

            //String str = "大风引起震动,机舱震动幅度超限,断轴或联轴器打滑,风向突变,风速突变,单机风向标故障,单机风速仪故障,机组逆功率,发电机轴A损坏或碳刷室温度过高,主轴温度温差大于8度,主轴温度高,主轴温升大于40度,齿轮箱油温温度传感器异常值,齿轮箱温控阀故障或换热器效率低,齿轮箱轴承温升超过40度,齿轮箱轴承温升速度超过1.5,齿轮箱轴承温差大于8度,发电机轴承温度高,发电机轴承温升速度大于1.5,齿轮箱轴承温度高,机端低电压,温度测点开路,停机收浆超时,液压泵运行,滤网压差,偏航,解缆动作,变桨通信不良,叶轮转速超速,发电机转速超速,三相电流不平衡";
            String str  = "发电机轴承温升速度大于1.5,断轴或联轴器打滑,齿轮箱轴承温升超过40度,三相电流不平衡,发电机轴A损坏或碳刷室温度过高,风向突变,发电机转速超速,单机风向标故障,变桨通信不良,机舱震动幅度超限,齿轮箱轴承温升速度超过1.5,发电机轴承温升大于40度,齿轮箱温控阀故障或换热器效率低,滤网压差,解缆动作,液压泵运行,偏航,主轴温度温差大于8度,风速突变,风机偏航过程震动,齿轮箱轴承温差大于8度,叶轮转速超速,单机发电机W2绕组超限报警,单机发电机W2绕组温度传感器异常值,单机发电机W2绕组温度预警,单机发电机U2绕组温度预警,单机发电机U2绕组超限报警,停机收浆超时,单机发电机V2绕组超限报警,单机发电机V2绕组温度预警,单机发电机V2绕组温度传感器异常值,机组逆功率,风机启动过程震动,桨叶角过小,单机风速仪故障,AGC 负荷调节延迟预警,主轴叶轮侧温度温度传感器异常值,主轴温升大于40度,主轴温升速度大于1.5,主轴温度高,主轴齿轮箱侧温度传感器异常值,单机发电机U1绕组温度传感器异常值,单机发电机U1绕组温度预警,单机发电机U1绕组超限报警,单机发电机U2绕组温度传感器异常值,单机发电机V1绕组温度传感器异常值,单机发电机V1绕组温度预警,单机发电机V1绕组超限报警,单机发电机W1绕组温度传感器异常值,单机发电机W1绕组温度预警,单机发电机W1绕组超限报警,单机发电机轴a温度传感器异常值,单机发电机轴a温度超限报警,单机发电机轴a温度预警,单机发电机轴b温度传感器异常值,单机发电机轴b温度超限报警,单机发电机轴b温度预警,单机变桨电池柜温度1温度传感器异常值,单机变桨电池柜温度2温度传感器异常值,单机变桨电池柜温度2超限报警,单机变桨电池柜温度2预警,单机变桨电池柜温度3温度传感器异常值,单机变桨电池柜温度3超限报警,单机变桨电池柜温度3预警,单机齿轮箱油温超限报警,发电机轴a每分钟温升超过1.5,发电机轴承温度高,变桨电容电压低,变桨电容电压高,塔底柜温度传感器异常值,夏季单机齿轮箱油温温度传感器异常值,大风引起震动,暴风天气,机端低电压,机端过电压,机组过功率,机舱温度温度传感器异常值,机舱温度高,桨叶角度差,温度测点开路,石板泉发电机轴承温度超过70度,风机停机过程震动,风机轴a轴b温差大,风机轴a轴b温差大_UP82,齿轮箱油温度高,齿轮箱油温温度传感器异常值,齿轮箱轴承温度高,齿轮箱输入轴1温度传感器异常值,齿轮箱输出轴2温度传感器异常值,齿轮箱高速轴轴温大于90度";
            List<String> mustCloumList = Arrays.asList(str.split(","));
            //去重获取所有列
            List<String> cloumList = result.stream().map(AlarmStat1::getAlertText).collect(Collectors.toList());
            cloumList = cloumList.stream().distinct().collect(Collectors.toList());
            //去重复并集
            cloumList.addAll(mustCloumList);
            List<String> allCloumList = cloumList.stream().distinct().collect(Collectors.toList());


            //根据风机编号分组
            Map<String, List<AlarmStat1>> alarmStatMapGroupbyWindturbineId = result.stream().collect(Collectors.groupingBy(AlarmStat1::getWindturbineId));

            Map<String, List<AlarmStat1>> resultMap = new TreeMap<>();

            for (Map.Entry<String, List<AlarmStat1>> entry : alarmStatMapGroupbyWindturbineId.entrySet()) {
                String windturbineId = entry.getKey();
                String stationId = entry.getValue().get(0).getStationId();

                List<String> lst = entry.getValue().stream().map(AlarmStat1::getAlertText).collect(Collectors.toList());
                //与总列的差集
                List<String> subList = allCloumList.stream().filter(item -> !lst.contains(item)).collect(Collectors.toList());
                //将差集(缺失数据添加到原数据中)

                List<AlarmStat1> dataList = entry.getValue();

                for (String txt : subList) {

                    AlarmStat1 stat = new AlarmStat1();
                    stat.setAlertText(txt);
                    stat.setCount(0);
                    stat.setTime(0);
                    stat.setWindturbineId(windturbineId);
                    stat.setStationId(stationId);

                    if (alarmInfoCache.getAlertTextToPartMap().containsKey(txt)) {
                        String relatedParts = alarmInfoCache.getAlertTextToPartMap().get(txt);
                        stat.setRelateParts(relatedParts);
                        //根据预警所属部件code获取所属部件名称
                        if (alarmInfoCache.getRelatePartsMap().containsKey(relatedParts)) {
                            String releatedPartsText = alarmInfoCache.getRelatePartsMap().get(relatedParts);
                            stat.setRelatePartsText(releatedPartsText);
                        }
                    }

                    dataList.add(stat);
                }
                if (!resultMap.containsKey(windturbineId)) {
                    resultMap.put(windturbineId, dataList);
                }
            }

            //根据风机编号进行排序
            List<Map.Entry<String, List<AlarmStat1>>> list = new ArrayList<Map.Entry<String, List<AlarmStat1>>>(resultMap.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<String, List<AlarmStat1>>>() {
                @Override
                public int compare(Map.Entry<String, List<AlarmStat1>> o1, Map.Entry<String, List<AlarmStat1>> o2) {
                    String[]num1Array=StringUtil.split(o1.getKey(),"_");
                    String []num2Array=StringUtil.split(o2.getKey(),"_");
                    if(num1Array.length!=2||num2Array.length!=2){
                        int comapreResult=0;
                        try{

                            int num1 = Integer.valueOf(num2Array[1]);
                            int num2 = Integer.valueOf(num2Array[1]);
                            int diff = num1 - num2;
                            if (diff > 0) {
                                comapreResult= 1;
                            } else if (diff < 0) {
                                comapreResult= -1;
                            }else{
                                comapreResult=0;
                            }

                        }catch (Exception ex){

                        }
                        finally {
                            return comapreResult;
                        }
                    }else{
                        return 0;
                    }

                }
            });
            result.addAll(pressureList);
            return AjaxResult.successData(list);
        } catch (ParseException e) {
            return AjaxResult.error();
        }
    }

    @GetMapping(value = "/count/querymap")
    public Map<String, AlarmInformation> query1(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "datebegin", required = true) String startDate,
            @RequestParam(value = "dateend", required = true) String endDate,
            @RequestParam(value = "windturbineid", required = true) String windturbineid
    ) throws ParseException {
        return alarmCountService.query1(stId, startDate, endDate,windturbineid);
    }


    @GetMapping(value = "/count/querymap2")
    public AjaxResult query2(
            @RequestParam(value = "stationid", required = true) String stationid,
            @RequestParam(value = "datebegin", required = true) String startDate,
            @RequestParam(value = "dateend", required = true) String endDate,
            @RequestParam(value = "windturbineid", required = false) String windturbineid
    ) throws ParseException {

        Map<String, AlarmInformation> map = alarmCountService.query1(stationid, startDate, endDate,windturbineid);
        return AjaxResult.successData(map);
    }


    @GetMapping(value = "/count/querybyname")
    public Map<String, AlarmStat> querybyname(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "datebegin", required = true) String startDate,
            @RequestParam(value = "dateend", required = true) String endDate,
            @RequestParam(value = "snapid", required = true) String name
    ) throws ParseException {
        return alarmCountService.querybyname(stId, startDate, endDate, name);
    }


    @GetMapping(value = "/count/querybyname2")
    public AjaxResult querybyname2(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "datebegin", required = true) String startDate,
            @RequestParam(value = "dateend", required = true) String endDate,
            @RequestParam(value = "snapid", required = true) String snapId
    ) throws ParseException {
        try {
            Map<String, AlarmStat> map = alarmCountService.querybyname(stId, startDate, endDate, snapId);
            return AjaxResult.successData(map);
        } catch (ParseException e) {
            return AjaxResult.error();
        }

    }

    @GetMapping(value = "/count/lineandproject")
    public List<AlarmStatLine> lineandproject(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "datebegin", required = true) String startDate,
            @RequestParam(value = "dateend", required = true) String endDate,
            @RequestParam(value = "snapid", required = true) String name
    ) throws ParseException {
        return alarmCountService.lineandproject(stId, startDate, endDate, name);
    }


    @GetMapping(value = "/count/lineandproject2")
    public AjaxResult lineandproject2(
            @RequestParam(value = "stationid", required = true) String stId,
            @RequestParam(value = "datebegin", required = true) String startDate,
            @RequestParam(value = "dateend", required = true) String endDate,
            @RequestParam(value = "snapid", required = true) String name
    ) throws ParseException {
        try {
            List<AlarmStatLine> list = alarmCountService.lineandproject(stId, startDate, endDate, name);
         list.sort(new Comparator<AlarmStatLine>() {
             @Override
             public int compare(AlarmStatLine o1, AlarmStatLine o2) {
                 return o1.getLinenum().compareTo(o2.getLinenum());
             }
         });
            return AjaxResult.successData(list);
        } catch (ParseException e) {
            return AjaxResult.error();
        }

    }


    @GetMapping(value = "/count/windturbines")
    public List<Windturbine> windturbine() {
        alarmCountService.startCount();
        return null;
    }

    @GetMapping(value = "/rule/names")
    public List<CodeValue> selectAlertRuleNames() {
        return alarmCountService.selectAlertRuleNames();
    }


    /**
     * 报警事件全场统计时间和次数
     * @param stId 场站id（例：MHS_FDC）
     * @return
     */
    @GetMapping(value = "/count/query/alltotal")
    public AjaxResult querynew1(
            @RequestParam(value = "stationid", required = false) String stId
    ) {
        try {
            if(null ==stId || "0".equals(stId.trim())){
                stId = "";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            calendar.getTime();
            Date currentDate = sdf.parse( sdf.format(calendar.getTime()));
            List<AlarmStat1> result = new ArrayList<>();
            result = alarmCountService.statAlarmCount2(stId, currentDate);
            List<AlarmStat1> pressureList = featureStatService.findByCondition2(stId, currentDate);

            result.addAll(pressureList);

            for (int i = 0; i < result.size(); i++) {
                AlarmStat1 data = result.get(i);
                //根据预警内容获取所属部件code
                if (alarmInfoCache.getAlertTextToPartMap().containsKey(data.getAlertText())) {
                    String relatedParts = alarmInfoCache.getAlertTextToPartMap().get(data.getAlertText());
                    data.setRelateParts(relatedParts);
                    //根据预警所属部件code获取所属部件名称
                    if (alarmInfoCache.getRelatePartsMap().containsKey(relatedParts)) {
                        String releatedPartsText = alarmInfoCache.getRelatePartsMap().get(relatedParts);
                        data.setRelatePartsText(releatedPartsText);
                    }
                }
            }


            String str  = "发电机轴承温升速度大于1.5,断轴或联轴器打滑,齿轮箱轴承温升超过40度,三相电流不平衡,发电机轴A损坏或碳刷室温度过高,风向突变,发电机转速超速,单机风向标故障,变桨通信不良,机舱震动幅度超限,齿轮箱轴承温升速度超过1.5,发电机轴承温升大于40度,齿轮箱温控阀故障或换热器效率低,滤网压差,解缆动作,液压泵运行,偏航,主轴温度温差大于8度,风速突变,风机偏航过程震动,齿轮箱轴承温差大于8度,叶轮转速超速,单机发电机W2绕组超限报警,单机发电机W2绕组温度传感器异常值,单机发电机W2绕组温度预警,单机发电机U2绕组温度预警,单机发电机U2绕组超限报警,停机收浆超时,单机发电机V2绕组超限报警,单机发电机V2绕组温度预警,单机发电机V2绕组温度传感器异常值,机组逆功率,风机启动过程震动,桨叶角过小,单机风速仪故障,AGC 负荷调节延迟预警,主轴叶轮侧温度温度传感器异常值,主轴温升大于40度,主轴温升速度大于1.5,主轴温度高,主轴齿轮箱侧温度传感器异常值,单机发电机U1绕组温度传感器异常值,单机发电机U1绕组温度预警,单机发电机U1绕组超限报警,单机发电机U2绕组温度传感器异常值,单机发电机V1绕组温度传感器异常值,单机发电机V1绕组温度预警,单机发电机V1绕组超限报警,单机发电机W1绕组温度传感器异常值,单机发电机W1绕组温度预警,单机发电机W1绕组超限报警,单机发电机轴a温度传感器异常值,单机发电机轴a温度超限报警,单机发电机轴a温度预警,单机发电机轴b温度传感器异常值,单机发电机轴b温度超限报警,单机发电机轴b温度预警,单机变桨电池柜温度1温度传感器异常值,单机变桨电池柜温度2温度传感器异常值,单机变桨电池柜温度2超限报警,单机变桨电池柜温度2预警,单机变桨电池柜温度3温度传感器异常值,单机变桨电池柜温度3超限报警,单机变桨电池柜温度3预警,单机齿轮箱油温超限报警,发电机轴a每分钟温升超过1.5,发电机轴承温度高,变桨电容电压低,变桨电容电压高,塔底柜温度传感器异常值,夏季单机齿轮箱油温温度传感器异常值,大风引起震动,暴风天气,机端低电压,机端过电压,机组过功率,机舱温度温度传感器异常值,机舱温度高,桨叶角度差,温度测点开路,石板泉发电机轴承温度超过70度,风机停机过程震动,风机轴a轴b温差大,风机轴a轴b温差大_UP82,齿轮箱油温度高,齿轮箱油温温度传感器异常值,齿轮箱轴承温度高,齿轮箱输入轴1温度传感器异常值,齿轮箱输出轴2温度传感器异常值,齿轮箱高速轴轴温大于90度";
            List<String> mustCloumList = Arrays.asList(str.split(","));
            //去重获取所有列
            List<String> cloumList = result.stream().map(AlarmStat1::getAlertText).collect(Collectors.toList());
            cloumList = cloumList.stream().distinct().collect(Collectors.toList());
            //去重复并集
            cloumList.addAll(mustCloumList);
            List<String> allCloumList = cloumList.stream().distinct().collect(Collectors.toList());
            List<String> lst = result.stream().map(AlarmStat1::getAlertText).collect(Collectors.toList());
            //与总列的差集
            List<String> subList = allCloumList.stream().filter(item -> !lst.contains(item)).collect(Collectors.toList());
            List<AlarmStat1> addalarmsStat =  new ArrayList<>();
            for (String txt : subList) {

                AlarmStat1 stat = new AlarmStat1();
                stat.setAlertText(txt);
                stat.setCount(0);
                stat.setTime(0);

                if (alarmInfoCache.getAlertTextToPartMap().containsKey(txt)) {

                    String relatedParts = alarmInfoCache.getAlertTextToPartMap().get(txt);
                    if (null == relatedParts || "".equals(relatedParts.trim())){
                        continue;
                    }
                    stat.setRelateParts(relatedParts);
                    //根据预警所属部件code获取所属部件名称
                    if (alarmInfoCache.getRelatePartsMap().containsKey(relatedParts)) {
                        String releatedPartsText = alarmInfoCache.getRelatePartsMap().get(relatedParts);
                        stat.setRelatePartsText(releatedPartsText);
                    }
                }
                addalarmsStat.add(stat);
            }
            result.addAll(addalarmsStat);

            //Map<String, List<AlarmStat1>> alarmStatMapGroupbyRelatePartsId = result.stream().collect(Collectors.groupingBy(AlarmStat1::getRelateParts));
            Map<String, List<AlarmStat1>> alarmStatMapGroupbyRelatePartsId = result.stream().filter(item-> StringUtil.isNotBlank(item.getRelateParts())).collect(Collectors.groupingBy(AlarmStat1::getRelateParts));
            Map<String, AlarmStat1> resultMap = new TreeMap<>();
            for (Map.Entry<String, List<AlarmStat1>> entry : alarmStatMapGroupbyRelatePartsId.entrySet()) {
                AlarmStat1 stat = new AlarmStat1();
                int count = entry.getValue().stream().mapToInt(AlarmStat1::getCount).sum();
                double time = entry.getValue().stream().mapToDouble(AlarmStat1::getTime).sum();
                stat.setCount(count);
                stat.setTime(time);
                stat.setRelateParts(entry.getKey());
                //根据预警所属部件code获取所属部件名称
                stat.setRelatePartsText(entry.getValue().get(0).getRelatePartsText());
                resultMap.put(entry.getKey(),stat);
            }
            return  AjaxResult.successData(resultMap);
        }catch (Exception e){
            logger.error("操作错误",e);
            return null;
        }
    }


}




