package com.jiayue.ipfcst.dataexchange.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
import com.jiayue.ipfcst.common.data.entity.Nwp;
import com.jiayue.ipfcst.common.data.entity.WeatherStationStatusData;
import com.jiayue.ipfcst.common.data.entity.WindTowerStatusData;
import com.jiayue.ipfcst.common.data.repository.NwpRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 测风塔、环境监测仪补数据
 *
 * @author bizy
 * @version 3.0
 */
@Slf4j
@Service
public class GetDataService {

  @Autowired
  NwpRepository nwpRepository;

  /**
   * 补测风塔数据
   *
   * @param equipmentNo 设备编号
   * @param time        时间
   * @return 测风塔实体
   */
  public WindTowerStatusData getWindTowerStatusData(int equipmentNo, Date time) {
    WindTowerStatusData windTowerStatusData = new WindTowerStatusData();
    try {
      Long startTime = DateMomentUtil.getMomentTime(time.getTime(), 1, 15 * 60 * 1000L);
      List<Nwp> nwpList = nwpRepository.findByPreTimeBetween(startTime, startTime);
      if (nwpList.size() > 0) {
        Nwp nwpData = nwpList.get(0);
        // 10米风速
        double ws10 = nwpData.getWs10().doubleValue();
        double ws30 = nwpData.getWs30().doubleValue();
        double ws50 = nwpData.getWs50().doubleValue();
        double ws70 = nwpData.getWs70().doubleValue();
        double ws80 = nwpData.getWs80().doubleValue();
        double ws90 = nwpData.getWs90().doubleValue();
        double ws100 = nwpData.getWs100().doubleValue();
        double ws110 = nwpData.getWs170().doubleValue();
        // 10米风向
        double wd10 = nwpData.getWd10().doubleValue();
        double wd30 = nwpData.getWd30().doubleValue();
        double wd50 = nwpData.getWd50().doubleValue();
        double wd70 = nwpData.getWd70().doubleValue();
        double wd80 = nwpData.getWd80().doubleValue();
        double wd90 = nwpData.getWd90().doubleValue();
        double wd100 = nwpData.getWd100().doubleValue();
        double wd110 = nwpData.getWd170().doubleValue();
        // 湿度
        double rh = nwpData.getRh().doubleValue();
        // 温度
        double t = nwpData.getT().doubleValue();
        // 气压
        double p = nwpData.getPressure().doubleValue();
        //米风速产生0-3随机数
        double valueWs = RandomUtil.randomDouble(0, 3, 2, RoundingMode.HALF_UP);
        // 风向产生40-60随机数
        double valueWd = RandomUtil.randomDouble(40, 60, 2, RoundingMode.HALF_UP);
        // 湿度产生50-60随机数
        //       // double rhRandomValue = RandomUtil.randomDouble(50, 60, 2, RoundingMode.HALF_UP);
        //        // 温度产生0-5随机数
        //       // double tRandomValue = RandomUtil.randomDouble(0, 5, 2, RoundingMode.HALF_UP);
        //        // 气压产生75-90随机数
        //       // double paRandomValue = RandomUtil.randomDouble(75, 90, 2, RoundingMode.HALF_UP);

        windTowerStatusData.setTInst(new BigDecimal(t));
        windTowerStatusData.setRhInst(new BigDecimal(rh));
        windTowerStatusData.setPaInst(new BigDecimal(p));
        windTowerStatusData.setWsInst10(new BigDecimal(ws10 + valueWs));
        windTowerStatusData.setWdInst10(new BigDecimal(wd10 + valueWd));
        windTowerStatusData.setWsInst30(new BigDecimal(ws30 + valueWs));
        windTowerStatusData.setWdInst30(new BigDecimal(wd30 + valueWd));
        windTowerStatusData.setWsInst50(new BigDecimal(ws50 + valueWs));
        windTowerStatusData.setWdInst50(new BigDecimal(wd50 + valueWd));
        windTowerStatusData.setWsInst70(new BigDecimal(ws70 + valueWs));
        windTowerStatusData.setWdInst70(new BigDecimal(wd70 + valueWd));
        windTowerStatusData.setWsInst80(new BigDecimal(ws80 + valueWs));
        windTowerStatusData.setWdInst80(new BigDecimal(wd80 + valueWd));
        windTowerStatusData.setWsInst90(new BigDecimal(ws90 + valueWs));
        windTowerStatusData.setWdInst90(new BigDecimal(wd90 + valueWd));
        windTowerStatusData.setWsInst100(new BigDecimal(ws100 + valueWs));
        windTowerStatusData.setWdInst100(new BigDecimal(wd100 + valueWd));
        windTowerStatusData.setWsInst110(new BigDecimal(ws110 + valueWs));
        windTowerStatusData.setWdInst110(new BigDecimal(wd110 + valueWd));
        //轮毂高度默认70
        windTowerStatusData.setWsInstHubHeight(new BigDecimal(ws70 + valueWs));
        windTowerStatusData.setWdInstHubHeight(new BigDecimal(wd70 + valueWd));
        windTowerStatusData.setEquipmentNo(equipmentNo);
        windTowerStatusData.setStatus(1);
        windTowerStatusData.setTime(time);
        windTowerStatusData.setIsGenerated(true);
      } else {
        log.warn("当前数据库内没有" + startTime + "的NWP数据，未能生成合理的测风塔数据");
      }
    } catch (Exception e) {
      log.error("测风塔补数失败，设备id：" + equipmentNo + "，时间：" + time, e);
    }
    return windTowerStatusData;
  }

