package com.jiayue.ssi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ssi.entity.ElectricField;
import com.jiayue.ssi.entity.Nwp;
import com.jiayue.ssi.mapper.NwpMapper;
import com.jiayue.ssi.service.ElectricFieldService;
import com.jiayue.ssi.service.NwpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
@Slf4j
public class NwpServiceImpl extends ServiceImpl<NwpMapper, Nwp> implements NwpService {
  @Autowired
  NwpMapper nwpMapper;
  @Autowired
  ElectricFieldService electricFieldService;

  /**
   * 按时间段删除数据
   *
   * @param startTime
   * @param endTime
   * @param listNwp
   */
  @Transactional(propagation = Propagation.REQUIRED)
  @Override
  public void deleteBetweenAndPreTime(Long startTime, Long endTime, List<Nwp> listNwp) {
    QueryWrapper wrapper = new QueryWrapper<>();
    wrapper.ge("pre_time",startTime);
    wrapper.le("pre_time",endTime);
    this.remove(wrapper);
    //保存NWP数据
    this.saveBatch(listNwp);
  }
  /**
   * 按时间查询实时预测短期
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @return 结果集
   */
  @Override
  public Map<String, Object> findByPreTimeBetween(Long startTime, Long endTime) {
    Map<String,Object> map = new HashMap<>();
    List<Nwp> list = new ArrayList<>();
    List<Nwp> checkList = new ArrayList<>();
    Map<String,Object> datas = new LinkedHashMap<>();
    list = nwpMapper.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<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 = electricFieldService.getOne(null);
    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<>();
    if("0".equals(electricField.getFieldType())){
      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);
    }else{
      ws10Collect = checkList.stream().map(Nwp::getWs10).collect(Collectors.toList());
      ws30Collect = checkList.stream().map(Nwp::getWs30).collect(Collectors.toList());
      ws50Collect = checkList.stream().map(Nwp::getWs50).collect(Collectors.toList());
      ws70Collect = checkList.stream().map(Nwp::getWs70).collect(Collectors.toList());
      ws80Collect = checkList.stream().map(Nwp::getWs80).collect(Collectors.toList());
      ws90Collect = checkList.stream().map(Nwp::getWs90).collect(Collectors.toList());
      ws100Collect = checkList.stream().map(Nwp::getWs100).collect(Collectors.toList());
      ws170Collect = checkList.stream().map(Nwp::getWs170).collect(Collectors.toList());
      datas.put("ws10",ws10Collect);
      datas.put("ws30",ws30Collect);
      datas.put("ws50",ws50Collect);
      datas.put("ws70",ws70Collect);
      datas.put("ws80",ws80Collect);
      datas.put("ws90",ws90Collect);
      datas.put("ws100",ws100Collect);
      datas.put("ws170",ws170Collect);

      map1.put("ws10","10米风速");
      map1.put("ws30","30米风速");
      map1.put("ws50","50米风速");
      map1.put("ws70","70米风速");
      map1.put("ws80","80米风速");
      map1.put("ws90","90米风速");
      map1.put("ws100","100米风速");
      map1.put("ws170","170米风速");
    }

    map.put("cName",map1);
    map.put("times",times);
    map.put("datas",datas);
    return map;
  }
  /**
   *  对集合进行 -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);
      }

    }

  }
}
