Skip to content

Commit 7a7abdd

Browse files
authored
Merge pull request #17 from DevKor-github/dev
fix: 투표 결과 정렬 로직 및 전체 데이터 반환 수정
2 parents 36dc6e5 + be7cd5e commit 7a7abdd

4 files changed

Lines changed: 73 additions & 53 deletions

File tree

src/main/java/com/workingdead/meet/dto/VoteResultDtos.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public record VoteResultRes(
1414

1515
// 순위별 정보
1616
public record RankingRes(
17-
int rank, // 순위 (1, 2, 3)
18-
LocalDate date, // 날짜
19-
String period, // "LUNCH" or "DINNER"
20-
int voteCount, // 투표 인원 수
21-
double priorityScore, // 우선순위 가중치 합계
22-
List<VoterDetailRes> voters // 투표자 상세 (드롭다운용)
23-
) {}
17+
Integer rank, // 순위 (1, 2, 3, null) - null이면 4위 이하
18+
LocalDate date,
19+
String period,
20+
int voteCount,
21+
double priorityScore,
22+
List<VoterDetailRes> voters
23+
) {}
2424

2525
// 투표자 상세 정보
2626
public record VoterDetailRes(

src/main/java/com/workingdead/meet/repository/ParticipantSelectionRepository.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import java.util.List;
66

7+
import org.springframework.data.jpa.repository.Modifying;
8+
import org.springframework.data.jpa.repository.Query;
9+
import org.springframework.data.repository.query.Param;
10+
711
public interface ParticipantSelectionRepository extends JpaRepository<ParticipantSelection, Long> {
812
List<ParticipantSelection> findByVoteId(Long voteId);
913
List<ParticipantSelection> findByVoteIdAndParticipantId(Long voteId, Long participantId);
14+
15+
@Modifying
16+
@Query("DELETE FROM ParticipantSelection ps WHERE ps.participant.id = :participantId")
17+
void deleteByParticipantId(@Param("participantId") Long participantId);
1018
}

src/main/java/com/workingdead/meet/service/ParticipantService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public ParticipantDtos.ParticipantScheduleRes submitSchedule(
149149

150150
// participantRepository -> participantRepo로 수정!
151151
Participant saved = participantRepo.save(participant);
152+
participantRepo.flush();
152153

153154
// 5. 응답 생성
154155
List<ParticipantDtos.SelectionRes> selections = saved.getSelections().stream()

src/main/java/com/workingdead/meet/service/VoteResultService.java

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public VoteResultRes getVoteResult(Long voteId) {
3737
Map<String, List<ParticipantSelection>> groupedSelections = selections.stream()
3838
.collect(Collectors.groupingBy(s -> keyOf(s.getDate(), s.getPeriod())));
3939

40-
// 4. 우선순위 맵 생성 (빠른 조회용)
40+
// 4. 우선순위 맵 생성
4141
Map<String, Map<Long, PriorityPreference>> priorityMap = new HashMap<>();
4242
for (PriorityPreference pref : priorities) {
4343
String key = keyOf(pref.getDate(), pref.getPeriod());
@@ -47,76 +47,87 @@ public VoteResultRes getVoteResult(Long voteId) {
4747

4848
// 5. 각 날짜+시간대별 점수 계산
4949
List<SlotScore> slotScores = new ArrayList<>();
50-
50+
5151
for (Map.Entry<String, List<ParticipantSelection>> entry : groupedSelections.entrySet()) {
52-
String key = entry.getKey();
53-
List<ParticipantSelection> slotSelections = entry.getValue();
54-
55-
if (slotSelections.isEmpty()) continue;
56-
57-
LocalDate date = slotSelections.get(0).getDate();
58-
String period = slotSelections.get(0).getPeriod();
59-
60-
int voteCount = slotSelections.size();
61-
double priorityScore = 0.0;
62-
63-
List<VoterDetailRes> voters = new ArrayList<>();
64-
65-
for (ParticipantSelection selection : slotSelections) {
52+
String key = entry.getKey();
53+
List<ParticipantSelection> slotSelections = entry.getValue();
54+
55+
if (slotSelections.isEmpty()) continue;
56+
57+
LocalDate date = slotSelections.get(0).getDate();
58+
String period = slotSelections.get(0).getPeriod();
59+
60+
int voteCount = slotSelections.size();
61+
double priorityScore = 0.0;
62+
int priorityIndexSum = 0; // 추가!
63+
64+
List<VoterDetailRes> voters = new ArrayList<>();
65+
66+
for (ParticipantSelection selection : slotSelections) {
6667
Long participantId = selection.getParticipant().getId();
6768
String participantName = selection.getParticipant().getDisplayName();
6869

69-
// 우선순위 정보 확인
7070
PriorityPreference pref = priorityMap.getOrDefault(key, Collections.emptyMap())
7171
.get(participantId);
7272

7373
Integer priorityIndex = pref != null ? pref.getPriorityIndex() : null;
7474
Double weight = pref != null ? pref.getWeight() : null;
7575

7676
if (weight != null) {
77-
priorityScore += weight;
77+
priorityScore += weight;
78+
}
79+
80+
// priorityIndex 합계 (없으면 999로 처리해서 뒤로 밀림)
81+
if (priorityIndex != null) {
82+
priorityIndexSum += priorityIndex;
83+
} else {
84+
priorityIndexSum += 999;
7885
}
7986

8087
voters.add(new VoterDetailRes(participantId, participantName, priorityIndex, weight));
81-
}
82-
83-
slotScores.add(new SlotScore(date, period, voteCount, priorityScore, voters));
8488
}
8589

86-
// 6. 정렬: 최다 인원 > 우선순위 가중치 > 빠른 날짜
90+
slotScores.add(new SlotScore(date, period, voteCount, priorityScore, priorityIndexSum, voters));
91+
}
92+
93+
// 6. 정렬: 최다 인원 > priorityIndex 합계 작을수록 상위
8794
slotScores.sort(Comparator
88-
.comparingInt(SlotScore::voteCount).reversed()
89-
.thenComparingDouble(SlotScore::priorityScore).reversed()
90-
.thenComparing(SlotScore::date));
91-
92-
// 7. 상위 3개만 선택 및 순위 부여
95+
.comparingInt(SlotScore::voteCount).reversed() // 1. 인원수 많은 순
96+
.thenComparingInt(SlotScore::priorityIndexSum) // 2. priorityIndex 합계 작은 순
97+
.thenComparing(SlotScore::date)); // 3. 날짜 빠른 순
98+
99+
// 7. 모든 결과에 순위 부여
93100
List<RankingRes> rankings = new ArrayList<>();
94-
List<SlotScore> top3 = slotScores.stream().limit(3).toList();
95101

96-
for (int i = 0; i < top3.size(); i++) {
97-
SlotScore slot = top3.get(i);
98-
rankings.add(new RankingRes(
99-
i + 1, // rank
100-
slot.date(),
101-
slot.period(),
102-
slot.voteCount(),
103-
slot.priorityScore(),
104-
slot.voters()
105-
));
106-
}
102+
for (int i = 0; i < slotScores.size(); i++) {
103+
SlotScore slot = slotScores.get(i);
104+
Integer rank = i < 3 ? (i + 1) : null;
105+
106+
rankings.add(new RankingRes(
107+
rank,
108+
slot.date(),
109+
slot.period(),
110+
slot.voteCount(),
111+
slot.priorityScore(),
112+
slot.voters()
113+
));
114+
}
115+
107116
return new VoteResultRes(vote.getId(), vote.getName(), rankings);
108-
}
117+
}
109118

110119
private String keyOf(LocalDate date, String period) {
111120
return date.toString() + "|" + period;
112121
}
113122

114123
// 내부 DTO (정렬용)
115124
private record SlotScore(
116-
LocalDate date,
117-
String period,
118-
int voteCount,
119-
double priorityScore,
120-
List<VoterDetailRes> voters
121-
) {}
125+
LocalDate date,
126+
String period,
127+
int voteCount,
128+
double priorityScore,
129+
int priorityIndexSum, // 추가!
130+
List<VoterDetailRes> voters
131+
) {}
132+
122133
}

0 commit comments

Comments
 (0)