package com.jiayue.ipfcst.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.repository.SysParameterRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RealTimeComprehensiveContrastOneService extends BaseService {


  private final ForecastPowerShortTermOneService forecastPowerShortTermOneService;
  private final PowerStationStatusDataService powerStationStatusDataService;
  private final DisplayConfigService displayConfigService;

  @Autowired
  public RealTimeComprehensiveContrastOneService(ForecastPowerShortTermOneService forecastPowerShortTermOneService,
                                                 PowerStationStatusDataService powerStationStatusDataService,
                                                 DisplayConfigService displayConfigService) {
    this.forecastPowerShortTermOneService = forecastPowerShortTermOneService;
    this.powerStationStatusDataService = powerStationStatusDataService;
    this.displayConfigService = displayConfigService;
  }


  public Map<String, Object> realTimeComprehensiveContrast(Long startTime,
                                                           Long endTime,
                                                           Long jgTime) throws BusinessException {
    Map<String, Object> map = new HashMap<>();
    Map<String, Object> rpMap = new HashMap<>();
    Map<String, Object> rDqMap = new HashMap<>();

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

    rDqMap = forecastPowerShortTermOneService.findByForecastTimeBetweenForComprehensive(startTime, endTime, jgTime);
    List<Float> rDqList = (List<Float>) rDqMap.get("datas");
    List<String> times = new ArrayList<>();
    List<Map<String, Object>> tableResult = new ArrayList<>();
    times = (List<String>) rpMap.get("times");
    List<Float> realvalueList = (List<Float>) rpMap.get("realDatas");

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

    String displayKyLl = "0";
    DisplayConfig displayConfig = displayConfigService.findByDisplayCode("rtWindTowerInfos");
    for (int i = 0; i < times.size(); i++) {

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

      if (Arrays.asList(showCode).contains("realValue")) {
        if (realvalueList.get(i) != null) {
          dtoMap.put("realValue", realvalueList.get(i));
        } else {
          dtoMap.put("realValue", "");
        }
      }

      if (Arrays.asList(showCode).contains("rDq")) {
        if (rDqList.get(i) != null) {
          dtoMap.put("rDq", rDqList.get(i));
        } else {
          dtoMap.put("rDq", "");
        }
      }
      tableResult.add(dtoMap);
    }

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