alarmDao.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "time"
  5. )
  6. // Alarm 一键报警服务
  7. type Alarm struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. ServeName string `gorm:"varchar(64)" json:"serveName"` //设备名称
  10. ServeSN string `gorm:"type:varchar(60)" json:"serveSn"` //设备序编码
  11. BrandID int `gorm:"type:int" json:"brandId"` //设备品牌ID
  12. CountRel int `gorm:"-" json:"countRel"` //关联终端数
  13. ModelID int `gorm:"type:int" json:"modelId"` //设备型号ID
  14. ServeType int `gorm:"type:int" json:"serveType"` //服务
  15. ServeIP string `gorm:"type:varchar(50)" json:"serveIp"` //所属服务IP地址
  16. ServePort int `gorm:"type:int" json:"servePort"` //端口
  17. Account string `gorm:"type:varchar(4000)" json:"account"` //账号
  18. Pwd string `gorm:"type:varchar(4000)" json:"pwd"` //密码
  19. Numeration int `gorm:"type:int" json:"numeration"` //编号
  20. IPAddress string `gorm:"type:varchar(50)" json:"ipAddress"` //IP地址
  21. InstallTime *time.Time `gorm:"type:date" json:"installTime"` //安装时间
  22. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户id
  23. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  24. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  25. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  26. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  27. IsDeleted int `gorm:"type:int" json:"isDeleted"` //是否删除 0=未删除,1=删除
  28. Status int `gorm:"type:int" json:"status"` //告警状态 1处理完成2待处理3无需处理
  29. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,(备用,逗号区分)
  30. }
  31. func (Alarm) TableName() string {
  32. return "device_a_key_alarm_serve"
  33. }
  34. func (c Alarm) Delete() error {
  35. 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
  36. }
  37. func (c Alarm) IsExistedBySN() bool {
  38. var count int64
  39. _ = Db.Debug().Model(&c).Where(" serve_sn = ? and is_deleted = ?",
  40. c.ServeSN, c.IsDeleted).Count(&count).Error
  41. return count > 0
  42. }
  43. func (c Alarm) IsExistedByNameAndCode() bool {
  44. var devices []Alarm
  45. err := Db.Debug().Model(&c).Where(" serve_sn = ? and is_deleted = ?",
  46. c.ServeSN, c.IsDeleted).Find(&devices).Error
  47. //如果查询不到,返回相应的错误
  48. if gorm.ErrRecordNotFound == err {
  49. return false
  50. }
  51. for _, d := range devices {
  52. if d.ID != c.ID {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. func (c *Alarm) Create() error {
  59. return Db.Debug().Model(&c).Save(&c).Error
  60. }
  61. func (c *Alarm) Update() error {
  62. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  63. }
  64. func (c *Alarm) GetDevice() error {
  65. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  66. return err
  67. }
  68. func (c Alarm) GetDevices(offset, limit int) ([]Alarm, int64, error) {
  69. var devices []Alarm
  70. var count int64
  71. db := Db.Debug().Model(&c).Where(" serve_name like ? and is_deleted = 0", "%"+c.ServeName+"%")
  72. db.Count(&count)
  73. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  74. return devices, count, err
  75. }
  76. func (c Alarm) GetAllDevices() ([]*Alarm, error) {
  77. var devices []*Alarm
  78. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  79. return devices, err
  80. }