package dao

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

type Tunnel struct {
	global.GVA_MODEL
	TunnelSn    string        `json:"tunnelSn" gorm:"comment:隧道序号"`
	Name        string        `json:"name" gorm:"comment:名称"`
	Tactics     int           `json:"tactics" gorm:"comment:策略"`
	RegionId    int           `json:"regionId" gorm:"column:region_id"`
	Region      Region        `json:"region" gorm:"foreignKey:RegionId"` // 关系
	TimeId      int           `json:"timeId" gorm:"comment:定时id"`
	SwitchType  string        `json:"switchType" gorm:"comment:开关类型"`
	LampValue1  int           `json:"lampValue1" gorm:"comment:1路单灯值"`
	LampValue2  int           `json:"lampValue2" gorm:"comment:2路单灯值"`
	Longitude   float64       `json:"longitude" gorm:"comment:经度"`
	Latitude    float64       `json:"latitude" gorm:"comment:纬度"`
	TunnelTime  TunnelTime    `json:"tunnelTime" gorm:"foreignKey:TimeId"`
	Users       []SysUser     `json:"users" gorm:"many2many:user_tunnel"`
	Devices     []Device      `json:"devices" gorm:"foreignKey:TunnelId"`
	EnvData     []EnvData     `json:"envData" gorm:"foreignKey:TunnelSn;references:TunnelSn"`
	OpticalData []OpticalData `json:"opticalData" gorm:"foreignKey:TunnelSn;references:TunnelSn"`
}

func (Tunnel) TableName() string {
	return "tunnel"
}

func QueryAllTunnels() (tunnels []Tunnel, err error) {
	err = global.GVA_DB.Find(&tunnels).Preload("Users").Preload("TunnelTime").Preload("Devices").Preload("Devices.DeviceRelays").Preload("Devices.Tunnel").Preload("Region").Preload("EnvData").Preload("OpticalData").Error
	return tunnels, err
}

func QueryTunnelList(name string, regionId, userId, limit, offset int) (tunnels []Tunnel, total int64, err error) {
	db := global.GVA_DB.Model(Tunnel{})

	if name != "" {
		db = db.Where("name LIKE ?", "%"+name+"%")
	}

	if regionId != 0 {
		db = db.Where("region_id = ?", regionId)
	}
	db = db.Joins("JOIN user_tunnel ON user_tunnel.tunnel_id = tunnel.id").
		Where("user_tunnel.sys_user_id = ?", userId)
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Preload("Users").Preload("TunnelTime").Preload("Devices").Preload("Devices.DeviceRelays").Preload("Devices.Tunnel").Preload("Region").Preload("EnvData").Preload("OpticalData").Find(&tunnels).Error
	return
}

func QueryNoRegionTunnels() (tunnels []Tunnel, err error) {
	err = global.GVA_DB.Where("region_id =?", 0).Find(&tunnels).Error
	return
}

func QueryTunnelById(id int) (tunnel Tunnel, err error) {
	err = global.GVA_DB.Where("id =?", id).Preload("Users").Preload("TunnelTime").Preload("Devices").Preload("Devices.DeviceRelays").Preload("Devices.Tunnel").Preload("Region").Preload("EnvData").Preload("OpticalData").First(&tunnel).Error
	return
}

func QueryTunnelByRegionId(id int) (tunnels []Tunnel, err error) {
	err = global.GVA_DB.Where("region_id =?", id).Find(&tunnels).Error
	return
}

func (t Tunnel) CreateTunnel() error {
	err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		err := tx.Create(&t).Error
		if err != nil {
			return err
		}
		time := TunnelTime{
			TunnelSn:  t.TunnelSn,
			StartTime: "00:00",
			EndTime:   "00:00",
		}
		err = tx.Create(&time).Error
		if err != nil {
			return err
		}
		return nil
	})
	return err
}

func (t Tunnel) UpdateTunnel() error {
	return global.GVA_DB.Where("id = ?", t.ID).Updates(&t).Error
}

func UpdateTunnelLamp(id, lamp1, lamp2 int) error {
	return global.GVA_DB.Model(&Tunnel{}).Where("id =?", id).Update("lamp_value1", lamp1).Update("lamp_value2", lamp2).Error
}

func UpdateTunnelLamp1(sn string, lamp int) error {
	return global.GVA_DB.Model(&Tunnel{}).Where("tunnel_sn =?", sn).Update("lamp_value1", lamp).Error
}

func UpdateTunnelLamp2(sn string, lamp int) error {
	return global.GVA_DB.Model(&Tunnel{}).Where("tunnel_sn =?", sn).Update("lamp_value2", lamp).Error
}

func UpdateTactics(sn string, tactics int) error {
	return global.GVA_DB.Model(&Tunnel{}).Where("tunnel_sn =?", sn).Update("tactics", tactics).Error
}

func UpdateTunnelsRegion(tunnelIds []int, regionId int) error {
	return global.GVA_DB.Model(&Tunnel{}).Where("id in (?)", tunnelIds).Update("region_id", regionId).Error
}

func DeleteTunnel(id int) error {
	return global.GVA_DB.Unscoped().Where("id = ?", id).Delete(&Tunnel{}).Error
}

// 权限相关
