package com.jiayue.ipfcst.calculate.service;

import com.jiayue.ipfcst.common.data.constant.enums.ElectricFieldTypeEnum;
import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.repository.*;
import com.jiayue.ipfcst.console.service.SysParameterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 湖北准确率
 * 华中区域“两个细则”（2019模拟运行版）
 * 按日统计 00:00 - 23:45
 * @author yh
 * @version 2.0
 * @since 2020/7/17 9:30
 */
@Service
@Slf4j
public class AccuarcyPassRateCalculateServiceE42 implements IAccuracyPassRateCalculate {
  private final PowerStationStatusDataRepository powerStationStatusDataRepository;
  private final PowerStationStatusDataOneRepository powerStationStatusDataOneRepository;
  private final ForecastPowerUltraShortTermHisRepository forecastPowerUltraShortTermHisRepository;
  private final ForecastPowerUltraShortTermOneHisRepository forecastPowerUltraShortTermOneHisRepository;
  private final ForecastPowerShortTermHisRepository forecastPowerShortTermHisRepository;
  private final ForecastPowerShortTermOneHisRepository forecastPowerShortTermOneHisRepository;
  private final SysParameterService sysParameterService;
  private final AccuracyPassRateRepository accuracyPassRateRepository;
  private final AccuracyPassRateOneRepository accuracyPassRateOneRepository;

  @Autowired
  public AccuarcyPassRateCalculateServiceE42(PowerStationStatusDataRepository powerStationStatusDataRepository, PowerStationStatusDataOneRepository powerStationStatusDataOneRepository, ForecastPowerUltraShortTermHisRepository forecastPowerUltraShortTermHisRepository, ForecastPowerUltraShortTermOneHisRepository forecastPowerUltraShortTermOneHisRepository, ForecastPowerShortTermHisRepository forecastPowerShortTermHisRepository, ForecastPowerShortTermOneHisRepository forecastPowerShortTermOneHisRepository, SysParameterService sysParameterService, AccuracyPassRateRepository accuracyPassRateRepository, AccuracyPassRateOneRepository accuracyPassRateOneRepository) {
    this.powerStationStatusDataRepository = powerStationStatusDataRepository;
    this.powerStationStatusDataOneRepository = powerStationStatusDataOneRepository;
    this.forecastPowerUltraShortTermHisRepository = forecastPowerUltraShortTermHisRepository;
    this.forecastPowerUltraShortTermOneHisRepository = forecastPowerUltraShortTermOneHisRepository;
    this.forecastPowerShortTermHisRepository = forecastPowerShortTermHisRepository;
    this.forecastPowerShortTermOneHisRepository = forecastPowerShortTermOneHisRepository;
    this.sysParameterService = sysParameterService;
    this.accuracyPassRateRepository = accuracyPassRateRepository;
    this.accuracyPassRateOneRepository = accuracyPassRateOneRepository;
  }

