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.ElectricField;
import com.jiayue.ipfcst.common.data.entity.PowerStationStatusData;
import com.jiayue.ipfcst.common.data.entity.PrValue;
import com.jiayue.ipfcst.common.data.entity.WeatherStationStatusData;
import com.jiayue.ipfcst.common.data.repository.ElectricFieldRepository;
import com.jiayue.ipfcst.common.data.repository.PowerStationStatusDataRepository;
import com.jiayue.ipfcst.common.data.repository.PrValueRepository;
import com.jiayue.ipfcst.common.data.repository.WeatherStationStatusDataRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * pr值业务层
 *
 * @author fxy
 * @version 1.0
 * @since 2022/10/26 11:23
 */
@Service
@Slf4j
public class PrValueService {
  @Autowired
  WeatherStationStatusDataRepository weatherStationStatusDataRepository;
  @Autowired
  PowerStationStatusDataRepository powerStationStatusDataRepository;
  @Autowired
  ElectricFieldRepository electricFieldRepository;
  @Autowired
  PrValueRepository prValueRepository;

  public void setPrValue() {
    //获取当天0:00
    Long startTime = DateUtil.beginOfDay(DateTime.now()).getTime();
    //获取前一天的23:59:59
    Long endTime = startTime - 1000;
    //获取前一天的0:00
    startTime = startTime - 24 * 60 * 60 * 1000;
    //实例化PrValue
    PrValue prValue = new PrValue();
    //查询日照小时数
    List<WeatherStationStatusData> weatherStationStatusDataList = weatherStationStatusDataRepository.findByTimeBetween(new Date(startTime), new Date(endTime));
    //按照日照小时数降序排列
    weatherStationStatusDataList.sort(Comparator.comparing(WeatherStationStatusData::getHourDA).reversed());
    //查询实际功率
    List<PowerStationStatusData> powerStationStatusDataList = powerStationStatusDataRepository.findByTimeBetween(new Date(startTime), new Date(endTime));
    //获取场站信息、装机容量
    ElectricField electricField = electricFieldRepository.findAll().get(0);
    BigDecimal theoreticalPowerGeneration = BigDecimal.ZERO;
    //判断集合是否为空
    if (weatherStationStatusDataList != null && weatherStationStatusDataList.size() > 0) {
      //如果日照小时数小于0，理论发电量为0
      if (weatherStationStatusDataList.get(0).getHourDA().compareTo(BigDecimal.ZERO) == -1) {
        theoreticalPowerGeneration = BigDecimal.ZERO;
      } else {
        //计算理论发电量 理论发电量 = 装机容量 * 日照小时数 * 系统效率
        theoreticalPowerGeneration = electricField.getCapacity().multiply(weatherStationStatusDataList.get(0).getHourDA().multiply(new BigDecimal(0.98).setScale(4, BigDecimal.ROUND_HALF_UP)));
      }
      prValue.setHourDA(weatherStationStatusDataList.get(0).getHourDA());
    }
    //计算实际发电量
    BigDecimal actualPowerGeneration = BigDecimal.ZERO;
    BigDecimal sum = BigDecimal.ZERO;
    for (PowerStationStatusData powerStationStatusData : powerStationStatusDataList) {
      actualPowerGeneration = powerStationStatusData.getRealValue();
      sum = sum.add(actualPowerGeneration);
    }
    sum = sum.divide(new BigDecimal(60).setScale(4), BigDecimal.ROUND_HALF_UP);
    //计算pr值 pr值 = 实际发电量 / 理论发电量
    BigDecimal pr = BigDecimal.ZERO;
    //判断理论发电量是否为0
    if (theoreticalPowerGeneration.compareTo(BigDecimal.ZERO) == 0) {
      pr = BigDecimal.ZERO;
    } else {
      pr = sum.divide(theoreticalPowerGeneration, 4, BigDecimal.ROUND_HALF_UP);
    }
    //将得到的值存入数据库
    prValue.setActualPowerGeneration(sum);
    prValue.setCapacity(electricField.getCapacity());
    prValue.setUltraShortTermAccuracy(new BigDecimal(0.98));
    prValue.setPrValue(pr);
    prValue.setTheoreticalPowerGeneration(theoreticalPowerGeneration);
    //存入时间与数据是同一天（因为计算的数据是昨天的数据，所以存入时间也是昨天的时间）
    prValue.setTime(DateUtils.addDays(new Date(), -1));
    prValueRepository.save(prValue);
  }
}
