package devices

import (
	"gorm.io/gorm"
	"server/global"
	"server/model/common/request"
	"time"
)

type Screens struct {
	ID            int            `gorm:"primarykey" json:"ID"`                    //主键ID
	ScreensName   string         `gorm:"type:varchar(64)" json:"screensName"`     //设备名称
	ScreensCode   string         `gorm:"type:varchar(64)" json:"screensCode"`     //设备编码
	Sn            string         `gorm:"type:varchar(64)" json:"sn"`              //设备sn
	GatewayId     int            `gorm:"type:int" json:"gatewayId"`               //所属网关
	ScreensBrand  string         `gorm:"type:varchar(60)" json:"screensBrand"`    //屏幕品牌
	ScreensModel  string         `gorm:"type:varchar(60)" json:"screensModel"`    //屏幕型号
	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"`   //分路描述
	ResolutionId  int            `gorm:"type:int" json:"resolutionId"`            //分辨率
	ScreensSizeId int            `gorm:"type:int" json:"screensSizeId"`           //信息屏尺寸
	IsDeleted     gorm.DeletedAt `gorm:"default:null" json:"isDeleted"`           //是否删除
	Status        int            `gorm:"type:int;default:0" json:"status"`        //在线状态 0=离线,1=在线
	PlayStatus    int            `gorm:"type:int;default:0" json:"playStatus"`    //播放状态
	DisplayStatus int            `gorm:"type:int;default:0" json:"displayStatus"` //显示模板状态
	SourceStatus  int            `gorm:"type:int;default:0" json:"sourceStatus"`  //素材状态

	GateWay     GateWay     `gorm:"foreignkey:GatewayId"`
	Resolution  Resolution  `gorm:"foreignkey:ResolutionId"`
	ScreensSize ScreensSize `gorm:"foreignkey:ScreensSizeId"`
}

func (Screens) TableName() string {
	return "dev_screens"
}

func (s Screens) DelScreens(id int) error {
	err := global.GVA_DB.Model(&s).Where("id = ?", id).Delete(&s).Error
	return err
}
func (s Screens) IsExistedByCode() error {
	err := global.GVA_DB.Model(&Screens{}).Where("screens_code = ?", s.ScreensCode).First(&s).Error
	return err
}
func (s Screens) AddScreens() error {
	err := global.GVA_DB.Model(&Screens{}).Create(&s).Error
	return err
}

func (s Screens) UpdateScreens() error {
	err := global.GVA_DB.Model(&s).
		Select("screens_name", "screens_code", "sn", "gateway_id", "screens_brand", "screens_model", "ip_address", "install_time", "shunt", "shunt_describe", "resolution_id", "screens_size_id").
		Where("id=?", s.ID).
		Updates(map[string]interface{}{
			"screens_name":    s.ScreensName,
			"screens_code":    s.ScreensCode,
			"sn":              s.Sn,
			"gateway_id":      s.GatewayId,
			"screens_brand":   s.ScreensBrand,
			"screens_model":   s.ScreensModel,
			"ip_address":      s.IPAddress,
			"install_time":    s.InstallTime,
			"shunt":           s.Shunt,
			"shunt_describe":  s.ShuntDescribe,
			"resolution_id":   s.ResolutionId,
			"screens_size_id": s.ScreensSizeId,
		}).Error
	return err
}

func QueryScreensInfoList(limit, offset int, keyword, shunt string) (screensList []Screens, total int64, err error) {
	db := global.GVA_DB.Model(&Screens{})
	if keyword != "" {
		db.Where("screens_name like ? or screens_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").Preload("Resolution").Preload("ScreensSize").Find(&screensList).Error
	return
}

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

func UpdateScreensStatusByGatewayId(id, status int) error {
	var screens Screens
	err := global.GVA_DB.Model(&screens).
		Select("status", "play_status", "display_status", "source_status").
		Where("gateway_id = ?", id).
		Updates(map[string]interface{}{
			"status":         status,
			"play_status":    status,
			"display_status": status,
			"source_status":  status,
		}).Error
	return err
}

func UpdateScreensStatus(code string, sta request.DeviceStatus) error {
	var screens Screens
	//err := global.GVA_DB.Model(&screens).
	//	Select("status").
	//	Where("screens_code = ?", code).
	//	Updates(map[string]interface{}{
	//		"status": status,
	//	}).Error
	//return err
	return global.GVA_DB.Model(&screens).Where("screens_code = ?", code).Updates(Screens{
		Status:        sta.Status,
		PlayStatus:    sta.PlayStatus,
		DisplayStatus: sta.DisplayStatus,
		SourceStatus:  sta.SourceStatus,
	}).Error
}

func QueryAllScreens() (screensList []Screens) {
	_ = global.GVA_DB.Model(&Screens{}).Find(&screensList)
	return screensList
}
