@@ -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