package com.gyee.alarm.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.model.vo.AlarmCustomType;
import com.gyee.alarm.model.vo.AlarmType;
import com.gyee.alarm.model.vo.AlarmTypeValue;
import com.gyee.alarm.service.auto.*;
import com.gyee.alarm.util.SnowflakeGenerator;
import com.gyee.alarm.util.DateUtils;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

@Service
public class EarlyReportService  {


    @Resource
    private  IProEconAlarmInfoService proEconAlarmInfoService;
    @Resource
    private  IProEconEarlyReportService proEconEarlyReportService;
    @Resource
    private IProEconEarlyReportConclusioService proEconEarlyReportConclusioService;
    @Resource
    private IAlarmTsService alarmTsService;

    @Resource
    private  IProEconEarlyReportWtService proEconEarlyReportWtService;
    @Resource
    private  IProEconEarlyReportAlarmService proEconEarlyReportAlarmService;


    //时间间隔（天），即从当日0点，向前间隔天数，为计算区间
    @Value("${interval_day}")
    private  int interval_day;
    @Value("${limit}")
    private  int limit;

    public boolean createEarlyReport(Date recordDate) {

        boolean result=false;
        //报告时间
        Date reportDate = DateUtils.truncate(recordDate);
        //获取计算开始时间
        Date statStartTime = DateUtils.addDays(DateUtils.today(), - interval_day);
        //获取计算结束时间
        Date statEndTime = DateUtils.truncate(recordDate);


        List<ProEconAlarmType> fjtypels=CacheContext.alarmTypeMap.get(AlarmType.FJBJ.getCode());

        List<ProEconAlarmType> gftypels=CacheContext.alarmTypeMap.get(AlarmType.GFBJ.getCode());

        QueryWrapper<ProEconEarlyReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_date",recordDate);
        //判断是否有重复记录，先删除重复记录
        List<String> idls = proEconEarlyReportService.list(queryWrapper).stream()
                .map(ProEconEarlyReport::getId)
                .collect(Collectors.toList());

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

            List<ProEconEarlyReportWt> wtls= proEconEarlyReportWtService.findByReportId(idls.get(i));
            if(!wtls.isEmpty())
            {
                List<String> wtidls = wtls.stream().map(ProEconEarlyReportWt::getId).collect(Collectors.toList());
                proEconEarlyReportService.removeByIds(wtidls);

                for(int j=0;j<wtidls.size();j++)
                {
                    List<ProEconEarlyReportAlarm> alarmls= proEconEarlyReportAlarmService.findByWindturbineReportId(wtidls.get(j));

                    if(!alarmls.isEmpty())
                    {
                        List<String> alarmidls = alarmls.stream().map(ProEconEarlyReportAlarm::getId).collect(Collectors.toList());
                        proEconEarlyReportAlarmService.removeByIds(alarmidls);

                    }
                }

            }
        }

        List<String> tempids=new ArrayList<>();

        for(int i=0;i<idls.size();i++)
        {
            tempids.add(idls.get(i));
            if(tempids.size()==100)
            {
                proEconEarlyReportService.removeByIds(tempids);
                tempids=new ArrayList<>();
            }
        }

        if(!tempids.isEmpty())
        {
            proEconEarlyReportService.removeByIds(tempids);
        }

