|
| 1 | +package com.gpt.geumpumtabackend.rank.service; |
| 2 | + |
| 3 | +import com.gpt.geumpumtabackend.global.exception.BusinessException; |
| 4 | +import com.gpt.geumpumtabackend.global.exception.ExceptionType; |
| 5 | +import com.gpt.geumpumtabackend.rank.dto.DepartmentRankingTemp; |
| 6 | +import com.gpt.geumpumtabackend.rank.dto.response.DepartmentRankingEntryResponse; |
| 7 | +import com.gpt.geumpumtabackend.rank.dto.response.DepartmentRankingResponse; |
| 8 | +import com.gpt.geumpumtabackend.rank.repository.DepartmentRankingRepository; |
| 9 | +import com.gpt.geumpumtabackend.study.repository.StudySessionRepository; |
| 10 | +import com.gpt.geumpumtabackend.user.domain.User; |
| 11 | +import com.gpt.geumpumtabackend.user.repository.UserRepository; |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | + |
| 15 | +import java.time.DayOfWeek; |
| 16 | +import java.time.LocalDate; |
| 17 | +import java.time.LocalDateTime; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +@Service |
| 22 | +@RequiredArgsConstructor |
| 23 | +public class DepartmentRankService { |
| 24 | + |
| 25 | + private final DepartmentRankingRepository departmentRankingRepository; |
| 26 | + private final StudySessionRepository studySessionRepository; |
| 27 | + private final UserRepository userRepository; |
| 28 | + |
| 29 | + /* |
| 30 | + 현재 진행중인 학과 랭킹 일간 조회 |
| 31 | + */ |
| 32 | + public DepartmentRankingResponse getCurrentDailyDepartmentRanking(Long userId){ |
| 33 | + LocalDate today = LocalDate.now(); |
| 34 | + LocalDateTime startDay = today.atStartOfDay(); |
| 35 | + LocalDateTime endDay = today.atTime(23, 59, 59); |
| 36 | + LocalDateTime nowTime = LocalDateTime.now(); |
| 37 | + List<DepartmentRankingTemp> departmentRankingList = studySessionRepository.calculateCurrentDepartmentRanking(startDay, endDay, nowTime); |
| 38 | + DepartmentRankingEntryResponse myRanking = null; |
| 39 | + List<DepartmentRankingEntryResponse> topRankings = new ArrayList<>(); |
| 40 | + User user = userRepository.findById(userId).orElseThrow(()->new BusinessException(ExceptionType.USER_NOT_FOUND)); |
| 41 | + for (DepartmentRankingTemp temp : departmentRankingList) { |
| 42 | + DepartmentRankingEntryResponse entry = DepartmentRankingEntryResponse.of(temp); |
| 43 | + topRankings.add(entry); |
| 44 | + |
| 45 | + if(user.getDepartment().equals(temp.getDepartmentName())){ |
| 46 | + myRanking = entry; |
| 47 | + } |
| 48 | + } |
| 49 | + return new DepartmentRankingResponse(topRankings, myRanking); |
| 50 | + } |
| 51 | + |
| 52 | + /* |
| 53 | + 완료된 학과 랭킹 일간 조회 |
| 54 | + */ |
| 55 | + public DepartmentRankingResponse getCompletedDailyDepartmentRanking(Long userId, LocalDateTime startDay){ |
| 56 | + List<DepartmentRankingTemp> departmentRankingList = departmentRankingRepository.getFinishedDepartmentRanking(startDay); |
| 57 | + DepartmentRankingEntryResponse myRanking = null; |
| 58 | + List<DepartmentRankingEntryResponse> topRankings = new ArrayList<>(); |
| 59 | + User user = userRepository.findById(userId).orElseThrow(()->new BusinessException(ExceptionType.USER_NOT_FOUND)); |
| 60 | + for (DepartmentRankingTemp temp : departmentRankingList) { |
| 61 | + DepartmentRankingEntryResponse entry = DepartmentRankingEntryResponse.of(temp); |
| 62 | + topRankings.add(entry); |
| 63 | + |
| 64 | + if(user.getDepartment().equals(temp.getDepartmentName())){ |
| 65 | + myRanking = entry; |
| 66 | + } |
| 67 | + } |
| 68 | + return new DepartmentRankingResponse(topRankings, myRanking); |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + 현재 진행중인 학과 랭킹 주간 조회 |
| 73 | + */ |
| 74 | + public DepartmentRankingResponse getCurrentWeeklyDepartmentRanking(Long userId){ |
| 75 | + LocalDate today = LocalDate.now(); |
| 76 | + LocalDateTime weekStart = today.with(DayOfWeek.MONDAY).atStartOfDay(); |
| 77 | + LocalDateTime weekEnd = today.with(DayOfWeek.SUNDAY).atTime(23, 59, 59); |
| 78 | + LocalDateTime nowTime = LocalDateTime.now(); |
| 79 | + List<DepartmentRankingTemp> departmentRankingList = studySessionRepository.calculateCurrentDepartmentRanking(weekStart, weekEnd, nowTime); |
| 80 | + DepartmentRankingEntryResponse myRanking = null; |
| 81 | + List<DepartmentRankingEntryResponse> topRankings = new ArrayList<>(); |
| 82 | + User user = userRepository.findById(userId).orElseThrow(()->new BusinessException(ExceptionType.USER_NOT_FOUND)); |
| 83 | + for (DepartmentRankingTemp temp : departmentRankingList) { |
| 84 | + DepartmentRankingEntryResponse entry = DepartmentRankingEntryResponse.of(temp); |
| 85 | + topRankings.add(entry); |
| 86 | + |
| 87 | + if(user.getDepartment().equals(temp.getDepartmentName())){ |
| 88 | + myRanking = entry; |
| 89 | + } |
| 90 | + } |
| 91 | + return new DepartmentRankingResponse(topRankings, myRanking); |
| 92 | + } |
| 93 | + |
| 94 | + /* |
| 95 | + 완료된 학과 랭킹 주간 조회 |
| 96 | + */ |
| 97 | + public DepartmentRankingResponse getCompletedWeeklyDepartmentRanking(Long userId, LocalDateTime weekFirstDay){ |
| 98 | + List<DepartmentRankingTemp> departmentRankingList = departmentRankingRepository.getFinishedDepartmentRanking(weekFirstDay); |
| 99 | + DepartmentRankingEntryResponse myRanking = null; |
| 100 | + List<DepartmentRankingEntryResponse> topRankings = new ArrayList<>(); |
| 101 | + User user = userRepository.findById(userId).orElseThrow(()->new BusinessException(ExceptionType.USER_NOT_FOUND)); |
| 102 | + for (DepartmentRankingTemp temp : departmentRankingList) { |
| 103 | + DepartmentRankingEntryResponse entry = DepartmentRankingEntryResponse.of(temp); |
| 104 | + topRankings.add(entry); |
| 105 | + |
| 106 | + if(user.getDepartment().equals(temp.getDepartmentName())){ |
| 107 | + myRanking = entry; |
| 108 | + } |
| 109 | + } |
| 110 | + return new DepartmentRankingResponse(topRankings, myRanking); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + /* |
| 115 | + 현재 진행중인 학과 랭킹 월간 조회 |
| 116 | + */ |
| 117 | + public DepartmentRankingResponse getCurrentMonthlyDepartmentRanking(Long userId){ |
| 118 | + LocalDate today = LocalDate.now(); |
| 119 | + LocalDateTime startMonth = today.withDayOfMonth(1).atStartOfDay(); |
| 120 | + LocalDateTime endMonth = today.withDayOfMonth(today.lengthOfMonth()).atTime(23, 59, 59); |
| 121 | + LocalDateTime nowTime = LocalDateTime.now(); |
| 122 | + List<DepartmentRankingTemp> departmentRankingList = studySessionRepository.calculateCurrentDepartmentRanking(startMonth, endMonth, nowTime); |
| 123 | + DepartmentRankingEntryResponse myRanking = null; |
| 124 | + List<DepartmentRankingEntryResponse> topRankings = new ArrayList<>(); |
| 125 | + User user = userRepository.findById(userId).orElseThrow(()->new BusinessException(ExceptionType.USER_NOT_FOUND)); |
| 126 | + for (DepartmentRankingTemp temp : departmentRankingList) { |
| 127 | + DepartmentRankingEntryResponse entry = DepartmentRankingEntryResponse.of(temp); |
| 128 | + topRankings.add(entry); |
| 129 | + |
| 130 | + if(user.getDepartment().equals(temp.getDepartmentName())){ |
| 131 | + myRanking = entry; |
| 132 | + } |
| 133 | + } |
| 134 | + return new DepartmentRankingResponse(topRankings, myRanking); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + /* |
| 139 | + 완료된 학과 랭킹 월간 조회 |
| 140 | + */ |
| 141 | + public DepartmentRankingResponse getCompletedMonthlyDepartmentRanking(Long userId, LocalDateTime monthFirstDay){ |
| 142 | + List<DepartmentRankingTemp> departmentRankingList = departmentRankingRepository.getFinishedDepartmentRanking(monthFirstDay); |
| 143 | + DepartmentRankingEntryResponse myRanking = null; |
| 144 | + List<DepartmentRankingEntryResponse> topRankings = new ArrayList<>(); |
| 145 | + User user = userRepository.findById(userId).orElseThrow(()->new BusinessException(ExceptionType.USER_NOT_FOUND)); |
| 146 | + for (DepartmentRankingTemp temp : departmentRankingList) { |
| 147 | + DepartmentRankingEntryResponse entry = DepartmentRankingEntryResponse.of(temp); |
| 148 | + topRankings.add(entry); |
| 149 | + |
| 150 | + if(user.getDepartment().equals(temp.getDepartmentName())){ |
| 151 | + myRanking = entry; |
| 152 | + } |
| 153 | + } |
| 154 | + return new DepartmentRankingResponse(topRankings, myRanking); |
| 155 | + } |
| 156 | +} |
0 commit comments