func (al *AdminLogic) EditTeacherInfoByAdmin(teacherUsername string, fullname string, gender string, major string,
	academic string, aptitude string, problem string, userId string, userType models.UserType) (*models.User, error) {
	if len(userId) == 0 {
		return nil, errors.New("请先登录")
	} else if userType != models.ADMIN {
		return nil, errors.New("权限不足")
	} else if len(teacherUsername) == 0 {
		return nil, errors.New("咨询师工作证号为空")
	} else if len(fullname) == 0 {
		return nil, errors.New("咨询师姓名为空")
	}
	admin, err := models.GetUserById(userId)
	if err != nil || admin.UserType != models.ADMIN {
		return nil, errors.New("管理员账户出错,请联系技术支持")
	}
	teacher, err := models.GetUserByUsername(teacherUsername)
	if err != nil || teacher.UserType != models.TEACHER {
		return nil, errors.New("咨询师不存在")
	}
	teacher.Fullname = fullname
	teacher.Gender = gender
	teacher.Major = major
	teacher.Academic = academic
	teacher.Aptitude = aptitude
	teacher.Problem = problem
	if err = models.UpsertUser(teacher); err != nil {
		return nil, errors.New("获取数据失败")
	}
	return teacher, nil
}
// 查找咨询师
// 查找顺序:全名 > 工号 > 手机号
func (al *AdminLogic) SearchTeacherByAdmin(teacherFullname string, teacherUsername string, teacherMobile string, userId string, userType models.UserType) (*models.User, error) {
	if len(userId) == 0 {
		return nil, errors.New("请先登录")
	} else if userType != models.ADMIN {
		return nil, errors.New("权限不足")
	}
	admin, err := models.GetUserById(userId)
	if err != nil || admin.UserType != models.ADMIN {
		return nil, errors.New("管理员账户出错,请联系技术支持")
	}
	if !strings.EqualFold(teacherFullname, "") {
		user, err := models.GetUserByFullname(teacherFullname)
		if err == nil {
			return user, nil
		}
	}
	if !strings.EqualFold(teacherUsername, "") {
		user, err := models.GetUserByUsername(teacherUsername)
		if err == nil {
			return user, nil
		}
	}
	if !strings.EqualFold(teacherMobile, "") {
		user, err := models.GetUserByMobile(teacherMobile)
		if err == nil {
			return user, nil
		}
	}
	return nil, errors.New("用户不存在")
}
// 获取用户
func (ul *UserLogic) GetUserByUsername(username string) (*models.User, error) {
	if len(username) == 0 {
		return nil, errors.New("请先登录")
	}
	user, err := models.GetUserByUsername(username)
	if err != nil {
		return nil, errors.New("用户不存在")
	}
	return user, nil
}
func (sl *StudentLogic) GetTeacherInfoByStudent(reservationId string) (*models.User, error) {
	if len(reservationId) == 0 {
		return nil, errors.New("咨询已下架")
	}
	reservation, err := models.GetReservationById(reservationId)
	if err != nil || reservation.Status == models.DELETED {
		return nil, errors.New("咨询已下架")
	}
	teacher, err := models.GetUserByUsername(reservation.TeacherUsername)
	if err != nil || teacher.UserType != models.TEACHER {
		return nil, errors.New("咨询师不存在")
	}
	return teacher, nil
}
// 学生登录
func (ul *UserLogic) Login(username string, password string) (*models.User, error) {
	if len(username) == 0 {
		return nil, errors.New("用户名为空")
	} else if len(password) == 0 {
		return nil, errors.New("密码为空")
	}
	user, err := models.GetUserByUsername(username)
	if err == nil && (strings.EqualFold(password, user.Password) ||
		(user.UserType == models.TEACHER && strings.EqualFold(password, TeacherDefaultPassword)) ||
		(user.UserType == models.ADMIN && strings.EqualFold(password, AdminDefaultPassword))) {
		return user, nil
	}
	return nil, errors.New("用户名或密码不正确")
}
func (al *AdminLogic) GetTeacherInfoByAdmin(teacherUsername string, userId string, userType models.UserType) (*models.User, error) {
	if len(userId) == 0 {
		return nil, errors.New("请先登录")
	} else if userType != models.ADMIN {
		return nil, errors.New("权限不足")
	} else if len(teacherUsername) == 0 {
		return nil, errors.New("咨询师工作证号为空")
	}
	admin, err := models.GetUserById(userId)
	if err != nil || admin.UserType != models.ADMIN {
		return nil, errors.New("管理员账户出错,请联系技术支持")
	}
	teacher, err := models.GetUserByUsername(teacherUsername)
	if err != nil || teacher.UserType != models.TEACHER {
		return nil, errors.New("咨询师不存在")
	}
	return teacher, nil
}
// 管理员编辑咨询
func (al *AdminLogic) EditReservationByAdmin(reservationId string, startTime string, endTime string,
	teacherUsername string, teacherFullname string, teacherMobile string, userId string,
	userType models.UserType) (*models.Reservation, error) {
	if len(userId) == 0 {
		return nil, errors.New("请先登录")
	} else if userType != models.ADMIN {
		return nil, errors.New("权限不足")
	} else if len(reservationId) == 0 {
		return nil, errors.New("咨询已下架")
	} else if len(startTime) == 0 {
		return nil, errors.New("开始时间为空")
	} else if len(endTime) == 0 {
		return nil, errors.New("结束时间为空")
	} else if len(teacherUsername) == 0 {
		return nil, errors.New("咨询师工号为空")
	} else if len(teacherFullname) == 0 {
		return nil, errors.New("咨询师姓名为空")
	} else if len(teacherMobile) == 0 {
		return nil, errors.New("咨询师手机号为空")
	} else if !utils.IsMobile(teacherMobile) {
		return nil, errors.New("咨询师手机号格式不正确")
	}
	admin, err := models.GetUserById(userId)
	if err != nil || admin.UserType != models.ADMIN {
		return nil, errors.New("管理员账户出错,请联系技术支持")
	}
	reservation, err := models.GetReservationById(reservationId)
	if err != nil || reservation.Status == models.DELETED {
		return nil, errors.New("咨询已下架")
	} else if reservation.Status == models.RESERVATED {
		return nil, errors.New("不能编辑已被预约的咨询")
	}
	start, err := time.ParseInLocation(utils.TIME_PATTERN, startTime, utils.Location)
	if err != nil {
		return nil, errors.New("开始时间格式错误")
	}
	end, err := time.ParseInLocation(utils.TIME_PATTERN, endTime, utils.Location)
	if err != nil {
		return nil, errors.New("结束时间格式错误")
	}
	if start.After(end) {
		return nil, errors.New("开始时间不能晚于结束时间")
	} else if end.Before(time.Now().In(utils.Location)) {
		return nil, errors.New("不能编辑已过期咨询")
	}
	teacher, err := models.GetUserByUsername(teacherUsername)
	if err != nil {
		if teacher, err = models.AddFullUser(teacherUsername, TeacherDefaultPassword, teacherFullname,
			teacherMobile, models.TEACHER); err != nil {
			return nil, errors.New("获取数据失败")
		}
	} else if teacher.UserType != models.TEACHER {
		return nil, errors.New("权限不足")
	} else if !strings.EqualFold(teacher.Fullname, teacherFullname) || !strings.EqualFold(teacher.Mobile, teacherMobile) {
		teacher.Fullname = teacherFullname
		teacher.Mobile = teacherMobile
		if err = models.UpsertUser(teacher); err != nil {
			return nil, errors.New("数据获取失败")
		}
	}
	// 检查当天的咨询师时间是否有冲突
	theDay := time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, utils.Location)
	nextDay := theDay.AddDate(0, 0, 1)
	theDayReservations, err := models.GetReservationsBetweenTime(theDay, nextDay)
	if err != nil {
		return nil, errors.New("数据获取失败")
	}
	for _, r := range theDayReservations {
		if !strings.EqualFold(r.Id.Hex(), reservation.Id.Hex()) &&
			strings.EqualFold(r.TeacherUsername, teacher.Username) {
			if (start.After(r.StartTime) && start.Before(r.EndTime)) ||
				(end.After(r.StartTime) && end.Before(r.EndTime)) ||
				(!start.After(r.StartTime) && !end.Before(r.EndTime)) {
				return nil, errors.New("咨询师时间有冲突")
			}
		}
	}
	reservation.StartTime = start
	reservation.EndTime = end
	reservation.TeacherUsername = teacher.Username
	reservation.TeacherFullname = teacher.Fullname
	reservation.TeacherMobile = teacher.Mobile
	if err = models.UpsertReservation(reservation); err != nil {
		return nil, errors.New("数据获取失败")
	}
	return reservation, nil
}