package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.repository.ForecastPowerShortTermRepository;
import com.jiayue.ipfcst.common.data.repository.ForecastPowerUltraShortTermRepository;
import com.jiayue.ipfcst.common.data.repository.ManualInterventionLogRepository;
import com.jiayue.ipfcst.console.util.ForecastPowerAndManualIntervention;
import com.jiayue.ipfcst.fileupload.service.BaseUploadFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 人工干预日志逻辑类
 *
 * @author whc
 * @version 2.0
 * @since 2020/08/26 09:41
 */
@Service
@Slf4j
public class ManualInterventionLogService extends BaseUploadFileService {

  private final ManualInterventionLogRepository manualInterventionLogRepository;
  private final ForecastPowerUltraShortTermService forecastPowerUltraShortTermService;
  private final ForecastPowerShortTermService forecastPowerShortTermService;
  private final ManualInterventionService manualInterventionService;
  private final ForecastPowerUltraShortTermRepository forecastPowerUltraShortTermRepository;
  private final ForecastPowerShortTermRepository forecastPowerShortTermRepository;
  private final ManualInterventionRatioService manualInterventionRatioService;
  private final ElectricFieldService electricFieldService;
  public ManualInterventionLogService(ManualInterventionLogRepository manualInterventionLogRepository, ForecastPowerUltraShortTermService forecastPowerUltraShortTermService, ForecastPowerShortTermService forecastPowerShortTermService, ManualInterventionService manualInterventionService, ForecastPowerUltraShortTermRepository forecastPowerUltraShortTermRepository, ForecastPowerShortTermRepository forecastPowerShortTermRepository, ManualInterventionRatioService manualInterventionRatioService, ElectricFieldService electricFieldService) {
    this.manualInterventionLogRepository = manualInterventionLogRepository;
    this.forecastPowerUltraShortTermService = forecastPowerUltraShortTermService;
    this.forecastPowerShortTermService = forecastPowerShortTermService;
    this.manualInterventionService = manualInterventionService;
    this.forecastPowerUltraShortTermRepository = forecastPowerUltraShortTermRepository;
    this.forecastPowerShortTermRepository = forecastPowerShortTermRepository;
    this.manualInterventionRatioService = manualInterventionRatioService;
    this.electricFieldService = electricFieldService;
  }

