package com.jiayue.ipfcst.console.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.repository.AccuracyPassRateRepository;
import com.jiayue.ipfcst.common.data.repository.ForecastPowerShortTermHisRepository;
import com.jiayue.ipfcst.common.data.repository.ForecastPowerUltraShortTermHisRepository;
import com.jiayue.ipfcst.common.data.repository.PowerStationStatusDataRepository;
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 java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 东北准确率
 *
 * @author yh
 * @version 2.0
 * @since 2019/7/17 9:30
 */
@Service
@Slf4j
public class AccuracyPassRateCalculateForDBService extends BaseService {

  private final PowerStationStatusDataRepository powerStationStatusDataRepository;
  private final ForecastPowerUltraShortTermHisRepository forecastPowerUltraShortTermHisRepository;
  private final ForecastPowerShortTermHisRepository forecastPowerShortTermHisRepository;
  private final SysParameterService sysParameterService;
  private final AccuracyPassRateRepository accuracyPassRateRepository;

  @Autowired
  public AccuracyPassRateCalculateForDBService(PowerStationStatusDataRepository powerStationStatusDataRepository,
                                               ForecastPowerUltraShortTermHisRepository forecastPowerUltraShortTermHisRepository,
                                               ForecastPowerShortTermHisRepository forecastPowerShortTermHisRepository,
                                               SysParameterService sysParameterService,
                                               AccuracyPassRateRepository accuracyPassRateRepository) {
    this.powerStationStatusDataRepository = powerStationStatusDataRepository;
    this.forecastPowerUltraShortTermHisRepository = forecastPowerUltraShortTermHisRepository;
    this.forecastPowerShortTermHisRepository = forecastPowerShortTermHisRepository;
    this.sysParameterService = sysParameterService;
    this.accuracyPassRateRepository = accuracyPassRateRepository;
  }

  public void generateData(ElectricField electricField) {
    try {
      long startTime = DateUtil.beginOfDay(DateTime.now()).getTime();
      long nowTime = DateTime.now().getTime();
      long moment15 = 900000L;
      BigDecimal nowTimeBig = new BigDecimal(String.valueOf(nowTime));
      BigDecimal moment15Big = new BigDecimal(String.valueOf(moment15));
      BigDecimal queryTimeBig = nowTimeBig.divideAndRemainder(moment15Big)[0].multiply(moment15Big);
      long endTime = queryTimeBig.longValue();

      // 获取短期计算准确率获取该天是提前几天预测的
      String sysParameter = sysParameterService.getSysParameter("ACCURACY_DQ_HOWLONGAGO", "1");
      int howLongAgoForDq = Integer.parseInt(sysParameter);
      // 获取短期数据
      ForecastPowerShortTermHis forecastPowerShortTermHis = this.forecastPowerShortTermHisRepository.findByForecastTimeAndForecastHowLongAgo(endTime, howLongAgoForDq);
      Map<Long, Float> map = new HashMap<>();

      // 获取短期计算准确率获取该天是提前几天预测的
      String sysParameterCdq = sysParameterService.getSysParameter("ACCURACY_CDQ_HOWLONGAGO", "1");
      int howLongAgoForCdq = Integer.parseInt(sysParameterCdq);
      // 获取超短期数据
      List<ForecastPowerUltraShortTermHis> ultraList = this.forecastPowerUltraShortTermHisRepository.findByForecastTimeBetweenAndForecastHowLongAgo(startTime,endTime,howLongAgoForCdq);
      // 获取实际功率
      PowerStationStatusData powerStationStatusData = this.powerStationStatusDataRepository.findByTime(new Date(endTime));

      BigDecimal capacity = electricField.getCapacity();
      BigDecimal compare = capacity.multiply(new BigDecimal("0.1"));
      BigDecimal zero = new BigDecimal("0");
      AccuracyPassRate accuracyPassRate = new AccuracyPassRate();
      accuracyPassRate.setTime(endTime);
      if(powerStationStatusData !=null){
        BigDecimal SJ = powerStationStatusData.getAbleValue();
        /************** ***********************************计算短期准确率-start*****************************************************/
        if (forecastPowerShortTermHis != null) {
          BigDecimal DQ = forecastPowerShortTermHis.getAbleValue();
          if (DQ.floatValue() < compare.floatValue() && SJ.floatValue() < compare.floatValue()) {
            accuracyPassRate.setShortTermAccuracy("不予考核");
          } else {
            if (SJ.floatValue() > compare.floatValue() && DQ.floatValue() == zero.floatValue()) {
              accuracyPassRate.setShortTermAccuracy("必定考核");
            } else {
              //短期
              BigDecimal b1 = DQ.subtract(SJ);
              BigDecimal b2 = b1.divide(DQ, 8, BigDecimal.ROUND_HALF_UP).abs();
              BigDecimal b3 = new BigDecimal(1).subtract(b2);
              String value = b3.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
              accuracyPassRate.setShortTermAccuracy(value);
            }
          }
        } else {
          accuracyPassRate.setShortTermAccuracy("不予考核");
        }
        /*************************************************计算短期准确率-end*****************************************************/

        /*************************************************计算超短期准确率-start*****************************************************/
        if (ultraList != null&&ultraList.size()>zero.intValue()) {
          BigDecimal countCdq = new BigDecimal("0");
          int index = 0;
          //平均值
          for (int i = 0; i < ultraList.size(); i++) {
            countCdq = countCdq.add(ultraList.get(i).getAbleValue());
            index++;
          }
          if (SJ.floatValue() < compare.floatValue() && countCdq.floatValue() < compare.floatValue()) {
            accuracyPassRate.setUltraShortTermAccuracy("不予考核");
          } else {
            BigDecimal temp = (countCdq.divide(new BigDecimal(index), 8, BigDecimal.ROUND_HALF_UP));
            BigDecimal p1 = temp.subtract(SJ);
            BigDecimal p2 = p1.divide(temp, 8, BigDecimal.ROUND_HALF_UP).abs();
            String p3 = (new BigDecimal(1).subtract(p2)).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            accuracyPassRate.setUltraShortTermAccuracy(p3);
          }
        } else {
          accuracyPassRate.setUltraShortTermAccuracy("不予考核");
        }
        /*************************************************计算超短期准确率-end*****************************************************/
      }else{
        accuracyPassRate.setShortTermAccuracy("不予考核");
        accuracyPassRate.setUltraShortTermAccuracy("不予考核");
      }
      accuracyPassRateRepository.save(accuracyPassRate);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("西南计算短期和超短期准确率失败" + e);
    }

  }


}
