Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.tookscan.tookscan.order.presentation.dto.response.ReadAdminOrderOverviewsResponseDto;
import com.tookscan.tookscan.order.repository.OrderRepository;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.RequiredArgsConstructor;
Expand All @@ -31,12 +32,19 @@ public ReadAdminOrderOverviewsResponseDto execute(int page, int size, String sta
Boolean isAsInProgress, Boolean isInProgress, UUID customerKey) {
Pageable pageable = PageRequest.of(page - 1, size);

// ํ•„ํ„ฐ๋ง๋œ ์ฃผ๋ฌธ ์กฐํšŒ (ํŽ˜์ด์ง€๋„ค์ด์…˜)
Page<Long> orderIdPages = orderRepository.findOrderOverviews(startDate, endDate, search,
searchType, sort, direction, pageable, orderStatus, isOneDayScan, hasRecoveryOption, isAsInProgress,
isInProgress, customerKey);

List<Order> orders = orderRepository.findAllWithDocumentsByIdIn(orderIdPages.getContent());
List<Order> filteredOrders = orderRepository.findAllWithDocumentsByIdIn(orderIdPages.getContent());

return ReadAdminOrderOverviewsResponseDto.of(orders, orderIdPages);
// ์ƒํƒœ๋ณ„ ๊ฐœ์ˆ˜ ์กฐํšŒ (isInProgress ํ•„ํ„ฐ๋งŒ ์ ์šฉ)
Map<EOrderStatus, Long> statusCounts = orderRepository.findOrderStatusCountsByIsInProgress(isInProgress);

// ์ „์ฒด ํ†ต๊ณ„ ์กฐํšŒ (๋ชจ๋“  ํ•„ํ„ฐ ์ œ์™ธ)
Map<EOrderStatus, Long> overallStatusCounts = orderRepository.findOrderStatusCountsByIsInProgress(null);

return ReadAdminOrderOverviewsResponseDto.of(filteredOrders, statusCounts, overallStatusCounts, orderIdPages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,153 @@

@Getter
public class ReadAdminOrderOverviewsResponseDto extends SelfValidating<ReadAdminOrderOverviewsResponseDto> {
@JsonProperty("orders_info")
private final OrderOverviewsInfoDto ordersInfo;

@JsonProperty("orders")
private final List<OrderOverviewsDto> orders;

@JsonProperty("page_info")
private final PageInfoDto pageInfo;

@Builder
public ReadAdminOrderOverviewsResponseDto(List<OrderOverviewsDto> orders, PageInfoDto pageInfo) {
public ReadAdminOrderOverviewsResponseDto(List<OrderOverviewsDto> orders, PageInfoDto pageInfo,
OrderOverviewsInfoDto ordersInfo) {
this.ordersInfo = ordersInfo;
this.orders = orders;
this.pageInfo = pageInfo;
this.validateSelf();
}

public static ReadAdminOrderOverviewsResponseDto of(List<Order> orders, Page<Long> pageInfo) {
public static ReadAdminOrderOverviewsResponseDto of(List<Order> filteredOrders, Map<EOrderStatus, Long> statusCounts, Map<EOrderStatus, Long> overallStatusCounts, Page<Long> pageInfo) {

List<Long> orderIds = pageInfo.getContent();

Map<Long, Order> orderMap = orders.stream()
Map<Long, Order> orderMap = filteredOrders.stream()
.collect(Collectors.toMap(Order::getId, Function.identity()));

List<Order> sortedOrders = orderIds.stream()
.map(orderMap::get)
.toList();
OrderOverviewsInfoDto ordersInfo = OrderOverviewsInfoDto.fromStatusCounts(sortedOrders, statusCounts, overallStatusCounts);

return ReadAdminOrderOverviewsResponseDto.builder()
.orders(sortedOrders.stream().map(OrderOverviewsDto::fromEntity).toList())
.pageInfo(PageInfoDto.fromEntity(pageInfo))
.ordersInfo(ordersInfo)
.build();
}

public static class OrderOverviewsInfoDto extends SelfValidating<OrderOverviewsInfoDto> {
@JsonProperty("total_count")
private final Integer totalCount;

@JsonProperty("apply_completed_count")
private final Integer applyCompletedCount;

@JsonProperty("company_arrived_count")
private final Integer companyArrivedCount;

@JsonProperty("payment_waiting_count")
private final Integer paymentWaitingCount;

@JsonProperty("payment_completed_count")
private final Integer paymentCompletedCount;

@JsonProperty("scan_in_progress_count")
private final Integer scanInProgressCount;

@JsonProperty("scan_completed_count")
private final Integer scanCompletedCount;

@JsonProperty("recovery_in_progress_count")
private final Integer recoveryInProgressCount;

@JsonProperty("post_waiting_count")
private final Integer postWaitingCount;

@JsonProperty("all_completed_count")
private final Integer allCompletedCount;

@JsonProperty("cancel_count")
private final Integer cancelCount;

@JsonProperty("overall_total_count")
private final Integer overallTotalCount;

@JsonProperty("overall_in_progress_count")
private final Integer overallInProgressCount;

@JsonProperty("overall_completed_count")
private final Integer overallCompletedCount;

@Builder
public OrderOverviewsInfoDto(Integer applyCompletedCount, Integer companyArrivedCount,
Integer paymentWaitingCount,
Integer paymentCompletedCount, Integer scanInProgressCount,
Integer scanCompletedCount, Integer recoveryInProgressCount,
Integer postWaitingCount, Integer allCompletedCount, Integer cancelCount,
Integer totalCount, Integer overallTotalCount, Integer overallInProgressCount,
Integer overallCompletedCount) {
this.totalCount = totalCount;
this.applyCompletedCount = applyCompletedCount;
this.companyArrivedCount = companyArrivedCount;
this.paymentWaitingCount = paymentWaitingCount;
this.paymentCompletedCount = paymentCompletedCount;
this.scanInProgressCount = scanInProgressCount;
this.scanCompletedCount = scanCompletedCount;
this.recoveryInProgressCount = recoveryInProgressCount;
this.postWaitingCount = postWaitingCount;
this.allCompletedCount = allCompletedCount;
this.cancelCount = cancelCount;
this.overallTotalCount = overallTotalCount;
this.overallInProgressCount = overallInProgressCount;
this.overallCompletedCount = overallCompletedCount;
this.validateSelf();
}

public static OrderOverviewsInfoDto fromStatusCounts(List<Order> currentPageOrders, Map<EOrderStatus, Long> statusCounts, Map<EOrderStatus, Long> overallStatusCounts) {
return OrderOverviewsInfoDto.builder()
.totalCount(currentPageOrders.size())
.applyCompletedCount(getStatusCount(statusCounts, EOrderStatus.APPLY_COMPLETED))
.companyArrivedCount(getStatusCount(statusCounts, EOrderStatus.COMPANY_ARRIVED))
.paymentWaitingCount(getStatusCount(statusCounts, EOrderStatus.PAYMENT_WAITING))
.paymentCompletedCount(getStatusCount(statusCounts, EOrderStatus.PAYMENT_COMPLETED))
.scanInProgressCount(getStatusCount(statusCounts, EOrderStatus.SCAN_IN_PROGRESS))
.scanCompletedCount(getStatusCount(statusCounts, EOrderStatus.SCAN_COMPLETED))
.recoveryInProgressCount(getStatusCount(statusCounts, EOrderStatus.RECOVERY_IN_PROGRESS))
.postWaitingCount(getStatusCount(statusCounts, EOrderStatus.POST_WAITING))
.allCompletedCount(getStatusCount(statusCounts, EOrderStatus.ALL_COMPLETED))
.cancelCount(getStatusCount(statusCounts, EOrderStatus.CANCEL))
.overallTotalCount(calculateTotalCount(overallStatusCounts))
.overallInProgressCount(calculateInProgressCount(overallStatusCounts))
.overallCompletedCount(calculateCompletedCount(overallStatusCounts))
.build();
}

private static Integer calculateTotalCount(Map<EOrderStatus, Long> statusCounts) {
return Math.toIntExact(statusCounts.values().stream().mapToLong(Long::longValue).sum());
}

private static Integer calculateInProgressCount(Map<EOrderStatus, Long> statusCounts) {
long cancelCount = statusCounts.getOrDefault(EOrderStatus.CANCEL, 0L);
long allCompletedCount = statusCounts.getOrDefault(EOrderStatus.ALL_COMPLETED, 0L);
long totalCount = statusCounts.values().stream().mapToLong(Long::longValue).sum();
return Math.toIntExact(totalCount - cancelCount - allCompletedCount);
}

private static Integer calculateCompletedCount(Map<EOrderStatus, Long> statusCounts) {
long cancelCount = statusCounts.getOrDefault(EOrderStatus.CANCEL, 0L);
long allCompletedCount = statusCounts.getOrDefault(EOrderStatus.ALL_COMPLETED, 0L);
return Math.toIntExact(cancelCount + allCompletedCount);
}

private static Integer getStatusCount(Map<EOrderStatus, Long> statusCounts, EOrderStatus status) {
return Math.toIntExact(statusCounts.getOrDefault(status, 0L));
}
}


public static class OrderOverviewsDto extends SelfValidating<OrderOverviewsDto> {
@JsonProperty("order_id")
private final String orderId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static DocumentInfoDto fromEntity(Document document) {
return DocumentInfoDto.builder()
.name(document.getName())
.pageCount(document.getPageCount())
.pagePrice(document.getDefaultPricePerPage())
.pagePrice(document.getPagePrice())
.documentPrice(document.getDocumentPrice())
.recoveryOption(document.getRecoveryOption())
.isOcrEnabled(document.getIsOcrEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ Page<Order> findAllByUserAndSearchAndOrderStatusInOrElseNull(User user, String s

Integer countByIsAsInProgressTrue();

Map<EOrderStatus, Long> findOrderStatusCountsByIsInProgress(Boolean isInProgress);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tookscan.tookscan.order.repository.impl;

import com.querydsl.core.Tuple;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
Expand Down Expand Up @@ -725,4 +726,38 @@ private BooleanExpression buildFilterPredicate(QOrder order, Boolean isApplied,

return predicate;
}

@Override
public Map<EOrderStatus, Long> findOrderStatusCountsByIsInProgress(Boolean isInProgress) {
QOrder order = QOrder.order;

BooleanExpression predicate = Expressions.TRUE;

if (isInProgress != null) {
if (isInProgress) {
// ์ง„ํ–‰ ์ค‘์ธ ์ƒํƒœ(์ทจ์†Œยท์™„๋ฃŒ ์ œ์™ธ)
predicate = predicate.and(
order.orderStatus.notIn(EOrderStatus.CANCEL, EOrderStatus.ALL_COMPLETED)
);
} else {
// ์ง„ํ–‰ ์ค‘์ด ์•„๋‹Œ ์ƒํƒœ(์ทจ์†Œ ํ˜น์€ ์ „๋ถ€ ์™„๋ฃŒ)
predicate = predicate.and(
order.orderStatus.in(EOrderStatus.CANCEL, EOrderStatus.ALL_COMPLETED)
);
}
}

List<Tuple> results = jpaQueryFactory
.select(order.orderStatus, order.count())
.from(order)
.where(predicate)
.groupBy(order.orderStatus)
.fetch();

return results.stream()
.collect(Collectors.toMap(
tuple -> tuple.get(order.orderStatus),
tuple -> tuple.get(order.count())
));
}
}