package dao

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

type BridgeSensor struct {
	ID               int        `gorm:"primary_key" json:"id"`                    //编号
	SensorType       int        `gorm:"type:int" json:"sensorType"`               //传感器类型
	Name             string     `gorm:"type:varchar(64)" json:"name"`             //名称
	SN               string     `gorm:"type:varchar(60)" json:"sn"`               //唯一编码
	BrandID          int        `gorm:"type:int" json:"brandID"`                  //设备名称
	ModelID          int        `gorm:"type:int" json:"modelID"`                  //设备型号
	BridgeId         int        `gorm:"type:int" json:"bridgeId"`                 //所属桥梁id
	BridgeSn         string     `gorm:"type:varchar(60)" json:"bridgeSn"`         //所属桥梁编码
	GatewayId        int        `gorm:"type:int" json:"gatewayId"`                //所属网关id
	GatewaySN        string     `gorm:"type:varchar(60)" json:"gatewaySn"`        //所属网关编码
	InstallPosition  string     `gorm:"type:varchar(100)" json:"installPosition"` //安装位置
	InstallTime      *time.Time `gorm:"type:date" json:"installTime"`             //安装时间
	IPAddress        string     `gorm:"type:varchar(64)" json:"ipAddress"`        //IP地址-备用
	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=删除
	Status           int        `gorm:"type:int" json:"status"`                   //状态 0=正常,1=异常
	IsEnable         int        `gorm:"type:tinyint; default 1" json:"isEnable"`  //启用禁用：1启用，2禁用
	TerrainClearance float32    `gorm:"double(4, 2)" json:"terrainClearance"`     //状态 0=正常,1=异常
}

func (BridgeSensor) TableName() string {
	return "device_bridge_sensor"
}

func (c BridgeSensor) 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 BridgeSensor) IsExistedBySN() bool {
	var count int64
	_ = Db.Debug().Model(&c).Where("  tsn = ? and is_deleted = ?",
		c.SN, c.IsDeleted).Count(&count).Error
	return count > 0
}

func (c BridgeSensor) IsExistedByNameAndCode() bool {
	var devices []BridgeSensor
	err := Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
		c.SN, c.IsDeleted).Find(&devices).Error
	//如果查询不到，返回相应的错误
	if gorm.ErrRecordNotFound == err {
		return false
	}
	for _, d := range devices {
		if d.ID != c.ID {
			return true
		}
	}
	return false
}

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

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

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

func (c BridgeSensor) GetDevices(offset, limit int) ([]BridgeSensor, int64, error) {
	var devices []BridgeSensor
	var count int64
	db := Db.Debug().Model(&c).Where(" name like ? and is_deleted = 0", "%"+c.Name+"%")
	db.Count(&count)
	err := db.Offset(offset).Limit(limit).Find(&devices).Error
	return devices, count, err
}

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

func (c *BridgeSensor) UpdateEnable() error {
	return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"is_enable": c.IsEnable}).Error
}
