|
@@ -1,19 +1,22 @@
|
|
|
package dao
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
|
|
|
type GatewayRelation struct {
|
|
|
- ID int `gorm:"type:int;primary_key"`
|
|
|
- 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"`
|
|
|
- AlarmTerminalCount int `gorm:"type:int;default 0"`
|
|
|
- CaptureUnitCount int `gorm:"type:int;default 0"`
|
|
|
- IpBroadcastCount int `gorm:"type:int;default 0"`
|
|
|
+ ID int `gorm:"type:int;primary_key"`
|
|
|
+ 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"`
|
|
|
+ AlarmTerminalCount int `gorm:"type:int;default 0"`
|
|
|
+ CaptureUnitCount int `gorm:"type:int;default 0"`
|
|
|
+ IpBroadcastCount int `gorm:"type:int;default 0"`
|
|
|
+ CurveSensorCount int `gorm:"type:int;default 0" comment:"信息屏"`
|
|
|
Total int `gorm:"type:int;default 0"`
|
|
|
TenantId int `gorm:"type:int" json:"tenantId"`
|
|
|
CreateTime time.Time `gorm:"type:datetime" json:"createTime"`
|
|
@@ -28,3 +31,43 @@ func (GatewayRelation) TableName() string {
|
|
|
func (c *GatewayRelation) Get() error {
|
|
|
return Db.Debug().Model(&c).Where("id = ?", c.ID).Find(&c).Error
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+func (c *GatewayRelation) Update(gatewayId int64) error {
|
|
|
+ relation := GatewayRelation{}
|
|
|
+ sql := `SELECT * from (
|
|
|
+ (SELECT count(1) camera_count from device_camera where is_deleted=0 and gateway_id in (48)) as camera_count,
|
|
|
+ (SELECT count(1) light_control_count from device_light_control where is_deleted=0 and gateway_id in (48)) as light_control_count,
|
|
|
+ (SELECT count(1) info_board_count from device_info_board where is_deleted=0 and gateway_id in (48)) as info_board_count,
|
|
|
+ (SELECT count(1) opto_sensor_count from device_opto_sensor where is_deleted=0 and gateway_id in (48)) as opto_sensor_count,
|
|
|
+ (SELECT count(1) zigbee_count from device_zigbee where is_deleted=0 and gateway_id in (48)) as zigbee_count,
|
|
|
+ (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,
|
|
|
+ (SELECT count(1) capture_unit_count from device_capture_unit where is_deleted=0 and gateway_id in (48)) as capture_unit_count,
|
|
|
+ (SELECT count(1) ip_broadcast_count from device_ip_broadcast where is_deleted=0 and gateway_id in (48)) as ip_broadcast_count,
|
|
|
+ (SELECT count(1) curve_sensor_count from device_curve_sensor where is_deleted=0 and gateway_id in (48)) as curve_sensor_count
|
|
|
+ ) limit 1`
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Db.Model(&c).Debug().Exec(sql).Scan(&relation)
|
|
|
+ fmt.Printf("relation1 = %v", relation)
|
|
|
+ return nil
|
|
|
+ relation.Total = relation.CameraCount + relation.LightControlCount + relation.InfoBoardCount +
|
|
|
+ relation.OptoSensorCount + relation.ZigbeeCount + relation.AlarmTerminalCount +
|
|
|
+ relation.CaptureUnitCount + relation.IpBroadcastCount + relation.CurveSensorCount
|
|
|
+ fmt.Printf("relation2 = %v", relation)
|
|
|
+
|
|
|
+ var count int64
|
|
|
+ _ = Db.Debug().Model(&c).Where(" id = ? ", gatewayId).Count(&count)
|
|
|
+ fmt.Printf("count = %v", 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
|
|
|
+ }
|
|
|
+}
|