11package com .ai .lawyer .domain .poll .service ;
22
3+ import com .ai .lawyer .domain .poll .dto .*;
34import com .ai .lawyer .domain .poll .entity .*;
45import com .ai .lawyer .domain .poll .repository .*;
5- import com .ai .lawyer .domain .poll .dto .PollDto ;
66import com .ai .lawyer .domain .member .entity .Member ;
77import com .ai .lawyer .domain .post .dto .PostDto ;
88import com .ai .lawyer .domain .post .entity .Post ;
1616import org .springframework .web .server .ResponseStatusException ;
1717
1818import java .util .*;
19- import com .ai .lawyer .domain .poll .dto .PollCreateDto ;
20- import com .ai .lawyer .domain .poll .dto .PollForPostDto ;
21- import com .ai .lawyer .domain .poll .dto .PollOptionCreateDto ;
22- import com .ai .lawyer .domain .poll .dto .PollStaticsDto ;
23- import com .ai .lawyer .domain .poll .dto .PollOptionDto ;
24- import com .ai .lawyer .domain .poll .dto .PollVoteDto ;
25- import com .ai .lawyer .domain .poll .dto .PollUpdateDto ;
19+
2620import com .ai .lawyer .domain .poll .entity .Poll ;
2721
2822import org .springframework .data .domain .Pageable ;
2923import java .time .LocalDateTime ;
3024
31- import com .ai .lawyer .domain .poll .dto .PollGenderStaticsDto ;
32- import com .ai .lawyer .domain .poll .dto .PollStaticsResponseDto ;
33- import com .ai .lawyer .domain .poll .dto .PollAgeStaticsDto ;
3425import com .ai .lawyer .global .util .AuthUtil ;
3526
3627@ Service
@@ -178,19 +169,12 @@ public PollStaticsResponseDto getPollStatics(Long pollId) {
178169 PollOptions opt = options .get (i );
179170 optionMap .put (opt .getOption (), opt );
180171 }
181- // age 통계 그룹핑
182- List <Object []> optionAgeRaw = pollVoteRepository .getOptionAgeStatics (pollId );
172+ List <PollAgeStaticsDto .AgeGroupCountDto > optionAgeRaw = pollVoteRepository .getOptionAgeStatics (pollId );
183173 java .util .Map <Long , java .util .List <PollAgeStaticsDto .AgeGroupCountDto >> ageGroupMap = new java .util .HashMap <>();
184- for (Object [] arr : optionAgeRaw ) {
185- String option = arr [0 ] != null ? arr [0 ].toString () : null ;
186- PollOptions opt = optionMap .get (option );
174+ for (PollAgeStaticsDto .AgeGroupCountDto dto : optionAgeRaw ) {
175+ PollOptions opt = optionMap .get (dto .getOption ());
187176 if (opt == null ) continue ;
188177 Long pollItemsId = opt .getPollItemsId ();
189- PollAgeStaticsDto .AgeGroupCountDto dto = PollAgeStaticsDto .AgeGroupCountDto .builder ()
190- .option (option )
191- .ageGroup (arr [1 ] != null ? arr [1 ].toString () : null )
192- .voteCount (arr [2 ] != null ? ((Number ) arr [2 ]).longValue () : 0L )
193- .build ();
194178 ageGroupMap .computeIfAbsent (pollItemsId , k -> new java .util .ArrayList <>()).add (dto );
195179 }
196180 java .util .List <PollAgeStaticsDto > optionAgeStatics = new java .util .ArrayList <>();
@@ -202,19 +186,12 @@ public PollStaticsResponseDto getPollStatics(Long pollId) {
202186 .ageGroupCounts (ageGroupMap .getOrDefault (opt .getPollItemsId (), java .util .Collections .emptyList ()))
203187 .build ());
204188 }
205- // gender 통계 그룹핑
206- List <Object []> optionGenderRaw = pollVoteRepository .getOptionGenderStatics (pollId );
189+ List <PollGenderStaticsDto .GenderCountDto > optionGenderRaw = pollVoteRepository .getOptionGenderStatics (pollId );
207190 java .util .Map <Long , java .util .List <PollGenderStaticsDto .GenderCountDto >> genderGroupMap = new java .util .HashMap <>();
208- for (Object [] arr : optionGenderRaw ) {
209- String option = arr [0 ] != null ? arr [0 ].toString () : null ;
210- PollOptions opt = optionMap .get (option );
191+ for (PollGenderStaticsDto .GenderCountDto dto : optionGenderRaw ) {
192+ PollOptions opt = optionMap .get (dto .getOption ());
211193 if (opt == null ) continue ;
212194 Long pollItemsId = opt .getPollItemsId ();
213- PollGenderStaticsDto .GenderCountDto dto = PollGenderStaticsDto .GenderCountDto .builder ()
214- .option (option )
215- .gender (arr [1 ] != null ? arr [1 ].toString () : null )
216- .voteCount (arr [2 ] != null ? ((Number ) arr [2 ]).longValue () : 0L )
217- .build ();
218195 genderGroupMap .computeIfAbsent (pollItemsId , k -> new java .util .ArrayList <>()).add (dto );
219196 }
220197 java .util .List <PollGenderStaticsDto > optionGenderStatics = new java .util .ArrayList <>();
@@ -236,7 +213,7 @@ public PollStaticsResponseDto getPollStatics(Long pollId) {
236213 .build ();
237214 }
238215
239- // 최대 7일 동안 투표 가능 (초기 요구사항)
216+ // 최대 7일 동안 투표 가능
240217 @ Override
241218 public void closePoll (Long pollId ) {
242219 Poll poll = pollRepository .findById (pollId )
@@ -268,7 +245,7 @@ public void deletePoll(Long pollId, Long memberId) {
268245
269246 @ Override
270247 public PollDto getTopPollByStatus (PollDto .PollStatus status , Long memberId ) {
271- List <Object [] > result = pollVoteRepository .findTopPollByStatus (Poll .PollStatus .valueOf (status .name ()));
248+ List <PollTopDto > result = pollVoteRepository .findTopPollByStatus (Poll .PollStatus .valueOf (status .name ()));
272249 if (result .isEmpty ()) {
273250 // 종료된 투표가 없으면 빈 PollDto 반환
274251 return PollDto .builder ()
@@ -282,18 +259,18 @@ public PollDto getTopPollByStatus(PollDto.PollStatus status, Long memberId) {
282259 .totalVoteCount (0L )
283260 .build ();
284261 }
285- Long pollId = ( Long ) result .get (0 )[ 0 ] ;
262+ Long pollId = result .get (0 ). getPollId () ;
286263 return getPoll (pollId , memberId );
287264 }
288265
289266 @ Override
290267 public List <PollDto > getTopNPollsByStatus (PollDto .PollStatus status , int n , Long memberId ) {
291268 Pageable pageable = org .springframework .data .domain .PageRequest .of (0 , n );
292- List <Object [] > result = pollVoteRepository .findTopNPollByStatus (
269+ List <PollTopDto > result = pollVoteRepository .findTopNPollByStatus (
293270 com .ai .lawyer .domain .poll .entity .Poll .PollStatus .valueOf (status .name ()), pageable );
294271 List <PollDto > pollDtos = new java .util .ArrayList <>();
295- for (Object [] row : result ) {
296- Long pollId = ( Long ) row [ 0 ] ;
272+ for (PollTopDto row : result ) {
273+ Long pollId = row . getPollId () ;
297274 pollDtos .add (status == PollDto .PollStatus .CLOSED
298275 ? getPollWithStatistics (pollId , memberId )
299276 : getPoll (pollId , memberId ));
@@ -455,18 +432,8 @@ private PollDto convertToDto(Poll poll, Long memberId, boolean withStatistics) {
455432 }
456433 List <PollStaticsDto > statics = null ;
457434 if (withStatistics && poll .getStatus () == Poll .PollStatus .CLOSED ) {
458- List <Object []> staticsRaw = pollVoteRepository .countStaticsByPollOptionIds (List .of (option .getPollItemsId ()));
459- statics = staticsRaw .stream ()
460- .map (arr -> {
461- String gender = arr [1 ] != null ? arr [1 ].toString () : null ;
462- Integer age = arr [2 ] != null ? ((Number ) arr [2 ]).intValue () : null ;
463- String ageGroup = getAgeGroup (age );
464- return PollStaticsDto .builder ()
465- .gender (gender )
466- .ageGroup (ageGroup )
467- .voteCount ((Long ) arr [3 ])
468- .build ();
469- }).toList ();
435+ List <PollStaticsDto > staticsRaw = pollVoteRepository .countStaticsByPollOptionIds (List .of (option .getPollItemsId ()));
436+ statics = staticsRaw ;
470437 }
471438 optionDtos .add (PollOptionDto .builder ()
472439 .pollItemsId (option .getPollItemsId ())
0 commit comments