package com.jiayue.ipfcst.controller;

import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.constant.enums.FileStatusEnum;
import com.jiayue.ipfcst.common.data.constant.enums.FileTypeEnum;
import com.jiayue.ipfcst.common.data.entity.ChannelAlarmInfo;
import com.jiayue.ipfcst.common.data.entity.ChannelDisconLog;
import com.jiayue.ipfcst.common.data.entity.PowerLimitPlanSign;
import com.jiayue.ipfcst.service.DashboardService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 首页
 *
 * @author xsl
 * @version 3.0
 */
@RestController
@RequestMapping("/dashboard")
@Slf4j
public class DashboardController {
  private final DashboardService dashboardService;

  public DashboardController(DashboardService dashboardService) {
    this.dashboardService = dashboardService;
  }

  /**
   * 获取上报对象状态
   *
   * @return
   */
  @GetMapping(value = "/getChannelStatus")
  public ResponseVO getChannelStatus() {
    return ResponseVO.success(dashboardService.getChannelStatus());
  }

  /**
   * 获取上报文件信息
   *
   * @return
   */
  @GetMapping(value = "/getUploadFileInfo")
  public ResponseVO getUploadFileInfo() {
    return ResponseVO.success(dashboardService.getDashboardUploadFileInfo());
  }

  /**
   * 获取环境设备信息
   *
   * @return
   */
  @GetMapping(value = "/getEquipmentInfo")
  public ResponseVO getEquipmentInfo() {
    return ResponseVO.success(dashboardService.getEquipmentInfo());
  }

  /**
   * 根据设备ID获取在首页显示的数据类型
   * @param equipmentId
   * @return
   */
  @RequestMapping(value = "/getEquipmentDataType/{equipmentId}")
  public ResponseVO getEquipmentDataType(@PathVariable final String equipmentId) {
    return ResponseVO.success(dashboardService.getEquipmentDataType(equipmentId));
  }

  /**
   * 获取首页短期、超短期、实际功率数据
   * @return
   */
  @RequestMapping(value = "/getCompositeData/{efiType}")
  public ResponseVO getCompositeData(@PathVariable final String efiType) {
    if ("E1".equals(efiType)){
      return ResponseVO.success(dashboardService.getCompositeData());
    }
    else{
      return ResponseVO.success(dashboardService.getCompositeDataOne());
    }
  }

  /**
   * 上报文件状态枚举
   * @return
   */
  @GetMapping("/getFileStatusEnum")
  public ResponseVO getFileStatusEnum(){
    List list = new ArrayList<>();
    for (FileStatusEnum fileStatusEnum : FileStatusEnum.values()) {
      Map map = new HashMap();
      map.put("name",fileStatusEnum.name());
      map.put("message",fileStatusEnum.getMessage());
      list.add(map);
    }
    return ResponseVO.success(list);
  }

  /**
   * 上报文件类型枚举
   * @return
   */
  @GetMapping("/getFileTypeEnum")
  public ResponseVO getFileTypeEnum(){
    List list = new ArrayList<>();
    for (FileTypeEnum fileTypeEnum : FileTypeEnum.values()) {
      Map map = new HashMap();
      map.put("name",fileTypeEnum.name());
      map.put("message",fileTypeEnum.getMessage());
      list.add(map);
    }
    return ResponseVO.success(list);
  }
  /**
   * 获取首页当天通道断开日志
   * @return 结果集
   */
  @GetMapping(value = "/getChannelDisconLog")
  public ResponseVO getChannelDisconLog(){
    List<ChannelDisconLog> list = new ArrayList<>();
    try{
      list = dashboardService.getChannelDisconLog();
      return ResponseVO.success(list);
    }catch(Exception e){
      log.error("查询首页通道断开日志错误",e);
      return ResponseVO.fail(e.toString());
    }
  }
  /**
   * 获取首页限电标识状态
   * @return 结果集
   */
  @GetMapping(value = "/getPowerLimitPlanSign")
  public ResponseVO getPowerLimitPlanSign(){
    try{
      return ResponseVO.success(dashboardService.getPowerLimitPlanSign());
    }catch(Exception e){
      log.error("首页获取限电标识不存在",e);
      return ResponseVO.fail(e.toString());
    }
  }

  /**
   * 保存限电标识
   *
   * @param powerLimitPlanSign 参数
   * @return 执行结果
   */
  @PostMapping(value = "/savePowerLimitPlanSign")
  public ResponseVO savePowerLimitPlanSign(@RequestBody PowerLimitPlanSign powerLimitPlanSign) {
    try {
      this.dashboardService.savePowerLimitPlanSign(powerLimitPlanSign);
      return ResponseVO.success(1);
    } catch (Exception ex) {
      log.error("系统错误" + ex);
      return ResponseVO.error(ex);
    }
  }

  /**
   * 获取首页通道告警不提示
   */
  @GetMapping(value = "/getChannelAlarmInfo")
  public ResponseVO getChannelAlarmInfo(){
    try{
      return ResponseVO.success(dashboardService.getChannelAlarmInfo());
    }catch(Exception e){
      log.error("首页获取通道告警不存在",e);
      return ResponseVO.fail(e.toString());
    }
  }

  /**
   * 保存通道告警不提示
   *
   *
   */
  @PostMapping(value = "/saveAllChannelAlarmInfo")
  public ResponseVO saveChannelAlarmInfo(@RequestBody List<ChannelAlarmInfo> channelAlarmInfos){
    try{
      dashboardService.saveChannelAlarmInfoAll(channelAlarmInfos);
      return ResponseVO.success();
    }catch(Exception e){
      log.error("首页获取通道告警不存在",e);
      return ResponseVO.fail(e.toString());
    }
  }
  /**
   * 获取系统版本号
   */
  @GetMapping(value = "/getSysVersion")
  public ResponseVO getSysVersion(){
    try{
      return ResponseVO.success(dashboardService.getSysVersion());
    }catch(Exception e){
      log.error("首页获取系统版本号失败",e);
      return ResponseVO.fail(e.toString());
    }
  }

  /**
   * 获取系统版本号
   */
  @GetMapping(value = "/getIsAbnormal")
  public ResponseVO getIsAbnormal(){
    try{
      return ResponseVO.success(dashboardService.getIsAbnormal());
    }catch(Exception e){
      log.error("获取测风塔数据是否是正常失败",e);
      return ResponseVO.fail(e.toString());
    }
  }
  /**
   * 获取首页限电标识状态
   * @return 结果集
   */
  @GetMapping(value = "/getServerTime")
  public ResponseVO getServerTime(){
    try{
      Date d = new Date();
      return ResponseVO.success(d);
    }catch(Exception e){
      log.error("系统时间获取失败",e);
      return ResponseVO.fail(e.toString());
    }
  }

  /**
   *获取上报对象中的上报文件类型
   */
  @GetMapping(value = "/getUploadFileType")
  public ResponseVO getUploadFileType() {
    return ResponseVO.success(dashboardService.getUploadFileType());
  }

  /**
   * 根据参数标识获取系统参数
   *
   * @param sysKey
   * @return 执行结果
   */
  @SneakyThrows
  @GetMapping(value = "/sysParameterKey/{sysKey}")
  public ResponseVO getById(@PathVariable("sysKey") String sysKey) {
    return ResponseVO.success(dashboardService.getParameterBySysKey(sysKey));
  }
}
