예제 #1
0
func GetStudentSchedule(user *models.Student, data []byte, cookies []*http.Cookie) (schedules map[*models.Term][]*models.TeacherCourse, err error) {
	if data == nil {
		return nil, models.ErrorInfo("GetStudentSchedule", errors.New("data in nil"))
	}
	userschedulehref, _ := getUserScheduleHref(string(data))
	userschedule_data, _, err := client.SendRequest("GET", Host_url+"/"+userschedulehref, "", cookies, header)
	if err != nil {
		return nil, models.ErrorInfo("GetStudentSchedule", err)
	}

	//	获取当前学年度的学期
	xq, _ := getUserXQ(string(userschedule_data))
	//	根据用户的入学年份和当前学年度的学期 列出学期列表
	xqs, err := getXQS(xq, user.EnterSchoolYear)
	if err != nil {
		return nil, models.ErrorInfo("GetStudentSchedule", err)
	}
	//	查询历年课表
	schedules = make(map[*models.Term][]*models.TeacherCourse, len(xqs))
	userschedule_xq_href, _ := getUserScheduleXQHref(string(userschedule_data))
	vierstate, eventvalidation, _ := getLoginKey(string(userschedule_data))
	for _, v := range xqs {
		//	判断学期是否存在 如果不存在则添加
		if len(v["学年度"]) > 0 {
			if years := strings.Split(v["学年度"], "-"); len(years) == 2 {
				startyear, _ := strconv.Atoi(years[0])
				endyear, _ := strconv.Atoi(years[1])
				if len(v["学期"]) > 0 {
					num, _ := strconv.Atoi(v["学期"])
					if exist := models.TermExist(num, startyear, endyear); !exist {
						if err := models.AddTerm(&models.Term{Number: num, StartYear: startyear, EndYear: endyear}); err == nil {
							EduLogCreate(&EduLog{Student: user, Content: `{"学年度": "` + v["学年度"] + `", "学期": "` + v["学期"] + `"}`, Type: "term", Result: 1})
						} else {
							EduLogCreate(&EduLog{Student: user, Content: `{"学年度": "` + v["学年度"] + `", "学期": "` + v["学期"] + `"}`, Type: "term", Result: 0})
						}
					}
					//	查询学期
					term, err := models.SearchTerm(num, startyear, endyear)
					if err != nil {
						models.Info("EduGetStudentSchedule", err)
						continue
					}
					//	获取login key
					userschedule_temp_data, _, err := client.SendRequest("POST", Host_url+"/"+userschedule_xq_href, url.Values{
						"__EVENTTARGET":     {"ddlXQ"},
						"__EVENTARGUMENT":   {""},
						"__LASTFOCUS":       {""},
						"__VIEWSTATE":       {vierstate},
						"__EVENTVALIDATION": {eventvalidation},
						"ddlXN":             {fmt.Sprintf("%d-%d", term.StartYear, term.EndYear)},
						"ddlXQ":             {fmt.Sprintf("%d", term.Number)},
					}.Encode(), cookies, header)
					if err != nil {
						EduLogCreate(&EduLog{Student: user, Content: `获取` + v["学年度"] + ` ` + v["学期"] + `课表失败`, Type: "t_course", Result: 0})
						continue
					}
					if !strings.Contains(string(userschedule_temp_data), `<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET"`) {
						EduLogCreate(&EduLog{Student: user, Content: `获取` + v["学年度"] + ` ` + v["学期"] + `课表失败`, Type: "t_course", Result: 0})
						continue
					}
					usercourse, _, _ := getUserCourse(string(userschedule_temp_data), cookies)
					if usercourse == nil {
						EduLogCreate(&EduLog{Student: user, Content: `获取` + v["学年度"] + ` ` + v["学期"] + `课表失败`, Type: "t_course", Result: 0})
						continue
					}
					// schedules[term] = make([]*models.TeacherCourse, len(usercourse))
					for _, v := range usercourse {
						if len(v) > 0 {
							if exist := models.CourseExist(v["课程名称"]); !exist {
								if exist := models.DepartmentExist(v["教师所在系"]); !exist {
									if _, err := models.AddDepartment(&models.Department{Name: v["教师所在系"]}); err == nil {
										EduLogCreate(&EduLog{Student: user, Content: v["教师所在系"], Type: "department", Result: 1})
									} else {
										EduLogCreate(&EduLog{Student: user, Content: v["教师所在系"], Type: "department", Result: 0})
									}
								}
								if department, err := models.GetDepartmentByName(v["教师所在系"]); err == nil {
									if exist := models.TeacherExistByDep(v["教师姓名"], department.Id); !exist {
										if err := models.AddTeacher(&models.Teacher{Name: v["教师姓名"], Department: department, Password: "******", Headimgurl: "/static/img/avatar.jpeg"}); err == nil {
											EduLogCreate(&EduLog{Student: user, Content: v["教师姓名"], Type: "teacher", Result: 1})
										} else {
											EduLogCreate(&EduLog{Student: user, Content: v["教师姓名"], Type: "teacher", Result: 0})
										}
									}
									f, _ := strconv.ParseFloat(v["学分"], 64)
									if err := models.AddCourse(&models.Course{Id: v["课程代码"], Name: v["课程名称"], Department: department, Type: v["课程性质"], Credit: f}); err == nil {
										EduLogCreate(&EduLog{Student: user, Content: v["课程名称"], Type: "course", Result: 1})
									} else {
										EduLogCreate(&EduLog{Student: user, Content: v["课程名称"], Type: "course", Result: 0})
									}
								}
							}
							if department, err := models.GetDepartmentByName(v["教师所在系"]); err == nil {
								if teacher, err := models.SearchTeacher(v["教师姓名"], department.Id); err == nil {
									//	search course
									if course, err := models.SearchCourse(v["课程名称"]); err == nil {
										if courseTime := getTime(v["上课时间"]); courseTime != nil {
											for _, v1 := range courseTime {
												end_week, _ := strconv.Atoi(v1["end_week"])
												start_week, _ := strconv.Atoi(v1["start_week"])
												spacing, _ := strconv.Atoi(v1["spacing"])
												time := `{"week_day":"` + v1["week_day"] + `", "week_time":"` + v1["time"] + `"}`
												//	add m2m between teacher and course
												if exist := models.TeacherCourseExist(course.Id, time, term.Id, teacher.Id); !exist {
													var teacher_course = &models.TeacherCourse{Course: course,
														Term:      term,
														Teacher:   teacher,
														Place:     v["上课地点"],
														StartWeek: start_week,
														EndWeek:   end_week,
														Time:      time,
														Remark:    v1["remark"],
														Spacing:   spacing,
													}
													if err := models.AddTeacherCourse(teacher_course); err == nil {
														EduLogCreate(&EduLog{Student: user, Content: `教师 ` + teacher_course.Teacher.Name + ` 教授的课程:` + teacher_course.Course.Name + `, 上课时间:` + v["上课时间"] + `, 上课地点:` + v["上课地点"], Type: "t_course", Result: 1})
													} else {
														models.Info("EduGetStudentSchedule", err)
														EduLogCreate(&EduLog{Student: user, Content: `教师 ` + teacher_course.Teacher.Name + ` 教授的课程:` + teacher_course.Course.Name + `, 上课时间:` + v["上课时间"] + `, 上课地点:` + v["上课地点"], Type: "t_course", Result: 0})
													}
												}
												//	search teacher course
												if t_course, err := models.SearchTeacherCourse(course.Id, time, term.Id, teacher.Id); err == nil {
													// add m2m between teacher_course and class
													if exist := models.ExistTeacherCourseAndClass(t_course.Id, user.Class.Id); !exist {
														if err := models.AddM2MBetweenTeacherCourseAndClass(t_course.Id, user.Class.Id); err == nil {
															EduLogCreate(&EduLog{Student: user, Content: `教师 ` + t_course.Teacher.Name + ` 教授的课程:` + t_course.Course.Name + `,班级:` + user.Class.Name, Type: "t_course_class", Result: 1})
														} else {
															EduLogCreate(&EduLog{Student: user, Content: `教师 ` + t_course.Teacher.Name + ` 教授的课程:` + t_course.Course.Name + `,班级:` + user.Class.Name, Type: "t_course_class", Result: 0})
														}
													}
													//	add m2m between teacher_course and student
													if exist := models.StudentCourseExist(user.Id, t_course.Id); !exist {
														//	add student course
														if err := models.AddStudentCourse(&models.StudentCourse{Student: user, TeacherCourse: t_course}); err == nil {
															EduLogCreate(&EduLog{Student: user, Content: `教师 ` + t_course.Teacher.Name + ` 教授的课程:` + t_course.Course.Name + `, 上课时间:` + v["上课时间"] + `, 上课地点:` + v["上课地点"], Type: "s_course", Result: 1})
														} else {
															EduLogCreate(&EduLog{Student: user, Content: `教师 ` + t_course.Teacher.Name + ` 教授的课程:` + t_course.Course.Name + `, 上课时间:` + v["上课时间"] + `, 上课地点:` + v["上课地点"], Type: "s_course", Result: 0})
														}
													}
													schedules[term] = append(schedules[term], t_course)
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return schedules, nil
}
예제 #2
0
func ControlSQLByScheduleAndScore(user *models.Student, schedules map[*models.Term][]*models.TeacherCourse, score map[string]map[string]string) error {
	if user == nil || len(schedules) <= 0 || len(score) <= 0 {
		return models.ErrorInfo("ControlSQLByScheduleAndScore", errors.New("user is nil"))
	}
	for k, v := range schedules {
		//	取出学年度 学期
		if k == nil || v == nil {
			continue
		}
		if exist := models.TermExist(k.Number, k.StartYear, k.EndYear); !exist {
			if err := models.AddTerm(k); err == nil {
				EduLogCreate(&EduLog{Student: user, Content: `{"学年度": "` + fmt.Sprintf("%d", k.StartYear) + `-` + fmt.Sprintf("%d", k.EndYear) + `", "学期": "` + fmt.Sprintf("%d", k.Number) + `"}`, Type: "term", Result: 1})
			} else {
				EduLogCreate(&EduLog{Student: user, Content: `{"学年度": "` + fmt.Sprintf("%d", k.StartYear) + `-` + fmt.Sprintf("%d", k.EndYear) + `", "学期": "` + fmt.Sprintf("%d", k.Number) + `"}`, Type: "term", Result: 0})
			}
		}
		_, err := models.SearchTerm(k.Number, k.StartYear, k.EndYear)
		if err != nil {
			models.Info("ControlSQLByScheduleAndScore", err)
			continue
		}
		for _, t_course := range v {
			if exist := models.TeacherCourseExist(t_course.Course.Id, t_course.Time, t_course.Term.Id, t_course.Teacher.Id); !exist {
				//	add teacher course
				if err := models.AddTeacherCourse(t_course); err == nil {
					EduLogCreate(&EduLog{Student: user, Content: `教师 ` + t_course.Teacher.Name + ` 教授的课程:` + t_course.Course.Name + `, 上课时间:` + t_course.Time + `, 上课地点:` + t_course.Place, Type: "t_course", Result: 1})
				} else {
					models.Info("EduGetStudentSchedule", err)
					EduLogCreate(&EduLog{Student: user, Content: `教师 ` + t_course.Teacher.Name + ` 教授的课程:` + t_course.Course.Name + `, 上课时间:` + t_course.Time + `, 上课地点:` + t_course.Place, Type: "t_course", Result: 0})
				}
			}
			if temp_t_course, err := models.SearchTeacherCourse(t_course.Course.Id, t_course.Time, t_course.Term.Id, t_course.Teacher.Id); err == nil {
				if exist := models.StudentCourseExist(user.Id, temp_t_course.Id); !exist {
					//	add student course
					if err := models.AddStudentCourse(&models.StudentCourse{Student: user, TeacherCourse: temp_t_course}); err == nil {
						EduLogCreate(&EduLog{Student: user, Content: `教师 ` + temp_t_course.Teacher.Name + ` 教授的课程:` + temp_t_course.Course.Name + `, 上课时间:` + temp_t_course.Time + `, 上课地点:` + temp_t_course.Place, Type: "s_course", Result: 1})
					} else {
						EduLogCreate(&EduLog{Student: user, Content: `教师 ` + temp_t_course.Teacher.Name + ` 教授的课程:` + temp_t_course.Course.Name + `, 上课时间:` + temp_t_course.Time + `, 上课地点:` + temp_t_course.Place, Type: "s_course", Result: 0})
					}
				}
				if temp_s_course, err := models.SearchStudentCourse(user.Id, temp_t_course.Id); err == nil {
					if exist := models.StudentGradeExist(temp_s_course.Id); !exist {
						//	取出成绩
						var (
							score = score[t_course.Course.Id]
							grade = 0
						)
						if score["重修成绩"] != "-" {
							num, _ := strconv.Atoi(score["重修成绩"])
							grade = num
						} else if score["补考成绩"] != "-" {
							num, _ := strconv.Atoi(score["补考成绩"])
							grade = num
						} else if len(score["成绩"]) > 0 {
							num, _ := strconv.Atoi(score["成绩"])
							grade = num
						}
						gpa, _ := strconv.ParseFloat(score["绩点"], 64)
						if err := models.AddStudentGrade(&models.StudentGrade{StudentCourse: temp_s_course, GradePointAverage: gpa, Grade: grade}); err == nil {
							EduLogCreate(&EduLog{Student: user, Content: `科目:` + temp_s_course.TeacherCourse.Course.Name + `的成绩为:` + fmt.Sprintf("%d", grade) + `,绩点为:` + fmt.Sprintf("%.2f", gpa), Type: "s_course_grade", Result: 1})
						} else {
							EduLogCreate(&EduLog{Student: user, Content: `科目:` + temp_s_course.TeacherCourse.Course.Name + `的成绩为:` + fmt.Sprintf("%d", grade) + `,绩点为:` + fmt.Sprintf("%.2f", gpa), Type: "s_course_grade", Result: 0})
						}
					} else {
						//	取出成绩
						var (
							score = score[t_course.Course.Id]
							grade = 0
						)
						if score["重修成绩"] != "-" {
							num, _ := strconv.Atoi(score["重修成绩"])
							grade = num
						} else if score["补考成绩"] != "-" {
							num, _ := strconv.Atoi(score["补考成绩"])
							grade = num
						} else if len(score["成绩"]) > 0 {
							num, _ := strconv.Atoi(score["成绩"])
							grade = num
						}
						gpa, _ := strconv.ParseFloat(score["绩点"], 64)
						//	查找成绩
						if student_grades, err := models.SearchStudentGradeByStudentCourse(temp_s_course); err == nil {
							for _, v := range student_grades {
								//	更新
								models.UpdateStudentGrade(&models.StudentGrade{Id: v.Id, StudentCourse: temp_s_course, GradePointAverage: gpa, Grade: grade})
							}
						}
					}
				}
			}
		}
	}
	return nil
}