package com.jiayue.ipfcst.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.core.util.CommonUtil;
import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.common.data.entity.Nwp;
import com.jiayue.ipfcst.common.data.entity.SysParameter;
import com.jiayue.ipfcst.common.data.repository.NwpRepository;
import com.jiayue.ipfcst.common.data.repository.SysParameterRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * NWP实时业务层
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/5 16:02
 */
@Service
@Slf4j
public class NwpService extends BaseService {

  private final NwpRepository nwpRepository;

  private final SysParameterRepository sysParameterRepository;
  @Autowired
  public NwpService(NwpRepository nwpRepository,SysParameterRepository sysParameterRepository){
    this.nwpRepository = nwpRepository;
    this.sysParameterRepository = sysParameterRepository;
  }
  /**
   * 按时间查询实时NWP yh
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @return 结果集
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public Map<String,Object> findByForecastTimeBetween(Long startTime, Long endTime) throws BusinessException {
    Map<String,Object> map = new HashMap<>();
    List<Nwp> list = new ArrayList<>();
    List<Nwp> checkList = new ArrayList<>();
    Map<String,Object> datas = new LinkedHashMap<>();
    list = nwpRepository.findByPreTimeBetween(startTime, endTime);
    list.sort(Comparator.comparing(Nwp::getPreTime));

    long startTimeLong  = startTime;
    long endTimeLong  = endTime;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    long timeStep = 900000L;
    if(startTimeLong%timeStep !=0){
      startTimeLong = startTimeLong -(startTimeLong%timeStep);
    }
    /*List<Float> datas = new ArrayList<>();*/
    List<String> times = new ArrayList<>();

    for(long i = startTimeLong;i<endTimeLong;i=i+timeStep){
      long finalI = i;
      List<Nwp> p = list.stream().filter(t -> t.getPreTime() == finalI).collect(Collectors.toList());
      if (p != null && p.size() > 0) {
        checkList.add(p.get(0));
      }else{
        checkList.add(new Nwp());
      }
      String timeFormat = sdf.format(new Date(i));
      times.add(timeFormat);
    }
    this.defaultReplace(checkList);
    ElectricField electricField = this.getElectricField();
    List<BigDecimal> ws10Collect = new ArrayList<>();
    List<BigDecimal> ws30Collect= new ArrayList<>();
    List<BigDecimal> ws50Collect= new ArrayList<>();
    List<BigDecimal> ws70Collect= new ArrayList<>();
    List<BigDecimal> ws80Collect= new ArrayList<>();
    List<BigDecimal> ws90Collect= new ArrayList<>();
    List<BigDecimal> ws100Collect= new ArrayList<>();
    List<BigDecimal> ws170Collect= new ArrayList<>();
    List<BigDecimal> swrCollect= new ArrayList<>();
    List<BigDecimal> diffuseRadiationCollect= new ArrayList<>();
    List<BigDecimal> directRadiationCollect= new ArrayList<>();
    Map<String,String> map1 = new HashMap<>();

    swrCollect = checkList.stream().map(Nwp::getSwr).collect(Collectors.toList());
    diffuseRadiationCollect = checkList.stream().map(Nwp::getDiffuseRadiation).collect(Collectors.toList());
    directRadiationCollect = checkList.stream().map(Nwp::getDirectRadiation).collect(Collectors.toList());
    datas.put("swr",swrCollect);
    datas.put("diffuseRadiation",diffuseRadiationCollect);
    datas.put("directRadiation",directRadiationCollect);

