package models

import "time"

type SosAlarm struct {
	ID      int64     `gorm:"primary_key"`                //主键ID
	DID     string    `gorm:"type:varchar(32)"`           //设备编码
	TStart  time.Time `gorm:"type:datetime"`              //告警开始时间
	TEnd    time.Time `gorm:"type:datetime;default:null"` //告警结束时间
	AType   string    `gorm:"type:varchar(32)"`           //告警主题
	Content string    `gorm:"type:varchar(128)"`          //告警内容
	LcModel
}

func (SosAlarm) TableName() string {
	return "t_device_sos_alarm"
}

func (o SosAlarm) Update() error {
	err := G_db.Model(&o).Where("id = ?", o.ID).Updates(
		map[string]interface{}{"t_end": o.TEnd}).Error
	return err
}

func GetSosAlarm(Codes []string, Start, End string) ([]SosAlarm, error) {
	var array []SosAlarm
	err := G_db.Model(&SosAlarm{}).Where("d_id IN (?) and t_start >= ? and t_start <= ?",
		Codes, Start, End).Order("id asc").Find(&array).Error
	return array, err
}

//用于web端告警信息同步
func GetUpdateSosAlarm(id uint64, update string) ([]SosAlarm, error) {
	var array []SosAlarm
	err := G_db.Model(&SosAlarm{}).Where("id > ? OR updated_at > ?", id, update).Order("id asc").Find(&array).Error
	return array, err
}
