|
| 1 | +package com.gpt.geumpumtabackend.rank.service; |
| 2 | + |
| 3 | +import com.gpt.geumpumtabackend.rank.dto.response.PersonalRankingResponse; |
| 4 | +import com.gpt.geumpumtabackend.rank.dto.response.PersonalRankingEntryResponse; |
| 5 | +import com.gpt.geumpumtabackend.rank.dto.UserRankingTemp; |
| 6 | +import com.gpt.geumpumtabackend.rank.repository.UserRankingRepository; |
| 7 | +import com.gpt.geumpumtabackend.study.repository.StudySessionRepository; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import org.springframework.stereotype.Service; |
| 10 | + |
| 11 | +import java.time.DayOfWeek; |
| 12 | +import java.time.LocalDate; |
| 13 | +import java.time.LocalDateTime; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +@Service |
| 20 | +@RequiredArgsConstructor |
| 21 | +public class PersonalRankService { |
| 22 | + |
| 23 | + private final UserRankingRepository userRankingRepository; |
| 24 | + private final StudySessionRepository studySessionRepository; |
| 25 | + /* |
| 26 | + 현재 진행 중인 세션의 일간 랭킹 조회 |
| 27 | + */ |
| 28 | + public PersonalRankingResponse getCurrentDaily(Long userId) { |
| 29 | + LocalDateTime startToday = LocalDate.now().atStartOfDay(); |
| 30 | + LocalDateTime endToday = LocalDate.now().atTime(23, 59, 59); |
| 31 | + LocalDateTime nowTime = LocalDateTime.now(); |
| 32 | + List<UserRankingTemp> userRankingTempList = studySessionRepository.calculateCurrentPeriodRanking(startToday, endToday, nowTime); |
| 33 | + PersonalRankingEntryResponse myRanking = null; |
| 34 | + List<PersonalRankingEntryResponse> topRankings = new ArrayList<>(); |
| 35 | + for (UserRankingTemp temp : userRankingTempList) { |
| 36 | + PersonalRankingEntryResponse entry = PersonalRankingEntryResponse.of(temp); |
| 37 | + topRankings.add(entry); |
| 38 | + |
| 39 | + if(temp.getUserId().equals(userId)){ |
| 40 | + myRanking = entry; |
| 41 | + } |
| 42 | + } |
| 43 | + return new PersonalRankingResponse(topRankings, myRanking); |
| 44 | + } |
| 45 | + |
| 46 | + /* |
| 47 | + 완료된 일간 랭킹 조회 |
| 48 | + */ |
| 49 | + public PersonalRankingResponse getCompletedDaily(Long userId, LocalDateTime day) { |
| 50 | + List<UserRankingTemp> userRankingTempList = userRankingRepository.getFinishedPersonalRanking(day); |
| 51 | + PersonalRankingEntryResponse myRanking = null; |
| 52 | + List<PersonalRankingEntryResponse> topRankings = new ArrayList<>(); |
| 53 | + for (UserRankingTemp temp : userRankingTempList) { |
| 54 | + PersonalRankingEntryResponse entry = PersonalRankingEntryResponse.of(temp); |
| 55 | + topRankings.add(entry); |
| 56 | + |
| 57 | + if(temp.getUserId().equals(userId)){ |
| 58 | + myRanking = entry; |
| 59 | + } |
| 60 | + } |
| 61 | + return new PersonalRankingResponse(topRankings, myRanking); |
| 62 | + } |
| 63 | + |
| 64 | + /* |
| 65 | + 현재 진행 중인 세션의 주간 랭킹 조회 |
| 66 | + */ |
| 67 | + public PersonalRankingResponse getCurrentWeekly(Long userId) { |
| 68 | + LocalDate today = LocalDate.now(); |
| 69 | + LocalDateTime weekStart = today.with(DayOfWeek.MONDAY).atStartOfDay(); |
| 70 | + LocalDateTime weekEnd = today.with(DayOfWeek.SUNDAY).atTime(23, 59, 59); |
| 71 | + LocalDateTime nowTime = LocalDateTime.now(); |
| 72 | + List<UserRankingTemp> userRankingTempList = studySessionRepository.calculateCurrentPeriodRanking(weekStart, weekEnd, nowTime); |
| 73 | + PersonalRankingEntryResponse myRanking = null; |
| 74 | + List<PersonalRankingEntryResponse> topRankings = new ArrayList<>(); |
| 75 | + for (UserRankingTemp temp : userRankingTempList) { |
| 76 | + PersonalRankingEntryResponse entry = PersonalRankingEntryResponse.of(temp); |
| 77 | + topRankings.add(entry); |
| 78 | + if(temp.getUserId().equals(userId)){ |
| 79 | + myRanking = entry; |
| 80 | + } |
| 81 | + } |
| 82 | + return new PersonalRankingResponse(topRankings, myRanking); |
| 83 | + } |
| 84 | + |
| 85 | + /* |
| 86 | + 완료된 주간 랭킹 조회 |
| 87 | + */ |
| 88 | + |
| 89 | + public PersonalRankingResponse getCompletedWeekly(Long userId, LocalDateTime weekFirstDay) { |
| 90 | + List<UserRankingTemp> userRankingTempList = userRankingRepository.getFinishedPersonalRanking(weekFirstDay); |
| 91 | + PersonalRankingEntryResponse myRanking = null; |
| 92 | + List<PersonalRankingEntryResponse> topRankings = new ArrayList<>(); |
| 93 | + for (UserRankingTemp temp : userRankingTempList) { |
| 94 | + PersonalRankingEntryResponse entry = PersonalRankingEntryResponse.of(temp); |
| 95 | + topRankings.add(entry); |
| 96 | + |
| 97 | + if(temp.getUserId().equals(userId)){ |
| 98 | + myRanking = entry; |
| 99 | + } |
| 100 | + } |
| 101 | + return new PersonalRankingResponse(topRankings, myRanking); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + 현재 진행 중인 세션의 월간 랭킹 조회 |
| 106 | + */ |
| 107 | + public PersonalRankingResponse getCurrentMonthly(Long userId) { |
| 108 | + LocalDate today = LocalDate.now(); |
| 109 | + LocalDateTime startMonth = today.withDayOfMonth(1).atStartOfDay(); |
| 110 | + LocalDateTime endMonth = today.withDayOfMonth(today.lengthOfMonth()).atTime(23, 59, 59); |
| 111 | + LocalDateTime nowTime = LocalDateTime.now(); |
| 112 | + List<UserRankingTemp> userRankingTempList = studySessionRepository.calculateCurrentPeriodRanking(startMonth, endMonth, nowTime); |
| 113 | + PersonalRankingEntryResponse myRanking = null; |
| 114 | + List<PersonalRankingEntryResponse> topRankings = new ArrayList<>(); |
| 115 | + for (UserRankingTemp temp : userRankingTempList) { |
| 116 | + PersonalRankingEntryResponse entry = PersonalRankingEntryResponse.of(temp); |
| 117 | + topRankings.add(entry); |
| 118 | + |
| 119 | + if(temp.getUserId().equals(userId)){ |
| 120 | + myRanking = entry; |
| 121 | + } |
| 122 | + } |
| 123 | + return new PersonalRankingResponse(topRankings, myRanking); |
| 124 | + } |
| 125 | + /* |
| 126 | + 완료된 월간 랭킹 조회 |
| 127 | + */ |
| 128 | + public PersonalRankingResponse getCompletedMonthly(Long userId, LocalDateTime monthFirstDay) { |
| 129 | + List<UserRankingTemp> userRankingTempList = userRankingRepository.getFinishedPersonalRanking(monthFirstDay); |
| 130 | + PersonalRankingEntryResponse myRanking = null; |
| 131 | + List<PersonalRankingEntryResponse> topRankings = new ArrayList<>(); |
| 132 | + for (UserRankingTemp temp : userRankingTempList) { |
| 133 | + PersonalRankingEntryResponse entry = PersonalRankingEntryResponse.of(temp); |
| 134 | + topRankings.add(entry); |
| 135 | + |
| 136 | + if(temp.getUserId().equals(userId)){ |
| 137 | + myRanking = entry; |
| 138 | + } |
| 139 | + } |
| 140 | + return new PersonalRankingResponse(topRankings, myRanking); |
| 141 | + } |
| 142 | +} |
0 commit comments