package com.bocloud.ims.entity.resource.azure.monitor;

import com.bocloud.common.utils.DateSerializer;
import com.bocloud.entity.annotations.Column;
import com.bocloud.entity.annotations.IgnoreAll;
import com.bocloud.entity.annotations.PK;
import com.bocloud.entity.annotations.Table;
import com.bocloud.entity.bean.GenericEntity;
import com.bocloud.entity.meta.PKStrategy;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 告警实体类
 *
 * @author wangyu
 * @Version 1.0
 * @since 2017年7月10日
 */
@Table("alarm")
public class Alarm extends GenericEntity {

    @PK(value = PKStrategy.AUTO)
    private Long id; // ID
    @Column("level")
    private String level;// 警告等级
    @Column("type")
    private String type;// 目标机器类型
    @Column("os_category")
    private String osCategory;// 目标机器类型
    @Column("target")
    private String target;// 目标机器
    @Column("deal_result")
    private String dealResult;// 处理结果
    @Column("alarm_val")
    private String alarmVal;// 告警的key值
    @Column("recovery_val")
    private String recoveryVal;// 告警恢复的值
    @Column("gmt_trigger")
    @JsonSerialize(using = DateSerializer.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtTrigger;// 告警触发时间
    @Column("gmt_confirm")
    @JsonSerialize(using = DateSerializer.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtConfirm;// 告警确认时间
    @Column("confirmer")
    private String confirmer;// 告警确认人
    @Column("gmt_solve")
    @JsonSerialize(using = DateSerializer.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtSolve;// 告警解决时间
    @Column("solver")
    private String solver;// 告警解决人
    @Column("rule_id")
    private Long ruleId;// 告警规则ID
    @Column("event_alarm")
    private Boolean eventAlarm;// 是否是事件告警
    @Column("send_notice")
    private Boolean sendNotice;// 是否已经发送通知
    @Column("send_recovery")
    private Boolean sendRecovery;// 是否已经发送告警恢复通知
    @Column("target_name")
    private String targetName; // 告警目标名称
    @Column("threshold_id")
    private Long thresholdId;//阈值Id
    @Column("alarm_id")
    private String alarmId;
    @Column("vendor_id")
    private Long vendorId; // 云供应商Id
    @Column("tenant_id")
    private Long tenantId;// 项目ID
    @Column("project_id")
    private Long projectId;// 项目ID
    @Column("metric_alert_id")
    private Long metricAlertId;//指标警报ID
    @Column("signal_type")
    private String signalType;//信号类型
    @Column("monitor_condition")
    private String monitorCondition;//监控状态 Fired激发  Resolved已解决
    @Column("user_id")
    private Long userId;
    @IgnoreAll
    private String vendorName; // 云供应商名称
    @IgnoreAll
    private String guid;
    @IgnoreAll
    private Boolean solved;// 是否已解决
    @IgnoreAll
    private Boolean confirmed;// 是否已确认
    @IgnoreAll
    private String duration;// 持续时长
    @IgnoreAll
    private String uuid;
    @IgnoreAll
    private String metricAlertUuid;//指标警报uuid
    @IgnoreAll
    private MetricAlert metricAlert;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public MetricAlert getMetricAlert() {
        return metricAlert;
    }

    public void setMetricAlert(MetricAlert metricAlert) {
        this.metricAlert = metricAlert;
    }

    public Long getMetricAlertId() {
        return metricAlertId;
    }

    public void setMetricAlertId(Long metricAlertId) {
        this.metricAlertId = metricAlertId;
    }

    public String getSignalType() {
        return signalType;
    }

    public void setSignalType(String signalType) {
        this.signalType = signalType;
    }

    public String getMonitorCondition() {
        return monitorCondition;
    }

    public void setMonitorCondition(String monitorCondition) {
        this.monitorCondition = monitorCondition;
    }

    public String getMetricAlertUuid() {
        return metricAlertUuid;
    }

    public void setMetricAlertUuid(String metricAlertUuid) {
        this.metricAlertUuid = metricAlertUuid;
    }

    public Long getProjectId() {
        return projectId;
    }

    public void setProjectId(Long projectId) {
        this.projectId = projectId;
    }

    public String getAlarmId() {
        return alarmId;
    }

    public void setAlarmId(String alarmId) {
        this.alarmId = alarmId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getOsCategory() {
        return osCategory;
    }

    public void setOsCategory(String osCategory) {
        this.osCategory = osCategory;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getDealResult() {
        return dealResult;
    }

    public void setDealResult(String dealResult) {
        this.dealResult = dealResult;
    }

    public String getAlarmVal() {
        return alarmVal;
    }

    public void setAlarmVal(String alarmVal) {
        this.alarmVal = alarmVal;
    }

    public Date getGmtTrigger() {
        return gmtTrigger;
    }

    public void setGmtTrigger(Date gmtTrigger) {
        this.gmtTrigger = gmtTrigger;
    }

    public Date getGmtConfirm() {
        return gmtConfirm;
    }

    public void setGmtConfirm(Date gmtConfirm) {
        this.gmtConfirm = gmtConfirm;
    }

    public String getConfirmer() {
        return confirmer;
    }

    public void setConfirmer(String confirmer) {
        this.confirmer = confirmer;
    }

    public Date getGmtSolve() {
        return gmtSolve;
    }

    public void setGmtSolve(Date gmtSolve) {
        this.gmtSolve = gmtSolve;
    }

    public String getSolver() {
        return solver;
    }

    public void setSolver(String solver) {
        this.solver = solver;
    }

    public Long getVendorId() {
        return vendorId;
    }

    public void setVendorId(Long vendorId) {
        this.vendorId = vendorId;
    }

    public Long getRuleId() {
        return ruleId;
    }

    public void setRuleId(Long ruleId) {
        this.ruleId = ruleId;
    }

    public Boolean getEventAlarm() {
        return eventAlarm;
    }

    public void setEventAlarm(Boolean eventAlarm) {
        this.eventAlarm = eventAlarm;
    }

    public Boolean getSendNotice() {
        return sendNotice;
    }

    public void setSendNotice(Boolean sendNotice) {
        this.sendNotice = sendNotice;
    }

    public Boolean isSendNotice() {
        return sendNotice;
    }

    public Boolean isSendRecovery() {
        return sendRecovery;
    }

    public void setSendRecovery(Boolean sendRecovery) {
        this.sendRecovery = sendRecovery;
    }

    public String getVendorName() {
        return vendorName;
    }

    public Long getTenantId() {
        return tenantId;
    }

    public void setTenantId(Long tenantId) {
        this.tenantId = tenantId;
    }

    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }

    public String getTargetName() {
        return targetName;
    }

    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public Boolean isSolved() {
        return this.gmtSolve != null;
    }

    public void setSolved(Boolean solved) {
        this.solved = solved;
    }

    public Boolean isConfirmed() {
        return this.gmtConfirm != null;
    }

    public void setConfirmed(Boolean confirmed) {
        this.confirmed = confirmed;
    }


    public Long getThresholdId() {
        return thresholdId;
    }

    public void setThresholdId(Long thresholdId) {
        this.thresholdId = thresholdId;
    }

    public String getRecoveryVal() {
        return recoveryVal;
    }

    public void setRecoveryVal(String recoveryVal) {
        this.recoveryVal = recoveryVal;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public Boolean getSendRecovery() {
        return sendRecovery;
    }

    public Boolean getSolved() {
        return this.solver != null && this.gmtSolve != null;
    }

    public Boolean getConfirmed() {
        return this.confirmer != null && this.gmtConfirm != null;
    }

    /**
     * 根据产生时间计算告警持续时长
     *
     * @return
     */
    public String getDuration() {
        if (this.gmtTrigger != null) {
            Date end = null;
            if (this.isSolved()) {
                end = this.getGmtSolve();
            } else {
                end = new Date();
            }
            long different = end.getTime() - this.gmtTrigger.getTime();
            long secondsInMilli = 1000;
            long minutesInMilli = secondsInMilli * 60;
            long hoursInMilli = minutesInMilli * 60;
            long daysInMilli = hoursInMilli * 24;
            long elapsedDays = different / daysInMilli;
            different = different % daysInMilli;
            long elapsedHours = different / hoursInMilli;
            different = different % hoursInMilli;
            long elapsedMinutes = different / minutesInMilli;
            different = different % minutesInMilli;
            long elapsedSeconds = different / secondsInMilli;
            String str = String.format("%d天%d小时%d分钟%d秒", elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
            if (elapsedDays == 0 && elapsedHours == 0 && elapsedMinutes == 0) {
                str = str.replace("0天0小时0分钟", "");
            }
            if (elapsedDays == 0 && elapsedHours == 0) {
                str = str.replace("0天0小时", "");
            }
            if (elapsedDays == 0) {
                str = str.replace("0天", "");
            }
            return str;
        } else {
            return null;
        }
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

}