Skip to content

Commit 8fb81da

Browse files
committed
feat: seasonScheduler
1 parent 802e5ec commit 8fb81da

File tree

6 files changed

+89
-36
lines changed

6 files changed

+89
-36
lines changed

โ€Žsrc/main/java/cmf/commitField/domain/mock/user/entity/MockUser.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

โ€Žsrc/main/java/cmf/commitField/domain/mock/user/repository/MockUserRepository.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

โ€Žsrc/main/java/cmf/commitField/domain/season/controller/ApiV1SeasonController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cmf.commitField.domain.season.entity.Season;
44
import cmf.commitField.domain.season.service.SeasonService;
5+
import cmf.commitField.global.scheduler.SeasonScheduler;
56
import lombok.RequiredArgsConstructor;
67
import org.springframework.web.bind.annotation.*;
78

@@ -13,11 +14,9 @@
1314
@RequiredArgsConstructor
1415
public class ApiV1SeasonController {
1516
private final SeasonService seasonService;
17+
private final SeasonScheduler seasonScheduler;
1618

17-
// ์ž…๋ ฅ ์˜ˆ์‹œ
18-
// name: 2025 1๋ถ„๊ธฐ
19-
// start: 2025-01-01T00:00:00Z
20-
// end: 2025-02-28T23:59:59Z
19+
// ์˜ˆ์‹œ๋กœ Season ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ํ•ด๋ณด๊ธฐ
2120
@PostMapping
2221
public Season createSeason() {
2322
String name = "2025 1๋ถ„๊ธฐ";
@@ -31,4 +30,10 @@ public Season createSeason() {
3130
public Season getActiveSeason() {
3231
return seasonService.getActiveSeason();
3332
}
33+
34+
//Season scheduler ํ…Œ์ŠคํŠธ ์ฝ”๋“œ
35+
@GetMapping("/scheduler")
36+
public void testResetSeason() {
37+
seasonScheduler.resetSeason();
38+
}
3439
}

โ€Žsrc/main/java/cmf/commitField/domain/season/entity/UserSeason.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package cmf.commitField.domain.season.entity;
22

3-
import cmf.commitField.domain.mock.user.entity.MockUser;
3+
import cmf.commitField.domain.user.entity.User;
44
import cmf.commitField.global.jpa.BaseEntity;
55
import jakarta.persistence.*;
66
import lombok.*;
7+
import lombok.experimental.SuperBuilder;
78

89
import java.time.LocalDateTime;
910

@@ -12,12 +13,13 @@
1213
@Setter
1314
@NoArgsConstructor
1415
@AllArgsConstructor
16+
@SuperBuilder
1517
@Table(name = "user_season")
1618
public class UserSeason extends BaseEntity {
1719

1820
@ManyToOne
1921
@JoinColumn(name = "user_id")
20-
private MockUser user;
22+
private User user;
2123

2224
@ManyToOne
2325
@JoinColumn(name = "season_id")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package cmf.commitField.domain.season.repository;
22

3-
import cmf.commitField.domain.mock.user.entity.MockUser;
43
import cmf.commitField.domain.season.entity.UserSeason;
4+
import cmf.commitField.domain.user.entity.User;
55
import org.springframework.data.jpa.repository.JpaRepository;
66
import org.springframework.stereotype.Repository;
77

88
import java.util.List;
99

1010
@Repository
1111
public interface UserSeasonRepository extends JpaRepository<UserSeason, Long> {
12-
List<UserSeason> findByUser(MockUser user);
12+
List<UserSeason> findByUser(User user);
1313
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.user.entity.User;
10+
import cmf.commitField.domain.user.repository.UserRepository;
11+
import jakarta.transaction.Transactional;
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.LocalDateTime;
18+
import java.util.List;
19+
20+
@Component
21+
@RequiredArgsConstructor
22+
@Slf4j
23+
public class SeasonScheduler {
24+
25+
private final SeasonRepository seasonRepository;
26+
private final UserSeasonRepository userSeasonRepository;
27+
private final UserRepository userRepository;
28+
29+
@Scheduled(cron = "0 0 0 1 1,4,7,10 *") // 1์›”, 4์›”, 7์›”, 10์›” 1์ผ 00:00:00์— ์‹คํ–‰
30+
@Transactional
31+
public void resetSeason() {
32+
log.info("๐Ÿ•’ ์‹œ์ฆŒ ์ดˆ๊ธฐํ™” ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰");
33+
34+
// ํ˜„์žฌ ํ™œ์„ฑํ™”๋œ ์‹œ์ฆŒ ์ข…๋ฃŒ
35+
Season currentSeason = seasonRepository.findByStatus(SeasonStatus.ACTIVE);
36+
if (currentSeason != null) {
37+
currentSeason.setStatus(SeasonStatus.INACTIVE);
38+
seasonRepository.save(currentSeason);
39+
log.info("โœ… ๊ธฐ์กด ์‹œ์ฆŒ '{}' ์ข…๋ฃŒ๋จ", currentSeason.getName());
40+
}
41+
42+
// ์ƒˆ๋กœ์šด ์‹œ์ฆŒ ์ƒ์„ฑ
43+
LocalDateTime now = LocalDateTime.now();
44+
LocalDateTime start = now.withDayOfMonth(1);
45+
LocalDateTime end = start.plusMonths(3).minusSeconds(1);
46+
47+
Season newSeason = Season.builder()
48+
.name("Season " + start.getYear() + " Q" + ((start.getMonthValue() - 1) / 3 + 1))
49+
.startDate(start)
50+
.endDate(end)
51+
.status(SeasonStatus.ACTIVE)
52+
.build();
53+
54+
seasonRepository.save(newSeason);
55+
log.info("๐ŸŒŸ ์ƒˆ๋กœ์šด ์‹œ์ฆŒ '{}' ์ƒ์„ฑ๋จ (๊ธฐ๊ฐ„: {} ~ {})", newSeason.getName(), start, end);
56+
57+
// ๋ชจ๋“  ์œ ์ €์˜ ๋žญํฌ ์ดˆ๊ธฐํ™”
58+
resetUserRanks(newSeason);
59+
}
60+
61+
private void resetUserRanks(Season newSeason) {
62+
List<User> users = userRepository.findAll();
63+
for (User user : users) {
64+
UserSeason userSeason = UserSeason.builder()
65+
.user(user)
66+
.season(newSeason)
67+
.rank(Rank.SEED) // ์ดˆ๊ธฐ ๋žญํฌ ์„ค์ • (์˜ˆ: SEED)
68+
.build();
69+
70+
userSeasonRepository.save(userSeason);
71+
log.info("๐Ÿ”„ ์œ ์ € '{}'์˜ ์‹œ์ฆŒ ๋žญํฌ ์ดˆ๊ธฐํ™” ({} -> ์”จ์•—)", user.getNickname(), newSeason.getName());
72+
}
73+
}
74+
}

0 commit comments

Comments
ย (0)