  @Override
  public void generateData(ElectricField electricField) {
    long[] queryTime =  IAccuracyPassRateCalculate.getQueryTimeSh();
    long startTime = queryTime[0];
    long endTime = queryTime[1];
    long moment15 = 900000L;

    for (int i=0;i<2;i++){
      if (i==0){
        // 光伏
        electricField.setCapacity(new BigDecimal(electricField.getBackupE()));
        electricField.setElectricFieldTypeEnum(ElectricFieldTypeEnum.E1);
        BigDecimal cap = electricField.getCapacity();
        AccuracyPassRate accuracyPassRate = new AccuracyPassRate();
        accuracyPassRate.setTime(endTime);
        BigDecimal accuarcyDq;
        BigDecimal accuarcyCdq;
        String eType = electricField.getElectricFieldTypeEnum().name();

        List<PowerStationStatusData> pList = powerStationStatusDataRepository.findByTimeBetween(new Date(startTime), new Date(endTime));
        List<PowerStationStatusData> psList = IAccuracyPassRateCalculate.filterData(pList);
        /************** ***********************************计算短期准确率-start*****************************************************/
        String sysParameter = this.sysParameterService.getSysParameter("ACCURACY_DQ_HOWLONGAGO", "1");
        int howLongAgoForDq = Integer.parseInt(sysParameter);
        List<ForecastPowerShortTermHis> fList = forecastPowerShortTermHisRepository.findByForecastTimeBetweenAndForecastHowLongAgo(startTime, endTime, howLongAgoForDq);
        List<Map<String, BigDecimal>> dataDq = new ArrayList<>();
        for (long stepTime = startTime; stepTime <= endTime; stepTime = stepTime + moment15) {
          long finalTime = stepTime;
          List<ForecastPowerShortTermHis> fFilter = fList.stream().filter(f -> f.getForecastTime() == finalTime).collect(Collectors.toList());
          List<PowerStationStatusData> pFilter = psList.stream().filter(p -> p.getTime().getTime() == finalTime).collect(Collectors.toList());
          if (fFilter != null && fFilter.size() > 0 && pFilter != null && pFilter.size() > 0) {
            Map<String, BigDecimal> map = new HashMap<>();
            map.put("sj", pFilter.get(0).getAbleValue());
            map.put("yc", fFilter.get(0).getAbleValue());
            dataDq.add(map);
          }
        }
        if (dataDq != null && dataDq.size() > 0) {
          try {
            //光场站
            accuarcyDq = CalculateMethod.calculateC2(dataDq, cap);
            accuracyPassRate.setShortTermAccuracy(accuarcyDq.toString() + "%");
          } catch (Exception e) {
            e.printStackTrace();
            log.error("光伏计算短期准确率错误");
          }

        } else {
          accuracyPassRate.setShortTermAccuracy("无可用数据计算");
        }
        /************** ***********************************计算短期准确率-end*******************************************************/

        /************** ***********************************计算超短期准确率-start****************************************************/
        // 获取超短期计算准确率获取第几个点
        String sysParameterCdq = this.sysParameterService.getSysParameter("ACCURACY_CDQ_HOWLONGAGO", "16");
        int howLongAgoForCdq = Integer.parseInt(sysParameterCdq);
        List<ForecastPowerUltraShortTermHis> uList = forecastPowerUltraShortTermHisRepository.findByForecastTimeBetweenAndForecastHowLongAgo(startTime, endTime, howLongAgoForCdq);
        List<Map<String, BigDecimal>> dataCdq = new ArrayList<>();
        for (long stepTime = startTime; stepTime <= endTime; stepTime = stepTime + moment15) {
          long finalTime = stepTime;
          List<ForecastPowerUltraShortTermHis> fFilter = uList.stream().filter(f -> f.getForecastTime() == finalTime).collect(Collectors.toList());
          List<PowerStationStatusData> pFilter = psList.stream().filter(p -> p.getTime().getTime() == finalTime).collect(Collectors.toList());
          if (fFilter != null && fFilter.size() > 0 && pFilter != null && pFilter.size() > 0) {
            Map<String, BigDecimal> map = new HashMap<>();
            map.put("sj", pFilter.get(0).getAbleValue());
            map.put("yc", fFilter.get(0).getAbleValue());
            dataCdq.add(map);
          }
        }
        if (dataCdq != null && dataCdq.size() > 0) {
          try {
            //光场站
            accuarcyCdq = CalculateMethod.calculateC2(dataCdq, cap);
            accuracyPassRate.setUltraShortTermAccuracy(accuarcyCdq.toString() + "%");
          } catch (Exception e) {
            e.printStackTrace();
            log.error("光伏计算超短期准确率错误");
          }

        } else {
          accuracyPassRate.setUltraShortTermAccuracy("无可用数据计算");
        }
        /************** ***********************************计算超短期准确率-end******************************************************/

        //湖北lqw
        accuracyPassRate.setUltraShortTermPassRate("100%");
        accuracyPassRate.setShortTermPassRate("100%");

        this.accuracyPassRateRepository.save(accuracyPassRate);
      }
      else{
        // 风电
        electricField.setCapacity(new BigDecimal(electricField.getBackupF()));
        electricField.setElectricFieldTypeEnum(ElectricFieldTypeEnum.E2);
        BigDecimal cap = electricField.getCapacity();
        AccuracyPassRateOne accuracyPassRateOne = new AccuracyPassRateOne();
        accuracyPassRateOne.setTime(endTime);
        BigDecimal accuarcyDq;
        BigDecimal accuarcyCdq;
        String eType = electricField.getElectricFieldTypeEnum().name();

        List<PowerStationStatusDataOne> pList = powerStationStatusDataOneRepository.findByTimeBetween(new Date(startTime), new Date(endTime));
        List<PowerStationStatusDataOne> psList = IAccuracyPassRateCalculate.filterDataOne(pList);
        /************** ***********************************计算短期准确率-start*****************************************************/
        String sysParameter = this.sysParameterService.getSysParameter("ACCURACY_DQ_HOWLONGAGO", "1");
        int howLongAgoForDq = Integer.parseInt(sysParameter);
        List<ForecastPowerShortTermOneHis> fList = forecastPowerShortTermOneHisRepository.findByForecastTimeBetweenAndForecastHowLongAgo(startTime, endTime, howLongAgoForDq);
        List<Map<String, BigDecimal>> dataDq = new ArrayList<>();
        for (long stepTime = startTime; stepTime <= endTime; stepTime = stepTime + moment15) {
          long finalTime = stepTime;
          List<ForecastPowerShortTermOneHis> fFilter = fList.stream().filter(f -> f.getForecastTime() == finalTime).collect(Collectors.toList());
          List<PowerStationStatusDataOne> pFilter = psList.stream().filter(p -> p.getTime().getTime() == finalTime).collect(Collectors.toList());
          if (fFilter != null && fFilter.size() > 0 && pFilter != null && pFilter.size() > 0) {
            Map<String, BigDecimal> map = new HashMap<>();
            map.put("sj", pFilter.get(0).getAbleValue());
            map.put("yc", fFilter.get(0).getAbleValue());
            dataDq.add(map);
          }
        }
        if (dataDq != null && dataDq.size() > 0) {
          try {
            //风电站
            accuarcyDq = CalculateMethod.calculateC1(dataDq, cap);
            accuracyPassRateOne.setShortTermAccuracy(accuarcyDq.toString() + "%");
          } catch (Exception e) {
            e.printStackTrace();
            log.error("风电计算短期准确率错误");
          }

        } else {
          accuracyPassRateOne.setShortTermAccuracy("无可用数据计算");
        }
        /************** ***********************************计算短期准确率-end*******************************************************/

        /************** ***********************************计算超短期准确率-start****************************************************/
        // 获取超短期计算准确率获取第几个点
        String sysParameterCdq = this.sysParameterService.getSysParameter("ACCURACY_CDQ_HOWLONGAGO", "16");
        int howLongAgoForCdq = Integer.parseInt(sysParameterCdq);
        List<ForecastPowerUltraShortTermOneHis> uList = forecastPowerUltraShortTermOneHisRepository.findByForecastTimeBetweenAndForecastHowLongAgo(startTime, endTime, howLongAgoForCdq);
        List<Map<String, BigDecimal>> dataCdq = new ArrayList<>();
        for (long stepTime = startTime; stepTime <= endTime; stepTime = stepTime + moment15) {
          long finalTime = stepTime;
          List<ForecastPowerUltraShortTermOneHis> fFilter = uList.stream().filter(f -> f.getForecastTime() == finalTime).collect(Collectors.toList());
          List<PowerStationStatusDataOne> pFilter = psList.stream().filter(p -> p.getTime().getTime() == finalTime).collect(Collectors.toList());
          if (fFilter != null && fFilter.size() > 0 && pFilter != null && pFilter.size() > 0) {
            Map<String, BigDecimal> map = new HashMap<>();
            map.put("sj", pFilter.get(0).getAbleValue());
            map.put("yc", fFilter.get(0).getAbleValue());
            dataCdq.add(map);
          }
        }
        if (dataCdq != null && dataCdq.size() > 0) {
          try {
            //风场站
            accuarcyCdq = CalculateMethod.calculateC1(dataCdq, cap);
            accuracyPassRateOne.setUltraShortTermAccuracy(accuarcyCdq.toString() + "%");
          } catch (Exception e) {
            e.printStackTrace();
            log.error("风电计算超短期准确率错误");
          }

        } else {
          accuracyPassRateOne.setUltraShortTermAccuracy("无可用数据计算");
        }
        /************** ***********************************计算超短期准确率-end******************************************************/

        //湖北lqw
        accuracyPassRateOne.setUltraShortTermPassRate("100%");
        accuracyPassRateOne.setShortTermPassRate("100%");

        this.accuracyPassRateOneRepository.save(accuracyPassRateOne);

      }
    }
  }

}
