Files
X-Agents/server/temp_add_data2.go

123 lines
3.6 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"math/rand"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type Teacher struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"size:50;charset=utf8mb4"`
Subject string `gorm:"size:50;charset=utf8mb4"`
Phone string `gorm:"size:20"`
CreatedAt time.Time
}
type Student struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"size:50;charset=utf8mb4"`
Age int
Gender string `gorm:"size:10;charset=utf8mb4"`
Class string `gorm:"size:50;charset=utf8mb4"`
Phone string `gorm:"size:20"`
CreatedAt time.Time
}
type Score struct {
ID uint `gorm:"primaryKey"`
StudentID uint
Subject string `gorm:"size:50;charset=utf8mb4"`
Score float64
TeacherID uint
ExamDate time.Time
CreatedAt time.Time
}
func main() {
dsn := "root:881116142@tcp(10.10.10.189:3306)/students?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("连接数据库失败: " + err.Error())
}
// 自动迁移表
db.AutoMigrate(&Teacher{}, &Student{}, &Score{})
// 清理旧数据
db.Exec("DELETE FROM scores")
db.Exec("DELETE FROM students")
db.Exec("DELETE FROM teachers")
rand.Seed(time.Now().UnixNano())
// 创建教师
teachers := []Teacher{
{Name: "张老师", Subject: "数学", Phone: "13800001001"},
{Name: "李老师", Subject: "语文", Phone: "13800001002"},
{Name: "王老师", Subject: "英语", Phone: "13800001003"},
{Name: "刘老师", Subject: "物理", Phone: "13800001004"},
{Name: "陈老师", Subject: "化学", Phone: "13800001005"},
{Name: "杨老师", Subject: "生物", Phone: "13800001006"},
{Name: "赵老师", Subject: "历史", Phone: "13800001007"},
{Name: "周老师", Subject: "地理", Phone: "13800001008"},
}
db.Create(&teachers)
// 创建30个学生
names := []string{"张三", "李四", "王五", "刘六", "陈七", "杨八", "赵九", "钱十",
"孙一", "周二", "吴三", "郑四", "冯五", "褚六", "卫七", "蒋八",
"沈九", "韩十", "朱十一", "秦十二", "许十三", "何十四", "吕十五", "施十六",
"张十七", "孔十八", "曹十九", "严二十", "华二十一", "金二十二"}
genders := []string{"男", "女"}
classes := []string{"高一(1)班", "高一(2)班", "高一(3)班", "高二(1)班", "高二(2)班"}
students := make([]Student, 30)
for i := 0; i < 30; i++ {
students[i] = Student{
Name: names[i],
Age: 15 + rand.Intn(3),
Gender: genders[rand.Intn(len(genders))],
Class: classes[rand.Intn(len(classes))],
Phone: fmt.Sprintf("139%08d", 10000000+rand.Intn(90000000)),
}
}
db.Create(&students)
// 为每个学生创建成绩记录
subjects := []string{"数学", "语文", "英语", "物理", "化学", "生物", "历史", "地理"}
scores := make([]Score, 0)
for i := 0; i < 30; i++ {
numSubjects := 4 + rand.Intn(3)
selectedSubjects := make(map[string]bool)
for len(selectedSubjects) < numSubjects {
subj := subjects[rand.Intn(len(subjects))]
if !selectedSubjects[subj] {
selectedSubjects[subj] = true
teacherID := uint(1 + rand.Intn(len(teachers)))
examDate := time.Now().AddDate(0, -rand.Intn(6), -rand.Intn(30))
score := Score{
StudentID: students[i].ID,
Subject: subj,
Score: 60 + rand.Float64()*40,
TeacherID: teacherID,
ExamDate: examDate,
}
scores = append(scores, score)
}
}
}
db.Create(&scores)
fmt.Println("数据创建成功!")
fmt.Printf("教师: %d 条\n", len(teachers))
fmt.Printf("学生: %d 条\n", len(students))
fmt.Printf("成绩: %d 条\n", len(scores))
}