       for(ProBasicEquipment wt: CacheContext.wtls)
       {


          if(wt.getWindpowerstationId().contains("FDC"))
          {
              for(ProEconAlarmType fjtype:fjtypels)
              {
                  if(!fjtype.getNemCode().equals("BJXT") && !fjtype.getNemCode().equals("FDJ"))
                  {

                      continue;
                  }
                      ProEconEarlyReport report = new ProEconEarlyReport();
                      report.setId(String.valueOf(SnowflakeGenerator.generateId()));
                      report.setReportDate(new Date());
                      report.setStationId(wt.getWindpowerstationId());
                      if(CacheContext.wpmap.containsKey(wt.getWindpowerstationId()))
                      {
                          report.setStationName(CacheContext.wpmap.get(wt.getWindpowerstationId()).getName());
                      }
                      report.setRelatedParts(fjtype.getNemCode());
                      StringBuilder sb=new StringBuilder();
                      sb.append(wt.getNemCode()).append(wt.getName()).append(fjtype.getName()).append("诊断报告");
                      report.setReportName(String.valueOf(sb));
                      report.setStatStartDate(statStartTime);
                      report.setStatEndDate(statEndTime);
                      report.setProEconEarlyReportWtList(new ArrayList<>());
                      report.setReportDate(reportDate);
                      report.setStatStartDate(new Date());

                      result = isResult(statStartTime, statEndTime, wt, report);


              }
          }else
          {
              for(ProEconAlarmType gftype:gftypels)
              {
                  ProEconEarlyReport report = new ProEconEarlyReport();
                  report.setId(String.valueOf(SnowflakeGenerator.generateId()));
                  report.setReportDate(new Date());
                  report.setStationId(wt.getWindpowerstationId());
                  if(CacheContext.wpmap.containsKey(wt.getWindpowerstationId()))
                  {
                      report.setStationName(CacheContext.wpmap.get(wt.getWindpowerstationId()).getName());
                  }
                  report.setRelatedParts(gftype.getNemCode());
                  StringBuilder sb=new StringBuilder();
                  sb.append(wt.getNemCode()).append(wt.getName()).append(gftype.getName()).append("诊断报告");
                  report.setReportName(String.valueOf(sb));
                  report.setStatStartDate(statStartTime);
                  report.setStatEndDate(statEndTime);
                  report.setProEconEarlyReportWtList(new ArrayList<>());
                  report.setReportDate(reportDate);
                  report.setStatStartDate(new Date());

                  result = isResult(statStartTime, statEndTime, wt, report);
              }
          }

       }
        return result;
    }

    private boolean isResult(Date statStartTime, Date statEndTime, ProBasicEquipment wt, ProEconEarlyReport report) {

        boolean result;

        //获取本次报告最差的风机信息集合
        List<ProEconEarlyReportWt> nowWindturbineInfoList = getNowWindturbineInfo(wt, wt.getModelId(), report.getId(), report.getRelatedParts(), statStartTime, statEndTime, limit);

        report.getProEconEarlyReportWtList().addAll(nowWindturbineInfoList);
        // 获取上次报告
        List<ProEconEarlyReport> earlyReport = proEconEarlyReportService.findEarlyReportByDate(wt.getId(), DateUtils.addDays(statEndTime, -60), statEndTime);
        earlyReport=earlyReport.stream().filter(s->s.getRelatedParts().equals(report.getRelatedParts())).collect(Collectors.toList());
        List<ProEconEarlyReport> collect = earlyReport.stream().sorted(Comparator.comparing(ProEconEarlyReport::getId, Comparator.reverseOrder())).collect(Collectors.toList());

        if (collect.size() > 0) {
            //已经进行了排序，因此获取第一个，即为最近一次报告
            List<ProEconEarlyReportWt> reportWindturbineInfoList = collect.get(0).getProEconEarlyReportWtList().stream().filter(s -> s.getCompared() == false && !s.getWindturbineId().equals("全场平均")).collect(Collectors.toList());
            List<ProEconEarlyReportWt> compareWindturbineInfoList = getCompareWindturbineInfo(wt,reportWindturbineInfoList, wt.getModelId(), report.getId(), report.getRelatedParts(), statStartTime, statEndTime);
            report.getProEconEarlyReportWtList().addAll(compareWindturbineInfoList);
        }

        if(!report.getProEconEarlyReportWtList().isEmpty())
        {
            for(ProEconEarlyReportWt earlyReportWt:report.getProEconEarlyReportWtList())
            {
                if(!earlyReportWt.getAlarmInfoList().isEmpty())
                {
                    for(ProEconEarlyReportAlarm earlyReportAlarm: earlyReportWt.getAlarmInfoList())
                    {
                        proEconEarlyReportAlarmService.save(earlyReportAlarm);
                    }
                    proEconEarlyReportWtService.save(earlyReportWt);
                }
            }

        }
        //执行写入操作
        result = proEconEarlyReportService.save(report);
        return result;
    }

    /**
     * @param model         风机型号
     * @param reportId      报告id
     * @param statStartTime 统计起始时间
     * @param statEndTime   统计结束时间
     * @param limit         结果限制数量
     * @return com.gyee.wisdom.alarm.schedule.entity.ProEconEarlyReportWt
     **/
    private List<ProEconEarlyReportWt> getNowWindturbineInfo(ProBasicEquipment wt,String model, String reportId,String relatedParts, Date statStartTime, Date statEndTime, int limit) {

        List<ProEconEarlyReportWt> resultList = new ArrayList<>();

        List<ProEconAlarmRule> alertRule2List = new ArrayList<>();
        List<String> components= new ArrayList<>();
        components.add(relatedParts);
        if(wt.getWindpowerstationId().contains("FDC"))
        {
            List<ProEconAlarmRule> rulels=CacheContext.alarmRulesMap.get(AlarmCustomType.WT.getCode());

            if(!rulels.isEmpty())
            {
                for(ProEconAlarmRule rule:rulels)
                {
                    if(rule.getRelatedParts().equals(relatedParts) && rule.getStationId().equals(wt.getWindpowerstationId())  && rule.getModelId().equals(model))
                    {
                        alertRule2List.add(rule);
                    }
                }
            }

        }else
        {
            List<ProEconAlarmRule> rulels=CacheContext.alarmRulesMap.get(AlarmCustomType.IN.getCode());

            if(!rulels.isEmpty())
            {
                for(ProEconAlarmRule rule:rulels)
                {
                    if(rule.getRelatedParts().equals(relatedParts) && rule.getStationId().equals(wt.getWindpowerstationId())  && rule.getModelId().equals(model))
                    {
                        alertRule2List.add(rule);
                    }
                }
            }
        }


        List<ProEconAlarmInfo> customAlarmSnapList = proEconAlarmInfoService. queryAlarmInfo(wt.getId(), AlarmTypeValue.CT.getCode());
        List<String> alarmidls=new ArrayList<>();
        if(!customAlarmSnapList.isEmpty())
        {
            for(ProEconAlarmInfo alarmInfo:customAlarmSnapList)
            {
                alarmidls.add(alarmInfo.getAlarmId());
            }
        }
        List<ProBasicFeatureStat> customAlarmCountList = alarmTsService.findCtFeatureStatCustom( statStartTime.getTime(), statEndTime.getTime(),wt.getWindpowerstationId(), wt.getModelId(), components,alarmidls);

        Map<String, List<ProBasicFeatureStat>> alarmCountMapByAlertValue = customAlarmCountList.stream().collect(Collectors.groupingBy(ProBasicFeatureStat::getAlarmid));

        ProEconEarlyReportWt avgWindturbineReport = new ProEconEarlyReportWt();
        avgWindturbineReport.setId(String.valueOf(SnowflakeGenerator.generateId()));
        avgWindturbineReport.setWindturbineId("全场平均");
        avgWindturbineReport.setWindturbineCode("");
        avgWindturbineReport.setModelId(model);
        avgWindturbineReport.setCompared(false);
        avgWindturbineReport.setAlarmInfoList(new ArrayList<>());
        avgWindturbineReport.setReportId(reportId);

        for (Map.Entry<String, List<ProBasicFeatureStat>> entry :
                alarmCountMapByAlertValue.entrySet()) {
            ProBasicFeatureStat avgAlarmCount = proEconEarlyReportService.getAvgAlarmCount(entry.getValue());
            Optional<ProEconAlarmRule> first = alertRule2List.stream().filter(s -> String.valueOf(s.getId()).equals(entry.getKey())).findFirst();
            if (first.isPresent()) {
                ProEconEarlyReportAlarm alarmInfo = new ProEconEarlyReportAlarm();
                alarmInfo.setId(String.valueOf(SnowflakeGenerator.generateId()));
                alarmInfo.setRuleId(first.get().getId());
                alarmInfo.setAlarmType("custom");
                alarmInfo.setRuleName(first.get().getName());
                alarmInfo.setRuleDescription(first.get().getDescription());
                alarmInfo.setRank(String.valueOf(first.get().getRank()));
                alarmInfo.setCount(avgAlarmCount.getTotal());
                alarmInfo.setKeepTime(avgAlarmCount.getTotalSeconds());
                alarmInfo.setReportWindturbineId(avgWindturbineReport.getId());

                avgWindturbineReport.getAlarmInfoList().add(alarmInfo);
            }
        }

        resultList.add(avgWindturbineReport);

        Map<String, ProBasicFeatureStat> sumAlarmCountByWindturbine = proEconEarlyReportService.getSumAlarmCountByWindturbine(customAlarmCountList, limit);

        List<String> windturbineIdList = new ArrayList<>(sumAlarmCountByWindturbine.keySet());

        Map<String, List<ProBasicFeatureStat>> windturbineLimitAlarmCountMap = customAlarmCountList.stream().filter(s -> windturbineIdList.contains(s.getWtId())).collect(Collectors.groupingBy(ProBasicFeatureStat::getWtId));

        for (Map.Entry<String, List<ProBasicFeatureStat>> entry :
                windturbineLimitAlarmCountMap.entrySet()) {
            Map<String, Long> sumCountByAlertValue = entry.getValue().stream().collect(Collectors.groupingBy(ProBasicFeatureStat::getAlarmid, Collectors.summingLong(ProBasicFeatureStat::getTotal)));
            Map<String, Double> sumKeepTimeByAlertValue = entry.getValue().stream().collect(Collectors.groupingBy(ProBasicFeatureStat::getAlarmid, Collectors.summingDouble(ProBasicFeatureStat::getTotalSeconds)));

            ProEconEarlyReportWt reportWindturbine = new ProEconEarlyReportWt();
            reportWindturbine.setId(String.valueOf(SnowflakeGenerator.generateId()));
            reportWindturbine.setWindturbineId(entry.getKey());
            reportWindturbine.setWindturbineCode(wt.getNemCode());
            reportWindturbine.setModelId(model);
            reportWindturbine.setCompared(false);
            reportWindturbine.setAlarmInfoList(new ArrayList<>());
            reportWindturbine.setReportId(reportId);;

            for (Map.Entry<String, Long> sumCountEntry :
                    sumCountByAlertValue.entrySet()) {
                Optional<ProEconAlarmRule> first = alertRule2List.stream().filter(s -> String.valueOf(s.getId()).equals(sumCountEntry.getKey())).findFirst();
                if (first.isPresent()) {
                    ProEconEarlyReportAlarm alarmInfo = new ProEconEarlyReportAlarm();
                    alarmInfo.setId(String.valueOf(SnowflakeGenerator.generateId()));
                    alarmInfo.setRuleId(first.get().getId());
                    alarmInfo.setAlarmType("custom");
                    alarmInfo.setRuleName(first.get().getName());
                    alarmInfo.setRuleDescription(first.get().getDescription());
                    alarmInfo.setRank(String.valueOf(first.get().getRank()));
                    alarmInfo.setCount(sumCountEntry.getValue());
                    alarmInfo.setReportWindturbineId(reportWindturbine.getId());;
                    if (sumKeepTimeByAlertValue.containsKey(sumCountEntry.getKey())) {
                        alarmInfo.setKeepTime(sumKeepTimeByAlertValue.get(sumCountEntry.getKey()).doubleValue());
                    }
                    reportWindturbine.getAlarmInfoList().add(alarmInfo);
                }
            }
            resultList.add(reportWindturbine);
        }

        List<ProEconEarlyReportWt> earlyReportWindturbineList = checkAndFixData(resultList, alertRule2List);

        return getConclusion(alertRule2List, earlyReportWindturbineList);

    }



    /**
     * @param
     * @param windturbineInfoList 需要对比的风机报告信息（上次报告）
     * @param model               风机型号
     * @param reportId            本次报告的id
     * @param startTime           分析起始时间（本次分析）
     * @param endTime             分析结束时间（本次分析）
     * @return com.gyee.wisdom.alarm.schedule.entity.ProEconEarlyReportWt
     * @author Wanghs
     * @description 获取上次报告中的风机与本周报告对比信息
     * @date 2022/8/29
     **/

    private List<ProEconEarlyReportWt> getCompareWindturbineInfo(ProBasicEquipment wt,List<ProEconEarlyReportWt> windturbineInfoList, String model, String reportId,String relatedParts, Date startTime, Date endTime) {

        List<ProEconEarlyReportWt> resultList = new ArrayList<>();
        List<String> windturbineIdList = windturbineInfoList.stream().map(s -> s.getWindturbineId()).collect(Collectors.toList());
        List<ProEconEarlyReportAlarm> reportAlarmList = new ArrayList<>();
        windturbineInfoList.forEach(s -> {
            reportAlarmList.addAll(s.getAlarmInfoList());
        });
//        List<ProEconAlarmRule> alertRule2List = proEconEarlyReportService.getAlertRule2(reportAlarmList.stream().map(s -> s.getRuleId()).collect(Collectors.toList()));
//        List<AlarmSnap> customAlarmSnapList = proEconEarlyReportService.getAlarmSnap(stationId,alertRule2List.stream().map(s -> s.getEdnaValue()).collect(Collectors.toList()), "custom");
//        List<ProBasicFeatureStat> customAlarmCountList = proEconEarlyReportService.getAlarmCount(customAlarmSnapList.stream().map(s -> s.getId()).collect(Collectors.toList()), startTime, endTime);
//        Map<String, List<ProBasicFeatureStat>> windturbineAlarmCountMap = customAlarmCountList.stream().filter(s -> windturbineIdList.contains(s.getWindturbineId())).collect(Collectors.groupingBy(ProBasicFeatureStat::getWindturbineId));
//


        List<ProEconAlarmRule> alertRule2List = new ArrayList<>();
        List<String> components= new ArrayList<>();
        components.add(relatedParts);
        if(wt.getWindpowerstationId().contains("FDC"))
        {
            List<ProEconAlarmRule> rulels=CacheContext.alarmRulesMap.get(AlarmCustomType.WT.getCode());

            if(!rulels.isEmpty())
            {
                for(ProEconAlarmRule rule:rulels)
                {
                    if(rule.getRelatedParts().equals(relatedParts) && rule.getStationId().equals(wt.getWindpowerstationId())  && rule.getModelId().equals(model))
                    {
                        alertRule2List.add(rule);
                    }
                }
            }

        }else
        {
            List<ProEconAlarmRule> rulels=CacheContext.alarmRulesMap.get(AlarmCustomType.IN.getCode());

            if(!rulels.isEmpty())
            {
                for(ProEconAlarmRule rule:rulels)
                {
                    if(rule.getRelatedParts().equals(relatedParts) && rule.getStationId().equals(wt.getWindpowerstationId())  && rule.getModelId().equals(model))
                    {
                        alertRule2List.add(rule);
                    }
                }
            }
        }


        List<ProEconAlarmInfo> customAlarmSnapList = proEconAlarmInfoService. queryAlarmInfo(wt.getId(), AlarmTypeValue.CT.getCode());
        List<String> alarmidls=new ArrayList<>();
        if(!customAlarmSnapList.isEmpty())
        {
            for(ProEconAlarmInfo alarmInfo:customAlarmSnapList)
            {
                alarmidls.add(alarmInfo.getAlarmId());
            }
        }
        List<ProBasicFeatureStat> customAlarmCountList = alarmTsService.findCtFeatureStatCustom( startTime.getTime(), endTime.getTime(),wt.getWindpowerstationId(), wt.getModelId(), components,alarmidls);


       Map<String, List<ProBasicFeatureStat>> windturbineAlarmCountMap = customAlarmCountList.stream().filter(s -> windturbineIdList.contains(s.getWtId())).collect(Collectors.groupingBy(ProBasicFeatureStat::getWtId));


        for (Map.Entry<String, List<ProBasicFeatureStat>> entry :
                windturbineAlarmCountMap.entrySet()) {
            ProEconEarlyReportWt reportWindturbine = new ProEconEarlyReportWt();
            reportWindturbine.setId(String.valueOf(SnowflakeGenerator.generateId()));
            reportWindturbine.setWindturbineId(entry.getKey());
            reportWindturbine.setWindturbineCode(wt.getNemCode());
            reportWindturbine.setModelId(model);
            reportWindturbine.setCompared(true);
            reportWindturbine.setAlarmInfoList(new ArrayList<>());
            reportWindturbine.setReportId(reportId);

            Optional<ProEconEarlyReportWt> oldReportWindturbine = windturbineInfoList.stream().filter(s -> s.getWindturbineId().equals(entry.getKey())).findFirst();
            if (oldReportWindturbine.isPresent())
                reportWindturbine.setCompareId(oldReportWindturbine.get().getId());

            Map<String, Long> sumCountByAlertValue = entry.getValue().stream().collect(Collectors.groupingBy(ProBasicFeatureStat::getAlarmid, Collectors.summingLong(ProBasicFeatureStat::getTotal)));
            Map<String, Double> sumKeepTimeByAlertValue = entry.getValue().stream().collect(Collectors.groupingBy(ProBasicFeatureStat::getAlarmid, Collectors.summingDouble(ProBasicFeatureStat::getTotalSeconds)));

            for (Map.Entry<String, Long> sumCountEntry :
                    sumCountByAlertValue.entrySet()) {
                Optional<ProEconAlarmRule> first = alertRule2List.stream().filter(s -> String.valueOf(s.getId()).equals(sumCountEntry.getKey())).findFirst();
                if (first.isPresent()) {
                    ProEconEarlyReportAlarm alarmInfo = new ProEconEarlyReportAlarm();
                    alarmInfo.setId(String.valueOf(SnowflakeGenerator.generateId()));
                    alarmInfo.setRuleId(first.get().getId());
                    alarmInfo.setAlarmType("custom");
                    alarmInfo.setRuleName(first.get().getName());
                    alarmInfo.setRuleDescription(first.get().getDescription());
                    alarmInfo.setRank(String.valueOf(first.get().getRank()));
                    alarmInfo.setCount(sumCountEntry.getValue());
                    alarmInfo.setReportWindturbineId(reportWindturbine.getId());
                    if (sumKeepTimeByAlertValue.containsKey(sumCountEntry.getKey())) {
                        alarmInfo.setKeepTime(sumKeepTimeByAlertValue.get(sumCountEntry.getKey()).doubleValue());
                    }
                    reportWindturbine.getAlarmInfoList().add(alarmInfo);
                }
            }
            resultList.add(reportWindturbine);
        }

        List<ProEconEarlyReportWt> earlyReportWindturbineList = checkAndFixData(resultList, alertRule2List);
        return earlyReportWindturbineList;
    }

    private List<ProEconEarlyReportWt> getConclusion(List<ProEconAlarmRule> alertRule2List, List<ProEconEarlyReportWt> reportWindturbineList) {

        List<ProEconEarlyReportConclusio> listByPart = proEconEarlyReportConclusioService.getListByPart(alertRule2List.stream().map(s -> s.getRelatedParts()).collect(Collectors.toList()));

        for (ProEconEarlyReportWt windturbineData :
                reportWindturbineList) {
            if (!windturbineData.getWindturbineId().equals("全场平均")) {
                StringBuilder firstStr = new StringBuilder("风机" + windturbineData.getWindturbineCode() + ":发生");
                for (int i = 0; i < windturbineData.getAlarmInfoList().size(); i++) {
                    ProEconEarlyReportAlarm earlyReportAlarm = windturbineData.getAlarmInfoList().get(i);
                    firstStr.append(earlyReportAlarm.getRuleName());
                    firstStr.append(earlyReportAlarm.getCount());
                    firstStr.append("次，总持续时长为");
                    firstStr.append(earlyReportAlarm.getKeepTime() + "分钟；");
                }
                StringBuilder secondStr = new StringBuilder("可能存在的隐患:").append("\n");
                StringBuilder thridStr = new StringBuilder("建议排查:").append("\n");

                for (int i = 0; i < listByPart.size(); i++) {
                    secondStr.append("\b");
                    secondStr.append("\b");
                    secondStr.append("\b");
                    secondStr.append("-" + listByPart.get(i).getHiddanger());
                    secondStr.append("\n");

                    thridStr.append("\b");
                    thridStr.append("\b");
                    thridStr.append("\b");
                    thridStr.append("-" + listByPart.get(i).getCheckMatter());
                    thridStr.append("\n");
                }
                StringBuilder resultStr = new StringBuilder();

                resultStr.append(firstStr);
                resultStr.append("\n");
                resultStr.append(secondStr);
                resultStr.append("\n");
                resultStr.append(thridStr);
                windturbineData.setConclusion(resultStr.toString());
            }
        }
        return reportWindturbineList;

    }

    //数据校验，避免部分规则没有数据的情况
    public List<ProEconEarlyReportWt> checkAndFixData(List<ProEconEarlyReportWt> earlyReportWindturbineList,List<ProEconAlarmRule> rule2List){

        List<String> ruleIdList = rule2List.stream().map(s -> s.getId()).collect(Collectors.toList());
        earlyReportWindturbineList.forEach(s -> {
//            if (!s.getWindturbineId().equals("全场平均") ) {
            List<ProEconEarlyReportAlarm> alarmInfoList = s.getAlarmInfoList();
            if (alarmInfoList.size() < rule2List.size()) {

                Collection subtract = CollectionUtils.subtract(ruleIdList, alarmInfoList.stream().map(k -> k.getRuleId()).collect(Collectors.toList()));

                List<String> arrayList = new ArrayList<String>(subtract);

                if (arrayList.size() > 0) {
                    arrayList.stream().forEach(l -> {

                        Optional<ProEconAlarmRule> first = rule2List.stream().filter(m -> m.getId().equals(l)).findFirst();
                        if (first.isPresent()) {
                            ProEconEarlyReportAlarm alarmInfo = new ProEconEarlyReportAlarm();
                            alarmInfo.setId(String.valueOf(SnowflakeGenerator.generateId()));
                            alarmInfo.setRuleId(first.get().getId());
                            alarmInfo.setAlarmType("custom");
                            alarmInfo.setRuleName(first.get().getName());
                            alarmInfo.setRuleDescription(first.get().getDescription());
                            alarmInfo.setRank(String.valueOf(first.get().getRank()));
                            alarmInfo.setCount(0l);
                            alarmInfo.setKeepTime(0.0);
                            alarmInfo.setReportWindturbineId(s.getId());
                            alarmInfoList.add(alarmInfo);
                        }

                    });
                }


            }
//            }

        });

        return earlyReportWindturbineList;
    }
}