Beispiel #1
0
func init() {
	orm.Debug = false                         //	数据库调试模式
	orm.RegisterDriver("mysql", orm.DR_MySQL) // 注册数据库驱动 MYSQL
	//	注册数据库
	err := orm.RegisterDataBase("default", "mysql", "root:timking@tcp(127.0.0.1:13306)/learn?charset=utf8&loc=Asia%2FShanghai")
	if err != nil {
		models.Info("main", err)
	}
	//	注册数据库表模型
	orm.RegisterModel(new(models.Student), new(models.Department), new(models.Major), new(models.Class), new(models.Teacher), new(models.Admin), new(models.Term))
	orm.RegisterModel(new(models.TeacherCourse), new(models.Course), new(models.StudentCourse), new(models.StudentGrade), new(Edu.EduLog), new(models.TeacherProfile))
	orm.RegisterModel(new(models.TeacherCourseHomework), new(models.StudentNotice), new(models.StudentHomework), new(models.StudentCheck))
	err = orm.RunSyncdb("default", false, false) //	运行数据库
	if err != nil {
		models.Info("main", err)
	}
}
// @Title 导入教务系统
// @router /eduLoading [get]
func (this *APIStudentController) EduLoading() {
	//	recevie data
	if user_type := this.GetSession("type").(string); user_type != "学生" {
		return
	}
	ws, err := websocket.Upgrade(this.Ctx.ResponseWriter, this.Ctx.Request, nil, 1024, 1024)
	defer func() {
		ws.WriteJSON(&Edu.WsData{Done: true, Data: "操作结束..."})
		ws.Close()
	}()
	if _, ok := err.(websocket.HandshakeError); ok {
		http.Error(this.Ctx.ResponseWriter, "Not a websocket handshake", 400)
		return
	} else if err != nil {
		models.Info("eduLoading", err)
		return
	}
	id := this.GetString("id")
	if user, err := models.GetStudentById(id); err == nil {
		if data, ok, cookies, _ := Edu.Sign_in(id, user.EduPwd, "学生"); ok {
			ws.WriteJSON(&Edu.WsData{Done: false, Data: "登录成功..."})
			ws.WriteJSON(&Edu.WsData{Done: false, Data: "正在获取个人信息..."})
			if userinfo, err := Edu.GetStudentProfile(data, cookies); err == nil {
				ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取个人信息成功..."})
				if err = Edu.UpdateStudentProfile(user, userinfo); err != nil {
					ws.WriteJSON(&Edu.WsData{Done: false, Data: "更新个人信息失败..."})
					return
				}
				ws.WriteJSON(&Edu.WsData{Done: false, Data: "更新个人信息成功..."})
				ws.WriteJSON(&Edu.WsData{Done: false, Data: "正在读取个人课表..."})

				//	add edu log
				userinfo_json, err := json.Marshal(userinfo)
				if err == nil {
					Edu.EduLogCreate(&Edu.EduLog{Student: user, Content: string(userinfo_json), Type: "userProfile", Result: 1})
				}

				// get user schedules
				if schedules, err := Edu.GetStudentSchedule(user, data, cookies); err == nil {
					ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取个人课表成功..."})
					ws.WriteJSON(&Edu.WsData{Done: false, Data: "正在读取历史成绩..."})
					if userscore, err := Edu.GetStudentScore(user, data, cookies); err == nil {
						//	通过遍历课程表 与 历史成绩 添加 教师课程表 添加 历史成绩
						if err := Edu.ControlSQLByScheduleAndScore(user, schedules, userscore); err == nil {
							ws.WriteJSON(&Edu.WsData{Done: false, Data: "添加历史成绩成功..."})
							return
						}
						ws.WriteJSON(&Edu.WsData{Done: false, Data: "添加历史成绩失败..."})
						return
					}
					ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取历史成绩失败..."})
					return
				}
				ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取个人课表失败..."})
				return
			}
			ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取个人信息失败..."})
			return
		}
	}
}
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
}
Beispiel #4
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
}