cameraDao.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 string `gorm:"type:varchar(12)" json:"tenantId"` //租户ID
  27. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  28. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  29. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  30. UpdateUser int `gorm:"type:int" 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:"streamId"` //流id
  36. }
  37. func (CameraDevice) TableName() string {
  38. return "device_camera"
  39. }
  40. func (c CameraDevice) IsExistedBySN() bool {
  41. var count int64
  42. _ = Db.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.Model(&c).Save(&c).Error
  48. }
  49. func (c *CameraDevice) Update() error {
  50. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  51. }
  52. func (c *CameraDevice) GetDevice() error {
  53. err := Db.Model(&c).Where(" id = ? ", c.ID).First(&c).Error
  54. return err
  55. }
  56. func (c CameraDevice) GetDevices(offset, limit int) ([]CameraDevice, int64, error) {
  57. var devices []CameraDevice
  58. var count int64
  59. db := Db.Model(&c)
  60. if c.DeviceSN != "" {
  61. db = db.Where("device_name like ? or device_sn like ?", "%"+c.DeviceSN+"%", "%"+c.DeviceSN+"%")
  62. }
  63. if c.CameraType != -1 {
  64. db = db.Where("camera_type = ?", c.CameraType)
  65. }
  66. db = db.Where("is_deleted = 0")
  67. db.Count(&count)
  68. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  69. return devices, count, err
  70. }
  71. func (c *CameraDevice) Delete() error {
  72. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  73. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  74. }
  75. func (c CameraDevice) GetAllDevices() ([]*CameraDevice, error) {
  76. var devices []*CameraDevice
  77. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  78. return devices, err
  79. }
  80. func (c *CameraDevice) UpdateEnable() error {
  81. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"is_enable": c.IsEnable}).Error
  82. }
  83. func (c CameraDevice) GetDevicesByGateway() []CameraDevice {
  84. var devices []CameraDevice
  85. Db.Model(&c).Where(" gateway_id = ? and is_deleted = 0",
  86. c.GatewayId).Find(&devices)
  87. return devices
  88. }
  89. func (c CameraDevice) GetDevicesByLampPole() []CameraDevice {
  90. var devices []CameraDevice
  91. Db.Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  92. c.LampPoleId).Find(&devices)
  93. return devices
  94. }
  95. func (c CameraDevice) GetDevicesByLampPoleGroup() []CameraDevice {
  96. var devices []CameraDevice
  97. Db.Model(&c).Where("group_id = ? and is_deleted = 0",
  98. c.GroupId).Find(&devices)
  99. return devices
  100. }
  101. func (c CameraDevice) GetDevicesByIds(ids string) []CameraDevice {
  102. var devices []CameraDevice
  103. db := Db.Model(&c).Where("is_deleted = 0")
  104. db.Where("id in (" + ids + ")")
  105. db.Find(&devices)
  106. return devices
  107. }
  108. func (c CameraDevice) DeviceCount1() (int64, int64) {
  109. var all, fault int64
  110. err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
  111. if err != nil {
  112. all = -1
  113. }
  114. err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
  115. if err != nil {
  116. fault = -1
  117. }
  118. return all, fault
  119. }
  120. func (c CameraDevice) ListDevices1() []Device {
  121. var devices []CameraDevice
  122. Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  123. var rsp = make([]Device, 0, len(devices))
  124. for _, v := range devices {
  125. var d = Device{
  126. Lng: v.LampLng,
  127. Lat: v.LampLat,
  128. Status: v.Status,
  129. Name: v.DeviceName,
  130. Code: v.DeviceSN,
  131. LampPoleName: v.LampPoleName,
  132. Model: "模型",
  133. LinkNum: getLinkNum(v.LampPoleSn),
  134. }
  135. rsp = append(rsp, d)
  136. }
  137. return rsp
  138. }