diff --git a/cmd/import_csv/import_csv.go b/cmd/import_csv/import_csv.go index cb789eb..6a28fa0 100644 --- a/cmd/import_csv/import_csv.go +++ b/cmd/import_csv/import_csv.go @@ -14,8 +14,6 @@ import ( "jcourse_go/model/po" ) -const Semester = "2024-2025-1" - var ( db *gorm.DB baseCourseKeyMap = make(map[string]po.BaseCoursePO) @@ -35,11 +33,35 @@ func main() { dal.InitDBClient() db = dal.GetDBClient() + // import first level queryAllBaseCourse() readCSV("./data/base_course.csv", importBaseCourse) queryAllTeacher() readCSV("./data/teacher.csv", importTeacher) - // queryAllCourse() + + // refresh + queryAllBaseCourse() + queryAllTeacher() + + // import second level + queryAllCourse() + readCSV("./data/course.csv", importCourse) + + // refresh + queryAllCourse() + queryAllOfferedCourse() + + // import + readCSV("./data/offered_course.csv", importOfferedCourse) + + // refresh + queryAllOfferedCourse() + queryAllOfferedCourseCategory() + queryAllOfferedCourseTeacherGroup() + + // import + readCSV("./data/offered_course_category.csv", importOfferedCourseCategory) + readCSV("./data/offered_course_teacher_group.csv", importOfferedCourseTeacherGroup) } func makeBaseCourseKey(courseCode string) string { @@ -113,7 +135,7 @@ func queryAllOfferedCourse() { if !ok { continue } - offeredCourseKeyMap[makeOfferedCourseKey(course.Code, teacher.Name, Semester)] = offeredCourse + offeredCourseKeyMap[makeOfferedCourseKey(course.Code, teacher.Name, offeredCourse.Semester)] = offeredCourse } return } @@ -138,7 +160,7 @@ func queryAllOfferedCourseTeacherGroup() { if !ok { continue } - offeredCourseTeacherKeyMap[makeOfferedCourseTeacherKey(course.Code, teacher.Name, Semester)] = offeredCourseTeacher + offeredCourseTeacherKeyMap[makeOfferedCourseTeacherKey(course.Code, teacher.Name, offeredCourseTeacher.Semester)] = offeredCourseTeacher } return } @@ -162,7 +184,7 @@ func queryAllOfferedCourseCategory() { if !ok { continue } - offeredCourseCategoryMap[makeOfferedCourseCategoryKey(course.Code, teacher.Name, offeredCourseCategory.Category, Semester)] = offeredCourseCategory + offeredCourseCategoryMap[makeOfferedCourseCategoryKey(course.Code, teacher.Name, offeredCourseCategory.Category, offeredCourseCategory.Semester)] = offeredCourseCategory } return } @@ -224,3 +246,143 @@ func generatePinyinAbbr(name string) string { result := pinyin2.LazyPinyin(name, pinyin2.Args{Style: pinyin2.FirstLetter}) return strings.Join(result, "") } + +func importCourse(data [][]string) { + newCourses := make([]po.CoursePO, 0) + for _, line := range data { + courseCode := line[0] + teacherCode := line[1] + baseCourse, ok := baseCourseKeyMap[makeBaseCourseKey(courseCode)] + if !ok { + continue + } + teacher, ok := teacherKeyMap[makeTeacherKey(teacherCode)] + if !ok { + continue + } + if _, exists := courseKeyMap[makeCourseKey(courseCode, teacher.Name)]; exists { + continue + } + course := po.CoursePO{ + Code: baseCourse.Code, + Name: baseCourse.Name, + Credit: baseCourse.Credit, + BaseCourseID: int64(baseCourse.ID), + MainTeacherID: int64(teacher.ID), + MainTeacherName: teacher.Name, + } + newCourses = append(newCourses, course) + } + println("new course length:", len(newCourses)) + db.Model(&po.CoursePO{}).CreateInBatches(&newCourses, 100) +} + +func importOfferedCourse(data [][]string) { + newOfferedCourses := make([]po.OfferedCoursePO, 0) + for _, line := range data { + courseCode := line[0] + teacherCode := line[1] + semester := line[2] + teacher, ok := teacherKeyMap[makeTeacherKey(teacherCode)] + if !ok { + continue + } + + course, ok := courseKeyMap[makeCourseKey(courseCode, teacher.Name)] + if !ok { + continue + } + + if _, exists := offeredCourseKeyMap[makeOfferedCourseKey(courseCode, teacher.Name, semester)]; exists { + continue + } + + offeredCourse := po.OfferedCoursePO{ + CourseID: int64(course.ID), + BaseCourseID: course.BaseCourseID, + MainTeacherID: int64(teacher.ID), + Semester: semester, + Department: line[3], + Grade: line[4], + Language: line[5], + } + newOfferedCourses = append(newOfferedCourses, offeredCourse) + } + println("new offered course length:", len(newOfferedCourses)) + db.Model(&po.OfferedCoursePO{}).CreateInBatches(&newOfferedCourses, 100) +} + +func importOfferedCourseCategory(data [][]string) { + newOfferedCourseCategories := make([]po.OfferedCourseCategoryPO, 0) + for _, line := range data { + courseCode := line[0] + teacherCode := line[1] + semester := line[2] + category := line[3] + teacher, ok := teacherKeyMap[makeTeacherKey(teacherCode)] + if !ok { + continue + } + + offeredCourse, ok := offeredCourseKeyMap[makeOfferedCourseKey(courseCode, teacher.Name, semester)] + if !ok { + continue + } + + if _, exists := offeredCourseCategoryMap[makeOfferedCourseCategoryKey(courseCode, teacher.Name, category, semester)]; exists { + continue + } + + offeredCourseCategory := po.OfferedCourseCategoryPO{ + CourseID: offeredCourse.CourseID, + BaseCourseID: offeredCourse.BaseCourseID, + MainTeacherID: int64(teacher.ID), + OfferedCourseID: int64(offeredCourse.ID), + Category: category, + } + newOfferedCourseCategories = append(newOfferedCourseCategories, offeredCourseCategory) + } + println("new offered course category length:", len(newOfferedCourseCategories)) + db.Model(&po.OfferedCourseCategoryPO{}).CreateInBatches(&newOfferedCourseCategories, 100) +} + +func importOfferedCourseTeacherGroup(data [][]string) { + newOfferedCourseTeachers := make([]po.OfferedCourseTeacherPO, 0) + for _, line := range data { + courseCode := line[0] + mainTeacherCode := line[1] + semester := line[2] + + mainTeacher, ok := teacherKeyMap[makeTeacherKey(mainTeacherCode)] + if !ok { + continue + } + + offeredCourse, ok := offeredCourseKeyMap[makeOfferedCourseKey(courseCode, mainTeacher.Name, semester)] + if !ok { + continue + } + + thisTeacherCode := line[3] + thisTeacher, ok := teacherKeyMap[makeTeacherKey(thisTeacherCode)] + if !ok { + continue + } + + if _, exists := offeredCourseTeacherKeyMap[makeOfferedCourseTeacherKey(courseCode, thisTeacher.Name, semester)]; exists { + continue + } + + offeredCourseTeacher := po.OfferedCourseTeacherPO{ + CourseID: offeredCourse.CourseID, + BaseCourseID: offeredCourse.BaseCourseID, + MainTeacherID: int64(mainTeacher.ID), + OfferedCourseID: int64(offeredCourse.ID), + TeacherID: int64(thisTeacher.ID), + TeacherName: thisTeacher.Name, + } + newOfferedCourseTeachers = append(newOfferedCourseTeachers, offeredCourseTeacher) + } + println("new offered course teacher length:", len(newOfferedCourseTeachers)) + db.Model(&po.OfferedCourseTeacherPO{}).CreateInBatches(&newOfferedCourseTeachers, 100) +} diff --git a/model/po/course.go b/model/po/course.go index bae82d0..19863ba 100644 --- a/model/po/course.go +++ b/model/po/course.go @@ -6,9 +6,9 @@ type CoursePO struct { gorm.Model Code string `gorm:"index"` Name string `gorm:"index"` - Credit float32 `gorm:"index"` - BaseCourseID int64 `gorm:"index"` - MainTeacherID int64 `gorm:"index"` + Credit float64 `gorm:"index"` + BaseCourseID int64 `gorm:"index;index:uniq_course,unique"` + MainTeacherID int64 `gorm:"index;index:uniq_course,unique"` MainTeacherName string `gorm:"index"` } @@ -19,9 +19,9 @@ func (po *CoursePO) TableName() string { type OfferedCoursePO struct { gorm.Model BaseCourseID int64 `gorm:"index"` - CourseID int64 `gorm:"index"` - MainTeacherID int64 `gorm:"index"` - Semester string `gorm:"index"` + CourseID int64 `gorm:"index;index:uniq_offered_course,unique"` + MainTeacherID int64 `gorm:"index;index:uniq_offered_course,unique"` + Semester string `gorm:"index;index:uniq_offered_course,unique"` Department string `gorm:"index"` Language string `gorm:"index"` Grade string `gorm:"index"` @@ -35,9 +35,11 @@ type OfferedCourseTeacherPO struct { gorm.Model BaseCourseID int64 `gorm:"index"` CourseID int64 `gorm:"index"` - OfferedCourseID int64 `gorm:"index"` - TeacherID int64 `gorm:"index"` + OfferedCourseID int64 `gorm:"index;index:uniq_offered_course_teacher,unique"` + MainTeacherID int64 `gorm:"index"` + TeacherID int64 `gorm:"index;index:uniq_offered_course_teacher,unique"` TeacherName string `gorm:"index"` + Semester string `gorm:"index;index:uniq_offered_course_teacher,unique"` } func (po *OfferedCourseTeacherPO) TableName() string { @@ -48,9 +50,10 @@ type OfferedCourseCategoryPO struct { gorm.Model BaseCourseID int64 `gorm:"index"` CourseID int64 `gorm:"index"` - OfferedCourseID int64 `gorm:"index"` + OfferedCourseID int64 `gorm:"index;index:uniq_offered_course_category,unique"` MainTeacherID int64 `gorm:"index"` - Category string `gorm:"index"` + Category string `gorm:"index;index:uniq_offered_course_category,unique"` + Semester string `gorm:"index;index:uniq_offered_course_category,unique"` } func (po *OfferedCourseCategoryPO) TableName() string { @@ -59,10 +62,10 @@ func (po *OfferedCourseCategoryPO) TableName() string { type TrainingPlanPO struct { gorm.Model - Degree string `gorm:"index"` - Major string `gorm:"index"` - Department string `gorm:"index"` - EntryYear string `gorm:"index"` + Degree string `gorm:"index;index:uniq_training_plan,unique"` + Major string `gorm:"index;index:uniq_training_plan,unique"` + Department string `gorm:"index;index:uniq_training_plan,unique"` + EntryYear string `gorm:"index;index:uniq_training_plan,unique"` } func (po *TrainingPlanPO) TableName() string { @@ -71,8 +74,8 @@ func (po *TrainingPlanPO) TableName() string { type TrainingPlanCoursePO struct { gorm.Model - CourseID int64 `gorm:"index"` - TrainingPlanID int64 `gorm:"index"` + CourseID int64 `gorm:"index;index:uniq_training_plan_course,unique"` + TrainingPlanID int64 `gorm:"index;index:uniq_training_plan_course,unique"` } func (po *TrainingPlanCoursePO) TableName() string { @@ -81,7 +84,7 @@ func (po *TrainingPlanCoursePO) TableName() string { type BaseCoursePO struct { gorm.Model - Code string `gorm:"index"` + Code string `gorm:"index;uniqueIndex"` Name string `gorm:"index"` Credit float64 `gorm:"index"` } @@ -93,7 +96,7 @@ func (po *BaseCoursePO) TableName() string { type TeacherPO struct { gorm.Model Name string `gorm:"index"` - Code string `gorm:"index"` + Code string `gorm:"index;uniqueIndex"` Department string `gorm:"index"` Title string Pinyin string `gorm:"index"`