diff --git a/src/main/java/com/softeer/backend/fo_domain/draw/domain/DrawParticipationInfo.java b/src/main/java/com/softeer/backend/fo_domain/draw/domain/DrawParticipationInfo.java index 9dfa8136..40f566ac 100644 --- a/src/main/java/com/softeer/backend/fo_domain/draw/domain/DrawParticipationInfo.java +++ b/src/main/java/com/softeer/backend/fo_domain/draw/domain/DrawParticipationInfo.java @@ -4,13 +4,15 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; + +import java.time.LocalDateTime; @Getter @Entity +@Builder @NoArgsConstructor +@AllArgsConstructor @Table(name = "draw_participation_info") public class DrawParticipationInfo { @Id @@ -23,14 +25,9 @@ public class DrawParticipationInfo { @Column(name = "draw_losing_count") private Integer drawLosingCount; - @Column(name = "draw_participation_count") - private Integer drawParticipationCount; + @Column(name = "draw_attendance_count") + private Integer drawAttendanceCount; - @Builder - public DrawParticipationInfo(Integer userId, Integer drawWinningCount, Integer drawLosingCount, Integer drawParticipationCount) { - this.userId = userId; - this.drawWinningCount = drawWinningCount; - this.drawLosingCount = drawLosingCount; - this.drawParticipationCount = drawParticipationCount; - } + @Column(name = "last_attendance") + private LocalDateTime lastAttendance; } diff --git a/src/main/java/com/softeer/backend/fo_domain/draw/repository/DrawParticipationInfoRepository.java b/src/main/java/com/softeer/backend/fo_domain/draw/repository/DrawParticipationInfoRepository.java index 736e7b40..da2e97a3 100644 --- a/src/main/java/com/softeer/backend/fo_domain/draw/repository/DrawParticipationInfoRepository.java +++ b/src/main/java/com/softeer/backend/fo_domain/draw/repository/DrawParticipationInfoRepository.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.Optional; @Repository @@ -22,4 +23,19 @@ public interface DrawParticipationInfoRepository extends JpaRepository new ShareInfoException(ErrorStatus._NOT_FOUND)); - int drawParticipationCount = drawParticipationInfo.getDrawParticipationCount(); + int drawAttendanceCount = handleAttendanceCount(userId, drawParticipationInfo); int invitedNum = shareInfo.getInvitedNum(); int remainDrawCount = shareInfo.getRemainDrawCount(); - if (drawParticipationCount == 7) { + System.out.println("Draw Attendance = " + drawAttendanceCount); + + if (drawAttendanceCount >= 7) { // 7일 연속 출석자라면 - return drawResponseGenerateUtil.generateMainFullAttendResponse(invitedNum, remainDrawCount, drawParticipationCount); + return drawResponseGenerateUtil.generateMainFullAttendResponse(invitedNum, remainDrawCount, drawAttendanceCount % 8); } else { // 연속 출석자가 아니라면 - return drawResponseGenerateUtil.generateMainNotAttendResponse(invitedNum, remainDrawCount, drawParticipationCount); + return drawResponseGenerateUtil.generateMainNotAttendResponse(invitedNum, remainDrawCount, drawAttendanceCount); + } + } + + /** + * 연속 출석인지 판단 + * 1. 연속 출석이면 연속 출석일수 1 증가하여 DB에 업데이트 + * 2. 연속 출석이 아니면 DB에 연속 출석일수 1로 초기화 + * 3. 현재 출석시각을 마지막 출석시각으로 DB에 업데이트 + * + * @param userId 사용자 아이디 + * @param drawParticipationInfo 참여 정보 + * @return 연속출석 일수 반환 + */ + private int handleAttendanceCount(Integer userId, DrawParticipationInfo drawParticipationInfo) { + LocalDateTime lastAttendance = drawParticipationInfo.getLastAttendance(); + + // 한 번도 접속한 적이 없는 사람이라면 + if (lastAttendance == null) { + // 연속출석일수 1로 초기화 + drawParticipationInfoRepository.setAttendanceCountToOne(userId); + + // lastAttendance를 현재 시각으로 설정 + drawParticipationInfoRepository.setLastAttendance(userId, LocalDateTime.now()); + + return 1; + } + + // 마지막 접속 시간이 오늘이라면 false 반환 + if (isLastAttendanceToday(lastAttendance)) { + // lastAttendance를 현재 시각으로 설정 + drawParticipationInfoRepository.setLastAttendance(userId, LocalDateTime.now()); + + return drawParticipationInfo.getDrawAttendanceCount(); + } + + if (isContinuousAttendance(lastAttendance)) { + // 연속 출석이라면 연속출석일수 1 증가 + drawParticipationInfoRepository.increaseAttendanceCount(userId); + + // lastAttendance를 현재 시각으로 설정 + drawParticipationInfoRepository.setLastAttendance(userId, LocalDateTime.now()); + return drawParticipationInfo.getDrawAttendanceCount() + 1; + } else { + // 연속출석이 아니라면 연속출석일수 1로 초기화 + drawParticipationInfoRepository.setAttendanceCountToOne(userId); + + // lastAttendance를 현재 시각으로 설정 + drawParticipationInfoRepository.setLastAttendance(userId, LocalDateTime.now()); + return 1; } } + /** + * 연속 출석인지 판단 + * + * @param lastAttendance 마지막 출석 날짜 + * @return 연속 출석이면 true, 연속출석이 아니면 false 반환 + */ + private boolean isContinuousAttendance(LocalDateTime lastAttendance) { + LocalDateTime now = LocalDateTime.now(); + LocalDateTime startDateTime = lastAttendance.plusDays(1).with(LocalTime.MIDNIGHT); // 마지막 접속일자의 다음날 자정 + LocalDateTime endDateTime = lastAttendance.plusDays(2).with(LocalTime.MIDNIGHT); // 마지막 접속일자의 2일 후 자정 + + return (now.isAfter(startDateTime) && now.isBefore(endDateTime)); + } + + /** + * 마지막 출석 시간이 오늘인지 판단 + * + * @param lastAttendance 마지막 출석 날짜 + * @return 마지막 출석 시간이 오늘이면 true, 아니면 false 반환 + */ + private boolean isLastAttendanceToday(LocalDateTime lastAttendance) { + LocalDateTime now = LocalDateTime.now(); + LocalDateTime startDateTime = lastAttendance.with(LocalTime.MIDNIGHT); + LocalDateTime endDateTime = lastAttendance.plusDays(1).with(LocalTime.MIDNIGHT); + + return (now.isAfter(startDateTime) && now.isBefore(endDateTime)); + } + /** * 추첨 이벤트 당첨 로직 작성 * diff --git a/src/main/java/com/softeer/backend/fo_domain/user/service/LoginService.java b/src/main/java/com/softeer/backend/fo_domain/user/service/LoginService.java index 34d2365a..61ce3552 100644 --- a/src/main/java/com/softeer/backend/fo_domain/user/service/LoginService.java +++ b/src/main/java/com/softeer/backend/fo_domain/user/service/LoginService.java @@ -137,7 +137,7 @@ private void createDrawParticipationInfo(Integer userId) { .userId(userId) .drawWinningCount(0) .drawLosingCount(0) - .drawParticipationCount(1) + .drawAttendanceCount(1) .build(); drawParticipationInfoRepository.save(drawParticipationInfo);