package dao import ( "time" ) //GatewayRelation 网关挂载设备关联信息表 type GatewayRelation struct { ID int `gorm:"type:int;primary_key"` //编号 网关ID CameraCount int `gorm:"type:int;default 0" comment:"球机数量"` LightControlCount int `gorm:"type:int;default 0"` //灯控数量 InfoBoardCount int `gorm:"type:int;default 0"` //信息屏数量 OptoSensorCount int `gorm:"type:int;default 0"` //环境传感器数量 ZigbeeCount int `gorm:"type:int;default 0"` //Zigbee设备数量 AlarmTerminalCount int `gorm:"type:int;default 0"` //告警终端数量 CaptureUnitCount int `gorm:"type:int;default 0"` //抓拍单元数量 IpBroadcastCount int `gorm:"type:int;default 0"` //IP音柱数量 CurveSensorCount int `gorm:"type:int;default 0" comment:"信息屏"` Total int `gorm:"type:int;default 0"` //总数 TenantId int `gorm:"type:int" json:"tenantId"` //租户ID CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间 UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间 IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删 } func (GatewayRelation) TableName() string { return "device_gateway_relation" } func (c *GatewayRelation) Get() error { return Db.Debug().Model(&c).Where("id = ?", c.ID).Find(&c).Error } // 2022-12-08 dsx 查出所有数据更新进去 func (c *GatewayRelation) Update() error { gatewayId := c.ID relation := GatewayRelation{} sql := `SELECT * from ( (SELECT count(1) camera_count from device_camera where is_deleted=0 and gateway_id in (?)) as camera_count, (SELECT count(1) light_control_count from device_light_control where is_deleted=0 and gateway_id in (?)) as light_control_count, (SELECT count(1) info_board_count from device_info_board where is_deleted=0 and gateway_id in (?)) as info_board_count, (SELECT count(1) opto_sensor_count from device_opto_sensor where is_deleted=0 and gateway_id in (?)) as opto_sensor_count, (SELECT count(1) zigbee_count from device_zigbee where is_deleted=0 and gateway_id in (?)) as zigbee_count, (SELECT count(1) alarm_terminal_count from device_a_key_alarm_terminal where is_deleted=0 and gateway_id in (?)) as alarm_terminal_count, (SELECT count(1) capture_unit_count from device_capture_unit where is_deleted=0 and gateway_id in (?)) as capture_unit_count, (SELECT count(1) ip_broadcast_count from device_ip_broadcast where is_deleted=0 and gateway_id in (?)) as ip_broadcast_count, (SELECT count(1) curve_sensor_count from device_curve_sensor where is_deleted=0 and gateway_id in (?)) as curve_sensor_count ) limit 1` Db.Debug().Model(&c).Raw(sql, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId, gatewayId).Scan(&relation) relation.Total = relation.CameraCount + relation.LightControlCount + relation.InfoBoardCount + relation.OptoSensorCount + relation.ZigbeeCount + relation.AlarmTerminalCount + relation.CaptureUnitCount + relation.IpBroadcastCount + relation.CurveSensorCount var count int64 _ = Db.Debug().Model(&c).Where(" id = ? ", gatewayId).Count(&count) if count > 0 { relation.UpdateTime = time.Now() return Db.Debug().Model(&c).Where(" id = ? ", gatewayId).Updates(&relation).Error } else { relation.ID = int(gatewayId) relation.UpdateTime = time.Now() relation.CreateTime = time.Now() return Db.Debug().Model(&c).Create(&relation).Error } }