package com.jiayue.ipfcst.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.repository.*;
import com.jiayue.ipfcst.common.data.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 历史预测短期业务层
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/5 16:02
 */
@Service
@Slf4j
public class ComprehensiveContrastService extends BaseService {
  private final ForecastPowerUltraShortTermHisRepository forecastPowerUltraShortTermHisRepository;
  private final ForecastPowerShortTermHisRepository forecastPowerShortTermHisRepository;
  private final WeatherStationStatusDataRepository weatherStationStatusDataRepository;
  private final WindTowerStatusDataRepository windTowerStatusDataRepository;
  private final PowerStationStatusDataRepository powerStationStatusDataRepository;
  private final ForecastPowerUltraShortTermHisService forecastPowerUltraShortTermHisService;
  private final ForecastPowerShortTermHisService forecastPowerShortTermHisService;

  private final ForecastPowerUltraShortTermOneHisService forecastPowerUltraShortTermOneHisService;
  private final ForecastPowerShortTermOneHisService forecastPowerShortTermOneHisService;

  private final PowerStationStatusDataService powerStationStatusDataService;
  private final PowerStationStatusDataOneService powerStationStatusDataOneService;
  private final WeatherStationStatusDataService weatherStationStatusDataService;
  private final WindTowerStatusDataService windTowerStatusDataService;
  private final DisplayConfigService displayConfigService;
  private final NwpHisService nwpHisService;
  private final NwpService nwpService;
  private final NwpOneService nwpOneService;
  private final SysParameterRepository sysParameterRepository;

  @Autowired
  public ComprehensiveContrastService(ForecastPowerUltraShortTermHisService forecastPowerUltraShortTermHisService,
                                      ForecastPowerShortTermHisService forecastPowerShortTermHisService,
                                      PowerStationStatusDataService powerStationStatusDataService,
                                      WeatherStationStatusDataService weatherStationStatusDataService,
                                      WindTowerStatusDataService windTowerStatusDataService,
                                      DisplayConfigService displayConfigService,
                                      NwpHisService nwpHisService,
                                      SysParameterRepository sysParameterRepository,
                                      PowerStationStatusDataRepository powerStationStatusDataRepository,
                                      ForecastPowerUltraShortTermHisRepository forecastPowerUltraShortTermHisRepository,
                                      ForecastPowerShortTermHisRepository forecastPowerShortTermHisRepository,
                                      WeatherStationStatusDataRepository weatherStationStatusDataRepository,
                                      WindTowerStatusDataRepository windTowerStatusDataRepository, ForecastPowerUltraShortTermOneHisService forecastPowerUltraShortTermOneHisService, ForecastPowerShortTermOneHisService forecastPowerShortTermOneHisService, PowerStationStatusDataOneService powerStationStatusDataOneService, NwpService nwpService, NwpOneService nwpOneService) {
    this.forecastPowerUltraShortTermHisService = forecastPowerUltraShortTermHisService;
    this.forecastPowerShortTermHisService = forecastPowerShortTermHisService;
    this.powerStationStatusDataService = powerStationStatusDataService;
    this.weatherStationStatusDataService = weatherStationStatusDataService;
    this.windTowerStatusDataService = windTowerStatusDataService;
    this.displayConfigService = displayConfigService;
    this.nwpHisService = nwpHisService;
    this.sysParameterRepository = sysParameterRepository;
    this.powerStationStatusDataRepository = powerStationStatusDataRepository;
    this.forecastPowerShortTermHisRepository = forecastPowerShortTermHisRepository;
    this.forecastPowerUltraShortTermHisRepository = forecastPowerUltraShortTermHisRepository;
    this.weatherStationStatusDataRepository = weatherStationStatusDataRepository;
    this.windTowerStatusDataRepository = windTowerStatusDataRepository;

    this.forecastPowerUltraShortTermOneHisService = forecastPowerUltraShortTermOneHisService;
    this.forecastPowerShortTermOneHisService = forecastPowerShortTermOneHisService;
    this.powerStationStatusDataOneService = powerStationStatusDataOneService;
    this.nwpService = nwpService;
    this.nwpOneService = nwpOneService;
  }


