diff --git a/src/main/java/poomasi/domain/admin/statistics/service/StatisticsService.java b/src/main/java/poomasi/domain/admin/statistics/service/StatisticsService.java index dfb5d9f8..063dd430 100644 --- a/src/main/java/poomasi/domain/admin/statistics/service/StatisticsService.java +++ b/src/main/java/poomasi/domain/admin/statistics/service/StatisticsService.java @@ -8,6 +8,7 @@ import org.springframework.transaction.annotation.Transactional; import poomasi.domain.admin.statistics.dto.response.StoreMonthlySalesResponse; import poomasi.domain.order.entity.Order; +import poomasi.domain.order.entity.OrderedProduct; import poomasi.domain.order.entity.OrderedProductStatus; import poomasi.domain.order.service.OrderService; import poomasi.domain.admin.statistics.dto.response.CategoryMonthlySalesResponse; @@ -15,7 +16,6 @@ import poomasi.domain.product._category.service.CategoryService; import poomasi.domain.product.entity.Product; - import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; @@ -32,61 +32,73 @@ public class StatisticsService { private final OrderService orderService; private final CategoryService categoryService; + private List getDeliveredProducts(List orders) { + return orders.stream() + .flatMap(order -> order.getOrderedProducts().stream()) + .filter(orderedProduct -> orderedProduct.getOrderedProductStatus() == OrderedProductStatus.DELIVERED) + .collect(Collectors.toList()); + } + + private LocalDateTime[] calculateDateRange(LocalDate startDate, int months) { + LocalDate endDate = startDate.plusMonths(months); + return new LocalDateTime[]{ + startDate.atStartOfDay(), + endDate.atTime(23, 59, 59) + }; + } + + private StoreMonthlySalesResponse createStoreSalesResponse(OrderedProduct orderedProduct) { + BigDecimal totalSales = orderedProduct.getPrice().multiply(BigDecimal.valueOf(orderedProduct.getCount())); + return new StoreMonthlySalesResponse(orderedProduct.getStore(), totalSales); + } + + private void updateCategorySales(Map salesMap, OrderedProduct orderedProduct) { + Product product = orderedProduct.getProduct(); + Category category = categoryService.getCategory(product.getCategoryId()); + + CategoryMonthlySalesResponse salesResponse = salesMap.computeIfAbsent(category, k -> + new CategoryMonthlySalesResponse(category.getName(), 0, BigDecimal.ZERO) + ); + + salesResponse.setCount(salesResponse.getCount() + 1); + BigDecimal productTotal = product.getPrice().multiply(BigDecimal.valueOf(orderedProduct.getCount())); + salesResponse.setTotalSales(salesResponse.getTotalSales().add(productTotal)); + } + public Page getMonthlyStoreSales(Long storeId, String startMonth, String endMonth, Pageable pageable) { LocalDate start = LocalDate.parse(startMonth + "-01"); - LocalDate end = LocalDate.parse(endMonth + "-01"); - end = end.withDayOfMonth(end.lengthOfMonth()); + LocalDate end = LocalDate.parse(endMonth + "-01").withDayOfMonth(LocalDate.parse(endMonth + "-01").lengthOfMonth()); LocalDateTime startDate = start.atStartOfDay(); LocalDateTime endDate = end.atTime(23, 59, 59); List orders = orderService.getOrdersByUpdateAtBetween(startDate, endDate); - List salesResponses = orders.stream() - .flatMap(order -> order.getOrderedProducts().stream()) - .filter(orderedProduct -> orderedProduct.getStoreId() - .equals(storeId) && orderedProduct.getOrderedProductStatus() == OrderedProductStatus.DELIVERED) - .map(orderedProduct -> { - BigDecimal totalSales = orderedProduct.getPrice().multiply(BigDecimal.valueOf(orderedProduct.getCount())); - return new StoreMonthlySalesResponse(orderedProduct.getStore(), totalSales); - }) + List deliveredProducts = getDeliveredProducts(orders); + + List salesResponses = deliveredProducts.stream() + .filter(orderedProduct -> orderedProduct.getStoreId().equals(storeId)) + .map(this::createStoreSalesResponse) .collect(Collectors.toList()); return new PageImpl<>(salesResponses, pageable, salesResponses.size()); } public Page getCategoryMonthlySales(LocalDate startDate, Pageable pageable) { + LocalDateTime[] dateRange = calculateDateRange(startDate, 5); + List orders = orderService.getOrdersByUpdateAtBetween(dateRange[0], dateRange[1]); - LocalDate endDate = startDate.plusMonths(5); - - List orders = orderService.getOrdersByUpdateAtBetween(startDate.atStartOfDay(), endDate.atTime(23, 59, 59)); + List deliveredProducts = getDeliveredProducts(orders); Map salesMap = new HashMap<>(); - orders.stream() - .flatMap(order -> order.getOrderedProducts().stream()) - .filter(orderedProduct -> orderedProduct.getOrderedProductStatus() == OrderedProductStatus.DELIVERED) - .forEach(orderedProduct -> { - Product product = orderedProduct.getProduct(); - Long categoryId = product.getCategoryId(); - Category category = categoryService.getCategory(categoryId); - - CategoryMonthlySalesResponse salesResponse = salesMap.computeIfAbsent(category, k -> - new CategoryMonthlySalesResponse(category.getName(), 0, BigDecimal.ZERO) - ); - - salesResponse.setCount(salesResponse.getCount() + 1); - BigDecimal productTotal = product.getPrice().multiply(BigDecimal.valueOf(orderedProduct.getCount())); - salesResponse.setTotalSales(salesResponse.getTotalSales().add(productTotal)); - }); + deliveredProducts.forEach(orderedProduct -> updateCategorySales(salesMap, orderedProduct)); List responseList = new ArrayList<>(salesMap.values()); int start = (int) pageable.getOffset(); - int end = Math.min((start + pageable.getPageSize()), responseList.size()); - Page page = new PageImpl<>(responseList.subList(start, end), pageable, responseList.size()); - - return page; + int end = Math.min(start + pageable.getPageSize(), responseList.size()); + return new PageImpl<>(responseList.subList(start, end), pageable, responseList.size()); } }