@@ -16,63 +16,77 @@ public class NotificationScheduler {
1616 private final SubscribeRepository subscribeRepository ;
1717 private final NotificationService notificationService ;
1818
19- @ Scheduled (cron = "0 0 9 * * *" ) // 매일 오후 6시 (한국시간 기준)
19+ @ Scheduled (cron = "0 0 9 * * *" ) // 매일 오전 9시
2020 public void checkNotifications () {
2121 System .out .println ("Scheduler 실행 시작" );
2222
2323 try {
2424 List <Subscribe > subscribeList = subscribeRepository .findAllByUserId (1 );
25- // System.out.println("구독 개수: " + subscribeList.size());
26-
2725 LocalDate today = LocalDate .now ();
2826 int totalSpending = 0 ;
2927
3028 for (Subscribe s : subscribeList ) {
31- if (s == null ) {
29+ if (s == null || s . getStartDate () == null || s . getSubscribeName () == null || s . getPrice () == null ) {
3230 continue ;
3331 }
3432
35- try {
36- // System.out.println("처리 중인 서비스: " + s.getSubscribeName());
37-
38- if (s .getStartDate () == null || s .getSubscribeName () == null || s .getPrice () == null ) {
39- continue ;
40- }
33+ LocalDate nextPayDate ;
34+ long dDay ;
4135
36+ // "달"인 경우 처리
37+ if ("달" .equals (s .getPeriodUnit ())) {
4238 long monthsPassed = ChronoUnit .MONTHS .between (s .getStartDate (), today );
39+ nextPayDate = s .getStartDate ().plusMonths (monthsPassed * s .getPeriod ());
4340
44- LocalDate nextPayDate = s .getStartDate ().plusMonths (monthsPassed );
4541 if (!nextPayDate .isAfter (today )) {
46- nextPayDate = nextPayDate .plusMonths (1 );
42+ nextPayDate = nextPayDate .plusMonths (s . getPeriod () );
4743 }
4844
49- long dDay = ChronoUnit .DAYS .between (today , nextPayDate );
45+ dDay = ChronoUnit .DAYS .between (today , nextPayDate );
5046
51- // 1. 3일 전 알림
52- if (dDay == 3 ) {
53- notificationService .saveNotification (1 , s .getSubscribeName () + " 구독 결제가 3일 후 예정되어 있어요!" );
47+ // 6개월 이상 구독 유지 알림
48+ long totalMonths = ChronoUnit .MONTHS .between (s .getStartDate (), today );
49+ if (totalMonths >= 6 && totalMonths % 6 == 0 ) {
50+ notificationService .saveNotification (1 , s .getSubscribeName () + " 구독을 " + totalMonths + "개월 동안 이어오셨어요. 잠깐 해지도 고려해보세요!" );
5451 }
5552
56- // 2. 6개월 경과(사용하고 있음을) 알림
57- if (monthsPassed >= 6 && monthsPassed % 6 == 0 ) {
58- notificationService .saveNotification (1 , s .getSubscribeName () + " 구독을 " + monthsPassed + "개월 동안 이어오셨어요. 잠깐 해지도 고려해보세요!" );
53+ }
54+ // "년"인 경우 처리
55+ else if ("년" .equals (s .getPeriodUnit ())) {
56+ long yearsPassed = ChronoUnit .YEARS .between (s .getStartDate (), today );
57+ nextPayDate = s .getStartDate ().plusYears (yearsPassed * s .getPeriod ());
58+
59+ if (!nextPayDate .isAfter (today )) {
60+ nextPayDate = nextPayDate .plusYears (s .getPeriod ());
5961 }
6062
61- // 3. 월 지출 누적합 계산
62- if (nextPayDate .getMonthValue () == today .getMonthValue () && nextPayDate .getYear () == today .getYear ()) {
63- totalSpending += s .getPrice ();
63+ dDay = ChronoUnit .DAYS .between (today , nextPayDate );
64+
65+ // 6개월 이상 구독 유지 알림
66+ long totalMonths = ChronoUnit .MONTHS .between (s .getStartDate (), today );
67+ if (totalMonths >= 6 && totalMonths % 6 == 0 ) {
68+ notificationService .saveNotification (1 , s .getSubscribeName () + " 구독을 " + totalMonths + "개월 동안 이어오셨어요. 잠깐 해지도 고려해보세요!" );
6469 }
6570
66- } catch (Exception ex ) {
67- // System.err.println("반복문 내부 처리 중 에러: " + ex.getMessage());
68- ex .printStackTrace ();
71+ } else {
72+ // 잘못된 periodUnit 값일 경우 skip
73+ continue ;
74+ }
75+
76+ // 1. 디데이 3일 전 알림
77+ if (dDay == 3 ) {
78+ notificationService .saveNotification (1 , s .getSubscribeName () + " 구독 결제가 3일 후 예정되어 있어요!" );
79+ }
80+
81+ // 2. 이번 달에 결제 예정이면 지출 누적합에 추가
82+ if (nextPayDate .getMonthValue () == today .getMonthValue () && nextPayDate .getYear () == today .getYear ()) {
83+ totalSpending += s .getPrice ();
6984 }
7085 }
7186
72- // 3-2. 월 지출 3만원 초과
87+ // 3. 월간 지출 3만 원 초과 알림
7388 if (totalSpending > 30000 ) {
7489 notificationService .saveNotification (1 , "이번 달 구독 지출이 총 " + totalSpending + "원이에요. 구독하고 있는 서비스들을 점검해 보세요!" );
75- // System.out.println("지출 초과 알림: " + totalSpending + "원");
7690 }
7791
7892 } catch (Exception e ) {
@@ -82,4 +96,5 @@ public void checkNotifications() {
8296
8397 System .out .println ("Scheduler 실행 종료" );
8498 }
99+
85100}
0 commit comments