  public Map<String, Object> comprehensiveContrast(Long startTime,
                                                   Long endTime,
                                                   Integer no,
                                                   String otherType,
                                                   Integer dqAgo,
                                                   Integer cdqAgo,
                                                   Integer nwpAgo,
                                                   Long jgTime) throws BusinessException {

    Map<String, Object> map = new HashMap<>();
    Map<String, Object> dqMap;
    Map<String, Object> cdqMap;
    Map<String, Object> rpMap;
    List<WeatherStationStatusData> weatherStationStatusDataList;
    List<WindTowerStatusData> windTowerStatusDataList;
    List<String> times;
    List<Map<String, Object>> tableResult = new ArrayList<>();

    //预测数据
    if (otherType.equals("E1")){
      // 光伏
      dqMap = forecastPowerShortTermHisService.findByForecastTimeBetweenAndForecastHowLongAgoOrderByForecastTimeForDB(startTime, endTime, dqAgo, jgTime);
      cdqMap = forecastPowerUltraShortTermHisService.findByForecastTimeBetweenAndForecastHowLongAgoOrderByForecastTimeForDB(startTime, endTime, cdqAgo, jgTime);
    }
    else{
      // 风电
      dqMap = forecastPowerShortTermOneHisService.findByForecastTimeBetweenAndForecastHowLongAgoOrderByForecastTimeForDB(startTime, endTime, dqAgo, jgTime);
      cdqMap = forecastPowerUltraShortTermOneHisService.findByForecastTimeBetweenAndForecastHowLongAgoOrderByForecastTimeForDB(startTime, endTime, cdqAgo, jgTime);
    }
    List<Float> dqList = (List<Float>) dqMap.get("datas");
    List<Float> cdqList = (List<Float>) cdqMap.get("datas");


    if (otherType.equals("E1")){
      // 光伏
      rpMap = powerStationStatusDataService.findByTimeBetweenForContrast(new Date(startTime), new Date(endTime), jgTime);
    }
    else{
      // 风电
      rpMap = powerStationStatusDataOneService.findByTimeBetweenForContrast(new Date(startTime), new Date(endTime), jgTime);
    }

    //实时数据
    times = (List<String>) rpMap.get("times");
    List<Float> realValueList = (List<Float>) rpMap.get("realDatas");
    List<Float> ableValueList = (List<Float>) rpMap.get("ableDatas");
    List<Float> theoryValueList = (List<Float>) rpMap.get("theoryDatas");
    List<Float> referencePowerByMeasuringList = (List<Float>) rpMap.get("referencePowerByMeasuringDatas");
    List<Float> referencePowerBySampleList = (List<Float>) rpMap.get("referencePowerBySampleDatas");


    List<Map<String, Object>> resultList = new ArrayList<>();

    String displayKyLl = "0";
    SysParameter sysParameter = sysParameterRepository.findBySysKeyEquals("DISPLAY_KY_LL");

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    if (otherType.equals("E2")) {
      //风电对比数据
      windTowerStatusDataList = windTowerStatusDataService.findByEquipmentNoAndTimeBetweenOrderByTimeAsc(new Date(startTime), new Date(endTime), no, jgTime);

      DisplayConfig displayConfig = displayConfigService.findByDisplayCode("windTowerInfos");
      for (int i = 0; i < times.size(); i++) {
        Map<String, Object> resultMap = new HashMap<>();

        Map<String, Object> dtoMap = new HashMap<>();
        //
        String[] showCode = displayConfig.getShowCode().split(",");
        dtoMap.put("time", times.get(i));

        if (Arrays.asList(showCode).contains("realValue")) {
          if (realValueList.get(i) != null) {
            dtoMap.put("realValue", realValueList.get(i));
          } else {
            dtoMap.put("realValue", "");
          }

          if (sysParameter != null && sysParameter.getSysValue().equals("1")) {
            displayKyLl = "1";

            if (ableValueList.get(i) != null) {
              dtoMap.put("ableValue", ableValueList.get(i));
            } else {
              dtoMap.put("ableValue", "");
            }

            if (theoryValueList.get(i) != null) {
              dtoMap.put("theoryValue", theoryValueList.get(i));
            } else {
              dtoMap.put("theoryValue", "");
            }

            if (referencePowerByMeasuringList.get(i) != null) {
              dtoMap.put("referencePowerByMeasuring", referencePowerByMeasuringList.get(i));
            } else {
              dtoMap.put("referencePowerByMeasuring", "");
            }

            if (referencePowerBySampleList.get(i) != null) {
              dtoMap.put("referencePowerBySample", referencePowerBySampleList.get(i));
            } else {
              dtoMap.put("referencePowerBySample", "");
            }

          }
        }

        if (Arrays.asList(showCode).contains("dq")) {
          if (dqList.get(i) != null) {
            dtoMap.put("dq", dqList.get(i));
          } else {
            dtoMap.put("dq", "");
          }
        }
        if (Arrays.asList(showCode).contains("cdq")) {
          if (cdqList.get(i) != null) {
            dtoMap.put("cdq", cdqList.get(i));
          } else {
            dtoMap.put("cdq", "");
          }
        }

        tableResult.add(dtoMap);
        final String time = times.get(i);
        boolean b = windTowerStatusDataList.stream().filter(m -> sdf.format(m.getTime()).equals(time)).findAny().isPresent();
        if (b) {
          WindTowerStatusData windTowerStatusData = windTowerStatusDataList.stream().filter(o -> sdf.format(o.getTime()).equals(time)).findAny().orElse(null);
          if (Arrays.asList(showCode).contains("wsInst10")) {
            //十米
            resultMap.put("wsInst10", windTowerStatusData.getWsInst10().floatValue() == -99 ? "" : windTowerStatusData.getWsInst10());
          }
          if (Arrays.asList(showCode).contains("wsInst30")) {
            //三十米
            resultMap.put("wsInst30", windTowerStatusData.getWsInst30().floatValue() == -99 ? "" : windTowerStatusData.getWsInst30());
          }
          if (Arrays.asList(showCode).contains("wsInst50")) {
            //五十米
            resultMap.put("wsInst50", windTowerStatusData.getWsInst50().floatValue() == -99 ? "" : windTowerStatusData.getWsInst50());
          }
          if (Arrays.asList(showCode).contains("wsInst60")) {
            //六十米
            resultMap.put("wsInst60", windTowerStatusData.getWsInst60().floatValue() == -99 ? "" : windTowerStatusData.getWsInst60());
          }
          if (Arrays.asList(showCode).contains("wsInst70")) {
            //七十米
            resultMap.put("wsInst70", windTowerStatusData.getWsInst70().floatValue() == -99 ? "" : windTowerStatusData.getWsInst70());
          }
          if (Arrays.asList(showCode).contains("wsInst80")) {
            //八十米
            resultMap.put("wsInst80", windTowerStatusData.getWsInst80().floatValue() == -99 ? "" : windTowerStatusData.getWsInst80());
          }
          if (Arrays.asList(showCode).contains("wsInst90")) {
            //九十米
            resultMap.put("wsInst90", windTowerStatusData.getWsInst90().floatValue() == -99 ? "" : windTowerStatusData.getWsInst90());
          }
          if (Arrays.asList(showCode).contains("wsInst100")) {
            //一百米
            resultMap.put("wsInst100", windTowerStatusData.getWsInst100().floatValue() == -99 ? "" : windTowerStatusData.getWsInst100());
          }
          if (Arrays.asList(showCode).contains("wsInst110")) {
            //一百一十米
            resultMap.put("wsInst110", windTowerStatusData.getWsInst110().floatValue() == -99 ? "" : windTowerStatusData.getWsInst110());
          }
          if (Arrays.asList(showCode).contains("wsInst120")) {
            //一百二十米
            resultMap.put("wsInst120", windTowerStatusData.getWsInst120().floatValue() == -99 ? "" : windTowerStatusData.getWsInst120());
          }

        } else {
          if (Arrays.asList(showCode).contains("wsInst10")) {
            //十米
            resultMap.put("wsInst10", "");
          }
          if (Arrays.asList(showCode).contains("wsInst30")) {
            //三十米
            resultMap.put("wsInst30", "");
          }
          if (Arrays.asList(showCode).contains("wsInst50")) {
            //五十米
            resultMap.put("wsInst50", "");
          }
          if (Arrays.asList(showCode).contains("wsInst60")) {
            //一百一十米
            resultMap.put("wsInst60", "");
          }
          if (Arrays.asList(showCode).contains("wsInst70")) {
            //七十米
            resultMap.put("wsInst70", "");
          }
          if (Arrays.asList(showCode).contains("wsInst80")) {
            //八十米
            resultMap.put("wsInst80", "");
          }
          if (Arrays.asList(showCode).contains("wsInst90")) {
            //九十米
            resultMap.put("wsInst90", "");
          }
          if (Arrays.asList(showCode).contains("wsInst100")) {
            //一百米
            resultMap.put("wsInst100", "");
          }
          if (Arrays.asList(showCode).contains("wsInst110")) {
            //一百一十米
            resultMap.put("wsInst110", "");
          }
          if (Arrays.asList(showCode).contains("wsInst120")) {
            //一百二十米
            resultMap.put("wsInst120", "");
          }
        }
        List<NwpOne> nwpHisList = nwpOneService.findByPreTimeBetween(startTime, endTime, jgTime);
        boolean nb = nwpHisList.stream().filter(m -> sdf.format(m.getPreTime()).equals(time)).findAny().isPresent();
        if (nb) {
          NwpOne nwpHis = nwpHisList.stream().filter(o -> sdf.format(o.getPreTime()).equals(time)).findAny().orElse(null);
          if (Arrays.asList(showCode).contains("ws10")) {
            //十米
            resultMap.put("ws10", nwpHis.getWs10().floatValue() == -99 ? "" : nwpHis.getWs10());
          }
          if (Arrays.asList(showCode).contains("ws30")) {
            //三十米
            resultMap.put("ws30", nwpHis.getWs30().floatValue() == -99 ? "" : nwpHis.getWs30());
          }
          if (Arrays.asList(showCode).contains("ws50")) {
            //五十米
            resultMap.put("ws50", nwpHis.getWs50().floatValue() == -99 ? "" : nwpHis.getWs50());
          }
          if (Arrays.asList(showCode).contains("ws70")) {
            //七十米
            resultMap.put("ws70", nwpHis.getWs70().floatValue() == -99 ? "" : nwpHis.getWs70());
          }
          if (Arrays.asList(showCode).contains("ws80")) {
            //八十米
            resultMap.put("ws80", nwpHis.getWs80().floatValue() == -99 ? "" : nwpHis.getWs80());
          }
          if (Arrays.asList(showCode).contains("ws90")) {
            //九十米
            resultMap.put("ws90", nwpHis.getWs90().floatValue() == -99 ? "" : nwpHis.getWs90());
          }
          if (Arrays.asList(showCode).contains("ws100")) {
            //一百米
            resultMap.put("ws100", nwpHis.getWs100().floatValue() == -99 ? "" : nwpHis.getWs100());
          }
          if (Arrays.asList(showCode).contains("ws170")) {
            //一百一十米
            resultMap.put("ws170", nwpHis.getWs170().floatValue() == -99 ? "" : nwpHis.getWs170());
          }
        } else {
          if (Arrays.asList(showCode).contains("ws10")) {
            //十米
            resultMap.put("ws10", "");
          }
          if (Arrays.asList(showCode).contains("ws30")) {
            //三十米
            resultMap.put("ws30", "");
          }
          if (Arrays.asList(showCode).contains("ws50")) {
            //五十米
            resultMap.put("ws50", "");
          }
          if (Arrays.asList(showCode).contains("ws70")) {
            //七十米
            resultMap.put("ws70", "");
          }
          if (Arrays.asList(showCode).contains("ws80")) {
            //八十米
            resultMap.put("ws80", "");
          }
          if (Arrays.asList(showCode).contains("ws90")) {
            //九十米
            resultMap.put("ws90", "");
          }
          if (Arrays.asList(showCode).contains("ws100")) {
            //一百米
            resultMap.put("ws100", "");
          }
          if (Arrays.asList(showCode).contains("ws170")) {
            //一百七十米
            resultMap.put("ws170", "");
          }
        }

        resultList.add(resultMap);
      }
    } else {
      //光伏对比数据
      weatherStationStatusDataList = weatherStationStatusDataService.findByTimeBetweenAndEquipmentNoForContrastData(new Date(startTime), new Date(endTime), no, jgTime);
      DisplayConfig displayConfig = displayConfigService.findByDisplayCode("weatherStationInfos");
      if (displayConfig != null) {
        for (int i = 0; i < times.size(); i++) {
          Map<String, Object> resultMap = new HashMap<>();
          Map<String, Object> dtoMap = new HashMap<>();
          String[] showCode = displayConfig.getShowCode().split(",");
          dtoMap.put("time", times.get(i));
          if (Arrays.asList(showCode).contains("realValue")) {
            if (realValueList.get(i) != null) {
              dtoMap.put("realValue", realValueList.get(i));
            } else {
              dtoMap.put("realValue", "");
            }

            if (sysParameter != null && sysParameter.getSysValue().equals("1")) {
              displayKyLl = "1";

              if (ableValueList.get(i) != null) {
                dtoMap.put("ableValue", ableValueList.get(i));
              } else {
                dtoMap.put("ableValue", "");
              }

              if (theoryValueList.get(i) != null) {
                dtoMap.put("theoryValue", theoryValueList.get(i));
              } else {
                dtoMap.put("theoryValue", "");
              }

              if (referencePowerByMeasuringList.get(i) != null) {
                dtoMap.put("referencePowerByMeasuring", referencePowerByMeasuringList.get(i));
              } else {
                dtoMap.put("referencePowerByMeasuring", "");
              }

              if (referencePowerBySampleList.get(i) != null) {
                dtoMap.put("referencePowerBySample", referencePowerBySampleList.get(i));
              } else {
                dtoMap.put("referencePowerBySample", "");
              }

            }
          }

          if (Arrays.asList(showCode).contains("dq")) {
            if (dqList.get(i) != null) {
              dtoMap.put("dq", dqList.get(i));
            } else {
              dtoMap.put("dq", "");
            }
          }
          if (Arrays.asList(showCode).contains("cdq")) {
            if (cdqList.get(i) != null) {
              dtoMap.put("cdq", cdqList.get(i));
            } else {
              dtoMap.put("cdq", "");
            }
          }

          tableResult.add(dtoMap);
          final String time = times.get(i);
          //判断该时间点是否有值
          boolean b = weatherStationStatusDataList.stream().filter(m -> sdf.format(m.getTime()).equals(time)).findAny().isPresent();
          if (b) {
            WeatherStationStatusData weatherStationStatusData = weatherStationStatusDataList.stream().filter(o -> sdf.format(o.getTime()).equals(time)).findAny().orElse(null);

            resultMap.put("time", times.get(i));
            //判断配置项中是否存在该项，存在时存入集合中
            if (Arrays.asList(showCode).contains("globalR")) {
              //总辐射
              resultMap.put("globalR", weatherStationStatusData.getGlobalR().floatValue() == -99 ? "" : weatherStationStatusData.getGlobalR());
            }
            if (Arrays.asList(showCode).contains("directR")) {
              //直辐射
              resultMap.put("directR", weatherStationStatusData.getDirectR().floatValue() == -99 ? "" : weatherStationStatusData.getDirectR());
            }
            if (Arrays.asList(showCode).contains("diffuseR")) {
              //散辐射
              resultMap.put("diffuseR", weatherStationStatusData.getDiffuseR().floatValue() == -99 ? "" : weatherStationStatusData.getDiffuseR());
            }
            if (Arrays.asList(showCode).contains("obliqueR")) {
              //斜辐射
              resultMap.put("obliqueR", weatherStationStatusData.getObliqueR().floatValue() == -99 ? "" : weatherStationStatusData.getObliqueR());
            }
          } else {
            resultMap.put("time", times.get(i));
            if (Arrays.asList(showCode).contains("globalR")) {
              //总辐射
              resultMap.put("globalR", "");
            }
            if (Arrays.asList(showCode).contains("directR")) {
              //直辐射
              resultMap.put("directR", "");
            }
            if (Arrays.asList(showCode).contains("diffuseR")) {
              //散辐射
              resultMap.put("diffuseR", "");
            }
            if (Arrays.asList(showCode).contains("obliqueR")) {
              //斜辐射
              resultMap.put("obliqueR", "");
            }
          }
          List<Nwp> nwpHisList = nwpService.findByPreTimeBetween(startTime, endTime, jgTime);
          boolean nb = nwpHisList.stream().filter(m -> sdf.format(m.getPreTime()).equals(time)).findAny().isPresent();
          if (nb) {
            Nwp nwpHis = nwpHisList.stream().filter(o -> sdf.format(o.getPreTime()).equals(time)).findAny().orElse(null);
            if (Arrays.asList(showCode).contains("swr")) {
              //辐射
              resultMap.put("swr", nwpHis.getSwr().floatValue() == -99 ? "" : nwpHis.getSwr());
            }
            if (Arrays.asList(showCode).contains("directRadiation")) {
              //直辐射
              resultMap.put("directRadiation", nwpHis.getDirectRadiation().floatValue() == -99 ? "" : nwpHis.getDirectRadiation());
            }
            if (Arrays.asList(showCode).contains("diffuseRadiation")) {
              //散辐射
              resultMap.put("diffuseRadiation", nwpHis.getDiffuseRadiation().floatValue() == -99 ? "" : nwpHis.getDiffuseRadiation());
            }

          } else {
            if (Arrays.asList(showCode).contains("swr")) {
              //辐射
              resultMap.put("swr", "");
            }
            if (Arrays.asList(showCode).contains("directRadiation")) {
              //直辐射
              resultMap.put("directRadiation", "");
            }
            if (Arrays.asList(showCode).contains("diffuseRadiation")) {
              //散辐射
              resultMap.put("diffuseRadiation", "");
            }
          }
          resultList.add(resultMap);
        }

      }
    }

    map.put("tableResult", tableResult);
    map.put("resultList", resultList);
    map.put("displayKyLl", displayKyLl);
    return map;

  }

