package com.jiayue.ipfcst.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.repository.*;
import com.jiayue.ipfcst.common.data.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RealTimeComprehensiveContrastService extends BaseService {


  private final ForecastPowerUltraShortTermService forecastPowerUltraShortTermService;
  private final ForecastPowerShortTermService forecastPowerShortTermService;
  private final PowerStationStatusDataService powerStationStatusDataService;
  private final WeatherStationStatusDataService weatherStationStatusDataService;
  private final WindTowerStatusDataService windTowerStatusDataService;
  private final DisplayConfigService displayConfigService;
  private final NwpService nwpService;
  private final SysParameterRepository sysParameterRepository;

  @Autowired
  public RealTimeComprehensiveContrastService(ForecastPowerUltraShortTermService forecastPowerUltraShortTermService,
                                              ForecastPowerShortTermService forecastPowerShortTermService,
                                              PowerStationStatusDataService powerStationStatusDataService,
                                              WeatherStationStatusDataService weatherStationStatusDataService,
                                              WindTowerStatusDataService windTowerStatusDataService,
                                              DisplayConfigService displayConfigService,
                                              NwpService nwpService,
                                              SysParameterRepository sysParameterRepository) {
    this.forecastPowerUltraShortTermService = forecastPowerUltraShortTermService;
    this.forecastPowerShortTermService = forecastPowerShortTermService;
    this.powerStationStatusDataService = powerStationStatusDataService;
    this.weatherStationStatusDataService = weatherStationStatusDataService;
    this.windTowerStatusDataService = windTowerStatusDataService;
    this.displayConfigService = displayConfigService;
    this.nwpService = nwpService;
    this.sysParameterRepository = sysParameterRepository;

  }


  public Map<String, Object> realTimeComprehensiveContrast(Long startTime,
                                                           Long endTime,
                                                           Integer no,
                                                           String otherType,
                                                           Long jgTime) throws BusinessException {
    Map<String, Object> map = new HashMap<>();
    Map<String, Object> rpMap = new HashMap<>();
    Map<String, Object> rDqMap = new HashMap<>();
    Map<String, Object> rCdqMap = new HashMap<>();
    List<WeatherStationStatusData> weatherStationStatusDataList = new ArrayList<>();
    List<WindTowerStatusData> windTowerStatusDataList = new ArrayList<>();

    rpMap = powerStationStatusDataService.findByTimeBetweenForContrast(new Date(startTime), new Date(endTime), jgTime);

    rDqMap = forecastPowerShortTermService.findByForecastTimeBetweenForComprehensive(startTime, endTime, jgTime);
    rCdqMap = forecastPowerUltraShortTermService.findByForecastTimeBetweenForComprehensive(startTime, endTime, jgTime);

    List<Float> rCdqList = (List<Float>) rCdqMap.get("datas");
    List<Float> rDqList = (List<Float>) rDqMap.get("datas");
    List<String> times = new ArrayList<>();
    List<Map<String, Object>> tableResult = new ArrayList<>();
    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");
    List<Nwp> nwpList = nwpService.findByPreTimeBetween(startTime, endTime, jgTime);
    if (otherType.equals("E2")) {
      //风电对比数据
      windTowerStatusDataList = windTowerStatusDataService.findByEquipmentNoAndTimeBetweenOrderByTimeAsc(new Date(startTime), new Date(endTime), no, jgTime);

      DisplayConfig displayConfig = displayConfigService.findByDisplayCode("rtWindTowerInfos");
      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("rCdq")) {
          if (rCdqList.get(i) != null) {
            dtoMap.put("rCdq", rCdqList.get(i));
          } else {
            dtoMap.put("rCdq", "");
          }
        }

        if (Arrays.asList(showCode).contains("rDq")) {
          if (rDqList.get(i) != null) {
            dtoMap.put("rDq", rDqList.get(i));
          } else {
            dtoMap.put("rDq", "");
          }
        }
        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());
          }
          if (Arrays.asList(showCode).contains("wsInstHubHeight")) {
            //一百二十米
            resultMap.put("wsInstHubHeight", windTowerStatusData.getWsInstHubHeight().floatValue() == -99 ? "" : windTowerStatusData.getWsInstHubHeight());
          }
        } 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", "");
          }
          if (Arrays.asList(showCode).contains("wsInstHubHeight")) {
            //一百二十米
            resultMap.put("wsInstHubHeight", "");
          }
        }
        boolean nb = nwpList.stream().filter(m -> sdf.format(m.getPreTime()).equals(time)).findAny().isPresent();
        if (nb) {
          Nwp nwp = nwpList.stream().filter(o -> sdf.format(o.getPreTime()).equals(time)).findAny().orElse(null);
          if (Arrays.asList(showCode).contains("ws10")) {
            //十米
            resultMap.put("ws10", nwp.getWs10().floatValue() == -99 ? "" : nwp.getWs10());
          }
          if (Arrays.asList(showCode).contains("ws30")) {
            //三十米
            resultMap.put("ws30", nwp.getWs30().floatValue() == -99 ? "" : nwp.getWs30());
          }
          if (Arrays.asList(showCode).contains("ws50")) {
            //五十米
            resultMap.put("ws50", nwp.getWs50().floatValue() == -99 ? "" : nwp.getWs50());
          }
          if (Arrays.asList(showCode).contains("ws70")) {
            //七十米
            resultMap.put("ws70", nwp.getWs70().floatValue() == -99 ? "" : nwp.getWs70());
          }
          if (Arrays.asList(showCode).contains("ws80")) {
            //八十米
            resultMap.put("ws80", nwp.getWs80().floatValue() == -99 ? "" : nwp.getWs80());
          }
          if (Arrays.asList(showCode).contains("ws90")) {
            //九十米
            resultMap.put("ws90", nwp.getWs90().floatValue() == -99 ? "" : nwp.getWs90());
          }
          if (Arrays.asList(showCode).contains("ws100")) {
            //一百米
            resultMap.put("ws100", nwp.getWs100().floatValue() == -99 ? "" : nwp.getWs100());
          }
          if (Arrays.asList(showCode).contains("ws170")) {
            //一百一十米
            resultMap.put("ws170", nwp.getWs170().floatValue() == -99 ? "" : nwp.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("rtWeatherStationInfos");
      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("rCdq")) {
            if (rCdqList.get(i) != null) {
              dtoMap.put("rCdq", rCdqList.get(i));
            } else {
              dtoMap.put("rCdq", "");
            }
          }

          if (Arrays.asList(showCode).contains("rDq")) {
            if (rDqList.get(i) != null) {
              dtoMap.put("rDq", rDqList.get(i));
            } else {
              dtoMap.put("rDq", "");
            }
          }
          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", "");
            }
          }
          boolean nb = nwpList.stream().filter(m -> sdf.format(m.getPreTime()).equals(time)).findAny().isPresent();
          if (nb) {
            Nwp nwp = nwpList.stream().filter(o -> sdf.format(o.getPreTime()).equals(time)).findAny().orElse(null);
            if (Arrays.asList(showCode).contains("swr")) {
              //辐射
              resultMap.put("swr", nwp.getSwr().floatValue() == -99 ? "" : nwp.getSwr());
            }
            if (Arrays.asList(showCode).contains("directRadiation")) {
              //直辐射
              resultMap.put("directRadiation", nwp.getDirectRadiation().floatValue() == -99 ? "" : nwp.getDirectRadiation());
            }
            if (Arrays.asList(showCode).contains("diffuseRadiation")) {
              //散辐射
              resultMap.put("diffuseRadiation", nwp.getDiffuseRadiation().floatValue() == -99 ? "" : nwp.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;
  }
}
