diff --git a/src/main/java/igoMoney/BE/domain/Challenge.java b/src/main/java/igoMoney/BE/domain/Challenge.java index 425ba4b..baf2fdf 100644 --- a/src/main/java/igoMoney/BE/domain/Challenge.java +++ b/src/main/java/igoMoney/BE/domain/Challenge.java @@ -35,7 +35,7 @@ public class Challenge extends BaseEntity { private Integer term=7; // days private LocalDate endDate; @Builder.Default - private String status="notStarted"; // notStarted/inProgress/cancel/done + private String status="notStarted"; // notStarted/recruited/inProgress/cancel/done/unmatched @Builder.Default @OneToMany(mappedBy = "challenge", cascade = CascadeType.ALL) @@ -61,7 +61,9 @@ public static Challenge createChallenge(ChallengeUser... challengeUsers) { return challenge; } - public void startChallenge() { this.startDate = LocalDate.now().plusDays(1); this.status = "inProgress";} + public void startChallenge() { this.status = "inProgress";} + public void setChallengeRecruited() { this.status = "recruited"; } + public void setChallengeUnmatched() { this.status = "unmatched"; } public void stopChallenge() { this.endDate = LocalDate.now(); this.status = "cancel";} public void finishChallenge() { this.endDate = LocalDate.now().plusDays(-1); this.status = "done";} public void setWinner(Long userId) { this.winnerId = userId;} diff --git a/src/main/java/igoMoney/BE/repository/ChallengeRepository.java b/src/main/java/igoMoney/BE/repository/ChallengeRepository.java index c3e680e..5764fc7 100644 --- a/src/main/java/igoMoney/BE/repository/ChallengeRepository.java +++ b/src/main/java/igoMoney/BE/repository/ChallengeRepository.java @@ -2,16 +2,13 @@ import igoMoney.BE.domain.Challenge; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; import java.time.LocalDate; import java.util.List; public interface ChallengeRepository extends JpaRepository, ChallengeCustomRepository { - @Query("select c from Challenge c where c.startDate > :date") - List findAllByStartDateIsAfter(@Param("date") LocalDate date); + List findAllByStartDateAndStatus(LocalDate date, String status); List findAllByStatus(String status); List findAllByEndDateAndStatus(LocalDate date, String status); } diff --git a/src/main/java/igoMoney/BE/service/ChallengeService.java b/src/main/java/igoMoney/BE/service/ChallengeService.java index f3b5416..70e7b97 100644 --- a/src/main/java/igoMoney/BE/service/ChallengeService.java +++ b/src/main/java/igoMoney/BE/service/ChallengeService.java @@ -145,6 +145,7 @@ public void applyChallenge(Long userId, Long challengeId) { throw new CustomException(ErrorCode.EXIST_USER_CHALLENGE); } Challenge findChallenge = getChallengeOrThrow(challengeId); + findChallenge.setChallengeRecruited(); findUser.updateUserStatus(true, challengeId); // 챌린지 참여 정보 업데이트 findUser.addChallengeCount(); @@ -251,16 +252,41 @@ public ChallengeTotalCostResponse getTotalCostPerChallengeUser(Long challengeId, return response; } + @Scheduled(cron="0 0 0 * * *", zone = "Asia/Seoul") // 초 분 시 일 월 요일 + public void startChallenge(){ + List challenges = challengeRepository.findAllByStartDateAndStatus( LocalDate.now(),"recruited"); + for(Challenge c : challenges){ + c.startChallenge(); + } + } + + @Scheduled(cron="0 0 0 * * *", zone = "Asia/Seoul") // 초 분 시 일 월 요일 + public void handleUnmatchedChallenge(){ + List challenges = challengeRepository.findAllByStartDateAndStatus( LocalDate.now(),"notStarted"); + for(Challenge c : challenges){ + c.setChallengeUnmatched(); + User findUser = getUserOrThrow(c.getLeaderId()); + findUser.updateUserStatus(false, null); // 사용자 챌린지 상태 변경 + findUser.resetReportedCount(); + Notification notification = Notification.builder() + .user(findUser) + .title("챌린지 현황") + .message(findUser.getNickname() +"님! 지정하신 챌린지 시작일까지 상대방 매칭이 안 되어서 챌린지가 취소되었어요. 새로운 챌린지를 도전해보세요.") + .build(); + notificationService.makeNotification(notification); + } + } + // 챌린지 완료 (마지막날까지 성공) @Scheduled(cron="0 0 0 * * *", zone = "Asia/Seoul") // 초 분 시 일 월 요일 public void finishChallenge() { - List challengeList = challengeRepository.findAllByStatus("inProgress"); + List challenges = challengeRepository.findAllByStatus("inProgress"); Integer minCost = 99999999; Long winnerId = null; Boolean check =false; Integer tempCost = 99999999; - for (Challenge c : challengeList) { + for (Challenge c : challenges) { if (c.getStartDate().plusDays(7).isEqual(LocalDate.now())){ // Challenge : 챌린지 종료 설정 c.finishChallenge(); @@ -419,12 +445,12 @@ private User getChallengeOtherUser(Long challengeId, Long userId) { // 챌린지 참가자 정보 조회 private List getAllChallengeUser(Long challengeId) { - List userList = new ArrayList<>(); + List users = new ArrayList<>(); List challengeUserList = challengeUserRepository.findAllByChallengeId(challengeId); for (ChallengeUser c : challengeUserList) { - userList.add(c.getUser()); + users.add(c.getUser()); } - return userList; + return users; } // 특정 챌린지에 유저가 참여했는지 조회