alarmDao.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  25. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  26. UpdateUser int `gorm:"type:int" 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.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.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.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.Model(&c).Save(&c).Error
  60. }
  61. func (c *Alarm) Update() error {
  62. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  63. }
  64. func (c *Alarm) GetDevice() error {
  65. err := Db.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.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.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  79. return devices, err
  80. }
  81. func (c Alarm) DeviceCount1() (int64, int64) {
  82. var all, fault int64
  83. err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
  84. if err != nil {
  85. all = -1
  86. }
  87. err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
  88. if err != nil {
  89. fault = -1
  90. }
  91. return all, fault
  92. }
  93. func (c Alarm) ListDevices1() []Device {
  94. var devices []Alarm
  95. Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  96. var rsp = make([]Device, 0, len(devices))
  97. for _, v := range devices {
  98. var d = Device{
  99. Status: v.Status,
  100. Name: v.ServeName,
  101. Code: v.ServeSN,
  102. Model: "模型",
  103. }
  104. rsp = append(rsp, d)
  105. }
  106. return rsp
  107. }