package devices

import (
	"gorm.io/gorm"
	"server/global"
	"time"
)

type Camera struct {
	ID            int            `gorm:"primarykey" json:"ID"`                  //主键ID
	CameraName    string         `gorm:"type:varchar(64)" json:"cameraName"`    //设备名称
	CameraCode    string         `gorm:"type:varchar(64)" json:"cameraCode"`    //设备编码
	CameraType    int            `gorm:"type:int" json:"cameraType"`            //摄像机类型 0=球机,1=枪机
	GatewayId     int            `gorm:"type:int" json:"gatewayId"`             //所属网关
	CameraBrand   string         `gorm:"type:varchar(60)" json:"cameraBrand"`   //摄像头品牌
	CameraModel   string         `gorm:"type:varchar(60)" json:"cameraModel"`   //摄像头型号
	IPAddress     string         `gorm:"type:varchar(40)" json:"ipAddress"`     //IP地址
	InstallTime   time.Time      `gorm:"type:date" json:"installTime"`          //安装时间
	Shunt         int            `gorm:"type:int" json:"shunt"`                 //所属分路 1主路，2支路
	ShuntDescribe string         `gorm:"type:varchar(60)" json:"shuntDescribe"` //分路描述
	StreamId      string         `gorm:"type:varchar(80)" json:"streamId"`      //流地址
	IsDeleted     gorm.DeletedAt `gorm:"default:null" json:"isDeleted"`         //是否删除
	Status        int            `gorm:"type:int;default:0" json:"status"`      //在线状态 0=离线,1=在线

	GateWay GateWay `gorm:"foreignkey:GatewayId"`
}

func (Camera) TableName() string {
	return "dev_camera"
}

func (c Camera) IsExistedByCode() error {
	err := global.GVA_DB.Model(&Camera{}).Where("camera_code = ?", c.CameraCode).First(&c).Error
	return err
}

func (c Camera) AddScreens() error {
	err := global.GVA_DB.Model(&Camera{}).Create(&c).Error
	return err
}

func (c Camera) DelCamera(id int) error {
	err := global.GVA_DB.Model(&c).Where("id = ?", id).Delete(&c).Error
	return err
}

func (c Camera) UpdateCamera() error {
	err := global.GVA_DB.Model(&c).
		Select("camera_name", "camera_code", "camera_type", "gateway_id", "camera_brand", "camera_model", "ip_address", "install_time", "shunt", "shunt_describe", "stream_id").
		Where("id=?", c.ID).
		Updates(map[string]interface{}{
			"camera_name":    c.CameraName,
			"camera_code":    c.CameraCode,
			"camera_type":    c.CameraType,
			"gateway_id":     c.GatewayId,
			"camera_brand":   c.CameraBrand,
			"camera_model":   c.CameraModel,
			"ip_address":     c.IPAddress,
			"install_time":   c.InstallTime,
			"shunt":          c.Shunt,
			"shunt_describe": c.ShuntDescribe,
			"stream_id":      c.StreamId,
		}).Error
	return err
}

func (c Camera) GetCamera(id int) (Camera, error) {
	err := global.GVA_DB.Model(&c).Preload("GateWay").Preload("GateWay.Intersect").Where("id = ?", id).Find(&c).Error
	return c, err
}

func (c Camera) GetCameraTree() (cameraList []Camera, err error) {
	err = global.GVA_DB.Model(&c).Preload("GateWay").Preload("GateWay.Intersect").Find(&cameraList).Error
	return cameraList, err
}

func QueryCameraInfoList(limit, offset int, keyword, shunt string) (cameraList []Camera, total int64, err error) {
	db := global.GVA_DB.Model(&Camera{})
	if keyword != "" {
		db.Where("camera_name like ? or camera_code like ?", "%"+keyword+"%", "%"+keyword+"%")
	}
	if shunt != "" {
		db.Where("shunt = ?", shunt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Preload("GateWay").Find(&cameraList).Error
	return
}

func QueryCameraToDetail(id int) (cameraList []Camera, err error) {
	db := global.GVA_DB.Model(&Camera{})
	err = db.Where("gateway_id = ?", id).Find(&cameraList).Error
	return
}

func UpdateCameraStatus(code string, status int) error {
	var camera Camera
	err := global.GVA_DB.Model(&camera).Debug().
		Select("status").
		Where("camera_code = ?", code).
		Updates(map[string]interface{}{
			"status": status,
		}).Error
	return err
}

func UpdateCameraStatusByGatewayId(id, status int) error {
	var camera Camera
	err := global.GVA_DB.Model(&camera).
		Select("status").
		Where("gateway_id = ?", id).
		Updates(map[string]interface{}{
			"status": status,
		}).Error
	return err
}
