|
| 1 | +package com.example.smartair.service; |
| 2 | + |
| 3 | +import com.example.smartair.dto.CourseDto; |
| 4 | +import com.example.smartair.dto.TrackProgressDto; |
| 5 | +import com.example.smartair.entity.StudentCourse; |
| 6 | +import com.example.smartair.entity.Track; |
| 7 | +import com.example.smartair.entity.TrackCourse; |
| 8 | +import com.example.smartair.repository.StudentCourseRepository; |
| 9 | +import com.example.smartair.repository.TrackRepository; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | +import lombok.RequiredArgsConstructor; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
| 19 | +@Service |
| 20 | +@RequiredArgsConstructor |
| 21 | +public class TrackService { |
| 22 | + |
| 23 | + private final TrackRepository trackRepository; |
| 24 | + private final StudentCourseRepository studentCourseRepository; // 기존 기능 |
| 25 | + |
| 26 | + // ... (기존의 saveStudentCoursesFromExcel, calculateTrackProgress 메서드) ... |
| 27 | + |
| 28 | + /** |
| 29 | + * 모든 트랙 정보를 학과별로 그룹화하여 반환하는 메서드 |
| 30 | + */ |
| 31 | + public Map<String, List<Track>> getAllTracksGroupedByDepartment() { |
| 32 | + // 1. DB에서 모든 트랙과 관련 과목들을 한번에 조회 |
| 33 | + List<Track> allTracks = trackRepository.findAllWithCourses(); |
| 34 | + |
| 35 | + // 2. 조회된 트랙 리스트를 '학과' 이름으로 그룹화하여 Map으로 변환 후 반환 |
| 36 | + return allTracks.stream() |
| 37 | + .collect(Collectors.groupingBy(Track::getDepartment)); |
| 38 | + } |
| 39 | + |
| 40 | + public List<TrackProgressDto> calculateTrackProgress(Long studentId) { |
| 41 | + // 1. 학생의 이수 과목 목록 조회 |
| 42 | + Set<String> completedCourseNames = studentCourseRepository.findByStudentId(studentId) |
| 43 | + .stream() |
| 44 | + .map(StudentCourse::getCourseName) |
| 45 | + .collect(Collectors.toSet()); |
| 46 | + |
| 47 | + // 2. 모든 트랙 정보 조회 |
| 48 | + List<Track> allTracks = trackRepository.findAllWithCourses(); |
| 49 | + |
| 50 | + List<TrackProgressDto> progressList = new ArrayList<>(); |
| 51 | + |
| 52 | + // 3. 각 트랙별로 진행 현황 계산 |
| 53 | + for (Track track : allTracks) { |
| 54 | + |
| 55 | + // 현재 트랙에서 완료한 과목과 남은 과목을 담을 리스트 초기화 |
| 56 | + List<CourseDto> completedInThisTrack = new ArrayList<>(); |
| 57 | + List<CourseDto> remainingInThisTrack = new ArrayList<>(); |
| 58 | + |
| 59 | + // 현재 트랙에 속한 모든 교과목을 하나씩 확인 |
| 60 | + for (TrackCourse trackCourse : track.getCourses()) { |
| 61 | + |
| 62 | + // 학생이 해당 과목을 이수했는지 확인 (현재 이름 또는 과거 이름으로 체크) |
| 63 | + if (completedCourseNames.contains(trackCourse.getCourseName()) || |
| 64 | + (trackCourse.getCourseAlias() != null && completedCourseNames.contains(trackCourse.getCourseAlias()))) { |
| 65 | + // 이수한 경우: 완료 리스트에 추가 |
| 66 | + completedInThisTrack.add(new CourseDto(trackCourse.getCourseName(), trackCourse.getCourseAlias())); |
| 67 | + } else { |
| 68 | + // 이수하지 않은 경우: 남은 과목 리스트에 추가 |
| 69 | + remainingInThisTrack.add(new CourseDto(trackCourse.getCourseName(), trackCourse.getCourseAlias())); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // 최종 결과를 담을 DTO 객체 생성 및 데이터 세팅 |
| 74 | + TrackProgressDto progressDto = new TrackProgressDto(); |
| 75 | + progressDto.setTrackName(track.getName()); |
| 76 | + progressDto.setDepartment(track.getDepartment()); |
| 77 | + |
| 78 | + int completedCount = completedInThisTrack.size(); |
| 79 | + progressDto.setCompletedCount(completedCount); |
| 80 | + progressDto.setRequiredCount(6); // 트랙 이수 요구 과목 수는 6개 |
| 81 | + progressDto.setCompleted(completedCount >= 6); // 6개 이상이면 true |
| 82 | + |
| 83 | + progressDto.setCompletedCourses(completedInThisTrack); |
| 84 | + progressDto.setRemainingCourses(remainingInThisTrack); |
| 85 | + |
| 86 | + // 완성된 DTO를 최종 결과 리스트에 추가 |
| 87 | + progressList.add(progressDto); |
| 88 | + } |
| 89 | + |
| 90 | + return progressList; |
| 91 | + } |
| 92 | +} |
0 commit comments