  /**
   * 根据提前多久，开始结束时间查询历史超短期 yh
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @param ago       提前多久预测
   * @return 结果集
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public Map<String, Object> forecastPowerShortTermHisComprehensiveContrast(Long startTime, Long endTime, Integer ago) {
    Map<String, Object> map = new HashMap<>();
    List<ForecastPowerShortTermHis> list = forecastPowerShortTermHisRepository.findByForecastTimeBetweenAndForecastHowLongAgo(startTime, endTime, ago);
    Collections.sort(list, Comparator.comparing(ForecastPowerShortTermHis::getForecastTime));
    long timeStep = 900000L;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    List<String> times = new ArrayList<>();
    List<Float> datas = new ArrayList<>();
    if (startTime % timeStep != 0) {
      startTime = startTime - (startTime % timeStep);
    }
    if (list != null && list.size() > 0) {
      for (Long i = startTime; i < endTime; i = i + timeStep) {
        long finalI = i;
        List<ForecastPowerShortTermHis> filterList = list.stream().filter(t -> t.getForecastTime() == finalI).collect(Collectors.toList());
        if (filterList != null && filterList.size() > 0) {
          datas.add(filterList.get(0).getAbleValue().floatValue());
        } else {
          datas.add(null);
        }

        times.add(sdf.format(new Date(finalI)));
      }
    }
    map.put("times", times);
    map.put("datas", datas);

    return map;
  }


  /**
   * 根据提前多久，开始结束时间查询历史超短期 yh
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @param ago       提前多久预测
   * @return 结果集
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public Map<String, Object> forecastPowerUltraShortTermHisComprehensiveContrast(Long startTime, Long endTime, Integer ago) {
    Map<String, Object> map = new HashMap<>();
    List<ForecastPowerUltraShortTermHis> list = forecastPowerUltraShortTermHisRepository.findByForecastTimeBetweenAndForecastHowLongAgo(startTime, endTime, ago);
    list.sort(Comparator.comparing(ForecastPowerUltraShortTermHis::getForecastTime));
    long timeStep = 900000L;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    List<String> times = new ArrayList<>();
    List<Float> datas = new ArrayList<>();
    if (startTime % timeStep != 0) {
      startTime = startTime - (startTime % timeStep);
    }
    if (list != null && list.size() > 0) {
      for (Long i = startTime; i < endTime; i = i + timeStep) {
        long finalI = i;
        List<ForecastPowerUltraShortTermHis> filterList = list.stream().filter(t -> t.getForecastTime() == finalI).collect(Collectors.toList());
        if (filterList != null && filterList.size() > 0) {
          datas.add(filterList.get(0).getAbleValue().floatValue());
        } else {
          datas.add(null);
        }

        times.add(sdf.format(new Date(finalI)));
      }
    }
    map.put("times", times);
    map.put("datas", datas);

    return map;

  }

  /**
   * 根据开始时间和结束时间查询 场站功率状态 yh
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @return 结果集
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public Map<String, Object> powerStationStatusDataComprehensiveContrast(Date startTime, Date endTime) {
    Map<String, Object> map = new HashMap<>();
    List<PowerStationStatusData> list = new ArrayList<>();

    list = powerStationStatusDataRepository.findByTimeBetween(startTime, endTime);


    long startTimeLong = startTime.getTime();
    long endTimeLong = endTime.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    long timeStep = 900000L;
    if (startTimeLong % timeStep != 0) {
      startTimeLong = startTimeLong - (startTimeLong % timeStep);
    }
    List<Float> realDatas = new ArrayList<>();
    List<Float> ableDatas = new ArrayList<>();
    List<Float> theoryDatas = new ArrayList<>();
    List<String> times = new ArrayList<>();

    for (long i = startTimeLong; i < endTimeLong; i = i + 300000L) {
      long finalI = i;
      List<PowerStationStatusData> p = list.stream().filter(t -> t.getTime().getTime() == finalI).collect(Collectors.toList());
      if (p != null && p.size() > 0) {

        if (p.get(0).getRealValue().compareTo(new BigDecimal(-99)) == 0) {
          realDatas.add(null);
        } else {
          realDatas.add(p.get(0).getRealValue().floatValue());
        }

        if (p.get(0).getAbleValue().compareTo(new BigDecimal(-99)) == 0) {
          ableDatas.add(null);
        } else {
          ableDatas.add(p.get(0).getRealValue().floatValue());
        }

        if (p.get(0).getTheoryValue().compareTo(new BigDecimal(-99)) == 0) {
          theoryDatas.add(null);
        } else {
          realDatas.add(p.get(0).getRealValue().floatValue());
        }


      } else {
        realDatas.add(null);
        ableDatas.add(null);
        theoryDatas.add(null);

      }
      String timeFormat = sdf.format(new Date(i));
      times.add(timeFormat);
    }
    map.put("times", times);
    map.put("realDatas", realDatas);
    map.put("ableDatas", ableDatas);
    map.put("theoryDatas", theoryDatas);
    return map;
  }

  /**
   * 根据开始时间和结束时间，设备编号 查询 环境监测仪数据 yh
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @param no        设备编号
   * @return 结果集
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public Map<String, Object> weatherStationStatusDataComprehensiveContrast(Date startTime, Date endTime, Integer no) {
    Map<String, Object> map = new HashMap<>();
    List<WeatherStationStatusData> list = new ArrayList<>();
    List<Float> datas = new ArrayList<>();
    list = weatherStationStatusDataRepository.findByTimeBetweenAndEquipmentNo(startTime, endTime, no);
    list.sort(Comparator.comparing(WeatherStationStatusData::getTime));
    long startTimeLong = startTime.getTime();
    long endTimeLong = endTime.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    List<String> times = new ArrayList<>();
    long timeStep = 900000L;
    if (startTimeLong % timeStep != 0) {
      startTimeLong = startTimeLong - (startTimeLong % timeStep);
    }
    BigDecimal nullValue = new BigDecimal(-99);
    for (long i = startTimeLong; i < endTimeLong; i = i + timeStep) {
      long finalI = i;
      List<WeatherStationStatusData> w = list.stream().filter(t -> t.getTime().getTime() == finalI).collect(Collectors.toList());
      if (w != null && w.size() > 0) {
        WeatherStationStatusData wResult = w.get(0);
        if (wResult.getGlobalR().compareTo(nullValue) != 0) {
          datas.add(wResult.getGlobalR().floatValue());
        } else {
          datas.add(null);
        }
      } else {
        datas.add(null);
      }
      String timeFormat = sdf.format(new Date(i));
      times.add(timeFormat);
    }


    map.put("times", times);
    map.put("datas", datas);

    return map;
  }

  /**
   * 根据开始时间和结束时间，设备编号 查询 测风塔数据 yh
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @param no        设备编号
   * @return 结果集
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public Map<String, Object> windTowerStatusDataComprehensiveContrast(Date startTime, Date endTime, Integer no) {
    Map<String, Object> map = new HashMap<>();
    List<WindTowerStatusData> list = new ArrayList<>();
    List<Float> datas = new ArrayList<>();
    list = windTowerStatusDataRepository.findByEquipmentNoAndTimeBetween(no, startTime, endTime);
    list.sort(Comparator.comparing(WindTowerStatusData::getTime));
    long startTimeLong = startTime.getTime();
    long endTimeLong = endTime.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    List<String> times = new ArrayList<>();
    long timeStep = 900000L;
    if (startTimeLong % timeStep != 0) {
      startTimeLong = startTimeLong - (startTimeLong % timeStep);
    }
    BigDecimal nullValue = new BigDecimal(-99);
    for (long i = startTimeLong; i < endTimeLong; i = i + timeStep) {
      long finalI = i;
      List<WindTowerStatusData> w = list.stream().filter(t -> t.getTime().getTime() == finalI).collect(Collectors.toList());
      if (w != null && w.size() > 0) {
        WindTowerStatusData wResult = w.get(0);
        if (wResult.getWsInst30().compareTo(nullValue) != 0) {
          datas.add(wResult.getWsInst30().floatValue());
        } else {
          datas.add(null);
        }
      } else {
        datas.add(null);
      }
      String timeFormat = sdf.format(new Date(i));
      times.add(timeFormat);
    }


    map.put("times", times);
    map.put("datas", datas);

    return map;
  }




  public Map<String,List<Map<String,String>>> getSelects(){
    Map<String,List<Map<String,String>>> resultMap = new HashMap<>();
    try {
      SysParameter file_forecast_days = sysParameterRepository.findBySysKey("FILE_FORECAST_DAYS").get();
      int forecast = Integer.parseInt(file_forecast_days.getSysValue());
      List<Map<String,String>> forecastList = new ArrayList<>();
      for (int i = 0; i < forecast; i++) {
        Map<String,String> map = new HashMap<>();
        map.put("label","预测前"+(i+1)+"天");
        map.put("value",i+1+"");
        forecastList.add(map);
      }

      resultMap.put("forecast", forecastList);

      SysParameter file_cdq_point = sysParameterRepository.findBySysKey("FILE_CDQ_POINT").get();
      int cdq = Integer.parseInt(file_cdq_point.getSysValue());
      List<Map<String,String>> cdqList = new ArrayList<>();
      for (int i = 0; i < cdq; i++) {
        Map<String,String> map = new HashMap<>();
        map.put("label","预测前第"+(i+1)+"点");
        map.put("value",i+1+"");
        cdqList.add(map);
      }

      resultMap.put("cdq", cdqList);

      SysParameter file_nwp_days = sysParameterRepository.findBySysKey("FILE_NWP_DAYS").get();
      int nwp = Integer.parseInt(file_nwp_days.getSysValue());
      List<Map<String,String>> nwpList = new ArrayList<>();
      for (int i = 0; i < nwp; i++) {
        Map<String,String> map = new HashMap<>();
        map.put("label","预测前"+(i+1)+"天");
        map.put("value",i+1+"");
        nwpList.add(map);
      }

      resultMap.put("nwp", nwpList);

    }catch (Exception e){
      log.info("获取前天上报数据查询点位时出现错误："+e);
    }

    return resultMap;
  }
}
