package dao

import (
	"time"
)

//CaptureUnit 抓拍单元
type CaptureUnit struct {
	ID                     int       `gorm:"primary_key" json:"id"`                                //编号
	CaptureName            string    `gorm:"type:varchar(64)" json:"captureName"`                  //设备名称
	CaptureSn              string    `gorm:"type:varchar(60)" json:"captureSn"`                    //设备序列号
	PointId                int       `gorm:"type:int" json:"pointId"`                              //卡口ID
	LampPoleId             int       `gorm:"type:int" json:"lampPoleId"`                           //灯杆ID
	WayName                string    `gorm:"type:varchar(64)" json:"wayName"`                      //道路名称
	CaptureDirection       int       `gorm:"type:int" json:"captureDirection"`                     //灯杆sn
	InstallTime            time.Time `gorm:"type:date" json:"installTime"`                         //安装时间
	TerrainClearance       float32   `gorm:"type:float(10,1);default 0.0" json:"terrainClearance"` //离地高度(米)
	CaptureLane            string    `gorm:"type:varchar(120)" json:"captureLane"`                 //抓拍车道
	VidiconPixel           int       `gorm:"type:int" json:"vidiconPixel"`                         //相机像素
	VidiconSpecification   string    `gorm:"type:varchar(255)" json:"vidiconSpecification"`        //相机规格
	RadarCount             int       `gorm:"type:int" json:"radarCount"`                           //雷达数量
	RadarSpecification     string    `gorm:"type:varchar(255)" json:"radarSpecification"`          //雷达规格
	SuggestSpeed           int       `gorm:"type:int" json:"suggestSpeed"`                         //雷达数量
	FillLightCount         int       `gorm:"type:int" json:"fillLightCount"`                       //补光灯数量
	FillLightSpecification string    `gorm:"type:varchar(255)" json:"fillLightSpecification"`      //补光灯规格
	PowerSpecification     string    `gorm:"type:varchar(255)" json:"powerSpecification"`          //电源规格
	CaptureIp              string    `gorm:"type:varchar(60)" json:"captureIp"`                    //ip
	CapturePort            int       `gorm:"type:int" json:"capturePort"`                          //端口
	GatewayId              int       `gorm:"type:int" json:"gatewayId"`                            //网关ID
	GatewayName            string    `gorm:"type:varchar(80)" json:"gatewayName"`                  //网关名称
	GatewaySn              string    `gorm:"type:varchar(30)" json:"gatewaySn"`                    //网关编码
	TenantId               string    `gorm:"type:varchar(12)" json:"tenantId"`                     //租户ID
	CreateTime             time.Time `gorm:"type:datetime" json:"createTime"`                      //新增时间
	CreateUser             int64     `gorm:"type:bigint" json:"createUser"`                        //新增记录操作用户ID
	UpdateTime             time.Time `gorm:"type:datetime" json:"updateTime"`                      //修改时间
	UpdateUser             int64     `gorm:"type:bigint" json:"updateUser"`                        //修改用户
	IsDeleted              int       `gorm:"type:int;default 0" json:"isDeleted"`                  //是否删除 0=未删除,1=删除
}

func (CaptureUnit) TableName() string {
	return "device_capture_unit"
}

func (c CaptureUnit) IsExistedBySN() bool {
	var count int64
	_ = Db.Debug().Model(&c).Where("capture_sn = ? and is_deleted = ?",
		c.CaptureSn, c.IsDeleted).Count(&count).Error
	return count > 0
}

func (c *CaptureUnit) Create() error {
	return Db.Debug().Model(&c).Save(&c).Error
}

func (c *CaptureUnit) Update() error {
	return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
}

func (c *CaptureUnit) GetDevice() error {
	err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).First(&c).Error
	return err
}

func (c CaptureUnit) GetDevices(offset, limit int) ([]CaptureUnit, int64, error) {
	var Captures []CaptureUnit
	var count int64
	db := Db.Debug().Model(&c)
	if c.CaptureSn != "" {
		db = db.Where("capture_name like ? or capture_sn like ?", "%"+c.CaptureSn+"%", "%"+c.CaptureSn+"%")
	}

	db = db.Where("is_deleted = 0")
	db.Count(&count)
	err := db.Offset(offset).Limit(limit).Find(&Captures).Error
	return Captures, count, err
}

func (c *CaptureUnit) Delete() error {
	return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
}

func (c CaptureUnit) GetAllDevices() ([]*CaptureUnit, error) {
	var Captures []*CaptureUnit
	err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&Captures).Error
	return Captures, err
}

func (c CaptureUnit) GetDevicesByGateway() []CaptureUnit {
	var devices []CaptureUnit
	Db.Debug().Model(&c).Where(" gateway_id = ? and is_deleted = 0",
		c.GatewayId).Find(&devices)
	return devices
}

func (c CaptureUnit) GetDevicesByLampPole() []CaptureUnit {
	var devices []CaptureUnit
	Db.Debug().Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
		c.LampPoleId).Find(&devices)
	return devices
}
