Skip to content

Commit cff9925

Browse files
authored
Merge pull request #64 from ChillCoders-CapstoneDesign/feature/notification
refactor: 디데이 계산 로직 수정(month, year 분리)
2 parents ba6839b + 2049c4c commit cff9925

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

src/main/java/com/project/submate/subscribe/service/SubscribeService.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public SubscribeListResponseDto subscribeAllList() {
3737
List<Subscribe> subscribeList = subscribeRepository.findAllByUserId(1);
3838

3939
List<SubscribeResponseDto> result = subscribeList.stream()
40-
.map(sub -> SubscribeResponseDto.from(sub, calculateDday(sub.getStartDate())))
40+
.map(sub -> SubscribeResponseDto.from(sub, calculateDday(sub.getStartDate(), sub.getPeriod(), sub.getPeriodUnit())))
4141
.sorted(Comparator.comparingInt(SubscribeResponseDto::getDDay))
4242
.toList();
4343

@@ -53,30 +53,29 @@ public SubscribeListResponseDto subscribeAllList() {
5353
);
5454
}
5555

56-
// 디데이 계산(******Asia/Seoul 시간으로 나오지 않아서 임의로 결과에 -1을 해주었다)
57-
private int calculateDday(LocalDate startDate) {
58-
// LocalDate.plusMonths: 자동으로 월의 마지막 날짜를 고려하여 계산한다.
59-
// LocalDate baseDate = startDate.plusMonths(1); // 기준일: startDate + 1달
60-
61-
if (startDate == null) {
56+
// 디데이 계산
57+
private int calculateDday(LocalDate startDate, int period, String periodUnit) {
58+
if (startDate == null || period <= 0 || periodUnit == null) {
6259
return 0;
6360
}
6461

65-
// 1) 오늘 날짜 (Asia/Seoul 기준)
6662
LocalDate now = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDate();
63+
LocalDate nextBillingDate = startDate;
64+
65+
// 다음 결제일이 오늘보다 이후가 될 때까지 반복
66+
while (!nextBillingDate.isAfter(now)) {
67+
switch (periodUnit) {
68+
case "달":
69+
nextBillingDate = nextBillingDate.plusMonths(period);
70+
break;
71+
case "년":
72+
nextBillingDate = nextBillingDate.plusYears(period);
73+
break;
74+
default:
75+
return 0;
76+
}
77+
}
6778

68-
// 2) startDate부터 지금까지 몇 개월이 지났는지 계산
69-
long monthsSinceStart = ChronoUnit.MONTHS.between(startDate, now);
70-
71-
// 3) 이번 달 결제일
72-
LocalDate billingDateThisMonth = startDate.plusMonths(monthsSinceStart);
73-
74-
// 4) 이번 달 결제일이 아직 남았다면 그대로, 지났으면 다음 달로
75-
LocalDate nextBillingDate = billingDateThisMonth.isAfter(now)
76-
? billingDateThisMonth
77-
: billingDateThisMonth.plusMonths(1);
78-
79-
// 5) D-day 계산 (절대 음수 방지)
8079
int dDay = (int) ChronoUnit.DAYS.between(now, nextBillingDate);
8180
return Math.max(dDay, 0);
8281
}
@@ -93,7 +92,7 @@ public SubscribeResponseDto update(Integer subscribeNo, SubscribeRequestDto subs
9392
subscribe.setUserId(1);
9493
// return subscribeRepository.save(subscribe);
9594
Subscribe updated = subscribeRepository.save(subscribe);
96-
return SubscribeResponseDto.from(updated, calculateDday(updated.getStartDate()));
95+
return SubscribeResponseDto.from(updated, calculateDday(updated.getStartDate(), updated.getPeriod(), updated.getPeriodUnit()));
9796
}
9897

9998
public SubscribeResponseDto save(SubscribeRequestDto subscribeRequestDto) {
@@ -137,7 +136,7 @@ public SubscribeResponseDto save(SubscribeRequestDto subscribeRequestDto) {
137136

138137
// return subscribeRepository.save(subscribe);
139138
Subscribe saved = subscribeRepository.save(subscribe);
140-
int dDay = calculateDday(saved.getStartDate());
139+
int dDay = calculateDday(saved.getStartDate(), saved.getPeriod(), saved.getPeriodUnit());
141140
return SubscribeResponseDto.from(saved, dDay);
142141
}
143142

@@ -148,7 +147,7 @@ public SubscribeCategoryListResponseDto getSubscribeByCategory(Integer categoryN
148147
List<Subscribe> subscribeList = subscribeRepository.findAllByUserIdAndCategory(1, category);
149148

150149
List<SubscribeResponseDto> result = subscribeList.stream()
151-
.map(sub -> SubscribeResponseDto.from(sub, calculateDday(sub.getStartDate())))
150+
.map(sub -> SubscribeResponseDto.from(sub, calculateDday(sub.getStartDate(), sub.getPeriod(), sub.getPeriodUnit())))
152151
.toList();
153152

154153
int totalCount = result.size();

0 commit comments

Comments
 (0)