package com.jiayue.ipfcst.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.entity.DisplayConfig;
import com.jiayue.ipfcst.common.data.entity.ReportWindTowerStatusData;
import com.jiayue.ipfcst.common.data.entity.WindTowerStatusData;
import com.jiayue.ipfcst.common.data.repository.ReportWindTowerStatusDataRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 上报测风塔数据业务层
 *
 * @author tl
 * @version 3.0
 * @since 2022/4/13 12:11
 */
@Service
public class ReportWindTowerStatusDataService extends BaseService {

  private final ReportWindTowerStatusDataRepository reportWindTowerStatusDataRepository;


  private final PowerStationStatusDataService powerStationStatusDataService;
  private final WindTowerStatusDataService windTowerStatusDataService;
  private final DisplayConfigService displayConfigService;



  @Autowired
  public ReportWindTowerStatusDataService(ReportWindTowerStatusDataRepository reportWindTowerStatusDataRepository,
                                          PowerStationStatusDataService powerStationStatusDataService,
                                          WindTowerStatusDataService windTowerStatusDataService,
                                          DisplayConfigService displayConfigService){
    this.reportWindTowerStatusDataRepository = reportWindTowerStatusDataRepository;
    this.powerStationStatusDataService = powerStationStatusDataService;
    this.windTowerStatusDataService = windTowerStatusDataService;
    this.displayConfigService = displayConfigService;
  }

  /**
   *
   * @return
   */
  public List<ReportWindTowerStatusData> getAll(){
    return reportWindTowerStatusDataRepository.findAll();
  }

