-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathRankingFacade.java
More file actions
173 lines (140 loc) · 6.74 KB
/
RankingFacade.java
File metadata and controls
173 lines (140 loc) · 6.74 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.loopers.application.ranking;
import com.loopers.domain.product.Product;
import com.loopers.domain.product.ProductRepository;
import com.loopers.infrastructure.ranking.ProductRankMonthly;
import com.loopers.infrastructure.ranking.ProductRankMonthlyRepository;
import com.loopers.infrastructure.ranking.ProductRankWeekly;
import com.loopers.infrastructure.ranking.ProductRankWeeklyRepository;
import com.loopers.infrastructure.ranking.RankingEntry;
import com.loopers.infrastructure.ranking.RankingRedisService;
import com.loopers.interfaces.api.ranking.RankingDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
@RequiredArgsConstructor
public class RankingFacade {
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
private final RankingRedisService rankingRedisService;
private final ProductRepository productRepository;
private final ProductRankWeeklyRepository productRankWeeklyRepository;
private final ProductRankMonthlyRepository productRankMonthlyRepository;
public RankingDto.RankingListResponse getRankings(String period, String dateStr, int page, int size) {
return switch (period.toLowerCase()) {
case "weekly" -> getWeeklyRankings(dateStr, page, size);
case "monthly" -> getMonthlyRankings(dateStr, page, size);
default -> getDailyRankings(dateStr, page, size);
};
}
private RankingDto.RankingListResponse getDailyRankings(String dateStr, int page, int size) {
LocalDate date = parseDate(dateStr);
int offset = page * size;
List<RankingEntry> entries = rankingRedisService.getTopProducts(date, offset, size);
if (entries.isEmpty()) {
return new RankingDto.RankingListResponse(List.of(), page, size, 0);
}
long totalCount = rankingRedisService.getTotalCount(date);
List<Long> productIds = entries.stream()
.map(RankingEntry::productId)
.toList();
Map<Long, Product> productMap = productRepository.findAllByIdIn(productIds).stream()
.collect(Collectors.toMap(Product::getId, p -> p));
List<RankingDto.RankingResponse> rankings = new ArrayList<>();
int rank = offset + 1;
for (RankingEntry entry : entries) {
Product product = productMap.get(entry.productId());
if (product != null) {
rankings.add(new RankingDto.RankingResponse(
rank++,
product.getId(),
product.getName(),
product.getPrice(),
entry.score()
));
}
}
return new RankingDto.RankingListResponse(rankings, page, size, totalCount);
}
private RankingDto.RankingListResponse getWeeklyRankings(String dateStr, int page, int size) {
LocalDate date = parseDate(dateStr);
LocalDate weekStart = date.with(DayOfWeek.MONDAY);
List<ProductRankWeekly> weeklyRanks = productRankWeeklyRepository
.findByPeriodStartOrderByRankingAsc(weekStart);
if (weeklyRanks.isEmpty()) {
return new RankingDto.RankingListResponse(List.of(), page, size, 0);
}
int offset = page * size;
int toIndex = Math.min(offset + size, weeklyRanks.size());
if (offset >= weeklyRanks.size()) {
return new RankingDto.RankingListResponse(List.of(), page, size, weeklyRanks.size());
}
List<ProductRankWeekly> pagedRanks = weeklyRanks.subList(offset, toIndex);
List<Long> productIds = pagedRanks.stream()
.map(ProductRankWeekly::getProductId)
.toList();
Map<Long, Product> productMap = productRepository.findAllByIdIn(productIds).stream()
.collect(Collectors.toMap(Product::getId, p -> p));
List<RankingDto.RankingResponse> rankings = new ArrayList<>();
for (ProductRankWeekly rank : pagedRanks) {
Product product = productMap.get(rank.getProductId());
if (product != null) {
rankings.add(new RankingDto.RankingResponse(
rank.getRanking(),
product.getId(),
product.getName(),
product.getPrice(),
rank.getScore().doubleValue()
));
}
}
return new RankingDto.RankingListResponse(rankings, page, size, weeklyRanks.size());
}
private RankingDto.RankingListResponse getMonthlyRankings(String dateStr, int page, int size) {
LocalDate date = parseDate(dateStr);
YearMonth yearMonth = YearMonth.from(date);
LocalDate monthStart = yearMonth.atDay(1);
List<ProductRankMonthly> monthlyRanks = productRankMonthlyRepository
.findByPeriodStartOrderByRankingAsc(monthStart);
if (monthlyRanks.isEmpty()) {
return new RankingDto.RankingListResponse(List.of(), page, size, 0);
}
int offset = page * size;
int toIndex = Math.min(offset + size, monthlyRanks.size());
if (offset >= monthlyRanks.size()) {
return new RankingDto.RankingListResponse(List.of(), page, size, monthlyRanks.size());
}
List<ProductRankMonthly> pagedRanks = monthlyRanks.subList(offset, toIndex);
List<Long> productIds = pagedRanks.stream()
.map(ProductRankMonthly::getProductId)
.toList();
Map<Long, Product> productMap = productRepository.findAllByIdIn(productIds).stream()
.collect(Collectors.toMap(Product::getId, p -> p));
List<RankingDto.RankingResponse> rankings = new ArrayList<>();
for (ProductRankMonthly rank : pagedRanks) {
Product product = productMap.get(rank.getProductId());
if (product != null) {
rankings.add(new RankingDto.RankingResponse(
rank.getRanking(),
product.getId(),
product.getName(),
product.getPrice(),
rank.getScore().doubleValue()
));
}
}
return new RankingDto.RankingListResponse(rankings, page, size, monthlyRanks.size());
}
private LocalDate parseDate(String dateStr) {
if (dateStr == null || dateStr.isBlank()) {
return LocalDate.now();
}
return LocalDate.parse(dateStr, DATE_FORMATTER);
}
}