cameraDao.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "iot_manager_service/util/common"
  4. "time"
  5. )
  6. //CameraDevice 网关下挂载的设备, 摄像头
  7. type CameraDevice struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. DeviceName string `gorm:"type:varchar(64)" json:"deviceName"` //设备名称
  10. DeviceSN string `gorm:"type:varchar(64)" json:"deviceSn"` //设备序列号
  11. CameraType int `gorm:"type:int" json:"cameraType"` //摄像机类型 0=枪机,1=球机
  12. GatewayId int `gorm:"type:int" json:"gatewayId"` //所属网关
  13. LampPoleId int `gorm:"type:int" json:"lampPoleId"` //归属灯杆 灯杆ID
  14. LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"` //灯杆名称
  15. LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"` //灯杆sn
  16. LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
  17. LampLat float64 `gorm:"type:double(17,14)" json:"lampLat"` //经度
  18. LampLng float64 `gorm:"type:double(17,14)" json:"lampLng"` //纬度
  19. GroupId int `gorm:"type:int" json:"groupId"` //灯杆分组ID
  20. BrandId int `gorm:"type:int" json:"brandId"` //设备品牌
  21. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  22. RatedPower float32 `gorm:"type:float(8,2);default 0.00" json:"ratedPower"` //功率
  23. MonitorAddress string `gorm:"type:varchar(255)" json:"monitorAddress"` //监控地址 ip:端口
  24. IPAddress string `gorm:"type:varchar(40)" json:"ipAddress"` //IP地址
  25. InstallTime common.Time `gorm:"type:date" json:"installTime"` //安装时间
  26. TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
  27. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  28. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  29. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  30. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  31. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  32. Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
  33. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,(备用,逗号区分)
  34. IsEnable int `gorm:"type:int;default 2" json:"isEnable"` //启用禁用:1启用,2禁用
  35. StreamId string `gorm:"type:varchar(100)" json:"stream_id"` //流id
  36. }
  37. func (CameraDevice) TableName() string {
  38. return "device_camera"
  39. }
  40. func (c CameraDevice) IsExistedBySN() bool {
  41. var count int64
  42. _ = Db.Debug().Model(&c).Where("device_sn = ? and is_deleted = ?",
  43. c.DeviceSN, c.IsDeleted).Count(&count).Error
  44. return count > 0
  45. }
  46. func (c *CameraDevice) Create() error {
  47. return Db.Debug().Model(&c).Save(&c).Error
  48. }
  49. func (c *CameraDevice) Update() error {
  50. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  51. }
  52. func (c *CameraDevice) GetDevice() error {
  53. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).First(&c).Error
  54. return err
  55. }
  56. func (c CameraDevice) GetDevices(offset, limit int) ([]CameraDevice, error) {
  57. var devices []CameraDevice
  58. db := Db.Debug().Model(&c)
  59. if c.DeviceSN != "" {
  60. db = db.Where("device_name like ? or device_sn like ?", "%"+c.DeviceSN+"%", "%"+c.DeviceSN+"%")
  61. }
  62. if c.CameraType != -1 {
  63. db = db.Where("camera_type = ?", c.CameraType)
  64. }
  65. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
  66. return devices, err
  67. }
  68. func (c *CameraDevice) Delete() error {
  69. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  70. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  71. }
  72. func (c CameraDevice) GetAllDevices() ([]*CameraDevice, error) {
  73. var devices []*CameraDevice
  74. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  75. return devices, err
  76. }
  77. func (c *CameraDevice) UpdateEnable() error {
  78. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"is_enable": c.IsEnable}).Error
  79. }
  80. func (c CameraDevice) GetDevicesByGateway() []CameraDevice {
  81. var devices []CameraDevice
  82. Db.Debug().Model(&c).Where(" gateway_id = ? and is_deleted = 0",
  83. c.GatewayId).Find(&devices)
  84. return devices
  85. }
  86. func (c CameraDevice) GetDevicesByLampPole() []CameraDevice {
  87. var devices []CameraDevice
  88. Db.Debug().Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  89. c.LampPoleId).Find(&devices)
  90. return devices
  91. }
  92. func (c CameraDevice) GetDevicesByLampPoleGroup() []CameraDevice {
  93. var devices []CameraDevice
  94. Db.Debug().Model(&c).Where("group_id = ? and is_deleted = 0",
  95. c.GroupId).Find(&devices)
  96. return devices
  97. }
  98. func (c CameraDevice) GetDevicesByIds(ids string) []CameraDevice {
  99. var devices []CameraDevice
  100. db := Db.Debug().Model(&c).Where("is_deleted = 0")
  101. db.Where("id in (" + ids + ")")
  102. db.Find(&devices)
  103. return devices
  104. }