  /**
   *
   * @param startTime
   * @param endTime
   * @param no
   * @param otherType
   * @param jgTime
   * @return
   * @throws BusinessException
   */
  public Map<String, Object> reportWindTowerStatusData(Long startTime,
                                                       Long endTime,
                                                       Integer no,
                                                       String otherType,
                                                       Long jgTime) throws BusinessException {
    Map<String, Object> map = new HashMap<>();
    Map<String, Object> rpMap;
    List<WindTowerStatusData> windTowerStatusDataList;
    List<ReportWindTowerStatusData> reportWindTowerStatusDataList;

    rpMap = powerStationStatusDataService.findByTimeBetweenForContrast(new Date(startTime), new Date(endTime), jgTime);


    List<String> times;
    List<Map<String, Object>> tableResult = new ArrayList<>();
    times = (List<String>) rpMap.get("times");

    List<Map<String, Object>> resultList = new ArrayList<>();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    if (otherType.equals("E2")) {
      //风速对比数据
      windTowerStatusDataList = windTowerStatusDataService.findByEquipmentNoAndTimeBetweenOrderByTimeAsc(new Date(startTime), new Date(endTime), no, jgTime);
      reportWindTowerStatusDataList = this.findByEquipmentNoAndTimeBetweenOrderByTimeAsc(new Date(startTime), new Date(endTime), no, jgTime);
      DisplayConfig displayConfig = displayConfigService.findByDisplayCode("reportWindTowerStatusData");
      for (int i = 0; i < times.size(); i++) {
        Map<String, Object> resultMap = new HashMap<>();

        Map<String, Object> dtoMap = new HashMap<>();
        //
        String[] showCode = displayConfig.getShowCode().split(",");
        dtoMap.put("time", times.get(i));

        tableResult.add(dtoMap);
        final String time = times.get(i);
        boolean b = windTowerStatusDataList.stream().filter(m -> sdf.format(m.getTime()).equals(time)).findAny().isPresent();
        if (b) {
          WindTowerStatusData windTowerStatusData = windTowerStatusDataList.stream().filter(o -> sdf.format(o.getTime()).equals(time)).findAny().orElse(null);
          if (Arrays.asList(showCode).contains("wsInst10")) {
            //十米
            resultMap.put("wsInst10", windTowerStatusData.getWsInst10().floatValue() == -99 ? "" : windTowerStatusData.getWsInst10());
          }
          if (Arrays.asList(showCode).contains("wsInst30")) {
            //三十米
            resultMap.put("wsInst30", windTowerStatusData.getWsInst30().floatValue() == -99 ? "" : windTowerStatusData.getWsInst30());
          }
          if (Arrays.asList(showCode).contains("wsInst50")) {
            //五十米
            resultMap.put("wsInst50", windTowerStatusData.getWsInst50().floatValue() == -99 ? "" : windTowerStatusData.getWsInst50());
          }
          if (Arrays.asList(showCode).contains("wsInst60")) {
            //六十米
            resultMap.put("wsInst60", windTowerStatusData.getWsInst60().floatValue() == -99 ? "" : windTowerStatusData.getWsInst60());
          }
          if (Arrays.asList(showCode).contains("wsInst70")) {
            //七十米
            resultMap.put("wsInst70", windTowerStatusData.getWsInst70().floatValue() == -99 ? "" : windTowerStatusData.getWsInst70());
          }
          if (Arrays.asList(showCode).contains("wsInst80")) {
            //八十米
            resultMap.put("wsInst80", windTowerStatusData.getWsInst80().floatValue() == -99 ? "" : windTowerStatusData.getWsInst80());
          }
          if (Arrays.asList(showCode).contains("wsInst90")) {
            //九十米
            resultMap.put("wsInst90", windTowerStatusData.getWsInst90().floatValue() == -99 ? "" : windTowerStatusData.getWsInst90());
          }
          if (Arrays.asList(showCode).contains("wsInst100")) {
            //一百米
            resultMap.put("wsInst100", windTowerStatusData.getWsInst100().floatValue() == -99 ? "" : windTowerStatusData.getWsInst100());
          }
          if (Arrays.asList(showCode).contains("wsInst110")) {
            //一百一十米
            resultMap.put("wsInst110", windTowerStatusData.getWsInst110().floatValue() == -99 ? "" : windTowerStatusData.getWsInst110());
          }
          if (Arrays.asList(showCode).contains("wsInst120")) {
            //一百二十米
            resultMap.put("wsInst120", windTowerStatusData.getWsInst120().floatValue() == -99 ? "" : windTowerStatusData.getWsInst120());
          }
        } else {
          if (Arrays.asList(showCode).contains("wsInst10")) {
            //十米
            resultMap.put("wsInst10", "");
          }
          if (Arrays.asList(showCode).contains("wsInst30")) {
            //三十米
            resultMap.put("wsInst30", "");
          }
          if (Arrays.asList(showCode).contains("wsInst50")) {
            //五十米
            resultMap.put("wsInst50", "");
          }
          if (Arrays.asList(showCode).contains("wsInst60")) {
            //一百一十米
            resultMap.put("wsInst60", "");
          }
          if (Arrays.asList(showCode).contains("wsInst70")) {
            //七十米
            resultMap.put("wsInst70", "");
          }
          if (Arrays.asList(showCode).contains("wsInst80")) {
            //八十米
            resultMap.put("wsInst80", "");
          }
          if (Arrays.asList(showCode).contains("wsInst90")) {
            //九十米
            resultMap.put("wsInst90", "");
          }
          if (Arrays.asList(showCode).contains("wsInst100")) {
            //一百米
            resultMap.put("wsInst100", "");
          }
          if (Arrays.asList(showCode).contains("wsInst110")) {
            //一百一十米
            resultMap.put("wsInst110", "");
          }
          if (Arrays.asList(showCode).contains("wsInst120")) {
            //一百二十米
            resultMap.put("wsInst120", "");
          }
        }
        boolean rb = reportWindTowerStatusDataList.stream().filter(m -> sdf.format(m.getTime()).equals(time)).findAny().isPresent();
        if (rb) {
          ReportWindTowerStatusData reportWindTowerStatusData = reportWindTowerStatusDataList.stream().filter(o -> sdf.format(o.getTime()).equals(time)).findAny().orElse(null);
          if (Arrays.asList(showCode).contains("ws10")) {
            //十米
            resultMap.put("ws10", reportWindTowerStatusData.getWs10().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs10());
          }
          if (Arrays.asList(showCode).contains("ws30")) {
            //三十米
            resultMap.put("ws30", reportWindTowerStatusData.getWs30().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs30());
          }
          if (Arrays.asList(showCode).contains("ws50")) {
            //五十米
            resultMap.put("ws50", reportWindTowerStatusData.getWs50().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs50());
          }
          if (Arrays.asList(showCode).contains("ws60")) {
            //六十米 -
            resultMap.put("ws60", reportWindTowerStatusData.getWs60().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs60());
          }
          if (Arrays.asList(showCode).contains("ws70")) {
            //七十米
            resultMap.put("ws70", reportWindTowerStatusData.getWs70().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs70());
          }
          if (Arrays.asList(showCode).contains("ws80")) {
            //八十米
            resultMap.put("ws80", reportWindTowerStatusData.getWs80().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs80());
          }
          if (Arrays.asList(showCode).contains("ws90")) {
            //九十米
            resultMap.put("ws90", reportWindTowerStatusData.getWs90().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs90());
          }
          if (Arrays.asList(showCode).contains("ws100")) {
            //一百米
            resultMap.put("ws100", reportWindTowerStatusData.getWs100().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs100());
          }
          if (Arrays.asList(showCode).contains("ws110")) {
            //一百一十米
            resultMap.put("ws110", reportWindTowerStatusData.getWs110().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs110());
          }
          if (Arrays.asList(showCode).contains("ws120")) {
            //一百二十米
            resultMap.put("ws120", reportWindTowerStatusData.getWs120().floatValue() == -99 ? "" : reportWindTowerStatusData.getWs120());
          }
        } else {
          if (Arrays.asList(showCode).contains("ws10")) {
            //十米
            resultMap.put("ws10", "");
          }
          if (Arrays.asList(showCode).contains("ws30")) {
            //三十米
            resultMap.put("ws30", "");
          }
          if (Arrays.asList(showCode).contains("ws50")) {
            //五十米
            resultMap.put("ws50", "");
          }
          if (Arrays.asList(showCode).contains("ws60")) {
            //六十米
            resultMap.put("ws60", "");
          }
          if (Arrays.asList(showCode).contains("ws70")) {
            //七十米
            resultMap.put("ws70", "");
          }
          if (Arrays.asList(showCode).contains("ws80")) {
            //八十米
            resultMap.put("ws80", "");
          }
          if (Arrays.asList(showCode).contains("ws90")) {
            //九十米
            resultMap.put("ws90", "");
          }
          if (Arrays.asList(showCode).contains("ws100")) {
            //一百米
            resultMap.put("ws100", "");
          }
          if (Arrays.asList(showCode).contains("ws110")) {
            //一百一十米
            resultMap.put("ws110", "");
          }
          if (Arrays.asList(showCode).contains("ws120")) {
            //一百二十米
            resultMap.put("ws120", "");
          }
        }

        resultList.add(resultMap);
      }
    } else {
    }

    map.put("tableResult", tableResult);
    map.put("resultList", resultList);
    return map;
  }



  /**
   * 条件查询
   *
   */
  public List<ReportWindTowerStatusData> findByEquipmentNoAndTimeBetweenOrderByTimeAsc(Date startTime,Date endTime,Integer no,Long timeStep){
    List<ReportWindTowerStatusData> list;

    List<ReportWindTowerStatusData> resultList = new ArrayList<>();
    list = reportWindTowerStatusDataRepository.findByEquipmentNoAndTimeBetween(no,startTime,endTime);

    long startTimeLong  = startTime.getTime();
    long endTimeLong  = endTime.getTime();

    if(startTimeLong%timeStep !=0){
      startTimeLong = startTimeLong -(startTimeLong%timeStep)+timeStep;
    }
    for(long i = startTimeLong;i<endTimeLong;i=i+timeStep) {
      long finalI =i;
      List<ReportWindTowerStatusData> w = list.stream().filter(t -> t.getTime().getTime() == finalI).collect(Collectors.toList());
      if(w.size()>0){
        resultList.add(w.get(0));
      }
    }

    return resultList;
  }


}
