package devices

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

type GateWay struct {
	ID           int            `gorm:"primary_key" json:"id"`                          //编号
	GatewayName  string         `gorm:"type:varchar(64)"  json:"gatewayName"`           //网关名称
	IntersectId  int            `gorm:"type:int" json:"intersectId"`                    //路口id
	GatewayCode  string         `gorm:"type:varchar(60)" json:"gatewayCode"`            //设备编号
	GatewayBrand string         `gorm:"type:varchar(60)" json:"gatewayBrand"`           //网关品牌
	GatewayModel string         `gorm:"type:varchar(60)" json:"gatewayModel"`           //网关型号
	IpAddress    string         `gorm:"type:varchar(50)" json:"ipAddress"`              //IP地址
	InstallTime  time.Time      `gorm:"type:date" json:"installTime"`                   //安装时间
	RecentOnline time.Time      `gorm:"type:datetime;default:null" json:"recentOnline"` //最近在线时间
	IsDeleted    gorm.DeletedAt `gorm:"default:null" json:"isDeleted"`                  //是否删除
	Status       int            `gorm:"type:int;default:0" json:"status"`               //在线状态 0=离线,1=在线

	Intersect Intersect `gorm:"foreignkey:IntersectId"`
}

func (GateWay) TableName() string {
	return "dev_gateway"
}

func QueryGateWayInfoList(limit, offset int, keyword string) (gatewayList []GateWay, total int64, err error) {
	db := global.GVA_DB.Model(&GateWay{})
	if keyword != "" {
		db.Where("gateway_name like ? or gateway_code like ?", "%"+keyword+"%", "%"+keyword+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Preload("Intersect").Find(&gatewayList).Error
	return
}

func (g GateWay) IsExistedByCode() error {
	err := global.GVA_DB.Model(&GateWay{}).Where("gateway_code = ?", g.GatewayCode).First(&g).Error
	return err
}

func (g GateWay) AddGateway() error {
	err := global.GVA_DB.Model(&GateWay{}).Create(&g).Error
	return err
}

func (g GateWay) UpdateGateway() error {
	err := global.GVA_DB.Model(&g).
		Select("gateway_name", "intersect_id", "gateway_brand", "gateway_model", "ip_address", "install_time").
		Where("id=?", g.ID).
		Updates(map[string]interface{}{
			"gateway_name":  g.GatewayName,
			"intersect_id":  g.IntersectId,
			"gateway_brand": g.GatewayBrand,
			"gateway_model": g.GatewayModel,
			"ip_address":    g.IpAddress,
			"install_time":  g.InstallTime,
		}).Error
	return err
}

func (g GateWay) DelGateway(id int) error {
	err := global.GVA_DB.Model(&g).Where("id = ?", id).Delete(&g).Error
	return err
}

func (g GateWay) PublicGateway() (gatewayList []GateWay, err error) {
	err = global.GVA_DB.Model(&GateWay{}).Select("id", "gateway_code", "gateway_name", "recent_online").Find(&gatewayList).Error
	return
}

func UpdateGatewayRecentOnline(code string, recentTime time.Time) error {
	var gateway GateWay
	err := global.GVA_DB.Model(&gateway).
		Select("recent_online").
		Where("gateway_code = ?", code).
		Updates(map[string]interface{}{
			"recent_online": recentTime,
		}).Error
	return err
}

func UpdateGatewayStatus(code string, statu int) error {
	var gateway GateWay
	err := global.GVA_DB.Model(&gateway).
		Select("status").
		Where("gateway_code = ?", code).
		Updates(map[string]interface{}{
			"status": statu,
		}).Error
	return err
}
