Skip to content

Commit

Permalink
#23 [Fix] 평점순 조회 가까운 거리순으로 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 20, 2022
1 parent 3a37090 commit a0ef5f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ReviewMainResponseDto {
private String storeName;
private String userName;
private String content; //리뷰 코멘트
private String address;
private double point; //별점
private List<ReviewImageResponseDto> reviewImageUrlList; //리뷰 이미지 사진
private List<ReviewResponseDto.TagResponseDto> tagList;
Expand All @@ -31,6 +32,7 @@ public ReviewMainResponseDto(Review review, Store store, User user) {
this.storeId = store.getId();
this.userId = user.getId();
this.storeName = store.getName();
this.address = store.getAddress();
this.userName = user.getEmail();
this.content = review.getContent();
this.point = review.getPoint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.List;

import static com.mpnp.baechelin.common.QuerydslLocation.locTwoPointAndConditions;
Expand Down Expand Up @@ -42,20 +43,6 @@ public Page<Store> findBetweenLngLat(BigDecimal latStart,
Pageable pageable) {

BooleanBuilder builder = QuerydslLocation.locAndConditions(latStart, latEnd, lngStart, lngEnd, category, facility);

// BigDecimal nowLat = (latStart.add(latEnd)).divide(new BigDecimal("2"), 22, RoundingMode.HALF_UP);
// BigDecimal nowLng = (lngStart.add(lngEnd)).divide(new BigDecimal("2"), 22, RoundingMode.HALF_UP);
// List<Store> storeResultList = queryFactory.selectFrom(store)
// .where(builder)
// .fetch();
// // 가까운순으로 정렬하기
// storeResultList.sort((thisStore, newStore) -> {
// BigDecimal thisDiff = nowLat.subtract(thisStore.getLatitude()).abs().add(nowLng.subtract(thisStore.getLongitude()).abs());
// BigDecimal newDiff = nowLat.subtract(newStore.getLatitude()).abs().add(nowLng.subtract(newStore.getLongitude()).abs());
// return thisDiff.compareTo(newDiff);
// });
// getStorePaged(storeResultList, pageable);

List<Store> storeList = queryFactory.selectFrom(store)
.where(builder)
.limit(pageable.getPageSize())
Expand All @@ -65,16 +52,20 @@ public Page<Store> findBetweenLngLat(BigDecimal latStart,
if (latEnd != null && latStart != null && lngStart != null && lngEnd != null) {
BigDecimal nowLat = (latStart.add(latEnd)).divide(new BigDecimal("2"), 22, RoundingMode.HALF_UP);
BigDecimal nowLng = (lngStart.add(lngEnd)).divide(new BigDecimal("2"), 22, RoundingMode.HALF_UP);
storeList.sort((thisStore, newStore) -> {
BigDecimal thisDiff = nowLat.subtract(thisStore.getLatitude()).abs().add(nowLng.subtract(thisStore.getLongitude()).abs());
BigDecimal newDiff = nowLat.subtract(newStore.getLatitude()).abs().add(nowLng.subtract(newStore.getLongitude()).abs());
return thisDiff.compareTo(newDiff);
});
storeSortByDistance(storeList, nowLat, nowLng);
}
int fetchCount = queryFactory.selectFrom(store).where(builder).fetch().size();
return new PageImpl<>(storeList, pageable, fetchCount);
}

private void storeSortByDistance(List<Store> storeList, BigDecimal nowLat, BigDecimal nowLng) {
storeList.sort((thisStore, newStore) -> {
BigDecimal thisDiff = nowLat.subtract(thisStore.getLatitude()).abs().add(nowLng.subtract(thisStore.getLongitude()).abs());
BigDecimal newDiff = nowLat.subtract(newStore.getLatitude()).abs().add(nowLng.subtract(newStore.getLongitude()).abs());
return thisDiff.compareTo(newDiff);
});
}

//TODO 별점순 - 쿼리 결과로 산출된 리스트의 평균 구하기, 정렬, 페이징
// public List<StoreCardResponseDto> findStoreOrderByPoint(BigDecimal lat,
public Page<Store> findStoreOrderByPoint(BigDecimal lat,
Expand All @@ -84,31 +75,14 @@ public Page<Store> findStoreOrderByPoint(BigDecimal lat,
Pageable pageable) {

BooleanBuilder builder = locTwoPointAndConditions(lat, lng, category, facility);
// 직접 DTO를 조작
// List<Store> resultList = queryFactory.selectFrom(store)
// .where(builder)
// .fetch();

// List<StoreCardResponseDto> resultAvgList = resultList.stream()
// .map(store -> {
// long count = user == null ? 0L : user.getBookmarkList().stream()
// .filter(b -> b.getUserId() == user && b.getStoreId() == store).count();
// double avg = Double.parseDouble(String.format(String.valueOf(store.getReviewList().stream()
// .collect(Collectors.averagingDouble(Review::getPoint))), "0.1f"));
// StoreCardResponseDto storeCardResponseDto = new StoreCardResponseDto(store, count > 0);
// storeCardResponseDto.setPointAvg(avg);
// return storeCardResponseDto;
// }).sorted().collect(Collectors.toList());
//
// return getStoreCardPaged(resultAvgList, pageable);

// 업데이트시 쿼리
List<Store> updateResultList = queryFactory.selectFrom(store)
.where(builder)
.limit(pageable.getPageSize())
.offset(pageable.getOffset())
.orderBy(store.pointAvg.desc())
.fetch();
storeSortByDistance(updateResultList, lat, lng);
int fetchCount = queryFactory.selectFrom(store).where(builder).fetch().size();
return new PageImpl<>(updateResultList, pageable, fetchCount);
}
Expand Down

0 comments on commit a0ef5f2

Please sign in to comment.