package com.dcs.hnyz.domain;

import com.dcs.hnyz.domain.vo.EquipmentParamFormVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

import com.dcs.common.annotation.Excel;
import com.dcs.common.core.domain.BaseEntity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.IdType;

/**
 * 告警中心管理对象 alarm_event
 *
 * @author hmy
 * @date 2025-04-30
 */

@Data
@TableName("alarm_event")
@ApiModel(value = "告警事件对象")
public class AlarmEvent extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 主键，自增
     */
    @TableId(value = "alarm_id", type = IdType.AUTO)
    @ApiModelProperty(value = "主键自增")
    private Long alarmId;

    /**
     * 对应设备的 ID（关联设备表）
     */
    @Excel(name = "对应设备的 ID")
    @TableField(value = "equipment_id")
    @ApiModelProperty(value = "对应设备的 ID")
    private Long equipmentId;

    /**
     * 告警代码
     */
    @Excel(name = "告警代码")
    @TableField(value = "alarm_code")
    @ApiModelProperty(value = "告警代码")
    private String alarmCode;

    /**
     * 告警等级（1-信息，2-警告，3-严重，4-紧急）
     */
    @Excel(name = "告警等级", readConverterExp = "1=信息,2=警告,3=严重,4=紧急")
    @TableField(value = "alarm_level")
    @ApiModelProperty(value = "告警等级:1=信息,2=警告,3=严重,4=紧急")
    private String alarmLevel;

    /**
     * 告警类型
     */
    @Excel(name = "告警类型")
    @TableField(value = "alarm_type")
    @ApiModelProperty(value = "告警类型:1通信2温度3压力4硬件")
    private String alarmType;

    /**
     * 告警描述
     */
    @Excel(name = "告警描述")
    @ApiModelProperty(value = "告警描述")
    private String message;

    /**
     * 当前值
     */
    @ApiModelProperty(value = "当前值")
    @Excel(name = "当前值")
    private String value;

    /**
     * 单位
     */
    @ApiModelProperty(value = "单位，例如：°C、Mpa 等")
    @Excel(name = "单位")
    private String unit;

    /**
     * 告警发生时间
     */
    @Excel(name = "告警发生时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    @TableField(value = "occur_time")
    @ApiModelProperty(value = "告警发生时间yyyy-MM-dd HH:mm:ss")
    private Date occurTime;

    /**
     * 告警恢复时间
     */
    @Excel(name = "告警恢复时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    @TableField(value = "recover_time")
    @ApiModelProperty(value = "告警恢复时间")
    private Date recoverTime;

    /**
     * 告警状态（0-未恢复，1-已恢复）
     */
    @Excel(name = "告警状态", readConverterExp = "0=未恢复,1=已恢复")
    @ApiModelProperty(value = "告警状态:0=未恢复,1=已恢复")
    private String status;

//    根据告警配置和valueStr生成告警事件
    public static AlarmEvent from(EquipmentParamFormVO param, AlarmConfig config) {
        AlarmEvent event = new AlarmEvent();
        event.setEquipmentId(config.getEquipmentId() != null ? config.getEquipmentId() : param.getRelationId());
        event.setAlarmCode(config.getAlarmCode());
        event.setAlarmLevel(config.getAlarmLevel());
        event.setAlarmType(config.getAlarmType());
        event.setUnit(config.getUnit());
        event.setValue(param.getValue().toString());
//        event.setMessage("告警触发: " + config.getAlarmName());
//        event.setOccurTime(new Date());
        return event;
    }


}
