1010import grep .neogulcoder .domain .timevote .repository .TimeVoteQueryRepository ;
1111import grep .neogulcoder .domain .timevote .service .TimeVoteMapper ;
1212import grep .neogulcoder .domain .timevote .service .stat .TimeVoteStatService ;
13+ import jakarta .persistence .EntityManager ;
14+ import java .time .LocalDateTime ;
1315import java .util .List ;
1416import lombok .RequiredArgsConstructor ;
1517import lombok .extern .slf4j .Slf4j ;
@@ -27,6 +29,7 @@ public class TimeVoteService {
2729 private final TimeVoteStatService timeVoteStatService ;
2830 private final TimeVoteMapper timeVoteMapper ;
2931 private final TimeVoteValidator timeVoteValidator ;
32+ private final EntityManager em ;
3033
3134 @ Transactional (readOnly = true )
3235 public TimeVoteResponse getMyVotes (Long studyId , Long userId ) {
@@ -42,56 +45,13 @@ public TimeVoteResponse getMyVotes(Long studyId, Long userId) {
4245 }
4346
4447 public TimeVoteResponse submitVotes (TimeVoteCreateRequest request , Long studyId , Long userId ) {
45- long totalStart = System .currentTimeMillis ();
46- log .info ("[TimeVote] 투표 제출 시작 - studyId={}, userId={}, 요청 슬롯 수={}" ,
47- studyId , userId , request .getTimeSlots ().size ());
48-
49- // 투표 전 필요한 모든 유효성 검증 및 도메인 정보 캡슐화
5048 TimeVoteContext context = timeVoteValidator .getSubmitContext (studyId , userId , request .getTimeSlots ());
51-
52- long voteStart = System .currentTimeMillis ();
53- List <TimeVote > votes = timeVoteMapper .toEntities (request , context .period (), context .studyMember ().getId ());
54- timeVoteRepository .saveAll (votes );
55- log .info ("[TimeVote] 투표 저장 완료 - {}건, {}ms 소요" , votes .size (), System .currentTimeMillis () - voteStart );
56-
57- long statStart = System .currentTimeMillis ();
58- timeVoteStatService .incrementStats (context .period ().getPeriodId (), request .getTimeSlots ());
59- log .info ("[TimeVote] 통계 반영 완료 - {}ms 소요" , System .currentTimeMillis () - statStart );
60-
61- List <TimeVote > saved = timeVoteRepository .findByPeriodAndStudyMemberIdAndActivatedTrue (context .period (), context .studyMember ().getId ());
62- log .info ("[TimeVote] 저장된 투표 재조회 완료 - {}건" , saved .size ());
63-
64- log .info ("[TimeVote] 투표 제출 전체 완료 - 총 {}ms 소요" , System .currentTimeMillis () - totalStart );
65-
66- return TimeVoteResponse .from (context .studyMember ().getId (), saved );
49+ return executeVoteSubmission (context , request .getTimeSlots ());
6750 }
6851
6952 public TimeVoteResponse updateVotes (TimeVoteUpdateRequest request , Long studyId , Long userId ) {
70- long totalStart = System .currentTimeMillis ();
71- log .info ("[TimeVote] 투표 수정 시작 - studyId={}, userId={}, 요청 슬롯 수={}" , studyId , userId , request .getTimeSlots ().size ());
72-
73- // 투표 전 필요한 모든 유효성 검증 및 도메인 정보 캡슐화
7453 TimeVoteContext context = timeVoteValidator .getUpdateContext (studyId , userId , request .getTimeSlots ());
75-
76- long deleteStart = System .currentTimeMillis ();
77- timeVoteRepository .deactivateByPeriodAndStudyMember (context .period (), context .studyMember ().getId ());
78- log .info ("[TimeVote] 기존 투표 soft delete 완료 - {}ms 소요" , System .currentTimeMillis () - deleteStart );
79-
80- long saveStart = System .currentTimeMillis ();
81- List <TimeVote > newVotes = timeVoteMapper .toEntities (request , context .period (), context .studyMember ().getId ());
82- timeVoteRepository .saveAll (newVotes );
83- log .info ("[TimeVote] 새로운 투표 저장 완료 - {}건, {}ms 소요" , newVotes .size (), System .currentTimeMillis () - saveStart );
84-
85- long statStart = System .currentTimeMillis ();
86- timeVoteStatService .recalculateStats (context .period ().getPeriodId ());
87- log .info ("[TimeVote] 통계 재계산 완료 - {}ms 소요" , System .currentTimeMillis () - statStart );
88-
89- List <TimeVote > saved = timeVoteRepository .findByPeriodAndStudyMemberIdAndActivatedTrue (context .period (), context .studyMember ().getId ());
90- log .info ("[TimeVote] 저장된 투표 재조회 완료 - {}건" , saved .size ());
91-
92- log .info ("[TimeVote] 투표 수정 전체 완료 - 총 {}ms 소요" , System .currentTimeMillis () - totalStart );
93-
94- return TimeVoteResponse .from (context .studyMember ().getId (), saved );
54+ return executeVoteSubmission (context , request .getTimeSlots ());
9555 }
9656
9757 public void deleteAllVotes (Long studyId , Long userId ) {
@@ -122,4 +82,27 @@ public List<TimeVoteSubmissionStatusResponse> getSubmissionStatusList(Long study
12282 log .info ("[TimeVote] 제출 현황 조회 완료 - 총 {}명" , result .size ());
12383 return result ;
12484 }
85+
86+ private TimeVoteResponse executeVoteSubmission (TimeVoteContext context , List <LocalDateTime > timeSlots ) {
87+ long totalStart = System .currentTimeMillis ();
88+
89+ timeVoteRepository .deactivateByPeriodAndStudyMember (context .period (), context .studyMember ().getId ());
90+ em .flush ();
91+ log .info ("[TimeVote] 기존 투표 soft delete 완료" );
92+
93+ List <TimeVote > newVotes = timeVoteMapper .toEntities (timeSlots , context .period (), context .studyMember ().getId ());
94+ timeVoteRepository .saveAll (newVotes );
95+ log .info ("[TimeVote] 새 투표 저장 완료 - {}건" , newVotes .size ());
96+
97+ timeVoteStatService .recalculateStats (context .period ().getPeriodId ());
98+ log .info ("[TimeVote] 통계 재계산 완료" );
99+
100+ List <TimeVote > saved = timeVoteRepository .findByPeriodAndStudyMemberIdAndActivatedTrue (
101+ context .period (), context .studyMember ().getId ());
102+
103+ log .info ("[TimeVote] 저장된 투표 재조회 완료 - {}건" , saved .size ());
104+ log .info ("[TimeVote] 전체 완료 - 총 {}ms" , System .currentTimeMillis () - totalStart );
105+
106+ return TimeVoteResponse .from (context .studyMember ().getId (), saved );
107+ }
125108}
0 commit comments