package devices

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

type Radar struct {
	ID             int            `gorm:"primarykey" json:"ID"`                   //主键ID
	RadarName      string         `gorm:"type:varchar(64)" json:"radarName"`      //设备名称
	RadarCode      string         `gorm:"type:varchar(64)" json:"radarCode"`      //设备编码
	GatewayId      int            `gorm:"type:int" json:"gatewayId"`              //所属网关
	RadarBrand     string         `gorm:"type:varchar(60)" json:"radarBrand"`     //雷达品牌
	RadarModel     string         `gorm:"type:varchar(60)" json:"radarModel"`     //雷达型号
	DetectionRange string         `gorm:"type:varchar(60)" json:"detectionRange"` //探测距离
	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"`  //分路描述
	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 (Radar) TableName() string {
	return "dev_radar"
}

func (r Radar) UpdateRadar() error {
	err := global.GVA_DB.Model(&r).
		Select("radar_name", "radar_code", "gateway_id", "radar_brand", "radar_model", "ip_address", "install_time", "shunt", "shunt_describe", "detection_range").
		Where("id=?", r.ID).
		Updates(map[string]interface{}{
			"radar_name":      r.RadarName,
			"radar_code":      r.RadarCode,
			"gateway_id":      r.GatewayId,
			"radar_brand":     r.RadarBrand,
			"radar_model":     r.RadarModel,
			"ip_address":      r.IPAddress,
			"install_time":    r.InstallTime,
			"shunt":           r.Shunt,
			"shunt_describe":  r.ShuntDescribe,
			"detection_range": r.DetectionRange,
		}).Error
	return err

}

func (r Radar) IsExistedByCode() error {
	err := global.GVA_DB.Model(&Radar{}).Where("radar_code = ?", r.RadarCode).First(&r).Error
	return err
}
func (r Radar) AddRadar() error {
	err := global.GVA_DB.Model(&Radar{}).Create(&r).Error
	return err
}

func (r Radar) DelRadar(id int) error {
	err := global.GVA_DB.Model(&r).Where("id = ?", id).Delete(&r).Error
	return err
}

func QueryRadarInfoList(limit, offset int, keyword, shunt string) (radarList []Radar, total int64, err error) {
	db := global.GVA_DB.Model(&Radar{})
	if keyword != "" {
		db.Where("radar_name like ? or radar_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(&radarList).Error
	return
}

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

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