package main

import (
	"fmt"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

//	type User struct {
//	   ID        uint      `gorm:"primaryKey"`
//	   Name      string    `gorm:"size:100"`
//	   Email     string    `gorm:"size:100;unique"`
//	   Age       int
//	   CreatedAt time.Time
//	}
func main() {
	db, _ := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	db.AutoMigrate(&User{})

	// 1. 单条插入
	user := User{
		Name:  "Alice",
		Email: "alice@example.com",
		Age:   30,
	}
	result := db.Create(&user)

	if result.Error != nil {
		panic(result.Error)
	}

	fmt.Printf("New user ID: %d\n", user.ID)
	fmt.Printf("Rows affected: %d\n", result.RowsAffected)

	// 2. 批量插入
	users := []User{
		{Name: "Bob", Email: "bob@example.com", Age: 25},
		{Name: "Charlie", Email: "charlie@example.com", Age: 35},
		{Name: "David", Email: "david@example.com", Age: 28},
	}

	db.Create(users)
	fmt.Println("Batch insert completed!")

	// 3. 指定字段插入
	user2 := User{Name: "Eve", Email: "eve@example.com", Age: 22}
	db.Select("name", "email").Create(&user2) // 仅插入 name 和 email

	fmt.Println("All users created successfully!")
}
