forked from OpenTreeHole/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teacher_table.go
94 lines (76 loc) · 2.37 KB
/
teacher_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"slices"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
const (
BatchSize = 1000
)
type Course struct {
ID int `json:"id"`
Name string `json:"name" gorm:"not null"` // 课程名称
Code string `json:"code" gorm:"not null"` // 课程编号
CodeID string `json:"code_id" gorm:"not null"` // 选课序号。用于区分同一课程编号的不同平行班
Teachers string `json:"teachers" gorm:"not null"`
CourseGroupID int `json:"course_group_id" gorm:"not null;index"` // 课程组编号
}
type Teacher struct {
ID int
Name string `gorm:"not null"` // 课程组 ID
}
type TeacherCourseLink struct {
TeacherID int `gorm:"primaryKey;autoIncrement:false"`
CourseGroupID int `gorm:"primaryKey;autoIncrement:false"` // 课程组编号
}
func AppendUnique[T comparable](slice []T, elems ...T) []T {
for _, elem := range elems {
if !slices.Contains(slice, elem) {
slice = append(slice, elem)
}
}
return slice
}
func GenerateTeacherTable(DB *gorm.DB) {
Init()
// reader := bufio.NewReader(os.Stdin)
dataMap := map[string][]int{}
var queryResult []Course
query := DB.Table("course")
query.FindInBatches(&queryResult, BatchSize, func(tx *gorm.DB, batch int) error {
for _, course := range queryResult {
teacherList := strings.Split(course.Teachers, ",")
for _, name := range teacherList {
courseList, found := dataMap[name]
if found {
dataMap[name] = AppendUnique(courseList, course.CourseGroupID)
} else {
dataMap[name] = []int{course.CourseGroupID}
}
}
}
fmt.Printf("Handled batchg %d\n", batch)
return nil
})
var teachers []*Teacher
for k := range dataMap {
teachers = append(teachers, &Teacher{Name: k})
}
// Avoid insertion failure due to duplication
DB.Clauses(clause.OnConflict{DoNothing: true}).Table("teacher").Create(teachers)
var links []*TeacherCourseLink
for index, teacher := range teachers {
for _, cid := range dataMap[teacher.Name] {
links = append(links, &TeacherCourseLink{TeacherID: teacher.ID, CourseGroupID: cid})
}
// Submit every 100 teachers to avoid SQL being too long
if index%100 == 0 {
fmt.Printf("Inserted %d teachers\n", index)
// Avoid insertion failure due to duplication
DB.Clauses(clause.OnConflict{DoNothing: true}).Table("teacher_course_groups").Create(links)
links = nil
}
}
}