// 咨询师编辑咨询
func (tl *TeacherLogic) EditReservationByTeacher(reservationId string, startTime string, endTime 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.TEACHER {
		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(teacherFullname) == 0 {
		return nil, errors.New("咨询师姓名为空")
	} else if len(teacherMobile) == 0 {
		return nil, errors.New("咨询师手机号为空")
	} else if !utils.IsMobile(teacherMobile) {
		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.GetUserById(userId)
	if err != nil {
		return nil, errors.New("咨询师账户失效")
	} else if teacher.UserType != models.TEACHER {
		return nil, errors.New("权限不足")
	} else if !strings.EqualFold(teacher.Username, reservation.TeacherUsername) {
		return nil, errors.New("只能编辑本人开设的咨询")
	}
	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.TeacherFullname = teacherFullname
	reservation.TeacherMobile = teacherMobile
	if err = models.UpsertReservation(reservation); err != nil {
		return nil, errors.New("数据获取失败")
	}
	return reservation, nil
}
// 学生预约咨询
func (sl *StudentLogic) MakeReservationByStudent(reservationId string, name string, gender string,
	studentId string, school string, hometown string, mobile string, email string, experience string,
	problem string) (*models.Reservation, error) {
	if len(reservationId) == 0 {
		return nil, errors.New("咨询已下架")
	} else if len(name) == 0 {
		return nil, errors.New("姓名为空")
	} else if len(gender) == 0 {
		return nil, errors.New("性别为空")
	} else if len(studentId) == 0 {
		return nil, errors.New("学号为空")
	} else if len(school) == 0 {
		return nil, errors.New("院系为空")
	} else if len(hometown) == 0 {
		return nil, errors.New("生源地为空")
	} else if len(mobile) == 0 {
		return nil, errors.New("手机号为空")
	} else if len(email) == 0 {
		return nil, errors.New("邮箱为空")
	} else if len(experience) == 0 {
		return nil, errors.New("咨询经历为空")
	} else if len(problem) == 0 {
		return nil, errors.New("咨询问题为空")
	} else if !utils.IsStudentId(studentId) {
		return nil, errors.New("学号不正确")
	} else if !utils.IsMobile(mobile) {
		return nil, errors.New("手机号格式不正确")
	} else if !utils.IsEmail(email) {
		return nil, errors.New("邮箱格式不正确")
	}
	reservation, err := models.GetReservationById(reservationId)
	if err != nil || reservation.Status == models.DELETED {
		return nil, errors.New("咨询已下架")
	} else if reservation.StartTime.Before(time.Now().In(utils.Location)) {
		return nil, errors.New("咨询已过期")
	} else if reservation.Status != models.AVAILABLE {
		return nil, errors.New("咨询已被预约")
	} else if time.Now().In(utils.Location).Add(3 * time.Hour).After(reservation.StartTime) {
		return nil, errors.New("距咨询开始不足3小时,无法预约")
	}
	studentReservations, err := models.GetReservationsByStudentId(studentId)
	if err != nil {
		return nil, errors.New("数据获取失败")
	}
	for _, r := range studentReservations {
		if r.Status == models.RESERVATED && r.StartTime.After(time.Now().In(utils.Location)) {
			return nil, errors.New("你好!你已有一个咨询预约,请完成这次咨询后再预约下一次,或致电62792453取消已有预约。")
		}
	}
	reservation.StudentInfo = models.StudentInfo{
		Name:       name,
		Gender:     gender,
		StudentId:  studentId,
		School:     school,
		Hometown:   hometown,
		Mobile:     mobile,
		Email:      email,
		Experience: experience,
		Problem:    problem,
	}
	reservation.Status = models.RESERVATED
	err = models.UpsertReservation(reservation)
	if err != nil {
		return nil, errors.New("获取数据失败")
	}

	// send success sms
	if checkReservation, err := models.GetReservationById(reservationId); err == nil &&
		checkReservation.Status == models.RESERVATED && strings.EqualFold(checkReservation.StudentInfo.Mobile, mobile) {
		utils.SendSuccessSMS(checkReservation)
	}
	return reservation, nil
}
示例#3
0
// 管理员添加咨询
func (al *AdminLogic) AddReservationByAdmin(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(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("管理员账户出错,请联系技术支持")
	}
	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("开始时间不能晚于结束时间")
	}
	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.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, err := models.AddReservation(start, end, teacher.Fullname, teacher.Username, teacher.Mobile)
	if err != nil {
		return nil, errors.New("数据获取失败")
	}
	return reservation, nil
}