alarmTerminalDao.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package dao
  2. import (
  3. "gorm.io/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 string `gorm:"type:varchar(12)" json:"tenantId"` //租户id
  25. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  26. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  27. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  28. UpdateUser int `gorm:"type:int" 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 "device_a_key_alarm_terminal"
  35. }
  36. func (c AlarmTerminal) Delete() error {
  37. return Db.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 int64
  42. _ = Db.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.Model(&c).Where(" terminal_sn = ? and is_deleted = ?",
  49. c.TerminalSN, c.IsDeleted).Find(&devices).Error
  50. //如果查询不到,返回相应的错误
  51. if gorm.ErrRecordNotFound == 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.Model(&c).Save(&c).Error
  63. }
  64. func (c *AlarmTerminal) Update() error {
  65. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  66. }
  67. func (c *AlarmTerminal) GetDevice() error {
  68. err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  69. return err
  70. }
  71. func (c AlarmTerminal) GetDevices(offset, limit int) ([]AlarmTerminal, int64, error) {
  72. var devices []AlarmTerminal
  73. var count int64
  74. db := Db.Model(&c).Where(" terminal_name like ? and is_deleted = 0", "%"+c.TerminalName+"%")
  75. db.Count(&count)
  76. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  77. return devices, count, err
  78. }
  79. func (c AlarmTerminal) GetAllDevices() ([]*AlarmTerminal, error) {
  80. var devices []*AlarmTerminal
  81. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  82. return devices, err
  83. }
  84. func (c AlarmTerminal) GetDevicesByGateway() []AlarmTerminal {
  85. var devices []AlarmTerminal
  86. Db.Model(&c).Where(" gateway_id = ? and is_deleted = 0",
  87. c.GatewayId).Find(&devices)
  88. return devices
  89. }
  90. func (c AlarmTerminal) GetDevicesByLampPole() []AlarmTerminal {
  91. var devices []AlarmTerminal
  92. Db.Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  93. c.LampPoleId).Find(&devices)
  94. return devices
  95. }
  96. func (c AlarmTerminal) GetTerminalCount(serverId int) int64 {
  97. var count int64
  98. Db.Model(&c).Where("serve_id = ? and is_deleted = 0",
  99. serverId).Count(&count)
  100. return count
  101. }
  102. func (c AlarmTerminal) DeviceCount1() (int64, int64) {
  103. var all, fault int64
  104. err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
  105. if err != nil {
  106. all = -1
  107. }
  108. err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
  109. if err != nil {
  110. fault = -1
  111. }
  112. return all, fault
  113. }
  114. func (c AlarmTerminal) ListDevices1() []Device {
  115. var devices []AlarmTerminal
  116. Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  117. var rsp = make([]Device, 0, len(devices))
  118. for _, v := range devices {
  119. var d = Device{
  120. Lng: v.PoleLng,
  121. Lat: v.PoleLat,
  122. Status: v.Status,
  123. Name: v.TerminalName,
  124. Code: v.TerminalSN,
  125. LampPoleName: v.LampPoleName,
  126. }
  127. rsp = append(rsp, d)
  128. }
  129. return rsp
  130. }