package com.jiayue.ipfcst.integration.controller;

import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.integration.service.IntegrationService;
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 java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 准确率合格率restful接口
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/7 10:12
 */
@RestController
@Slf4j
@RequestMapping("integration")
public class IntegrationController {
  private final IntegrationService integrationService;

  @Autowired
  public IntegrationController(IntegrationService integrationService) {
    this.integrationService = integrationService;
  }

  /**
   * 根据开始时间和结束时间查询 场站功率状态
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @return 结果集
   */
  @GetMapping(value = "/draw/{startTime}/{endTime}/{howAgo}")
  public ResponseVO findByTimeBetween(@PathVariable("startTime") Long startTime,
                                      @PathVariable("endTime") Long endTime,
                                      @PathVariable("howAgo") Integer howAgo) {
    Map<String, Object> map = new HashMap<>();
    try {
      map = integrationService.draw(startTime, endTime, howAgo);
      return ResponseVO.success(map);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("一体化图表查询错误");
      return ResponseVO.fail(e.getMessage());
    }
  }

  /**
   * 分页查询 场站功率状态
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @param page      页码
   * @param size      条数
   * @param sortOrder 排序
   * @return
   */
  @GetMapping(value = "/table/{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 = new HashMap<>();
    try {
      map = integrationService.findByTimeBetweenAndNoForPaging(startTime, endTime, page, size, sortOrder);
      return ResponseVO.success(map);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("一体化分页查询错误");
      return ResponseVO.fail(e.toString());
    }

  }

  @GetMapping(value = "/isMenu")
  public ResponseVO isMenu(){
    String isMenu = "no";
    try {
      isMenu = integrationService.isMenu();
      return ResponseVO.success(isMenu);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("一体化是否加载菜单出错");
      return ResponseVO.fail(e.toString());
    }
  }


}
