package com.jiayue.ssi.controller;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiayue.ssi.annotation.PreventReplay;
import com.jiayue.ssi.constant.CustomException;
import com.jiayue.ssi.entity.SysAlarm;
import com.jiayue.ssi.service.SysAlarmService;
import com.jiayue.ssi.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * 告警消息接口
 *
 * @author xsl
 * @since 2023/03/13
 */
@RestController
@RequestMapping("/sysAlarmController")
@Slf4j
public class SysAlarmController {
    @Autowired
    SysAlarmService sysAlarmService;

    /**
     * 获取告警消息
     *
     * @return
     */
    @GetMapping(value = "/getAll")
    @PreventReplay
    public ResponseVO getAll() throws CustomException {
        try {
            QueryWrapper<SysAlarm> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            List<SysAlarm> sysAlarmLis = sysAlarmService.list(wrapper);
            return ResponseVO.success(sysAlarmLis);
        } catch (Exception e) {
            throw new CustomException("获取所有告警消息异常", e);
        }
    }

    /**
     * 查收提交
     */
    @PostMapping(value = "/readDone")
    @PreventReplay
    public ResponseVO readDone(String id) throws CustomException {
        try {
            Long commitid;
            if (StringUtils.isNotEmpty(id)) {
                if (NumberUtil.isLong(id)){
                    commitid = Long.parseLong(id);
                }
                else{
                    return ResponseVO.fail("id不是类型不对！");
                }
            }
            else{
                return ResponseVO.fail("id不能为空！");
            }

            SysAlarm sysAlarm = new SysAlarm();
            sysAlarm.setId(commitid);
            boolean bo = sysAlarmService.removeById(sysAlarm);
            if (bo) {
                return ResponseVO.success("告警查收成功");
            } else {
                log.error("告警查收失败");
                return ResponseVO.fail("告警查收失败");
            }
        } catch (Exception e) {
            throw new CustomException("告警查收异常", e);
        }
    }
}
