package dao

import (
	"gorm.io/gorm"
	"time"
)

// Alarm 一键报警服务
type Alarm struct {
	ID          int        `gorm:"primary_key" json:"id"`             //编号
	ServeName   string     `gorm:"varchar(64)" json:"serveName"`      //设备名称
	ServeSN     string     `gorm:"type:varchar(60)" json:"serveSn"`   //设备序编码
	BrandID     int        `gorm:"type:int" json:"brandId"`           //设备品牌ID
	CountRel    int        `gorm:"-" json:"countRel"`                 //关联终端数
	ModelID     int        `gorm:"type:int" json:"modelId"`           //设备型号ID
	ServeType   int        `gorm:"type:int" json:"serveType"`         //服务
	ServeIP     string     `gorm:"type:varchar(50)" json:"serveIp"`   //所属服务IP地址
	ServePort   int        `gorm:"type:int" json:"servePort"`         //端口
	Account     string     `gorm:"type:varchar(4000)" json:"account"` //账号
	Pwd         string     `gorm:"type:varchar(4000)" json:"pwd"`     //密码
	Numeration  int        `gorm:"type:int" json:"numeration"`        //编号
	IPAddress   string     `gorm:"type:varchar(50)" json:"ipAddress"` //IP地址
	InstallTime *time.Time `gorm:"type:date" json:"installTime"`      //安装时间
	TenantId    string     `gorm:"type:varchar(12)" json:"tenantId"`  //租户id
	CreateTime  time.Time  `gorm:"type:datetime" json:"createTime"`   //新增时间
	CreateUser  int64      `gorm:"type:bigint" json:"createUser"`     //新增记录操作用户ID
	UpdateTime  time.Time  `gorm:"type:datetime" json:"updateTime"`   //修改时间
	UpdateUser  int64      `gorm:"type:bigint" json:"updateUser"`     //修改用户
	IsDeleted   int        `gorm:"type:int" json:"isDeleted"`         //是否删除 0=未删除,1=删除
	Status      int        `gorm:"type:int" json:"status"`            //告警状态 1处理完成2待处理3无需处理
	Tag         string     `gorm:"type:varchar(255)" json:"tag"`      //标签，(备用，逗号区分)
}

func (Alarm) TableName() string {
	return "device_a_key_alarm_serve"
}

func (c Alarm) Delete() error {
	return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime, "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
}

func (c Alarm) IsExistedBySN() bool {
	var count int64
	_ = Db.Debug().Model(&c).Where("  serve_sn = ? and is_deleted = ?",
		c.ServeSN, c.IsDeleted).Count(&count).Error
	return count > 0
}

func (c Alarm) IsExistedByNameAndCode() bool {
	var devices []Alarm
	err := Db.Debug().Model(&c).Where(" serve_sn = ? and is_deleted = ?",
		c.ServeSN, c.IsDeleted).Find(&devices).Error
	//如果查询不到，返回相应的错误
	if gorm.ErrRecordNotFound == err {
		return false
	}
	for _, d := range devices {
		if d.ID != c.ID {
			return true
		}
	}
	return false
}

func (c *Alarm) Create() error {
	return Db.Debug().Model(&c).Save(&c).Error
}

func (c *Alarm) Update() error {
	return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
}

func (c *Alarm) GetDevice() error {
	err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
	return err
}

func (c Alarm) GetDevices(offset, limit int) ([]Alarm, int64, error) {
	var devices []Alarm
	var count int64
	db := Db.Debug().Model(&c).Where(" serve_name like ? and is_deleted = 0", "%"+c.ServeName+"%")
	db.Count(&count)
	err := db.Offset(offset).Limit(limit).Find(&devices).Error
	return devices, count, err
}

func (c Alarm) GetAllDevices() ([]*Alarm, error) {
	var devices []*Alarm
	err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
	return devices, err
}
