alarmDao.go 3.4 KB

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