package com.jiayue.ipfcst.controller;

import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.service.ExportFileService;
import com.jiayue.ipfcst.service.ForecastPowerShortTermService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 接入数据属性获取restful接口
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/7 10:12
 */
@Slf4j
@RestController
@RequestMapping("exportFile")
public class ExportFileController {
  private final ForecastPowerShortTermService forecastPowerShortTermService;
  private final ExportFileService exportFileService;

  @Autowired
  public ExportFileController(ForecastPowerShortTermService forecastPowerShortTermService,
                              ExportFileService exportFileService) {
    this.forecastPowerShortTermService = forecastPowerShortTermService;
    this.exportFileService = exportFileService;
  }

  /**
   * 导出实时短期数据
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   */
  @GetMapping(value = "/exportForecastPowerShortTerm/{startTime}/{endTime}")
  public ResponseVO exportForecastPowerShortTerm(@PathVariable("startTime") Long startTime,
                                                 @PathVariable("endTime") Long endTime,
                                                    HttpServletResponse response) {

    try {
      Map<String, Object> dataMap = forecastPowerShortTermService.findByForecastTimeBetween(startTime, endTime);
      exportFileService.exportForForecastPowerShortTerm(dataMap,response);
      return ResponseVO.success();
    } catch (Exception e) {
      e.printStackTrace();
      return ResponseVO.fail(e.toString());
    }

  }
}
