123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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 int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
- UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
- UpdateUser int `gorm:"type:int" 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.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.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.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.Model(&c).Save(&c).Error
- }
- func (c *Alarm) Update() error {
- return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
- }
- func (c *Alarm) GetDevice() error {
- err := Db.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.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.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
- return devices, err
- }
- func (c Alarm) DeviceCount1() (int64, int64) {
- var all, fault int64
- err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
- if err != nil {
- all = -1
- }
- err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
- if err != nil {
- fault = -1
- }
- return all, fault
- }
- func (c Alarm) ListDevices1() []Device {
- var devices []Alarm
- Db.Model(&c).Where("is_deleted = 0").Find(&devices)
- var rsp = make([]Device, 0, len(devices))
- for _, v := range devices {
- var d = Device{
- Status: v.Status,
- Name: v.ServeName,
- Code: v.ServeSN,
- Model: "模型",
- }
- rsp = append(rsp, d)
- }
- return rsp
- }
|