package com.jiayue.ipfcst.service;

import com.jiayue.ipfcst.common.data.entity.NwpHis;
import com.jiayue.ipfcst.common.data.repository.NwpHisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * NWP历史实时业务层
 *
 * @author tl
 * @version 3.0
 * @since 2020/7/9 09:12
 */
@Service
public class NwpHisService {
  final private NwpHisRepository nwpHisRepository;



  @Autowired
  public NwpHisService(NwpHisRepository nwpHisRepository){
    this.nwpHisRepository = nwpHisRepository;
  }


  /**
   * 条件查询
   * nwp数据对比
   *
   */
  public List<NwpHis> findByPreTimeBetweenAndForecastHowLongAgo(Long startTime, Long endTime,Integer howLongAgo,Long timeStep){
    List<NwpHis> list = new ArrayList<>();
    List<NwpHis> resultList = new ArrayList<>();

    list = nwpHisRepository.findByPreTimeBetweenAndForecastHowLongAgo(startTime,endTime,howLongAgo);

    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<NwpHis> w = list.stream().filter(t -> t.getPreTime() == finalI).collect(Collectors.toList());
      if(w.size()>0){
        resultList.add(w.get(0));
      }
    }

    return resultList;
  }
}
