package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
import com.jiayue.ipfcst.common.data.entity.PowerStationStatusData;
import com.jiayue.ipfcst.common.data.repository.PowerStationStatusDataRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import com.sun.istack.internal.NotNull;
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.util.*;

@Service
@Slf4j
public class PowerStationStatusDataQueryService extends BaseService {
  private final PowerStationStatusDataRepository powerStationStatusDataRepository;


  @Autowired
  public PowerStationStatusDataQueryService(PowerStationStatusDataRepository powerStationStatusDataRepository){
    this.powerStationStatusDataRepository = powerStationStatusDataRepository;
  }

  @Transactional(propagation = Propagation.REQUIRED)
  public List<PowerStationStatusData> queryRealPowerFor15( Long startTime,  Long endTime) {
    List<PowerStationStatusData> powerStationStatusDataList = this.powerStationStatusDataRepository.findByTimeBetween(new Date(startTime), new Date(endTime));

    powerStationStatusDataList.sort(Comparator.comparing(PowerStationStatusData::getTime));

    try {
      powerStationStatusDataList = convertPowerStationStatusData(powerStationStatusDataList, 1, 900000L);
    } catch (Exception var6) {
      log.error("", var6);
    }

    return powerStationStatusDataList;
  }


  public List<PowerStationStatusData> convertPowerStationStatusData(List<PowerStationStatusData> powerStationStatusDataList, int signType,  Long momentLength) throws Exception {
    List<PowerStationStatusData> convertRealPowerList = new ArrayList();
    Map<String, List<PowerStationStatusData>> tempMap = new HashMap();
    Iterator var7 = powerStationStatusDataList.iterator();

    while(var7.hasNext()) {
      PowerStationStatusData powerStationStatusData = (PowerStationStatusData)var7.next();
      Long tempTime = DateMomentUtil.getMomentTime(powerStationStatusData.getTime().getTime(), signType, momentLength);
      this.putMap(tempMap, String.valueOf(tempTime), powerStationStatusData);
    }

    Iterator var16 = tempMap.keySet().iterator();

    while(var16.hasNext()) {
      String time = (String)var16.next();
      List<PowerStationStatusData> tempPowerStationStatusDataList = (List)tempMap.get(time);
      PowerStationStatusData tempRealPower = new PowerStationStatusData();
      BigDecimal tempAbleValue = new BigDecimal(0.0);

      PowerStationStatusData realPower;

      for(Iterator var18 = tempPowerStationStatusDataList.iterator(); var18.hasNext(); tempAbleValue = tempAbleValue.add(realPower.getAbleValue())) {
        realPower = (PowerStationStatusData)var18.next();
      }

      tempRealPower.setTime(new Date(Long.valueOf(time)));
      int n = tempPowerStationStatusDataList.size();
      tempRealPower.setAbleValue(tempAbleValue.divide(new BigDecimal(n),2,BigDecimal.ROUND_HALF_UP));
      convertRealPowerList.add(tempRealPower);
    }

    convertRealPowerList.sort(Comparator.comparing(PowerStationStatusData::getTime));
    return convertRealPowerList;
  }

  private <T> void putMap(Map<String, List<T>> map, String key, T t) {
    map.computeIfAbsent(key, (k) -> {
      return new ArrayList();
    });
    ((List)map.get(key)).add(t);
  }
}
