-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathMonthlyRankingService.java
More file actions
90 lines (74 loc) · 2.79 KB
/
MonthlyRankingService.java
File metadata and controls
90 lines (74 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.loopers.application.ranking;
import com.loopers.domain.ranking.MonthlyRankingMv;
import com.loopers.domain.ranking.MonthlyRankingRepository;
import com.loopers.domain.ranking.Period;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
@Component
@RequiredArgsConstructor
public class MonthlyRankingService {
private static final DateTimeFormatter BASIC = DateTimeFormatter.BASIC_ISO_DATE;
private final MonthlyRankingRepository monthlyRankingRepository;
public RankingV1Dto.ProductRankingPageResponse getMonthlyTop100(String date, Period period, int page, int size) {
int safePage = Math.max(page, 1);
int safeSize = Math.max(size, 1);
LocalDate end = LocalDate.parse(date, BASIC);
String endDate = end.format(BASIC);
String startDate = end.minusDays(30).format(BASIC);
System.out.println("startDate = " + startDate);
long totalElements = monthlyRankingRepository.countMonthly(startDate, endDate);
if (totalElements == 0) {
return new RankingV1Dto.ProductRankingPageResponse(
date,
period,
startDate,
endDate,
safePage,
safeSize,
0,
0,
List.of()
);
}
int totalPages = (int) Math.ceil((double) totalElements / safeSize);
long offset = (long) (safePage - 1) * safeSize;
if (offset >= totalElements) {
return new RankingV1Dto.ProductRankingPageResponse(
date,
period,
startDate,
endDate,
safePage,
safeSize,
totalElements,
totalPages,
List.of()
);
}
List<MonthlyRankingMv> rows = monthlyRankingRepository.getMonthlyTop100(endDate, startDate, safePage, safeSize);
List<RankingV1Dto.ProductRankingResponse> items = new ArrayList<>(rows.size());
long rank = offset + 1;
for (MonthlyRankingMv row : rows) {
items.add(new RankingV1Dto.ProductRankingResponse(
rank++,
row.getProductId(),
row.getScore().doubleValue()
));
}
return new RankingV1Dto.ProductRankingPageResponse(
date,
period,
startDate,
endDate,
safePage,
safeSize,
totalElements,
totalPages,
items
);
}
}