|
24 | 24 | import org.springframework.data.domain.PageRequest; |
25 | 25 | import org.springframework.stereotype.Service; |
26 | 26 | import org.springframework.transaction.annotation.Transactional; |
27 | | - |
28 | | -import java.time.LocalDate; |
29 | 27 | import java.util.*; |
30 | 28 | import java.util.stream.Collectors; |
31 | 29 |
|
@@ -102,7 +100,7 @@ public List<StoreMapResponseDTO> getStores(MapRequestDTO viewport, Long memberId |
102 | 100 | } |
103 | 101 |
|
104 | 102 | // 2) 해당 학생의 활성 UserPaper 조회 (paper, store, admin fetch join 포함) |
105 | | - final List<UserPaper> userPapers = userPaperRepository.findActivePartnershipsByStudentId(memberId, LocalDate.now()); |
| 103 | + final List<UserPaper> userPapers = userPaperRepository.findActivePartnershipsByStudentId(memberId); |
106 | 104 | if (userPapers.isEmpty()) { |
107 | 105 | return List.of(); // active 제휴가 없으면 빈 리스트 반환 |
108 | 106 | } |
@@ -230,79 +228,55 @@ private String generateBenefitText(PaperContent content) { |
230 | 228 | } |
231 | 229 |
|
232 | 230 | @Override |
233 | | - public List<StoreMapResponseDTO> searchStores(String keyword) { |
234 | | - List<Store> stores = storeRepository.findByNameContainingIgnoreCaseOrderByIdDescWithPartner(keyword); |
| 231 | + public List<StoreMapResponseDTO> searchStores(String keyword, Long memberId) { |
| 232 | + List<UserPaper> userPapers = userPaperRepository.findActivePartnershipsByStudentId(memberId); |
235 | 233 |
|
236 | | - if (stores.isEmpty()) { |
| 234 | + if (userPapers.isEmpty()) { |
237 | 235 | return List.of(); |
238 | 236 | } |
239 | 237 |
|
240 | | - List<Long> storeIds = stores.stream().map(Store::getId).toList(); |
241 | | - |
242 | | - // 매장별 모든 active Paper 조회 |
243 | | - List<Paper> papers = paperRepository.findByStoreIdIn(storeIds, ActivationStatus.ACTIVE); |
244 | | - |
245 | | - // active 제휴가 없는 매장 필터링 |
246 | | - Set<Long> storeIdsWithActivePaper = papers.stream() |
247 | | - .map(p -> p.getStore().getId()) |
248 | | - .collect(Collectors.toSet()); |
249 | | - |
250 | | - List<Store> storesWithActivePaper = stores.stream() |
251 | | - .filter(s -> storeIdsWithActivePaper.contains(s.getId())) |
252 | | - .toList(); |
253 | | - |
254 | | - if (storesWithActivePaper.isEmpty()) { |
| 238 | + Map<Store, List<Paper>> storePaperMap = userPapers.stream() |
| 239 | + .map(UserPaper::getPaper) |
| 240 | + .filter(paper -> paper.getStore().getName().toLowerCase().replace(" ","").contains(keyword.toLowerCase().replace(" ",""))) |
| 241 | + .collect(Collectors.groupingBy(Paper::getStore)); |
| 242 | + |
| 243 | + if (storePaperMap.isEmpty()) { |
255 | 244 | return List.of(); |
256 | 245 | } |
257 | 246 |
|
258 | | - // 매장별 Paper 그룹화 |
259 | | - Map<Long, List<Paper>> papersByStore = papers.stream() |
260 | | - .collect(Collectors.groupingBy(p -> p.getStore().getId())); |
261 | | - |
262 | | - // 모든 paper의 모든 PaperContent 조회 |
263 | | - List<Long> allPaperIds = papers.stream().map(Paper::getId).toList(); |
264 | | - Map<Long, List<PaperContent>> contentsByPaperId = allPaperIds.isEmpty() |
265 | | - ? Collections.emptyMap() |
266 | | - : paperContentRepository.findByPaperIdIn(allPaperIds).stream() |
267 | | - .collect(Collectors.groupingBy( |
268 | | - pc -> pc.getPaper().getId() |
269 | | - )); |
270 | | - |
271 | | - return storesWithActivePaper.stream().map(s -> { |
272 | | - List<Paper> storePapers = papersByStore.getOrDefault(s.getId(), List.of()); |
273 | | - |
274 | | - // admin별로 그룹화해서 benefits 리스트 생성 |
275 | | - Map<Long, List<String>> benefitsByAdmin = storePapers.stream() |
276 | | - .collect(Collectors.groupingBy( |
277 | | - paper -> paper.getAdmin().getId(), |
278 | | - Collectors.flatMapping( |
279 | | - paper -> { |
280 | | - List<PaperContent> contents = contentsByPaperId.getOrDefault(paper.getId(), List.of()); |
281 | | - return contents.stream().map(this::resolveBenefit); |
282 | | - }, |
283 | | - Collectors.filtering( |
284 | | - benefit -> benefit != null && !benefit.isBlank(), |
285 | | - Collectors.toList() |
286 | | - ) |
287 | | - ) |
288 | | - )); |
289 | | - |
290 | | - List<StoreMapResponseDTO.PartnershipInfo> partnerships = benefitsByAdmin.entrySet().stream() |
291 | | - .map(entry -> { |
292 | | - Long adminId = entry.getKey(); |
293 | | - List<String> benefits = entry.getValue(); |
294 | | - String adminName = storePapers.stream() |
295 | | - .filter(p -> p.getAdmin().getId().equals(adminId)) |
296 | | - .findFirst() |
297 | | - .map(p -> p.getAdmin().getName()) |
298 | | - .orElse(null); |
299 | | - return new StoreMapResponseDTO.PartnershipInfo(adminId, adminName, benefits); |
300 | | - }) |
301 | | - .filter(p -> p.adminId() != null && !p.benefits().isEmpty()) |
302 | | - .toList(); |
| 247 | + List<Long> paperIds = storePaperMap.values().stream() |
| 248 | + .flatMap(List::stream) |
| 249 | + .map(Paper::getId) |
| 250 | + .toList(); |
303 | 251 |
|
304 | | - return StoreMapResponseDTO.ofWithPartnerships(s, partnerships, amazonS3Manager); |
305 | | - }).toList(); |
| 252 | + Map<Long, List<PaperContent>> contentsByPaperId = paperContentRepository.findByPaperIdIn(paperIds).stream() |
| 253 | + .collect(Collectors.groupingBy(pc -> pc.getPaper().getId())); |
| 254 | + |
| 255 | + return storePaperMap.entrySet().stream() |
| 256 | + .map(entry -> { |
| 257 | + Store store = entry.getKey(); |
| 258 | + List<Paper> papers = entry.getValue(); |
| 259 | + |
| 260 | + List<StoreMapResponseDTO.PartnershipInfo> partnerships = papers.stream() |
| 261 | + .map(paper -> { |
| 262 | + List<String> benefits = contentsByPaperId.getOrDefault(paper.getId(), List.of()).stream() |
| 263 | + .map(this::resolveBenefit) |
| 264 | + .filter(benefit -> benefit != null && !benefit.isBlank()) |
| 265 | + .toList(); |
| 266 | + |
| 267 | + return new StoreMapResponseDTO.PartnershipInfo( |
| 268 | + paper.getAdmin().getId(), |
| 269 | + paper.getAdmin().getName(), |
| 270 | + benefits |
| 271 | + ); |
| 272 | + }) |
| 273 | + .filter(p -> !p.benefits().isEmpty()) |
| 274 | + .toList(); |
| 275 | + |
| 276 | + return StoreMapResponseDTO.ofWithPartnerships(store, partnerships, amazonS3Manager); |
| 277 | + }) |
| 278 | + .filter(dto -> !dto.partnerships().isEmpty()) |
| 279 | + .toList(); |
306 | 280 | } |
307 | 281 |
|
308 | 282 | @Override |
|
0 commit comments