-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from HydrogenC/teacher-name
Enable searching courses by teacher name
- Loading branch information
Showing
6 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package model | ||
|
||
type Teacher struct { | ||
ID int `json:"id"` | ||
Name string `json:"name" gorm:"not null"` // 教师姓名 | ||
CourseGroups []*CourseGroup `gorm:"many2many:teacher_course_groups;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/opentreehole/backend/common" | ||
"github.com/spf13/viper" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"gorm.io/gorm/schema" | ||
) | ||
|
||
var GormConfig = &gorm.Config{ | ||
NamingStrategy: schema.NamingStrategy{ | ||
SingularTable: true, // 表名使用单数, `User` -> `user` | ||
}, | ||
DisableForeignKeyConstraintWhenMigrating: true, // 禁用自动创建外键约束,必须手动创建或者在业务逻辑层维护 | ||
Logger: logger.New( | ||
log.New(os.Stdout, "\r\n", log.LstdFlags), | ||
logger.Config{ | ||
SlowThreshold: time.Second, // 慢 SQL 阈值 | ||
LogLevel: logger.Error, // 日志级别 | ||
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误 | ||
Colorful: false, // 禁用彩色打印 | ||
}, | ||
), | ||
} | ||
|
||
var DB *gorm.DB | ||
|
||
func Init() { | ||
viper.AutomaticEnv() | ||
dbType := viper.GetString(common.EnvDBType) | ||
dbUrl := viper.GetString(common.EnvDBUrl) | ||
|
||
var err error | ||
|
||
switch dbType { | ||
case "mysql": | ||
DB, err = gorm.Open(mysql.Open(dbUrl), GormConfig) | ||
case "postgres": | ||
DB, err = gorm.Open(postgres.Open(dbUrl), GormConfig) | ||
default: | ||
panic("db type not supported") | ||
} | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func main() { | ||
Init() | ||
// Call any script as needed | ||
// GenerateTeacherTable(DB) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} | ||
} | ||
} |