package com.jiayue.ipfcst.controller;

import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.service.ForecastPowerShortTermHisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 短期预测历史信息restful接口
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/7 10:12
 */
@RestController
@Slf4j
public class ForecastPowerShortTermHisController {
  private final ForecastPowerShortTermHisService forecastPowerShortTermHisService;

  @Autowired
  public ForecastPowerShortTermHisController(ForecastPowerShortTermHisService forecastPowerShortTermHisService) {
    this.forecastPowerShortTermHisService = forecastPowerShortTermHisService;
  }

  /**
   * 按时间查询历史预测短期 yh
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @return 结果集
   */
  @GetMapping(value = "/forecastPowerShortTermHis/{startTime}/{endTime}/{ago}")
  public ResponseVO findByForecastTimeBetween(@PathVariable("startTime") Long startTime,
                                              @PathVariable("endTime") Long endTime,
                                              @PathVariable("endTime") Integer ago) {
    Map<String, Object> map = new HashMap<>();
    try {
      map = forecastPowerShortTermHisService.findByForecastTimeBetweenAndForecastHowLongAgoOrderByForecastTime(startTime, endTime, ago);
      return ResponseVO.success(map);
    } catch (Exception e) {
      e.printStackTrace();
      return ResponseVO.fail(e.toString());
    }
  }


  /**
   *
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @return 结果集
   */
  @GetMapping(value = "/findByGenDateBetween/{startTime}/{endTime}/{timeStep}")
  public ResponseVO findByGenDateBetween(@PathVariable("startTime") Long startTime,
                                         @PathVariable("endTime") Long endTime,
                                         @PathVariable("timeStep") Long timeStep) {
    Map<String, Object> map;

    try {
      map = forecastPowerShortTermHisService.findByGenDateBetween(startTime, endTime, timeStep);
      return ResponseVO.success(map);
    } catch (Exception e) {
      e.printStackTrace();
      return ResponseVO.fail(e.toString());
    }
  }


  /**
   * 分页查询  实时短期预测
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param page 页码
   * @param size 条数
   * @param sortOrder 排序
   * @return
   */
  @GetMapping(value = "/forecastPowerShortTermHis/{startTime}/{endTime}/{page}/{size}")
  public ResponseVO findByTimeBetweenForPaging(@PathVariable("startTime") Long startTime,
                                               @PathVariable("endTime") Long endTime,
                                               @PathVariable("page") Integer page,
                                               @PathVariable("size") Integer size,
                                               String sortOrder){
    Map<String,Object> map;
    try{
      map =  forecastPowerShortTermHisService.findByTimeBetweenAndNoForPaging(startTime,endTime,page,size,sortOrder);
      return ResponseVO.success(map);
    }catch(Exception e){
      e.printStackTrace();
      return ResponseVO.fail(e.toString());
    }
  }


  /**
   * 导出实时短期数据
   *
   * @param newTime 当前时间
   */
  @GetMapping(value = "/exportForecastPowerShortTermHis/{startTime}")
  public ResponseVO exportForecastPowerShortTerm(@PathVariable("startTime") Long newTime,
                                                 HttpServletResponse response) {

    try {
      forecastPowerShortTermHisService.exportForForecastPowerShortTerm(newTime,response);
      return ResponseVO.success();
    } catch (Exception e) {
      e.printStackTrace();
      return ResponseVO.fail(e.toString());
    }

  }

}