  public List<ManualInterventionLog> queryManualInterventionLog(Long startTime, Long endTime, String fileType){
    System.out.println("开始时间"+startTime);
    System.out.println("结束时间"+endTime);
    System.out.println("类型"+fileType);
    List<ManualInterventionLog> manualInterventionLogList = manualInterventionLogRepository.findByOperationalTimeBetweenAndFileTypeOrderByOperationalTimeDesc(startTime,endTime,fileType);
    System.out.println("结果"+manualInterventionLogList);
    return manualInterventionLogList;
  }
  public List<List<Object>>  queryForecastAndManualInterventionData(Long startTime, Long endTime, String fileType){


    List<List<Object>> lists = new ArrayList<>();
    Map<String,Object> map = new HashMap<>();
    List<ForecastPowerUltraShortTerm> ulist = new ArrayList<>();
    List<ForecastPowerShortTerm> slist = new ArrayList<>();
    List<ForecastPowerUltraShortTerm> ulist1 = new ArrayList<>();
    List<ForecastPowerShortTerm> slist1 = new ArrayList<>();
    //查询预测数据
    if(fileType == "0"||fileType.equals("0")){
      slist = forecastPowerShortTermRepository.findByForecastTimeBetween(startTime,endTime);
    }else{
      ulist = forecastPowerUltraShortTermRepository.findByForecastTimeBetween(startTime,endTime);
    }
    //如果没有预测数据 则生成
    if(fileType == "0"||fileType.equals("0")){
      if(slist.size()==0){
        forecastPowerShortTermService.getForecastPowerShortTerm(startTime,endTime);
      }
    }else{
      if(ulist.size()==0){
        forecastPowerUltraShortTermService.getForecastPowerUltraShortTerm(startTime,endTime);
      }
    }
    //短期/超短期 曲线数据
    Map<String,Object> map1 = new HashMap<>();
    List<ManualInterventionData> manualInterventionDataList;
    Map<Long, BigDecimal> ultraMap = new HashMap<>();
    Map<Long, BigDecimal> sMap = new HashMap<>();
    Map<Long, BigDecimal> mMap = new HashMap<>();
    ForecastPowerAndManualIntervention forecastPowerAndManualIntervention;
    List<ForecastPowerAndManualIntervention> forecastPowerAndManualInterventionList = new ArrayList<>();
    if(fileType == "0"||fileType.equals("0")){
      slist1 = forecastPowerShortTermRepository.findByForecastTimeBetween(startTime,endTime);
      for (ForecastPowerShortTerm forecastPowerShortTerm : slist1) {
        sMap.put(forecastPowerShortTerm.getForecastTime(),forecastPowerShortTerm.getFpValue());
      }
    }else{
      ulist1 = forecastPowerUltraShortTermRepository.findByForecastTimeBetween(startTime,endTime);
      for (ForecastPowerUltraShortTerm forecastPowerUltraShortTerm : ulist1) {
        ultraMap.put(forecastPowerUltraShortTerm.getForecastTime(),forecastPowerUltraShortTerm.getFpValue());
      }
    }
    //干预数据 如果有干预数据则取出短期/超短期替换相应点位
    manualInterventionDataList = manualInterventionService.queryManualIntervention(startTime,endTime,fileType);
    if(manualInterventionDataList.size()>0){
      for (ManualInterventionData manualInterventionData : manualInterventionDataList) {
        mMap.put(manualInterventionData.getInterveneTime(),manualInterventionData.getInterveneData());
      }
    }

    for (Long tempDateTime = startTime; tempDateTime < endTime; tempDateTime = tempDateTime + 15 * 60 * 1000) {
      BigDecimal dq = sMap.get(tempDateTime);
      BigDecimal ma = mMap.get(tempDateTime);
      BigDecimal cdq = ultraMap.get(tempDateTime);
      if(fileType == "0"||fileType.equals("0")){
        if(ma==null||ma.equals(null)){
          forecastPowerAndManualIntervention = new ForecastPowerAndManualIntervention();
          forecastPowerAndManualIntervention.setIntervention(dq);
          forecastPowerAndManualIntervention.setTime(tempDateTime);
          forecastPowerAndManualInterventionList.add(forecastPowerAndManualIntervention);
        }else{
          forecastPowerAndManualIntervention = new ForecastPowerAndManualIntervention();
          forecastPowerAndManualIntervention.setIntervention(ma);
          forecastPowerAndManualIntervention.setTime(tempDateTime);
          forecastPowerAndManualInterventionList.add(forecastPowerAndManualIntervention);
        }
      }else{
        if(ma==null||ma.equals(null)){
          forecastPowerAndManualIntervention = new ForecastPowerAndManualIntervention();
          forecastPowerAndManualIntervention.setIntervention(cdq);
          forecastPowerAndManualIntervention.setTime(tempDateTime);
          forecastPowerAndManualInterventionList.add(forecastPowerAndManualIntervention);
        }else{
          forecastPowerAndManualIntervention = new ForecastPowerAndManualIntervention();
          forecastPowerAndManualIntervention.setIntervention(ma);
          forecastPowerAndManualIntervention.setTime(tempDateTime);
          forecastPowerAndManualInterventionList.add(forecastPowerAndManualIntervention);
        }
      }
    }
    ElectricField electricFieldInfo =   electricFieldService.get();
    List<ManualInterventionRatioData> manualInterventionRatioDataList = manualInterventionRatioService.queryManualInterventionRatio(startTime,endTime,fileType);
    if(manualInterventionRatioDataList.size()>0){
      //循环所有记录
      for(ManualInterventionRatioData manualInterventionRatioData :manualInterventionRatioDataList){

        for (Long tempTime = manualInterventionRatioData.getInterveneStartTime(); tempTime <= manualInterventionRatioData.getInterveneEndTime(); tempTime = tempTime + 15 * 60 * 1000L) {
          int mrflag = 0;
          for (ForecastPowerAndManualIntervention f : forecastPowerAndManualInterventionList) {
            //如果该时间设置了系数则数值*系数
            if(f.getTime().equals(tempTime)){
              BigDecimal fpValue;
              try{
                fpValue = f.getIntervention().multiply(new BigDecimal(manualInterventionRatioData.getRatio().toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
              }catch (Exception e){
                fpValue = new BigDecimal(0);
              }
              //如果计算后的数据大于装机容量 则fpValue=装机容量
              if((fpValue).compareTo(new BigDecimal(electricFieldInfo.getCapacity().toString())) > 0){
                fpValue = new BigDecimal(electricFieldInfo.getCapacity().toString());
              }
              f.setIntervention(fpValue);
              f.setTime(f.getTime());
              forecastPowerAndManualInterventionList.set(mrflag, f);
            }
            mrflag++;
          }
        }
      }
    }
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    for (long i = startTime; i < endTime; i = i + 900000L) {
      List<Object> list = new ArrayList<>();
      long finalI = i;
      list.add(simpleDateFormat.format(new Date(finalI)));
      List<ForecastPowerAndManualIntervention> p = forecastPowerAndManualInterventionList.stream().filter(t -> t.getTime() == finalI).collect(Collectors.toList());
      if (p != null && p.size() > 0) {
        if (p.get(0).getIntervention()!= null) {
          list.add(p.get(0).getIntervention().floatValue());
        }else {
          list.add(0);
        }
        /*list.add(p.get(0).getIntervention().floatValue());*/

      } else {
        list.add("");
      }
      lists.add(list);
    }

    return lists;
  }

  public List<ManualInterventionLog> findByFileTypeOrderByOperationalTimeDesc(String fileType){
    return manualInterventionLogRepository.findByFileTypeOrderByOperationalTimeDesc(fileType);
  }

  public void save(ManualInterventionLog manualInterventionLog){
    manualInterventionLogRepository.save(manualInterventionLog);
  }


/*
  public List<List<Object>> queryForecastAndManualInterventionData2(Long startTime, Long endTime, String fileType){
    List<List<Object>> lists = new ArrayList<>();
    Map<String,Object> map = new HashMap<>();
    List<ForecastPowerUltraShortTerm> ulist = new ArrayList<>();
    List<ForecastPowerShortTerm> slist = new ArrayList<>();
    List<ForecastPowerUltraShortTerm> ulist1 = new ArrayList<>();
    List<ForecastPowerShortTerm> slist1 = new ArrayList<>();
    //查询预测数据
    if(fileType == "0"||fileType.equals("0")){
      slist = forecastPowerShortTermRepository.findByForecastTimeBetween(startTime,endTime);
    }else{
      ulist = forecastPowerUltraShortTermRepository.findByForecastTimeBetween(startTime,endTime);
    }
    //如果没有预测数据 则生成
    if(fileType == "0"||fileType.equals("0")){
      if(slist.size()==0){
        forecastPowerShortTermService.getForecastPowerShortTerm(startTime,endTime);
      }
    }else{
      if(ulist.size()==0){
        forecastPowerUltraShortTermService.getForecastPowerUltraShortTerm(startTime,endTime);
      }
    }



    //短期/超短期 曲线数据
    Map<String,Object> map1 = new HashMap<>();
    List<ManualInterventionData> manualInterventionDataList;
    Map<Long, BigDecimal> ultraMap = new HashMap<>();
    Map<Long, BigDecimal> sMap = new HashMap<>();
    Map<Long, BigDecimal> mMap = new HashMap<>();
    ForecastPowerAndManualIntervention forecastPowerAndManualIntervention;
    List<ForecastPowerAndManualIntervention> forecastPowerAndManualInterventionList = new ArrayList<>();
    if(fileType == "0"||fileType.equals("0")){
      slist1 = forecastPowerShortTermRepository.findByForecastTimeBetween(startTime,endTime);
      for (ForecastPowerShortTerm forecastPowerShortTerm : slist1) {
        sMap.put(forecastPowerShortTerm.getForecastTime(),forecastPowerShortTerm.getFpValue());
      }
    }else{
      ulist1 = forecastPowerUltraShortTermRepository.findByForecastTimeBetween(startTime,endTime);
      for (ForecastPowerUltraShortTerm forecastPowerUltraShortTerm : ulist1) {
        ultraMap.put(forecastPowerUltraShortTerm.getForecastTime(),forecastPowerUltraShortTerm.getFpValue());
      }
    }
    //干预数据 如果有干预数据则取出短期/超短期替换相应点位
    manualInterventionDataList = manualInterventionService.queryManualIntervention(startTime,endTime,fileType);
    if(manualInterventionDataList.size()>0){
      for (ManualInterventionData manualInterventionData : manualInterventionDataList) {
        mMap.put(manualInterventionData.getInterveneTime(),manualInterventionData.getInterveneData());
      }
    }

    for (Long tempDateTime = startTime; tempDateTime < endTime; tempDateTime = tempDateTime + 15 * 60 * 1000) {
      BigDecimal dq = sMap.get(tempDateTime);
      BigDecimal ma = mMap.get(tempDateTime);
      BigDecimal cdq = ultraMap.get(tempDateTime);
      if(fileType == "0"||fileType.equals("0")){
        if(ma==null||ma.equals(null)){
          forecastPowerAndManualIntervention = new ForecastPowerAndManualIntervention();
          forecastPowerAndManualIntervention.setIntervention(dq);
          forecastPowerAndManualIntervention.setTime(tempDateTime);
          forecastPowerAndManualInterventionList.add(forecastPowerAndManualIntervention);
        }else{
          forecastPowerAndManualIntervention = new ForecastPowerAndManualIntervention();
          forecastPowerAndManualIntervention.setIntervention(ma);
          forecastPowerAndManualIntervention.setTime(tempDateTime);
          forecastPowerAndManualInterventionList.add(forecastPowerAndManualIntervention);
        }
      }else{
        if(ma==null||ma.equals(null)){
          forecastPowerAndManualIntervention = new ForecastPowerAndManualIntervention();
          forecastPowerAndManualIntervention.setIntervention(cdq);
          forecastPowerAndManualIntervention.setTime(tempDateTime);
          forecastPowerAndManualInterventionList.add(forecastPowerAndManualIntervention);
        }else{
          forecastPowerAndManualIntervention = new ForecastPowerAndManualIntervention();
          forecastPowerAndManualIntervention.setIntervention(ma);
          forecastPowerAndManualIntervention.setTime(tempDateTime);
          forecastPowerAndManualInterventionList.add(forecastPowerAndManualIntervention);
        }
      }
    }
    ElectricField electricFieldInfo =   electricFieldService.get();
    List<ManualInterventionRatioData> manualInterventionRatioDataList = manualInterventionRatioService.queryManualInterventionRatio(startTime,endTime,fileType);
    if(manualInterventionRatioDataList.size()>0){
      //循环所有记录
      for(ManualInterventionRatioData manualInterventionRatioData :manualInterventionRatioDataList){

        for (Long tempTime = manualInterventionRatioData.getInterveneStartTime(); tempTime <= manualInterventionRatioData.getInterveneEndTime(); tempTime = tempTime + 15 * 60 * 1000L) {
          int mrflag = 0;
          for (ForecastPowerAndManualIntervention f : forecastPowerAndManualInterventionList) {
            //如果该时间设置了系数则数值*系数
            if(f.getTime().equals(tempTime)){
              BigDecimal fpValue;
              try{
                fpValue = f.getIntervention().multiply(new BigDecimal(manualInterventionRatioData.getRatio().toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
              }catch (Exception e){
                fpValue = new BigDecimal(0);
              }
              //如果计算后的数据大于装机容量 则fpValue=装机容量
              if((fpValue).compareTo(new BigDecimal(electricFieldInfo.getCapacity().toString())) > 0){
                fpValue = new BigDecimal(electricFieldInfo.getCapacity().toString());
              }
              f.setIntervention(fpValue);
              f.setTime(f.getTime());
              forecastPowerAndManualInterventionList.set(mrflag, f);
            }
            mrflag++;
          }
        }
      }
    }
    return forecastPowerAndManualInterventionList;
  }
  */

}
