Skip to content

Commit 9d1b5dc

Browse files
committed
refactor/367 교환 요청 조회 정렬 메모리->db 정렬로 변경
1 parent b7ee52d commit 9d1b5dc

File tree

2 files changed

+180
-43
lines changed

2 files changed

+180
-43
lines changed

src/main/java/com/back/domain/dashboard/artist/service/ArtistDashboardServiceImpl.java

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,8 @@ private String convertRefundStatusToKorean(com.back.domain.order.refund.entity.R
11311131

11321132
@Override
11331133
public ArtistExchangeResponse.List getExchangeRequests(Long artistId, ArtistExchangeSearchRequest request) {
1134-
log.info("작가 교환 요청 목록 조회 시작 - artistId: {}, page: {}, size: {}, status: {}, keyword: {}",
1135-
artistId, request.page(), request.size(), request.status(), request.keyword());
1134+
log.info("작가 교환 요청 목록 조회 시작 - artistId: {}, page: {}, size: {}, status: {}, keyword: {}, sort: {}, order: {}",
1135+
artistId, request.page(), request.size(), request.status(), request.keyword(), request.sort(), request.order());
11361136

11371137
// status 문자열을 ExchangeStatus enum으로 변환
11381138
com.back.domain.order.exchange.entity.Exchange.ExchangeStatus exchangeStatus = null;
@@ -1149,24 +1149,50 @@ public ArtistExchangeResponse.List getExchangeRequests(Long artistId, ArtistExch
11491149
}
11501150
}
11511151

1152-
// Repository를 통한 실제 DB 조회 (최신순 정렬)
1153-
Page<com.back.domain.order.exchange.entity.Exchange> exchangePage = exchangeRepository.findExchangesByArtist(
1154-
artistId,
1155-
exchangeStatus,
1156-
request.keyword(),
1157-
PageRequest.of(request.page(), request.size())
1158-
);
1152+
// 정렬 방향
1153+
String sortOrder = request.order() != null ? request.order() : "DESC";
1154+
1155+
// 정렬 필드에 따라 적절한 Repository 메서드 호출
1156+
Page<com.back.domain.order.exchange.entity.Exchange> exchangePage;
1157+
PageRequest pageRequest = PageRequest.of(request.page(), request.size());
1158+
1159+
String sortField = request.sort() != null ? request.sort() : "requestDate";
1160+
1161+
switch (sortField) {
1162+
case "productName" -> {
1163+
// 상품명 정렬 (DB 쿼리)
1164+
if ("ASC".equalsIgnoreCase(sortOrder)) {
1165+
exchangePage = exchangeRepository.findExchangesByArtistSortedByProductNameAsc(
1166+
artistId, exchangeStatus, request.keyword(), pageRequest);
1167+
} else {
1168+
exchangePage = exchangeRepository.findExchangesByArtistSortedByProductNameDesc(
1169+
artistId, exchangeStatus, request.keyword(), pageRequest);
1170+
}
1171+
}
1172+
case "customerName" -> {
1173+
// 구매자 이름 정렬 (DB 쿼리)
1174+
if ("ASC".equalsIgnoreCase(sortOrder)) {
1175+
exchangePage = exchangeRepository.findExchangesByArtistSortedByCustomerNameAsc(
1176+
artistId, exchangeStatus, request.keyword(), pageRequest);
1177+
} else {
1178+
exchangePage = exchangeRepository.findExchangesByArtistSortedByCustomerNameDesc(
1179+
artistId, exchangeStatus, request.keyword(), pageRequest);
1180+
}
1181+
}
1182+
default -> {
1183+
// 주문일자, 상태 정렬 (Pageable Sort 사용)
1184+
org.springframework.data.domain.Sort sort = createExchangeSort(sortField, sortOrder);
1185+
pageRequest = PageRequest.of(request.page(), request.size(), sort);
1186+
exchangePage = exchangeRepository.findExchangesByArtist(
1187+
artistId, exchangeStatus, request.keyword(), pageRequest);
1188+
}
1189+
}
11591190

11601191
// Entity → DTO 변환
11611192
List<ArtistExchangeResponse.ExchangeRequest> content = exchangePage.getContent().stream()
11621193
.map(this::convertToExchangeDto)
11631194
.toList();
11641195

1165-
// 메모리에서 정렬 (간단한 정렬만 지원)
1166-
if (request.sort() != null && !request.sort().equals("requestDate")) {
1167-
content = sortExchangesInMemory(content, request.sort(), request.order());
1168-
}
1169-
11701196
int totalPages = exchangePage.getTotalPages();
11711197
long totalElements = exchangePage.getTotalElements();
11721198
boolean hasNext = exchangePage.hasNext();
@@ -1187,36 +1213,18 @@ public ArtistExchangeResponse.List getExchangeRequests(Long artistId, ArtistExch
11871213
}
11881214

11891215
/**
1190-
* 메모리에서 교환 요청 정렬 처리
1216+
* 교환 요청 정렬 생성 (requestDate, status 정렬용)
11911217
*/
1192-
private List<ArtistExchangeResponse.ExchangeRequest> sortExchangesInMemory(
1193-
List<ArtistExchangeResponse.ExchangeRequest> list, String sort, String order) {
1194-
1195-
boolean asc = "ASC".equalsIgnoreCase(order);
1218+
private org.springframework.data.domain.Sort createExchangeSort(String sortField, String sortOrder) {
1219+
org.springframework.data.domain.Sort.Direction direction =
1220+
"ASC".equalsIgnoreCase(sortOrder)
1221+
? org.springframework.data.domain.Sort.Direction.ASC
1222+
: org.springframework.data.domain.Sort.Direction.DESC;
11961223

1197-
return list.stream()
1198-
.sorted((a, b) -> {
1199-
int cmp = 0;
1200-
switch (sort) {
1201-
case "productName":
1202-
String nameA = a.orderItem() != null ? a.orderItem().productName() : "";
1203-
String nameB = b.orderItem() != null ? b.orderItem().productName() : "";
1204-
cmp = nameA.compareTo(nameB);
1205-
break;
1206-
case "customerName":
1207-
String customerA = a.customer() != null ? a.customer().nickname() : "";
1208-
String customerB = b.customer() != null ? b.customer().nickname() : "";
1209-
cmp = customerA.compareTo(customerB);
1210-
break;
1211-
case "status":
1212-
cmp = a.status().compareTo(b.status());
1213-
break;
1214-
default:
1215-
cmp = a.requestDate().compareTo(b.requestDate());
1216-
}
1217-
return asc ? cmp : -cmp;
1218-
})
1219-
.toList();
1224+
return switch (sortField) {
1225+
case "status" -> org.springframework.data.domain.Sort.by(direction, "status");
1226+
default -> org.springframework.data.domain.Sort.by(direction, "createDate"); // requestDate
1227+
};
12201228
}
12211229

12221230
/**

src/main/java/com/back/domain/order/exchange/repository/ExchangeRepository.java

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,136 @@ public interface ExchangeRepository extends JpaRepository<Exchange, Long> {
4343
List<Exchange> findByUserWithItems(@Param("user") User user);
4444

4545
/**
46-
* 작가의 상품에 대한 교환 요청 조회 (검색만 쿼리, 정렬은 메모리)
46+
* 작가의 상품에 대한 교환 요청 조회 (검색 + DB 정렬)
47+
* - 상품명 정렬 (ASC)
48+
*/
49+
@Query(value = "SELECT DISTINCT e FROM Exchange e " +
50+
"JOIN FETCH e.order o " +
51+
"JOIN FETCH e.user u " +
52+
"LEFT JOIN FETCH e.exchangeItems ei " +
53+
"LEFT JOIN FETCH ei.orderItem oi " +
54+
"LEFT JOIN FETCH oi.product p " +
55+
"WHERE p.user.id = :artistId " +
56+
"AND (:status IS NULL OR e.status = :status) " +
57+
"AND (:keyword IS NULL OR :keyword = '' OR " +
58+
" p.name LIKE CONCAT('%', :keyword, '%') OR " +
59+
" u.name LIKE CONCAT('%', :keyword, '%')) " +
60+
"ORDER BY p.name ASC",
61+
countQuery = "SELECT COUNT(DISTINCT e) FROM Exchange e " +
62+
"JOIN e.exchangeItems ei " +
63+
"JOIN ei.orderItem oi " +
64+
"JOIN oi.product p " +
65+
"WHERE p.user.id = :artistId " +
66+
"AND (:status IS NULL OR e.status = :status) " +
67+
"AND (:keyword IS NULL OR :keyword = '' OR " +
68+
" p.name LIKE CONCAT('%', :keyword, '%') OR " +
69+
" e.user.name LIKE CONCAT('%', :keyword, '%'))")
70+
Page<Exchange> findExchangesByArtistSortedByProductNameAsc(
71+
@Param("artistId") Long artistId,
72+
@Param("status") Exchange.ExchangeStatus status,
73+
@Param("keyword") String keyword,
74+
Pageable pageable
75+
);
76+
77+
/**
78+
* 작가의 상품에 대한 교환 요청 조회 (검색 + DB 정렬)
79+
* - 상품명 정렬 (DESC)
80+
*/
81+
@Query(value = "SELECT DISTINCT e FROM Exchange e " +
82+
"JOIN FETCH e.order o " +
83+
"JOIN FETCH e.user u " +
84+
"LEFT JOIN FETCH e.exchangeItems ei " +
85+
"LEFT JOIN FETCH ei.orderItem oi " +
86+
"LEFT JOIN FETCH oi.product p " +
87+
"WHERE p.user.id = :artistId " +
88+
"AND (:status IS NULL OR e.status = :status) " +
89+
"AND (:keyword IS NULL OR :keyword = '' OR " +
90+
" p.name LIKE CONCAT('%', :keyword, '%') OR " +
91+
" u.name LIKE CONCAT('%', :keyword, '%')) " +
92+
"ORDER BY p.name DESC",
93+
countQuery = "SELECT COUNT(DISTINCT e) FROM Exchange e " +
94+
"JOIN e.exchangeItems ei " +
95+
"JOIN ei.orderItem oi " +
96+
"JOIN oi.product p " +
97+
"WHERE p.user.id = :artistId " +
98+
"AND (:status IS NULL OR e.status = :status) " +
99+
"AND (:keyword IS NULL OR :keyword = '' OR " +
100+
" p.name LIKE CONCAT('%', :keyword, '%') OR " +
101+
" e.user.name LIKE CONCAT('%', :keyword, '%'))")
102+
Page<Exchange> findExchangesByArtistSortedByProductNameDesc(
103+
@Param("artistId") Long artistId,
104+
@Param("status") Exchange.ExchangeStatus status,
105+
@Param("keyword") String keyword,
106+
Pageable pageable
107+
);
108+
109+
/**
110+
* 작가의 상품에 대한 교환 요청 조회 (검색 + DB 정렬)
111+
* - 구매자 이름 정렬 (ASC)
112+
*/
113+
@Query(value = "SELECT DISTINCT e FROM Exchange e " +
114+
"JOIN FETCH e.order o " +
115+
"JOIN FETCH e.user u " +
116+
"LEFT JOIN FETCH e.exchangeItems ei " +
117+
"LEFT JOIN FETCH ei.orderItem oi " +
118+
"LEFT JOIN FETCH oi.product p " +
119+
"WHERE p.user.id = :artistId " +
120+
"AND (:status IS NULL OR e.status = :status) " +
121+
"AND (:keyword IS NULL OR :keyword = '' OR " +
122+
" p.name LIKE CONCAT('%', :keyword, '%') OR " +
123+
" u.name LIKE CONCAT('%', :keyword, '%')) " +
124+
"ORDER BY u.name ASC",
125+
countQuery = "SELECT COUNT(DISTINCT e) FROM Exchange e " +
126+
"JOIN e.exchangeItems ei " +
127+
"JOIN ei.orderItem oi " +
128+
"JOIN oi.product p " +
129+
"WHERE p.user.id = :artistId " +
130+
"AND (:status IS NULL OR e.status = :status) " +
131+
"AND (:keyword IS NULL OR :keyword = '' OR " +
132+
" p.name LIKE CONCAT('%', :keyword, '%') OR " +
133+
" e.user.name LIKE CONCAT('%', :keyword, '%'))")
134+
Page<Exchange> findExchangesByArtistSortedByCustomerNameAsc(
135+
@Param("artistId") Long artistId,
136+
@Param("status") Exchange.ExchangeStatus status,
137+
@Param("keyword") String keyword,
138+
Pageable pageable
139+
);
140+
141+
/**
142+
* 작가의 상품에 대한 교환 요청 조회 (검색 + DB 정렬)
143+
* - 구매자 이름 정렬 (DESC)
144+
*/
145+
@Query(value = "SELECT DISTINCT e FROM Exchange e " +
146+
"JOIN FETCH e.order o " +
147+
"JOIN FETCH e.user u " +
148+
"LEFT JOIN FETCH e.exchangeItems ei " +
149+
"LEFT JOIN FETCH ei.orderItem oi " +
150+
"LEFT JOIN FETCH oi.product p " +
151+
"WHERE p.user.id = :artistId " +
152+
"AND (:status IS NULL OR e.status = :status) " +
153+
"AND (:keyword IS NULL OR :keyword = '' OR " +
154+
" p.name LIKE CONCAT('%', :keyword, '%') OR " +
155+
" u.name LIKE CONCAT('%', :keyword, '%')) " +
156+
"ORDER BY u.name DESC",
157+
countQuery = "SELECT COUNT(DISTINCT e) FROM Exchange e " +
158+
"JOIN e.exchangeItems ei " +
159+
"JOIN ei.orderItem oi " +
160+
"JOIN oi.product p " +
161+
"WHERE p.user.id = :artistId " +
162+
"AND (:status IS NULL OR e.status = :status) " +
163+
"AND (:keyword IS NULL OR :keyword = '' OR " +
164+
" p.name LIKE CONCAT('%', :keyword, '%') OR " +
165+
" e.user.name LIKE CONCAT('%', :keyword, '%'))")
166+
Page<Exchange> findExchangesByArtistSortedByCustomerNameDesc(
167+
@Param("artistId") Long artistId,
168+
@Param("status") Exchange.ExchangeStatus status,
169+
@Param("keyword") String keyword,
170+
Pageable pageable
171+
);
172+
173+
/**
174+
* 작가의 상품에 대한 교환 요청 조회 (검색 + 기본 정렬)
175+
* - 주문일자, 상태 정렬 (Pageable로 처리)
47176
*/
48177
@Query(value = "SELECT DISTINCT e FROM Exchange e " +
49178
"JOIN FETCH e.order o " +

0 commit comments

Comments
 (0)