-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockerSearchResultQueryService.java
More file actions
243 lines (220 loc) · 8.8 KB
/
Copy pathLockerSearchResultQueryService.java
File metadata and controls
243 lines (220 loc) · 8.8 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.zimdugo.locker.application.search;
import com.zimdugo.locker.application.filter.LockerSearchFilterFactory;
import com.zimdugo.common.i18n.CurrentRequestLanguage;
import com.zimdugo.locker.application.pin.LockerSearchPinAssembler;
import com.zimdugo.locker.application.pin.LockerPinClusterer;
import com.zimdugo.locker.application.result.LockerItemType;
import com.zimdugo.locker.application.result.search.LockerSearchItemResult;
import com.zimdugo.locker.application.result.search.LockerSearchLockerResult;
import com.zimdugo.locker.application.result.search.LockerSearchResult;
import com.zimdugo.locker.application.result.suggest.LockerSuggestItemResult;
import com.zimdugo.locker.domain.favorite.FavoriteLockerReader;
import com.zimdugo.locker.domain.place.LockerPlaceLocker;
import com.zimdugo.locker.domain.place.LockerPlaceLockerReader;
import com.zimdugo.locker.domain.search.LockerSearchFilter;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class LockerSearchResultQueryService {
private final LockerSearchQueryService lockerSearchQueryService;
private final SearchKeywordCountCommandService searchKeywordCountCommandService;
private final LockerPlaceLockerReader lockerPlaceLockerReader;
private final LockerSearchPinAssembler lockerSearchPinAssembler;
private final LockerPinClusterer lockerPinClusterer;
private final FavoriteLockerReader favoriteLockerReader;
private final CurrentRequestLanguage currentRequestLanguage;
public LockerSearchResult getSearchResults(LockerSearchCommand command) {
return getSearchResults(null, command);
}
public LockerSearchResult getSearchResults(Long userId, LockerSearchCommand command) {
List<LockerSearchItemResult> items = getDisplayableItems(
userId,
command.latitude(),
command.longitude(),
command.keyword(),
normalizeFilter(toSearchFilter(command))
);
return LockerSearchResult.of(items);
}
public LockerSearchResult getSearchResultsWithPins(LockerSearchCommand command) {
return getSearchResultsWithPins(null, command);
}
public LockerSearchResult getSearchResultsWithPins(Long userId, LockerSearchCommand command) {
List<LockerSearchItemResult> items = getDisplayableItems(
userId,
command.latitude(),
command.longitude(),
command.keyword(),
normalizeFilter(toSearchFilter(command))
);
if (items.isEmpty()) {
return LockerSearchResult.empty();
}
List<com.zimdugo.locker.application.result.pin.LockerPinItemResult> pins =
lockerSearchPinAssembler.assemble(items);
if (command.zoom() != null) {
pins = lockerPinClusterer.cluster(pins, command.zoom());
}
return LockerSearchResult.of(items, pins);
}
public LockerSearchResult getSearchResults(
double latitude,
double longitude,
String keyword,
LockerSearchFilter filter
) {
return getSearchResults(null, latitude, longitude, keyword, filter);
}
public LockerSearchResult getSearchResults(
Long userId,
double latitude,
double longitude,
String keyword,
LockerSearchFilter filter
) {
List<LockerSearchItemResult> items = getDisplayableItems(
userId,
latitude,
longitude,
keyword,
normalizeFilter(filter)
);
return LockerSearchResult.of(items);
}
private LockerSearchFilter toSearchFilter(LockerSearchCommand command) {
return LockerSearchFilterFactory.create(
command.sizeTypes(),
command.indoorOutdoorTypes(),
command.lockerTypes()
);
}
private LockerSearchFilter normalizeFilter(LockerSearchFilter filter) {
return filter == null ? LockerSearchFilter.empty() : filter;
}
private Map<Long, List<LockerPlaceLocker>> readPlaceLockers(
double latitude,
double longitude,
List<Long> placeIds,
LockerSearchFilter filter
) {
if (placeIds.isEmpty()) {
return Map.of();
}
return lockerPlaceLockerReader.readByPlaceIds(
latitude,
longitude,
placeIds,
filter,
currentRequestLanguage.resolve().languageTag()
);
}
private LockerSearchItemResult toSearchItem(
LockerSuggestItemResult item,
Map<Long, List<LockerPlaceLocker>> placeLockersByPlaceId,
Set<Long> favoriteLockerIds
) {
if (item.type() == LockerItemType.LOCKER) {
return LockerSearchItemResult.locker(item, favoriteLockerIds.contains(item.lockerId()));
}
List<LockerSearchLockerResult> lockers = placeLockersByPlaceId
.getOrDefault(item.placeId(), List.of())
.stream()
.map(locker -> LockerSearchLockerResult.from(
locker,
favoriteLockerIds.contains(locker.lockerId())
))
.toList();
return LockerSearchItemResult.place(item, lockers);
}
private boolean hasDisplayableResult(LockerSearchItemResult item) {
return item.type() == LockerItemType.LOCKER || !item.lockers().isEmpty();
}
private Set<Long> collectTargetLockerIds(
List<LockerSuggestItemResult> suggestItems,
Map<Long, List<LockerPlaceLocker>> placeLockersByPlaceId
) {
Set<Long> directLockerIds = suggestItems.stream()
.filter(item -> item.type() == LockerItemType.LOCKER)
.map(LockerSuggestItemResult::lockerId)
.collect(Collectors.toSet());
Set<Long> placeLockerIds = placeLockersByPlaceId.values().stream()
.flatMap(List::stream)
.map(LockerPlaceLocker::lockerId)
.collect(Collectors.toSet());
directLockerIds.addAll(placeLockerIds);
return directLockerIds;
}
private Set<Long> resolveFavoriteLockerIds(Long userId, Set<Long> lockerIds) {
if (userId == null || lockerIds.isEmpty()) {
return Set.of();
}
return favoriteLockerReader.findFavoriteLockerIds(userId, lockerIds);
}
private void increaseSearchKeywordCount(String keyword) {
try {
searchKeywordCountCommandService.increase(keyword);
} catch (DataAccessException | TransactionException exception) {
log.warn("키워드 집계 저장에 실패해도 검색은 계속 진행합니다. keyword={}", keyword, exception);
}
}
public List<LockerSearchItemResult> getDisplayableSearchItemsForPins(
Long userId,
double latitude,
double longitude,
String keyword,
LockerSearchFilter filter
) {
return getDisplayableItemsWithoutCounting(userId, latitude, longitude, keyword, filter);
}
private List<LockerSearchItemResult> getDisplayableItems(
Long userId,
double latitude,
double longitude,
String keyword,
LockerSearchFilter filter
) {
increaseSearchKeywordCount(keyword);
return getDisplayableItemsWithoutCounting(userId, latitude, longitude, keyword, filter);
}
private List<LockerSearchItemResult> getDisplayableItemsWithoutCounting(
Long userId,
double latitude,
double longitude,
String keyword,
LockerSearchFilter filter
) {
filter = normalizeFilter(filter);
List<LockerSuggestItemResult> suggestItems = lockerSearchQueryService.search(
latitude,
longitude,
keyword,
filter
);
if (suggestItems.isEmpty()) {
return List.of();
}
List<Long> placeIds = suggestItems.stream()
.filter(item -> item.type() == LockerItemType.PLACE)
.map(LockerSuggestItemResult::placeId)
.toList();
Map<Long, List<LockerPlaceLocker>> placeLockersByPlaceId =
readPlaceLockers(latitude, longitude, placeIds, filter);
Set<Long> targetLockerIds = collectTargetLockerIds(suggestItems, placeLockersByPlaceId);
Set<Long> favoriteLockerIds = resolveFavoriteLockerIds(userId, targetLockerIds);
return suggestItems.stream()
.map(item -> toSearchItem(item, placeLockersByPlaceId, favoriteLockerIds))
.filter(this::hasDisplayableResult)
.toList();
}
}