    map.put("cName",map1);
    map.put("times",times);
    map.put("datas",datas);
    return map;
  }



  /**
   * 根据时间 分页查询 NWP数据 yh
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @param page      页码
   * @param size      条数
   * @param sortOrder 排序
   * @return
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public Map<String, Object> findByTimeBetweenAndNoForPaging(Long startTime, Long endTime,
                                                             Integer page, Integer size,
                                                             String sortOrder)  {
    Map<String, Object> map = new HashMap<>();
    Sort sort = Sort.by(Sort.Direction.DESC, "preTime");
    if (sortOrder.contains("asc")) {
      sort = Sort.by(Sort.Direction.ASC, "preTime");
    }
    Specification<Nwp> specification = this.specificationFindByTimeAndNo(startTime, endTime);
    Pageable pageable = PageRequest.of(page - 1, size, sort);
    Page nwps = nwpRepository.findAll(specification,pageable);
    List<Nwp> datas = new ArrayList<>();
    datas = nwps.getContent();
    this.defaultReplace(datas);
    map.put("content", datas);
    map.put("count", nwps.getTotalElements());
    Map<String,String> map1 = new HashMap<>();

    try {
      SysParameter sysParameter = sysParameterRepository.findBySysKeyEquals("NWP_CHANGE_WIND");
      if (sysParameter != null) {
        String nwpChangeWind = sysParameter.getSysValue();
        String[] windChangeName = nwpChangeWind.split(",");
        if (windChangeName.length == 16) {
          map1.put("preTime","预测时间");
          map1.put("scTime","生成时间");
          map1.put("pressure","气压(KPa)");
          map1.put("t","温度(℃)");
          map1.put("rh","湿度(%)");
          map1.put("swr","总辐射(W/㎡)");
          map1.put("directRadiation","直辐射(W/㎡)");
          map1.put("diffuseRadiation","散辐射(W/㎡)");
          map1.put("senf","热感通量(W/㎡)");
          map1.put("ws10",windChangeName[0]+"米风速(m/s)");
          map1.put("ws30",windChangeName[1]+"米风速(m/s)");
          map1.put("ws50",windChangeName[2]+"米风速(m/s)");
          map1.put("ws70",windChangeName[3]+"米风速(m/s)");
          map1.put("ws80",windChangeName[4]+"米风速(m/s)");
          map1.put("ws90",windChangeName[5]+"米风速(m/s)");
          map1.put("ws100",windChangeName[6]+"米风速(m/s)");
          map1.put("ws170",windChangeName[7]+"米风速(m/s)");
          map1.put("wd10",windChangeName[8]+"米风向(°)");
          map1.put("wd30",windChangeName[9]+"米风向(°)");
          map1.put("wd50",windChangeName[10]+"米风向(°)");
          map1.put("wd70",windChangeName[11]+"米风向(°)");
          map1.put("wd80",windChangeName[12]+"米风向(°)");
          map1.put("wd90",windChangeName[13]+"米风向(°)");
          map1.put("wd100",windChangeName[14]+"米风向(°)");
          map1.put("wd170",windChangeName[15]+"米风向(°)");
        }
      }
    } catch (RuntimeException e) {
      String errorInfo = "NWP风速风向别名参数获取失败" + CommonUtil.printStackTraceToString(e);
      log.error(errorInfo, e);
    }
    if (map1.isEmpty()){
      map1.put("preTime","预测时间");
      map1.put("scTime","生成时间");
      map1.put("pressure","气压(KPa)");
      map1.put("t","温度(℃)");
      map1.put("rh","湿度(%)");
      map1.put("swr","总辐射(W/㎡)");
      map1.put("directRadiation","直辐射(W/㎡)");
      map1.put("diffuseRadiation","散辐射(W/㎡)");
      map1.put("senf","热感通量(W/㎡)");
      map1.put("ws10","10米风速(m/s)");
      map1.put("ws30","30米风速(m/s)");
      map1.put("ws50","50米风速(m/s)");
      map1.put("ws70","70米风速(m/s)");
      map1.put("ws80","80米风速(m/s)");
      map1.put("ws90","90米风速(m/s)");
      map1.put("ws100","100米风速(m/s)");
      map1.put("ws170","170米风速(m/s)");
      map1.put("wd10","10米风向(m/s)");
      map1.put("wd30","30米风向(m/s)");
      map1.put("wd50","50米风向(m/s)");
      map1.put("wd70","70米风向(m/s)");
      map1.put("wd80","80米风向(m/s)");
      map1.put("wd90","90米风向(m/s)");
      map1.put("wd100","100米风向(m/s)");
      map1.put("wd170","170米风向(m/s)");
    }
    map.put("cName",map1);
    //################  统计  ##################
    int days = DateMomentUtil.getDaysBetweenTwoDate(startTime,endTime+1000);
    List<Nwp> dataList = new ArrayList<>();
    for (int i = 0 ; i < days; i++){
      Nwp nwp = new Nwp();
      List<Nwp> nwpList = nwpRepository.findByPreTimeBetween(startTime + ((long) i * 60 * 60 * 24 * 1000),startTime + ((long) (i+1) * 60 * 60 * 24 * 1000 - 1000));
      BigDecimal t = new BigDecimal(0);
      BigDecimal rh = new BigDecimal(0);
      BigDecimal pressure = new BigDecimal(0);
      BigDecimal swr = new BigDecimal(0);
      BigDecimal lwr = new BigDecimal(0);
      BigDecimal directRadiation = new BigDecimal(0);
      BigDecimal diffuseRadiation = new BigDecimal(0);
      BigDecimal senf = new BigDecimal(0);
      BigDecimal ws10 = new BigDecimal(0);
      BigDecimal ws30 = new BigDecimal(0);
      BigDecimal ws50 = new BigDecimal(0);
      BigDecimal ws70 = new BigDecimal(0);
      BigDecimal ws80 = new BigDecimal(0);
      BigDecimal ws90 = new BigDecimal(0);
      BigDecimal ws100 = new BigDecimal(0);
      BigDecimal ws170 = new BigDecimal(0);
      BigDecimal wd10 = new BigDecimal(0);
      BigDecimal wd30 = new BigDecimal(0);
      BigDecimal wd50 = new BigDecimal(0);
      BigDecimal wd70 = new BigDecimal(0);
      BigDecimal wd80 = new BigDecimal(0);
      BigDecimal wd90 = new BigDecimal(0);
      BigDecimal wd100 = new BigDecimal(0);
      BigDecimal wd170 = new BigDecimal(0);
      for(int j = 0; j < nwpList.size(); j++ ){
        t = t.add(nwpList.get(j).getT() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getT());
        rh = rh.add(nwpList.get(j).getRh() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getRh());
        pressure = pressure.add(nwpList.get(j).getPressure() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getPressure());
        swr = swr.add(nwpList.get(j).getSwr() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getSwr());
        lwr = lwr.add(nwpList.get(j).getLwr() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getLwr());
        directRadiation = directRadiation.add(nwpList.get(j).getDirectRadiation() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getDirectRadiation());
        diffuseRadiation = diffuseRadiation.add(nwpList.get(j).getDiffuseRadiation() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getDiffuseRadiation());
        senf = senf.add(nwpList.get(j).getSenf() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getSenf());
        ws10 = ws10.add(nwpList.get(j).getWs10() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWs10());
        ws30 = ws30.add(nwpList.get(j).getWs30() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWs30());
        ws50 = ws50.add(nwpList.get(j).getWs50() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWs50());
        ws70 = ws70.add(nwpList.get(j).getWs70() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWs70());
        ws80 = ws80.add(nwpList.get(j).getWs80() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWs80());
        ws90 = ws90.add(nwpList.get(j).getWs90() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWs90());
        ws100 = ws100.add(nwpList.get(j).getWs100() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWs100());
        ws170 = ws170.add(nwpList.get(j).getWs170() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWs170());
        wd10 = wd10.add(nwpList.get(j).getWd10() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWd10());
        wd30 = wd30.add(nwpList.get(j).getWd30() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWd30());
        wd50 = wd50.add(nwpList.get(j).getWd50() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWd50());
        wd70 = wd70.add(nwpList.get(j).getWd70() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWd70());
        wd80 = wd80.add(nwpList.get(j).getWd80() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWd80());
        wd90 = wd90.add(nwpList.get(j).getWd90() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWd90());
        wd100 = wd100.add(nwpList.get(j).getWd100() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWd100());
        wd170 = wd170.add(nwpList.get(j).getWd170() == null ? BigDecimal.valueOf(0) : nwpList.get(j).getWd170());
      }
      if(nwpList.size() > 0){
        nwp.setScTime(nwpList.get(0).getScTime());
        nwp.setScDate(nwpList.get(0).getScDate());
        nwp.setPreTime(nwpList.get(0).getPreTime());
        nwp.setPreDate(nwpList.get(0).getPreDate());
        nwp.setFarmId(nwpList.get(0).getFarmId());
        nwp.setT(t.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : t.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setRh(rh.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : rh.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setPressure(pressure.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : pressure.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setSwr(swr.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : swr.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setLwr(lwr.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : lwr.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setDirectRadiation(directRadiation.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : directRadiation.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setDiffuseRadiation(diffuseRadiation.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : diffuseRadiation.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setSenf(senf.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : senf.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWs10(ws10.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : ws10.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWs30(ws30.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : ws30.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWs50(ws50.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : ws50.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWs70(ws70.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : ws70.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWs80(ws80.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : ws80.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWs90(ws90.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : ws90.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWs100(ws100.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : ws100.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWs170(ws170.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : ws170.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWd10(wd10.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : wd10.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWd30(wd30.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : wd30.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWd50(wd50.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : wd50.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWd70(wd70.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : wd70.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWd80(wd80.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : wd80.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWd90(wd90.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : wd90.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWd100(wd100.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : wd100.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        nwp.setWd170(wd170.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue() < 0d ? new BigDecimal(0) : wd170.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP));
        dataList.add(nwp);
      }
    }

    map.put("statistics", dataList);
    return map;
  }

  /**
   * 根据时间和编号 分页查询条件 yh
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @return 过滤条件
   */
  Specification<Nwp> specificationFindByTimeAndNo(final Long startTime, final Long endTime) {
    return (Specification<Nwp>) (root, criteriaQuery, cb) -> {
      List<Predicate> predicates = new ArrayList<>();
      if (startTime!=null) {
        //大于或等于传入时间
        predicates.add(cb.greaterThanOrEqualTo(root.get("preTime").as(Long.class), startTime));
      }
      if (endTime!=null) {
        //小于传入时间
        predicates.add(cb.lessThan(root.get("preTime").as(Long.class), endTime));
      }

      //添加排序的功能
      return cb.and(predicates.toArray(new Predicate[predicates.size()]));

    };
  }

  /**
   *  对集合进行 -99替换null操作，主要用于图标展示空值
   *
   * @param datas  需要替换集合
   */
  public void defaultReplace(List<Nwp> datas){
    BigDecimal nullBig =  new BigDecimal(-99);
    for(Nwp i :datas){


      if(i.getT().compareTo(nullBig)==0){
        i.setT(null);
      }
      if(i.getRh().compareTo(nullBig)==0){
        i.setRh(null);
      }
      if(i.getPressure().compareTo(nullBig)==0){
        i.setPressure(null);
      }
      if(i.getSwr().compareTo(nullBig)==0){
        i.setSwr(null);
      }
      if(i.getDirectRadiation().compareTo(nullBig)==0){
        i.setDirectRadiation(null);
      }
      if(i.getDiffuseRadiation().compareTo(nullBig)==0){
        i.setDiffuseRadiation(null);
      }
      if(i.getSenf().compareTo(nullBig)==0){
        i.setSenf(null);
      }
      if(i.getWs10().compareTo(nullBig)==0){
        i.setWs10(null);
      }
      if(i.getWs30().compareTo(nullBig)==0){
        i.setWs30(null);
      }
      if(i.getWs50().compareTo(nullBig)==0){
        i.setWs50(null);
      }
      if(i.getWs70().compareTo(nullBig)==0){
        i.setWs70(null);
      }
      if(i.getWs80().compareTo(nullBig)==0){
        i.setWs80(null);
      }
      if(i.getWs90().compareTo(nullBig)==0){
        i.setWs90(null);
      }
      if(i.getWs100().compareTo(nullBig)==0){
        i.setWs100(null);
      }
      if(i.getWs170().compareTo(nullBig)==0){
        i.setWs170(null);
      }
      if(i.getWd10().compareTo(nullBig)==0){
        i.setWd10(null);
      }
      if(i.getWd30().compareTo(nullBig)==0){
        i.setWd30(null);
      }
      if(i.getWd50().compareTo(nullBig)==0){
        i.setWd50(null);
      }
      if(i.getWd70().compareTo(nullBig)==0){
        i.setWd70(null);
      }
      if(i.getWd80().compareTo(nullBig)==0){
        i.setWd80(null);
      }
      if(i.getWd90().compareTo(nullBig)==0){
        i.setWd90(null);
      }
      if(i.getWd100().compareTo(nullBig)==0){
        i.setWd100(null);
      }
      if(i.getWd170().compareTo(nullBig)==0){
        i.setWd170(null);
      }

    }

  }


  /**
   * （统计查询：nwp风速统计（吉林22））
   * @param startTime     开始时间
   * @param endTime       结束时间
   * @param statistics    统计类型
   * @param wsHeight      层高
   * @return
   */
  public List<Map<String, Object>> getWsStatistics( Long startTime,
                                                    Long endTime,
                                                    String statistics,
                                                    Integer wsHeight) {
    List<Map<String, Object>> list = new ArrayList<>();
    List<Map<String, Object>> resultList = new ArrayList<>();

    List<Nwp> nList = nwpRepository.findByPreTimeBetween(startTime, endTime);

    List<Map<String, Float>> beforeFormatting = new ArrayList<>();
    Map<String, List<Float>> afterFormatting = new HashMap<>();
    String dateformat = "yyyyMMdd";

    if (statistics.equals("month")) {
      dateformat = "yyyyMM";
    }

    if (statistics.equals("year")) {
      dateformat = "yyyy";
    }


    if (wsHeight == 10) {
      for (Nwp n : nList) {
        Map<String, Float> map1 = new HashMap();
        map1.put(DateFormatUtils.format(n.getPreTime(), dateformat), n.getWs10().floatValue());
        beforeFormatting.add(map1);
      }
      afterFormatting = beforeFormatting.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

    }
    if (wsHeight == 30) {
      for (Nwp n : nList) {
        Map<String, Float> map1 = new HashMap();
        map1.put(DateFormatUtils.format(n.getPreTime(), dateformat), n.getWs30().floatValue());
        beforeFormatting.add(map1);
      }
      afterFormatting = beforeFormatting.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

    }
    if (wsHeight == 50) {
      for (Nwp n : nList) {
        Map<String, Float> map1 = new HashMap();
        map1.put(DateFormatUtils.format(n.getPreTime(), dateformat), n.getWs50().floatValue());
        beforeFormatting.add(map1);
      }
      afterFormatting = beforeFormatting.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

    }

    if (wsHeight == 70) {
      for (Nwp n : nList) {
        Map<String, Float> map1 = new HashMap();
        map1.put(DateFormatUtils.format(n.getPreTime(), dateformat), n.getWs70().floatValue());
        beforeFormatting.add(map1);
      }
      afterFormatting = beforeFormatting.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

    }

    if (wsHeight == 80) {
      for (Nwp n : nList) {
        Map<String, Float> map1 = new HashMap();
        map1.put(DateFormatUtils.format(n.getPreTime(), dateformat), n.getWs80().floatValue());
        beforeFormatting.add(map1);
      }
      afterFormatting = beforeFormatting.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

    }
    if (wsHeight == 90) {
      for (Nwp n : nList) {
        Map<String, Float> map1 = new HashMap();
        map1.put(DateFormatUtils.format(n.getPreTime(), dateformat), n.getWs90().floatValue());
        beforeFormatting.add(map1);
      }
      afterFormatting = beforeFormatting.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

    }
    if (wsHeight == 100) {
      for (Nwp n : nList) {
        Map<String, Float> map1 = new HashMap();
        map1.put(DateFormatUtils.format(n.getPreTime(), dateformat), n.getWs100().floatValue());
        beforeFormatting.add(map1);
      }
      afterFormatting = beforeFormatting.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

    }
    if (wsHeight == 170) {
      for (Nwp n : nList) {
        Map<String, Float> map1 = new HashMap();
        map1.put(DateFormatUtils.format(n.getPreTime(), dateformat), n.getWs170().floatValue());
        beforeFormatting.add(map1);
      }
      afterFormatting = beforeFormatting.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

    }
    Map<String, List<Float>> resultMap = new TreeMap<>((str1, str2) -> str1.compareTo(str2));
    resultMap.putAll(afterFormatting);

    for (String key : resultMap.keySet()) {
      List<Float> n = afterFormatting.get(key);
      Float maxValue = Collections.max(n);
      Float minValue = Collections.min(n);
      Float init = 0f;
      BigDecimal sum = new BigDecimal(init);
      for (int i = 0;i<n.size();i++){
        BigDecimal value = new BigDecimal(n.get(i));
        sum = sum.add(value);
      }
      BigDecimal size = new BigDecimal(n.size());
      BigDecimal avgValue = sum.divide(size,4,BigDecimal.ROUND_HALF_UP);
      BigDecimal topWs = new BigDecimal(maxValue).multiply(new BigDecimal(0.05)).add(new BigDecimal(maxValue)).setScale(4,BigDecimal.ROUND_HALF_UP);
      Map<String,Object> map2 = new HashMap<>();
      map2.put("time",key);
      map2.put("wsHight",wsHeight);
      map2.put("maxWs",maxValue);
      map2.put("topWs",topWs.floatValue());
      map2.put("minWs",minValue);
      map2.put("avgWs",avgValue);
      resultList.add(map2);
    }


    return resultList;
  }




  /**
   * 用于实时对比查询
   * 实时nwp数据
   *
   */
  public List<Nwp> findByPreTimeBetween(Long startTime, Long endTime, Long timeStep){
    List<Nwp> list = new ArrayList<>();
    List<Nwp> resultList = new ArrayList<>();

    list = nwpRepository.findByPreTimeBetween(startTime,endTime);

    if(startTime%timeStep !=0){
      startTime = endTime -(startTime%timeStep);
    }
    BigDecimal nullValue = new BigDecimal(-99);
    for(long i = startTime;i<endTime;i=i+timeStep) {
      long finalI =i;
      List<Nwp> w = list.stream().filter(t -> t.getPreTime() == finalI).collect(Collectors.toList());
      if(w.size()>0){
        resultList.add(w.get(0));
      }
    }

    return resultList;
  }

  public List<Nwp> findByTimeBetween(Long startTime,Long endTime){
    List<Nwp> nwpList = nwpRepository.findByPreTimeBetween(startTime,endTime);

    Collections.sort(nwpList,Comparator.comparing(Nwp::getPreTime));

    return nwpList;
  }


  public int getNwpDay(){
    int nwpDays = 3;
    String days = super.getSysParameter("FILE_NWP_DAYS", "3");
    try {
      nwpDays = Integer.parseInt(days);
    }catch (Exception e){
      log.error("参数转换数据类型失败");
      e.printStackTrace();
    }
    return nwpDays;
  }
}
