You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.5 KiB
Go

7 months ago
package models
import (
5 months ago
"errors"
"fmt"
"go_mqtt/mydb"
"gorm.io/gorm"
7 months ago
)
type Useryf struct {
3 weeks ago
Username string `gorm:"primaryKey;size:100"`
5 months ago
Password string `gorm:"size:255"`
Date string `gorm:"size:32"`
Nfc string `gorm:"size:255"`
Name string `gorm:"size:255"`
7 months ago
}
5 months ago
7 months ago
func init() {
fmt.Println("Useryf init()")
mydb.DB.AutoMigrate(&Useryf{})
}
// TableName 会将 User 的表名重写为 `user`
func (Useryf) TableName() string {
return "useryf"
}
func SaveUseryf(user *Useryf) {
result := mydb.DB.Create(&user)
if result.Error != nil {
fmt.Println("Failed to create User:", result.Error)
} else {
fmt.Println("User created successfully!")
}
}
func UpdateUseryf(item *Useryf) {
fmt.Println("用户存在,更新操作")
var user Useryf
5 months ago
//mydb.DB.First(&user, item.Username)
mydb.DB.Where("username = ?", item.Username).First(&user)
user.Password = item.Password
user.Date = item.Date
user.Name = item.Name
user.Nfc = item.Nfc
7 months ago
mydb.DB.Save(&user)
}
func GetUseryf(item *Useryf) bool {
var user Useryf
//mydb.DB.First(&user, item.Username)
5 months ago
//result := mydb.DB.First(&user, item.Username).Error
result := mydb.DB.Where("username = ?", item.Username).First(&user).Error
7 months ago
fmt.Println("-----user---->>")
fmt.Println(user)
if errors.Is(result, gorm.ErrRecordNotFound) {
// 记录不存在
fmt.Println("用户不存在")
} else if result == nil {
// 记录存在
fmt.Println("用户存在")
return true
} else {
// 其他错误
fmt.Println("查询出错")
}
return false
}