package com.gym.service; import com.gym.model.Course; import com.gym.model.User; import com.gym.repository.CourseRepository; import com.gym.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @Service public class CourseService { @Autowired private CourseRepository courseRepository; @Autowired private UserRepository userRepository; // 创建课程 public Course createCourse(Course course, Long coachId) { // 验证教练是否存在 User coach = userRepository.findById(coachId) .orElseThrow(() -> new RuntimeException("教练不存在")); // 验证教练角色 if (!"COACH".equals(coach.getRole())) { throw new RuntimeException("该用户不是教练"); } course.setCoach(coach); course.setCurrentParticipants(0); course.setIsAvailable(true); course.setStatus("UPCOMING"); course.setCreateTime(LocalDateTime.now()); return courseRepository.save(course); } // 获取所有课程 public List getAllCourses() { return courseRepository.findAll(); } // 获取可用课程 public List getAvailableCourses() { return courseRepository.findAvailableCourses(); } // 根据ID获取课程 public Optional getCourseById(Long id) { return courseRepository.findById(id); } // 更新课程信息 public Course updateCourse(Long id, Course updatedCourse) { return courseRepository.findById(id) .map(course -> { if (updatedCourse.getName() != null) { course.setName(updatedCourse.getName()); } if (updatedCourse.getType() != null) { course.setType(updatedCourse.getType()); } if (updatedCourse.getStartTime() != null) { course.setStartTime(updatedCourse.getStartTime()); } if (updatedCourse.getEndTime() != null) { course.setEndTime(updatedCourse.getEndTime()); } if (updatedCourse.getDurationMinutes() != null) { course.setDurationMinutes(updatedCourse.getDurationMinutes()); } if (updatedCourse.getLocation() != null) { course.setLocation(updatedCourse.getLocation()); } if (updatedCourse.getMaxParticipants() != null) { course.setMaxParticipants(updatedCourse.getMaxParticipants()); } if (updatedCourse.getPrice() != null) { course.setPrice(updatedCourse.getPrice()); } if (updatedCourse.getDescription() != null) { course.setDescription(updatedCourse.getDescription()); } if (updatedCourse.getIsAvailable() != null) { course.setIsAvailable(updatedCourse.getIsAvailable()); } return courseRepository.save(course); }) .orElseThrow(() -> new RuntimeException("课程不存在")); } // 删除课程 public void deleteCourse(Long id) { courseRepository.deleteById(id); } // 预约课程(增加参与人数) public Course enrollCourse(Long courseId) { return courseRepository.findById(courseId) .map(course -> { if (!course.getIsAvailable()) { throw new RuntimeException("该课程不可预约"); } if (course.getCurrentParticipants() >= course.getMaxParticipants()) { throw new RuntimeException("课程已满"); } if ("COMPLETED".equals(course.getStatus()) || "CANCELLED".equals(course.getStatus())) { throw new RuntimeException("该课程已结束或取消"); } course.setCurrentParticipants(course.getCurrentParticipants() + 1); return courseRepository.save(course); }) .orElseThrow(() -> new RuntimeException("课程不存在")); } // 取消预约(减少参与人数) public Course cancelEnrollment(Long courseId) { return courseRepository.findById(courseId) .map(course -> { if (course.getCurrentParticipants() <= 0) { throw new RuntimeException("没有可取消的预约"); } course.setCurrentParticipants(course.getCurrentParticipants() - 1); return courseRepository.save(course); }) .orElseThrow(() -> new RuntimeException("课程不存在")); } // 设置课程状态 public Course setCourseStatus(Long id, String status) { return courseRepository.findById(id) .map(course -> { course.setStatus(status); return courseRepository.save(course); }) .orElseThrow(() -> new RuntimeException("课程不存在")); } // 搜索课程 public List searchCourses(String keyword) { return courseRepository.findByNameContaining(keyword); } // 获取教练的课程 public List getCoachCourses(Long coachId) { return courseRepository.findByCoachId(coachId); } // 获取按类型分类的课程 public List getCoursesByType(String type) { return courseRepository.findByType(type); } }