alarmTerminalDao.go 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package dao
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "time"
  5. )
  6. // AlarmTerminal 一键报警服务
  7. type AlarmTerminal struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. TerminalName string `gorm:"varchar(64)" json:"terminalName"` //设备名称
  10. TerminalSN string `gorm:"type:varchar(60)" json:"terminalSN"` //设备序编码
  11. BrandID int `gorm:"type:int" json:"brandID"` //设备品牌ID
  12. ModelID int `gorm:"type:int" json:"modelID"` //设备型号ID
  13. GatewayID int `gorm:"type:int" json:"gatewayID"` //所属网关
  14. LampPoleID int `gorm:"type:int" json:"lampPoleID"` //所属灯杆 灯杆ID
  15. LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"` //灯杆名称
  16. LampPoleSN string `gorm:"type:varchar(64)" json:"lampPoleSN"` //灯杆编码
  17. LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
  18. PoleLng float64 `gorm:"type:double(17, 14) " json:"poleLng"` //经度
  19. PoleLat float64 `gorm:"type:double(17, 14) " json:"poleLat"` //纬度
  20. Numeration int `gorm:"type:int" json:"numeration"` //编号
  21. ServeID int `gorm:"type:int" json:"serveID"` //所属服务ID
  22. IPAddress string `gorm:"type:varchar(50)" json:"ipAddress"` //IP地址
  23. InstallTime time.Time `gorm:"type:date" json:"installTime"` //安装时间
  24. TenantId int `gorm:"type:int" json:"tenantId"` //租户id
  25. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  26. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  27. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  28. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  29. IsDeleted int `gorm:"type:int" json:"isDeleted"` //是否删除 0=未删除,1=删除
  30. Status int `gorm:"type:int " json:"status"` //告警状态 1处理完成2待处理3无需处理
  31. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,(备用,逗号区分)
  32. }
  33. func (AlarmTerminal) TableName() string {
  34. return "t_dev_a_key_alarm_terminal"
  35. }
  36. func (c AlarmTerminal) Delete() error {
  37. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  38. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  39. }
  40. func (c AlarmTerminal) IsExistedBySN() bool {
  41. var count = 0
  42. _ = Db.Debug().Model(&c).Where(" terminal_sn = ? and is_deleted = ?",
  43. c.TerminalSN, c.IsDeleted).Count(&count).Error
  44. return count > 0
  45. }
  46. func (c AlarmTerminal) IsExistedByNameAndCode() bool {
  47. var devices []AlarmTerminal
  48. err := Db.Debug().Model(&c).Where(" terminal_sn = ? and is_deleted = ?",
  49. c.TerminalSN, c.IsDeleted).Find(&devices).Error
  50. //如果查询不到,返回相应的错误
  51. if gorm.IsRecordNotFoundError(err) {
  52. return false
  53. }
  54. for _, d := range devices {
  55. if d.ID != c.ID {
  56. return true
  57. }
  58. }
  59. return false
  60. }
  61. func (c *AlarmTerminal) Create() error {
  62. return Db.Debug().Model(&c).Save(&c).Error
  63. }
  64. func (c *AlarmTerminal) Update() error {
  65. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  66. }
  67. func (c *AlarmTerminal) GetDevice() error {
  68. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  69. return err
  70. }
  71. func (c AlarmTerminal) GetDevices(offset, limit int) ([]AlarmTerminal, error) {
  72. var devices []AlarmTerminal
  73. err := Db.Debug().Model(&c).Where(" terminal_name like ? and is_deleted = 0", "%"+c.TerminalName+"%").Offset(offset).Limit(limit).Find(&devices).Error
  74. return devices, err
  75. }
  76. func (c AlarmTerminal) GetAllDevices() ([]*AlarmTerminal, error) {
  77. var devices []*AlarmTerminal
  78. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  79. return devices, err
  80. }