package dao

import "gorm.io/gorm"

type Bluetooth struct {
	ID         uint64 `gorm:"primaryKey"`
	DeviceId   string `gorm:"unique;type varchar(64);not null;comment:'设备Id，唯一标识每个蓝牙设备'"`
	DeviceName string `gorm:"type varchar(64);comment:'蓝牙名称'"`
	DeviceType string `gorm:"type varchar(64);comment:'设备类型'"`
	UserId     int32  `gorm:"index;comment:'用户ID'"`
}

func AddBluetooth(data *Bluetooth) error {
	if err := s.db.Where("device_id = ? AND user_id = ?", data.DeviceId, data.UserId).FirstOrCreate(data).Error; err != nil {
		return err
	}
	return nil
}

func DeleteBluetooth(deviceid string, userid int32) error {
	var data *Bluetooth
	if err := s.db.Where("device_id = ? AND user_id = ?", deviceid, userid).First(&data).Error; err != nil && err != gorm.ErrRecordNotFound {
		return err
	}
	if err := s.db.Delete(&data).Error; err != nil && err != gorm.ErrRecordNotFound {
		return err
	}
	return nil
}

func GetAllBluetooth(userid int32) ([]*Bluetooth, error) {
	var list []*Bluetooth
	if err := s.db.Where("user_id = ?", userid).Find(&list).Error; err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return list, nil
}
