package com.jiayue.ipfcst.console.controller;

import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.constant.enums.AlarmStatusEnum;
import com.jiayue.ipfcst.common.data.constant.enums.AlarmTypeEnum;
import com.jiayue.ipfcst.common.data.entity.SysAlarm;
import com.jiayue.ipfcst.console.service.SysAlarmService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统告警restful接口
 *
 * @author tl
 * @version 3.0
 * @since 2020/6/28 10:12
 */
@RestController
@RequestMapping("SysAlarm")
@Slf4j
public class SysAlarmController {
  private final SysAlarmService sysAlarmService;

  @Autowired
  public SysAlarmController(SysAlarmService sysAlarmService) {
    this.sysAlarmService = sysAlarmService;
  }

  /**
   * 获取告警信息
   *
   * @param page
   * @param size
   * @return 执行结果
   */
  @SneakyThrows
  @GetMapping(value = "/{page}/{size}")
  public ResponseVO getAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
    SysAlarm sysAlarm = new SysAlarm();
    Page<SysAlarm> sysParameterPage = this.sysAlarmService.get(sysAlarm, page, size);
    return ResponseVO.success(sysParameterPage);
  }

  /**
   * 查询
   *
   * @param page
   * @param size
   * @return 执行结果
   */
  @SneakyThrows
  @GetMapping(value = "/{page}/{size}/{alarmTypeEnum}")
  public ResponseVO getForNameAndAlarmTypeEnum(@PathVariable("page") Integer page, @PathVariable("size") Integer size,@PathVariable("alarmTypeEnum") AlarmTypeEnum alarmTypeEnum) {
    SysAlarm sysAlarm = new SysAlarm();
    Page<SysAlarm> sysParameterPage = this.sysAlarmService.getForNameAndAlarmTypeEnum(sysAlarm, page, size,alarmTypeEnum);
    return ResponseVO.success(sysParameterPage);
  }


  /**
   * 获取告警信息
   *
   * @return 执行结果
   */
  @SneakyThrows
  @GetMapping()
  public ResponseVO getAll() {
    List<SysAlarm> list = this.sysAlarmService.getAll();
    return ResponseVO.success(list);
  }

  /**
   * 处理告警信息
   *
   * @return 执行结果
   */
  @SneakyThrows
  @PutMapping()
  public ResponseVO update(@RequestBody SysAlarm sysAlarm) {
    try {
      sysAlarmService.save(sysAlarm);
      return ResponseVO.success(1);
    } catch (Exception ex) {
      return ResponseVO.success("处理告警信息出错：" + ex);
    }
  }

  /**
   * 获取问题类型枚举
   *
   * @return 执行结果
   */
  @SneakyThrows
  @GetMapping("/getAlarmStatusEnum")
  public ResponseVO getAlarmStatusEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (AlarmStatusEnum e : AlarmStatusEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取问题处理状态异常");
      return ResponseVO.fail(list);
    }
  }

  /**
   * 获取问题类型枚举
   *
   * @return 执行结果
   */
  @SneakyThrows
  @GetMapping("/getAlarmTypeEnum")
  public ResponseVO getAlarmTypeEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (AlarmTypeEnum e : AlarmTypeEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取问题类型异常");
      return ResponseVO.fail(list);
    }
  }
}
