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.
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"go_mqtt/mydb"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Order struct {
|
|
|
|
|
ID uint
|
|
|
|
|
OrderNumber string
|
|
|
|
|
TotalAmount float64
|
|
|
|
|
|
|
|
|
|
//User User `gorm:"foreignKey:UserID"` // 一对一关联,通过 UserID 外键关联到 User 结构体
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
fmt.Println("Order init()")
|
|
|
|
|
mydb.DB.AutoMigrate(&Order{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName 会将 Product 的表名重写为 `product`
|
|
|
|
|
func (Order) TableName() string {
|
|
|
|
|
return "order"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SaveOrder() {
|
|
|
|
|
uuid := uuid.New()
|
|
|
|
|
// 创建记录
|
|
|
|
|
order := Order{OrderNumber: uuid.String(), TotalAmount: 1999.9}
|
|
|
|
|
result := mydb.DB.Create(&order)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
fmt.Println("Failed to create order:", result.Error)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("order created successfully!")
|
|
|
|
|
}
|
|
|
|
|
}
|