package com.zksy.base.gas.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 可燃气体监测仪数据表
 * @TableName gas_monitor_data
 */
@TableName(value = "gas_monitor_data", schema = "app_user")
@Data
public class GasMonitorData implements Serializable {
    /**
     * 主键ID
     */
    @TableId(value = "id", type = IdType.ASSIGN_UUID)
    private String id;

    /**
     * 帧类型
     */
    @TableField(value = "frame_type")
    private Integer frameType;

    /**
     * MAC地址
     */
    @TableField(value = "mac_address")
    private String macAddress;

    /**
     * 16位短地址
     */
    @TableField(value = "short_address")
    private String shortAddress;

    /**
     * 自动应答选项
     */
    @TableField(value = "auto_reply_option")
    private Integer autoReplyOption;

    /**
     * 数据帧序号
     */
    @TableField(value = "sequence_no")
    private Integer sequenceNo;

    /**
     * 命令字
     */
    @TableField(value = "command")
    private Integer command;

    /**
     * 设备属性
     */
    @TableField(value = "device_attr")
    private Integer deviceAttr;

    /**
     * 协议版本
     */
    @TableField(value = "protocol_version")
    private String protocolVersion;

    /**
     * 报警信息（解析后二进制）
     */
    @TableField(value = "alarm_info")
    private Integer alarmInfo;

    /**
     * 设备上报时间戳
     */
    @TableField(value = "report_time")
    private LocalDateTime reportTime;

    /**
     * 气体浓度（%LEL）
     */
    @TableField(value = "gas_concentration")
    private BigDecimal gasConcentration;

    /**
     * 气体类型（如CH4）
     */
    @TableField(value = "gas_type")
    private String gasType;

    /**
     * 电池电量
     */
    @TableField(value = "battery")
    private BigDecimal battery;

    /**
     * 信号强度
     */
    @TableField(value = "signal_strength")
    private Float signalStrength;

    /**
     * 经度
     */
    @TableField(value = "longitude")
    private BigDecimal longitude;

    /**
     * 纬度
     */
    @TableField(value = "latitude")
    private BigDecimal latitude;

    /**
     * 入库时间
     */
    @TableField(value = "create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public Map<String, Boolean> parseAlarmBits() {
        Map<String, Boolean> alarmBits = new HashMap<>();
        if (alarmInfo == null) {
            alarmInfo = 0;
        }
        alarmBits.put("unknownAlarm", (alarmInfo & (1 << 0)) != 0); // Bit0：未知报警
        alarmBits.put("highAlarm", (alarmInfo & (1 << 1)) != 0);     // Bit1：高报警
        alarmBits.put("overRange", (alarmInfo & (1 << 2)) != 0);     // Bit2：超量程
        alarmBits.put("calibrationCycle", (alarmInfo & (1 << 3)) != 0); // Bit3：标定周期
        alarmBits.put("overLife", (alarmInfo & (1 << 4)) != 0);       // Bit4：超寿命
        alarmBits.put("fallAlarm", (alarmInfo & (1 << 5)) != 0);      // Bit5：跌倒报警
        alarmBits.put("undervoltage", (alarmInfo & (1 << 6)) != 0);   // Bit6：欠压报警
        alarmBits.put("rangeAlarm", (alarmInfo & (1 << 7)) != 0);     // Bit7：区间报警
        alarmBits.put("keyAlarm", (alarmInfo & (1 << 8)) != 0);       // Bit8：按键报警
        alarmBits.put("vibrationAlarm", (alarmInfo & (1 << 9)) != 0); // Bit9：震动报警
        alarmBits.put("waterLevelAlarm", (alarmInfo & (1 << 10)) != 0); // Bit10：水位报警
        alarmBits.put("powerOffAlarm", (alarmInfo & (1 << 11)) != 0); // Bit11：断电报警
        alarmBits.put("sensorFault", (alarmInfo & (1 << 12)) != 0);   // Bit12：传感器故障
        alarmBits.put("overHumidity", (alarmInfo & (1 << 13)) != 0);  // Bit13：超湿报警
        alarmBits.put("overTemperature", (alarmInfo & (1 << 14)) != 0); // Bit14：超温报警
        alarmBits.put("systemFault", (alarmInfo & (1 << 15)) != 0);   // Bit15：系统故障
        return alarmBits;
    }
}