package com.jiayue.ipfcst.controller;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.service.QrCodeService;
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.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 准确率合格率restful接口
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/7 10:12
 */
@RestController
@Slf4j
public class QrCodeController {
  private final QrCodeService qrCodeService;
  @Autowired
  public QrCodeController(QrCodeService qrCodeService){
    this.qrCodeService = qrCodeService;
  }

  /**
   * 实际功率二维码内容生成
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @return 二维码生成内容集
   */
  @GetMapping(value = "/qrCode/realPower/{startTime}/{endTime}")
  public ResponseVO generateRealPowerCodeContent(@PathVariable("startTime") Long startTime,
                                          @PathVariable("endTime") Long endTime
                                          ){
    List<String> results;
    try{
      results = qrCodeService.generateRealPowerCodeContent(startTime, endTime);
      return ResponseVO.success(results);
    }catch (BusinessException | IOException e){
      e.printStackTrace();
      log.error("生成实际功率二维码失败");
      return ResponseVO.fail(e.toString());
    }
  }

  /**
   * 环境数据二维码内容生成
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param no 设备id
   * @param attr 属性
   * @param eqType 设备类型
   * @return 二维码生成内容集
   */
  @GetMapping(value = "/qrCode/env/{startTime}/{endTime}/{no}/{attr}/{eqType}")
  public ResponseVO generateEnvCode(@PathVariable("startTime") Long startTime,
                                    @PathVariable("endTime") Long endTime,
                                    @PathVariable("no") Integer no,
                                    @PathVariable("attr") String attr,
                                    @PathVariable("eqType") String eqType
  ){
    List<String> results;
    try{
      results = qrCodeService.generateEnvCodeContent(startTime, endTime,no,attr,eqType);
      return ResponseVO.success(results);
    }catch (Exception e){
      e.printStackTrace();
      log.error("生成环境数据二维码失败");
      return ResponseVO.fail(e.toString());
    }
  }
  /**
   * 可用二维码内容生成
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @return 二维码生成内容集
   */
  @GetMapping(value = "/qrCode/reportshortterm/{startTime}/{endTime}")
  public ResponseVO generateReportShortTermContent(@PathVariable("startTime") Long startTime,
                                                 @PathVariable("endTime") Long endTime
  ){
    List<String> results;
    try{
      results = qrCodeService.generateReportShortTermContent(startTime, endTime);
      return ResponseVO.success(results);
    }catch (BusinessException | IOException e){
      e.printStackTrace();
      log.error("生成可用二维码失败");
      return ResponseVO.fail(e.toString());
    }
  }

  /**
   * 上报短期二维码内容生成
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @return 二维码生成内容集
   */
  @GetMapping(value = "/qrCode/reportshortterm/{startTime}/{endTime}/{ago}")
  public ResponseVO generateShortTermContent(@PathVariable("startTime") Long startTime,
                                                   @PathVariable("endTime") Long endTime,
                                             @PathVariable("ago") Integer ago
  ){
    List<String> results;
    try{
      results = qrCodeService.generateShortTermContent(startTime, endTime,ago);
      List<String> resultsList = qrCodeService.generateReportShortTermContent(startTime, endTime);

      Map<String,List<String>> resultsMap = new HashMap<>();
      resultsMap.put("qrcode",resultsList);
      resultsMap.put("qrcode1",results);
      return ResponseVO.success(resultsMap);
    }catch (BusinessException | IOException e){
      e.printStackTrace();
      log.error("生成上报短期二维码失败");
      return ResponseVO.fail(e.toString());
    }
  }


  /**
   * 首页实际功率二维码生成

   * @param response response
   */
  @GetMapping(value = "/qrCode/realPower")
  public void generateRealPowerCode(HttpServletResponse response){
    try{
      qrCodeService.generateRealPowerCode(response);
    }catch (IOException | BusinessException e){
          e.printStackTrace();
          log.error("生成首页实际功率二维码失败");
    }

  }

  /**
   * 首页运行状态二维码生成

   * @param response response
   */
  @GetMapping(value = "/qrCode/runningStatus")
  public void generateRunningStatusCode(HttpServletResponse response){
    try{
      qrCodeService.generateRunningStatusCode(response);
    }catch (IOException | BusinessException e){
      e.printStackTrace();
      log.error("生成首页运行状态二维码失败");
    }

  }
  /**
   * 首页超短期二维码生成

   * @param response response
   */
  @GetMapping(value = "/qrCode/ultraShort")
  public void generateUltraShortCode(HttpServletResponse response){
    try{
      qrCodeService.generateForecastUltraShortCode(response);
    }catch (IOException | BusinessException e){
      e.printStackTrace();
      log.error("生成首页实际功率二维码失败");
    }

  }
}