  /**
   * 补环境监测仪数据
   *
   * @param equipmentNo 设备编号
   * @param time        时间
   * @return 环境监测仪实体
   */
  public WeatherStationStatusData getWeatherStationStatusData(int equipmentNo, Date time) {
    WeatherStationStatusData weatherStationStatusData = new WeatherStationStatusData();
    try {
      Long startTime = DateMomentUtil.getMomentTime(time.getTime(), 1, 15 * 60 * 1000L);
      List<Nwp> nwpList = nwpRepository.findByPreTimeBetween(startTime, startTime);
      if (nwpList.size() > 0) {
        Nwp nwpData = nwpList.get(0);
        // 温度产生20-35随机数
        double tRandomValue = RandomUtil.randomDouble(0, 5, 2, RoundingMode.HALF_UP);
        // 辐射产生0-50随机数
        double rRandomValue = RandomUtil.randomDouble(0, 50, 2, RoundingMode.HALF_UP);
        weatherStationStatusData.setGlobalR(nwpData.getSwr());
        weatherStationStatusData.setDirectR(nwpData.getDirectRadiation());
        weatherStationStatusData.setDiffuseR(nwpData.getDiffuseRadiation());
        weatherStationStatusData.setObliqueR(new BigDecimal(rRandomValue).add(nwpData.getSwr()).setScale(2,RoundingMode.HALF_UP));
        weatherStationStatusData.setAirT(nwpData.getT());
        weatherStationStatusData.setP(nwpData.getPressure());
        weatherStationStatusData.setRh(nwpData.getRh());
        weatherStationStatusData.setCellT(nwpData.getT());
        weatherStationStatusData.setWs(nwpData.getWs10());
        weatherStationStatusData.setWd(nwpData.getWd10());
        //日照日累计用当前小时数-7，默认早上7点开始有日照
        weatherStationStatusData.setHourDA(new BigDecimal(Integer.parseInt(DateUtil.format(time, "HH")) - 7));
        weatherStationStatusData.setEquipmentNo(equipmentNo);
        weatherStationStatusData.setStatus(1);
        weatherStationStatusData.setTime(time);
        weatherStationStatusData.setIsGenerated(true);
      } else {
        log.warn("当前数据库内没有" + startTime + "的NWP数据，未能生成合理的环境检测仪数据");
      }
    } catch (Exception e) {
      log.error("环境监测仪补数失败，设备id：" + equipmentNo + "，时间：" + time, e);
    }
    return weatherStationStatusData;
  }
}
