2024-05-20 11:02:55 +08:00
|
|
|
package dao
|
|
|
|
|
|
2024-05-20 16:01:04 +08:00
|
|
|
import (
|
2024-05-20 17:30:39 +08:00
|
|
|
"fmt"
|
2024-05-20 16:01:04 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-20 11:02:55 +08:00
|
|
|
type Device struct {
|
2024-05-20 16:01:04 +08:00
|
|
|
gorm.Model
|
2024-05-20 11:02:55 +08:00
|
|
|
AuthID int `gorm:"column:auth_id"`
|
|
|
|
|
DeviceName string `gorm:"column:device_name"`
|
|
|
|
|
DeviceType string `gorm:"column:device_type"`
|
2024-05-20 11:16:53 +08:00
|
|
|
DeviceStatus string `gorm:"column:device_status"`
|
2024-05-20 11:02:55 +08:00
|
|
|
DeviceLocation string `gorm:"column:device_location"`
|
|
|
|
|
DeviceIP string `gorm:"column:device_ip"`
|
|
|
|
|
DeviceInfo string `gorm:"column:device_info"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 16:01:04 +08:00
|
|
|
func CreateDevice(authID int, deviceName, deviceType string, deviceStatus, deviceLocation, deviceIP, deviceInfo string) uint {
|
2024-05-20 11:02:55 +08:00
|
|
|
device := Device{AuthID: authID, DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo}
|
2024-10-04 11:37:25 +08:00
|
|
|
result := DB.Create(&device)
|
2024-05-20 16:01:04 +08:00
|
|
|
if result.Error != nil {
|
2024-05-20 17:30:39 +08:00
|
|
|
fmt.Println("CreateDevice failed", result.Error)
|
2024-05-20 16:01:04 +08:00
|
|
|
return 0
|
|
|
|
|
}
|
2024-05-20 11:02:55 +08:00
|
|
|
return device.ID
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 16:01:04 +08:00
|
|
|
func DeleteDeviceByID(id, user int) bool {
|
2024-10-04 11:37:25 +08:00
|
|
|
res := DB.Model(&Device{}).Where("id = ? and auth_id = ?", id, user).Delete(&Device{})
|
2024-05-20 16:01:04 +08:00
|
|
|
if res.Error != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FindDeviceByID(id, auth_id int) Device {
|
|
|
|
|
var device Device
|
2024-10-04 11:37:25 +08:00
|
|
|
DB.Where("id = ? and auth_id = ?", id, auth_id).First(&device)
|
2024-05-20 11:02:55 +08:00
|
|
|
return device
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FindDeviceByAuthID(auth_id int) []Device {
|
|
|
|
|
var devices []Device
|
2024-10-04 11:37:25 +08:00
|
|
|
DB.Where("auth_id = ?", auth_id).Find(&devices)
|
2024-05-20 11:02:55 +08:00
|
|
|
return devices
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 11:16:53 +08:00
|
|
|
func SetDeviceStatus(status string, id, auth_id int) bool {
|
2024-10-04 11:37:25 +08:00
|
|
|
result := DB.Model(&Device{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceStatus: status})
|
2024-05-20 16:01:04 +08:00
|
|
|
if result.Error != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-05-20 11:16:53 +08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 16:01:04 +08:00
|
|
|
func UpdateDeviceByID(id, auth_id int, deviceName, deviceType, deviceStatus, deviceLocation, deviceIP, deviceInfo string) bool {
|
|
|
|
|
pd := FindDeviceByID(id, auth_id)
|
|
|
|
|
if pd.ID == 0 {
|
|
|
|
|
return false
|
|
|
|
|
} else {
|
|
|
|
|
if deviceName == "" {
|
|
|
|
|
deviceName = pd.DeviceName
|
|
|
|
|
}
|
|
|
|
|
if deviceType == "" {
|
|
|
|
|
deviceType = pd.DeviceType
|
|
|
|
|
}
|
|
|
|
|
if deviceStatus == "" {
|
|
|
|
|
deviceStatus = pd.DeviceStatus
|
|
|
|
|
}
|
|
|
|
|
if deviceLocation == "" {
|
|
|
|
|
deviceLocation = pd.DeviceLocation
|
|
|
|
|
}
|
|
|
|
|
if deviceIP == "" {
|
|
|
|
|
deviceIP = pd.DeviceIP
|
|
|
|
|
}
|
|
|
|
|
if deviceInfo == "" {
|
|
|
|
|
deviceInfo = pd.DeviceInfo
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-04 11:37:25 +08:00
|
|
|
res := DB.Model(&Device{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo})
|
2024-05-20 16:01:04 +08:00
|
|
|
if res.Error != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|