|
| 1 | +package cmf.commitField.global.scheduler; |
| 2 | + |
| 3 | +import cmf.commitField.domain.season.entity.Rank; |
| 4 | +import cmf.commitField.domain.season.entity.Season; |
| 5 | +import cmf.commitField.domain.season.entity.SeasonStatus; |
| 6 | +import cmf.commitField.domain.season.entity.UserSeason; |
| 7 | +import cmf.commitField.domain.season.repository.SeasonRepository; |
| 8 | +import cmf.commitField.domain.season.repository.UserSeasonRepository; |
| 9 | +import cmf.commitField.domain.season.service.SeasonService; |
| 10 | +import cmf.commitField.domain.user.entity.User; |
| 11 | +import cmf.commitField.domain.user.repository.UserRepository; |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import lombok.extern.slf4j.Slf4j; |
| 14 | +import org.springframework.scheduling.annotation.Scheduled; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | + |
| 17 | +import java.time.LocalDate; |
| 18 | +import java.time.LocalDateTime; |
| 19 | +import java.time.Month; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +@Component |
| 23 | +@RequiredArgsConstructor |
| 24 | +@Slf4j |
| 25 | +public class SeasonScheduler { |
| 26 | + |
| 27 | + private final SeasonRepository seasonRepository; |
| 28 | + private final UserSeasonRepository userSeasonRepository; |
| 29 | + private final UserRepository userRepository; |
| 30 | + private final SeasonService seasonService; |
| 31 | + |
| 32 | + // ๋งค์ผ ์์ ๋ง๋ค ์์ฆ ํ์ธ ๋ฐ ์์ฑ |
| 33 | + @Scheduled(cron = "0 0 0 * * *") |
| 34 | + public void checkAndCreateNewSeason() { |
| 35 | + LocalDate today = LocalDate.now(); |
| 36 | + String seasonName = today.getYear() + " " + getSeasonName(today.getMonthValue()); |
| 37 | + |
| 38 | + // ํ์ฌ ํ์ฑ ์์ฆ ์กฐํ |
| 39 | + Season activeSeason = seasonService.getActiveSeason(); |
| 40 | + |
| 41 | + // ํ์ฌ ํ์ฑ ์์ฆ์ด ์๊ฑฐ๋, ํ์ฌ ์์ฆ๊ณผ ๋ค๋ฅด๋ฉด ์๋ก์ด ์์ฆ ์์ฑ |
| 42 | + if (activeSeason == null || !activeSeason.getName().equals(seasonName)) { |
| 43 | + LocalDateTime startDate = getSeasonStartDate(today.getYear(), today.getMonth()); |
| 44 | + LocalDateTime endDate = startDate.plusMonths(3).minusSeconds(1); |
| 45 | + |
| 46 | + //ํ์ฌ ํ์ฑ ์์ฆ ์ข
๋ฃ |
| 47 | + if (activeSeason != null) { |
| 48 | + activeSeason.setStatus(SeasonStatus.INACTIVE); |
| 49 | + seasonRepository.save(activeSeason); |
| 50 | + log.info("โ
๊ธฐ์กด ์์ฆ '{}' ์ข
๋ฃ๋จ", activeSeason.getName()); |
| 51 | + } |
| 52 | + |
| 53 | + Season newSeason = seasonService.createNewSeason(seasonName, startDate, endDate); |
| 54 | + |
| 55 | + // ๋ชจ๋ ์ ์ ์ ๋ญํฌ ์ด๊ธฐํ |
| 56 | + resetUserRanks(newSeason); |
| 57 | + |
| 58 | + System.out.println("์ ์์ฆ ์์ฑ: " + newSeason.getName()); |
| 59 | + } else { |
| 60 | + System.out.println("์ด๋ฏธ ํ์ฑํ๋ ์์ฆ: " + activeSeason.getName()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void resetUserRanks(Season newSeason) { |
| 65 | + List<User> users = userRepository.findAll(); |
| 66 | + for (User user : users) { |
| 67 | + UserSeason userSeason = UserSeason.builder() |
| 68 | + .user(user) |
| 69 | + .season(newSeason) |
| 70 | + .rank(Rank.SEED) // ์ด๊ธฐ ๋ญํฌ ์ค์ (์: SEED) |
| 71 | + .build(); |
| 72 | + |
| 73 | + userSeasonRepository.save(userSeason); |
| 74 | + log.info("๐ ์ ์ '{}'์ ์์ฆ ๋ญํฌ ์ด๊ธฐํ ({} -> ์จ์)", user.getNickname(), newSeason.getName()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // ์์ ๊ธฐ์ค์ผ๋ก ์์ฆ ์ด๋ฆ ์ ํ๊ธฐ (์: 2025 Spring, 2025 Summer) |
| 79 | + private String getSeasonName(int month) { |
| 80 | + if (month >= 3 && month <= 5) { |
| 81 | + return "Spring"; // ๋ด |
| 82 | + } else if (month >= 6 && month <= 8) { |
| 83 | + return "Summer"; // ์ฌ๋ฆ |
| 84 | + } else if (month >= 9 && month <= 11) { |
| 85 | + return "Fall"; // ๊ฐ์ |
| 86 | + } else { |
| 87 | + return "Winter"; // ๊ฒจ์ธ |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // ํด๋น ์์ฆ์ ์์ ๋ ์ง ๋ฐํ |
| 92 | + private LocalDateTime getSeasonStartDate(int year, Month month) { |
| 93 | + int startMonth = switch (month) { |
| 94 | + case MARCH, APRIL, MAY -> 3; // ๋ด |
| 95 | + case JUNE, JULY, AUGUST -> 6; // ์ฌ๋ฆ |
| 96 | + case SEPTEMBER, OCTOBER, NOVEMBER -> 9; // ๊ฐ์ |
| 97 | + default -> 12; // ๊ฒจ์ธ (12์๋ถํฐ ์์) |
| 98 | + }; |
| 99 | + return LocalDateTime.of(year, startMonth, 1, 0, 0); |
| 100 | + } |
| 101 | +} |
0 commit comments