// 咨询师提交反馈
func (tl *TeacherLogic) SubmitFeedbackByTeacher(reservationId string, teacherFullname string, teacherId string,
	studentName string, problem string, solution string, adviceToCenter 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(teacherFullname) == 0 {
		return nil, errors.New("咨询师姓名为空")
	} else if len(teacherId) == 0 {
		return nil, errors.New("咨询师工作证号为空")
	} else if len(studentName) == 0 {
		return nil, errors.New("学生姓名为空")
	} else if len(problem) == 0 {
		return nil, errors.New("咨询问题为空")
	} else if len(solution) == 0 {
		return nil, errors.New("解决方法为空")
	} else if len(adviceToCenter) == 0 {
		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("权限不足")
	}
	reservation, err := models.GetReservationById(reservationId)
	if err != nil || reservation.Status == models.DELETED {
		return nil, errors.New("咨询已下架")
	} else if reservation.StartTime.After(time.Now().In(utils.Location)) {
		return nil, errors.New("咨询未开始,暂不能反馈")
	} else if reservation.Status == models.AVAILABLE {
		return nil, errors.New("咨询未被预约,不能反馈")
	} else if !strings.EqualFold(reservation.TeacherUsername, teacher.Username) {
		return nil, errors.New("只能反馈本人开设的咨询")
	}
	if reservation.TeacherFeedback.IsEmpty() && reservation.StudentFeedback.IsEmpty() {
		utils.SendFeedbackSMS(reservation)
	}
	reservation.TeacherFeedback = models.TeacherFeedback{
		TeacherFullname: teacherFullname,
		TeacherUsername: teacherId,
		StudentFullname: studentName,
		Problem:         problem,
		Solution:        solution,
		AdviceToCenter:  adviceToCenter,
	}
	if err = models.UpsertReservation(reservation); err != nil {
		return nil, errors.New("数据获取失败")
	}
	return reservation, nil
}
// 管理员提交反馈
func (al *AdminLogic) SubmitFeedbackByAdmin(reservationId string, teacherFullname string, teacherUsername string,
	studentFullname string, problem string, solution string, adviceToCenter 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(teacherFullname) == 0 {
		return nil, errors.New("咨询师姓名为空")
	} else if len(teacherUsername) == 0 {
		return nil, errors.New("咨询师工作证号为空")
	} else if len(studentFullname) == 0 {
		return nil, errors.New("学生姓名为空")
	} else if len(problem) == 0 {
		return nil, errors.New("咨询问题为空")
	} else if len(solution) == 0 {
		return nil, errors.New("解决方法为空")
	} else if len(adviceToCenter) == 0 {
		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.StartTime.After(time.Now().Local()) {
		return nil, errors.New("咨询未开始,暂不能反馈")
	} else if reservation.Status == models.AVAILABLE {
		return nil, errors.New("咨询未被预约,不能反馈")
	} else if !strings.EqualFold(teacherUsername, reservation.TeacherUsername) {
		return nil, errors.New("咨询师工号不匹配")
	}
	sendFeedbackSMS := reservation.TeacherFeedback.IsEmpty() && reservation.StudentFeedback.IsEmpty()
	reservation.TeacherFeedback = models.TeacherFeedback{
		TeacherFullname: teacherFullname,
		TeacherUsername: teacherUsername,
		StudentFullname: studentFullname,
		Problem:         problem,
		Solution:        solution,
		AdviceToCenter:  adviceToCenter,
	}
	if err = models.UpsertReservation(reservation); err != nil {
		return nil, errors.New("数据获取失败")
	}
	if sendFeedbackSMS {
		utils.SendFeedbackSMS(reservation)
	}
	return reservation, nil
}