package com.dcs.hnyz.domain;

import com.dcs.hnyz.domain.vo.EquipmentParamFormVo;
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")
public class AlarmEvent extends BaseEntity {

    private static final long serialVersionUID = 1L;

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

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

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

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

    /**
     * 告警类型
     */
    @Excel(name = "告警类型")
    @TableField(value = "alarm_type")
    private String alarmType;

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

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

    /**
     * 单位
     */
    @Excel(name = "单位")
    private String unit;

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

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

    /**
     * 告警状态（0-未恢复，1-已恢复）
     */
    @Excel(name = "告警状态", readConverterExp = "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;
    }


}
