gatewayRelationDao.go 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package dao
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. //GatewayRelation 网关挂载设备关联信息表
  7. type GatewayRelation struct {
  8. ID int `gorm:"type:int;primary_key"` //编号 网关ID
  9. CameraCount int `gorm:"type:int;default 0" comment:"球机数量"`
  10. LightControlCount int `gorm:"type:int;default 0"` //灯控数量
  11. InfoBoardCount int `gorm:"type:int;default 0"` //信息屏数量
  12. OptoSensorCount int `gorm:"type:int;default 0"` //环境传感器数量
  13. ZigbeeCount int `gorm:"type:int;default 0"` //Zigbee设备数量
  14. AlarmTerminalCount int `gorm:"type:int;default 0"` //告警终端数量
  15. CaptureUnitCount int `gorm:"type:int;default 0"` //抓拍单元数量
  16. IpBroadcastCount int `gorm:"type:int;default 0"` //IP音柱数量
  17. CurveSensorCount int `gorm:"type:int;default 0" comment:"信息屏"`
  18. Total int `gorm:"type:int;default 0"` //总数
  19. TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
  20. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  21. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  22. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删
  23. }
  24. func (GatewayRelation) TableName() string {
  25. return "device_gateway_relation"
  26. }
  27. func (c *GatewayRelation) Get() error {
  28. return Db.Debug().Model(&c).Where("id = ?", c.ID).Find(&c).Error
  29. }
  30. // 2022-12-08 dsx 查出所有数据更新进去
  31. func (c *GatewayRelation) Update(gatewayId int64) error {
  32. relation := GatewayRelation{}
  33. sql := `SELECT * from (
  34. (SELECT count(1) camera_count from device_camera where is_deleted=0 and gateway_id in (48)) as camera_count,
  35. (SELECT count(1) light_control_count from device_light_control where is_deleted=0 and gateway_id in (48)) as light_control_count,
  36. (SELECT count(1) info_board_count from device_info_board where is_deleted=0 and gateway_id in (48)) as info_board_count,
  37. (SELECT count(1) opto_sensor_count from device_opto_sensor where is_deleted=0 and gateway_id in (48)) as opto_sensor_count,
  38. (SELECT count(1) zigbee_count from device_zigbee where is_deleted=0 and gateway_id in (48)) as zigbee_count,
  39. (SELECT count(1) alarm_terminal_count from device_a_key_alarm_terminal where is_deleted=0 and gateway_id in (48)) as alarm_terminal_count,
  40. (SELECT count(1) capture_unit_count from device_capture_unit where is_deleted=0 and gateway_id in (48)) as capture_unit_count,
  41. (SELECT count(1) ip_broadcast_count from device_ip_broadcast where is_deleted=0 and gateway_id in (48)) as ip_broadcast_count,
  42. (SELECT count(1) curve_sensor_count from device_curve_sensor where is_deleted=0 and gateway_id in (48)) as curve_sensor_count
  43. ) limit 1`
  44. //fmt.Printf("gatewayId = %v", gatewayId)
  45. //sql = strings.ReplaceAll(sql, "@id", string(gatewayId))
  46. //fmt.Printf("sql = %v", sql)
  47. //Db.Model(&c).Debug().Exec(sql, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId).Scan(&relation)
  48. Db.Model(&c).Debug().Exec(sql).Scan(&relation)
  49. fmt.Printf("relation1 = %v", relation)
  50. return nil
  51. relation.Total = relation.CameraCount + relation.LightControlCount + relation.InfoBoardCount +
  52. relation.OptoSensorCount + relation.ZigbeeCount + relation.AlarmTerminalCount +
  53. relation.CaptureUnitCount + relation.IpBroadcastCount + relation.CurveSensorCount
  54. fmt.Printf("relation2 = %v", relation)
  55. //return nil
  56. var count int64
  57. _ = Db.Debug().Model(&c).Where(" id = ? ", gatewayId).Count(&count)
  58. fmt.Printf("count = %v", count)
  59. if count > 0 {
  60. relation.UpdateTime = time.Now()
  61. return Db.Debug().Model(&c).Where(" id = ? ", gatewayId).Updates(&relation).Error
  62. } else {
  63. relation.ID = int(gatewayId)
  64. relation.UpdateTime = time.Now()
  65. relation.CreateTime = time.Now()
  66. return Db.Debug().Model(&c).Create(&relation).Error
  67. }
  68. }