// 管理员查看指定日期后30天内的所有咨询 func (rl *ReservationLogic) GetReservationsMonthlyByAdmin(from 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("权限不足") } admin, err := models.GetUserById(userId) if err != nil || admin.UserType != models.ADMIN { return nil, errors.New("管理员账户出错,请联系技术支持") } fromDate, err := time.ParseInLocation(utils.DATE_PATTERN, from, utils.Location) if err != nil { return nil, errors.New("时间格式错误") } toDate := fromDate.AddDate(0, 0, 30) reservations, err := models.GetReservationsBetweenTime(fromDate, toDate) if err != nil { return nil, errors.New("获取数据失败") } var result []*models.Reservation for _, r := range reservations { if r.Status == models.AVAILABLE && r.StartTime.Before(time.Now().In(utils.Location)) { continue } result = append(result, r) } return result, nil }
// 管理员导出咨询 func (al *AdminLogic) ExportReservationsByAdmin(reservationIds []string, userId string, userType models.UserType) (string, error) { if len(userId) == 0 { return "", errors.New("请先登录") } else if userType != models.ADMIN { return "", errors.New("权限不足") } admin, err := models.GetUserById(userId) if err != nil || admin.UserType != models.ADMIN { return "", errors.New("管理员账户出错,请联系技术支持") } var reservations []*models.Reservation for _, reservationId := range reservationIds { reservation, err := models.GetReservationById(reservationId) if err != nil { continue } reservations = append(reservations, reservation) } filename := "export_" + time.Now().In(utils.Location).Format(utils.DATE_PATTERN) + utils.ExcelSuffix if len(reservations) == 0 { return "", nil } if err = utils.ExportReservationsToExcel(reservations, filename); err != nil { return "", err } return "/" + utils.ExportFolder + filename, nil }
// 咨询师查看负7天之后的所有咨询 func (rl *ReservationLogic) GetReservationsByTeacher(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("权限不足") } teacher, err := models.GetUserById(userId) if err != nil { return nil, errors.New("请先登录") } else if teacher.UserType != models.TEACHER { return nil, errors.New("权限不足") } from := time.Now().In(utils.Location).AddDate(0, 0, -7) reservations, err := models.GetReservationsAfterTime(from) if err != nil { return nil, errors.New("获取数据失败") } var result []*models.Reservation for _, r := range reservations { if r.Status == models.AVAILABLE && r.StartTime.Before(time.Now().In(utils.Location)) { continue } else if strings.EqualFold(r.TeacherUsername, teacher.Username) { result = append(result, r) } } return result, nil }
// 咨询师取消预约 func (tl *TeacherLogic) CancelReservationsByTeacher(reservationIds []string, userId string, userType models.UserType) (int, error) { if len(userId) == 0 { return 0, errors.New("请先登录") } else if userType != models.TEACHER { return 0, errors.New("权限不足") } teacher, err := models.GetUserById(userId) if err != nil { return 0, errors.New("咨询师账户失效") } else if teacher.UserType != models.TEACHER { return 0, errors.New("权限不足") } removed := 0 for _, reservationId := range reservationIds { reseravtion, err := models.GetReservationById(reservationId) if err != nil || reseravtion.Status == models.DELETED || !strings.EqualFold(reseravtion.TeacherUsername, teacher.Username) { continue } if reseravtion.Status == models.RESERVATED && reseravtion.StartTime.After(time.Now().In(utils.Location)) { reseravtion.Status = models.AVAILABLE reseravtion.StudentInfo = models.StudentInfo{} reseravtion.StudentFeedback = models.StudentFeedback{} reseravtion.TeacherFeedback = models.TeacherFeedback{} models.UpsertReservation(reseravtion) removed++ } } return removed, nil }
// 管理员取消预约 func (al *AdminLogic) CancelReservationsByAdmin(reservationIds []string, userId string, userType models.UserType) (int, error) { if len(userId) == 0 { return 0, errors.New("请先登录") } else if userType != models.ADMIN { return 0, errors.New("权限不足") } admin, err := models.GetUserById(userId) if err != nil || admin.UserType != models.ADMIN { return 0, errors.New("管理员账户出错,请联系技术支持") } removed := 0 for _, reservationId := range reservationIds { reseravtion, err := models.GetReservationById(reservationId) if err != nil || reseravtion.Status == models.DELETED { continue } if reseravtion.Status == models.RESERVATED && reseravtion.StartTime.After(time.Now().Local()) { reseravtion.Status = models.AVAILABLE reseravtion.StudentInfo = models.StudentInfo{} reseravtion.StudentFeedback = models.StudentFeedback{} reseravtion.TeacherFeedback = models.TeacherFeedback{} models.UpsertReservation(reseravtion) removed++ } } return removed, nil }
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 (tl *TeacherLogic) GetFeedbackByTeacher(reservationId 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("咨询已下架") } 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("只能反馈本人开设的咨询") } return reservation, 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) GetUserById(userId string) (*models.User, error) { if len(userId) == 0 { return nil, errors.New("请先登录") } user, err := models.GetUserById(userId) if err != nil { return nil, errors.New("用户不存在") } return user, nil }
// 咨询师提交反馈 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 }
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) RemoveReservationsByAdmin(reservationIds []string, userId string, userType models.UserType) (int, error) { if len(userId) == 0 { return 0, errors.New("请先登录") } else if userType != models.ADMIN { return 0, errors.New("权限不足") } admin, err := models.GetUserById(userId) if err != nil || admin.UserType != models.ADMIN { return 0, errors.New("管理员账户出错,请联系技术支持") } removed := 0 for _, reservationId := range reservationIds { if reservation, err := models.GetReservationById(reservationId); err == nil { reservation.Status = models.DELETED models.UpsertReservation(reservation) removed++ } } return removed, nil }
// 管理员查看学生信息 func (al *AdminLogic) GetStudentInfoByAdmin(reservationId string, userId string, userType models.UserType) (*models.StudentInfo, 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("咨询已下架") } 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.AVAILABLE { return nil, errors.New("咨询未被预约,无法查看") } return &reservation.StudentInfo, nil }
// 管理员拉取反馈 func (al *AdminLogic) GetFeedbackByAdmin(reservationId 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("咨询已下架") } 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("咨询未被预约,不能反馈") } return reservation, nil }
// 咨询师删除咨询 func (tl *TeacherLogic) RemoveReservationsByTeacher(reservationIds []string, userId string, userType models.UserType) (int, error) { if len(userId) == 0 { return 0, errors.New("请先登录") } else if userType != models.TEACHER { return 0, errors.New("权限不足") } teacher, err := models.GetUserById(userId) if err != nil { return 0, errors.New("咨询师账户失效") } else if teacher.UserType != models.TEACHER { return 0, errors.New("权限不足") } removed := 0 for _, reservationId := range reservationIds { if reservation, err := models.GetReservationById(reservationId); err == nil && strings.EqualFold(reservation.TeacherUsername, teacher.Username) { reservation.Status = models.DELETED models.UpsertReservation(reservation) removed++ } } return removed, nil }
// 咨询师查看学生信息 func (tl *TeacherLogic) GetStudentInfoByTeacher(reservationId string, userId string, userType models.UserType) (*models.StudentInfo, 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("咨询已下架") } 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.Status == models.AVAILABLE { return nil, errors.New("咨询未被预约,无法查看") } else if !strings.EqualFold(reservation.TeacherUsername, teacher.Username) { return nil, errors.New("只能查看本人开设的咨询") } return &reservation.StudentInfo, nil }
// 管理员查看负7天之后的所有咨询 func (rl *ReservationLogic) GetReservationsByAdmin(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("权限不足") } admin, err := models.GetUserById(userId) if err != nil || admin.UserType != models.ADMIN { return nil, errors.New("管理员账户出错,请联系技术支持") } from := time.Now().In(utils.Location).AddDate(0, 0, -7) reservations, err := models.GetReservationsAfterTime(from) if err != nil { return nil, errors.New("获取数据失败") } var result []*models.Reservation for _, r := range reservations { if r.Status == models.AVAILABLE && r.StartTime.Before(time.Now().In(utils.Location)) { continue } result = append(result, r) } return result, nil }
// 咨询师编辑咨询 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 (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 }