Skip to content

Commit

Permalink
refactor: 매출 통계 리팩토링 #225
Browse files Browse the repository at this point in the history
  • Loading branch information
jjt4515 committed Nov 20, 2024
1 parent 84984fc commit e610adb
Showing 1 changed file with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
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;
import poomasi.domain.product._category.entity.Category;
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;
Expand All @@ -32,61 +32,73 @@ public class StatisticsService {
private final OrderService orderService;
private final CategoryService categoryService;

private List<OrderedProduct> getDeliveredProducts(List<Order> 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<Category, CategoryMonthlySalesResponse> 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<StoreMonthlySalesResponse> 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<Order> orders = orderService.getOrdersByUpdateAtBetween(startDate, endDate);

List<StoreMonthlySalesResponse> 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<OrderedProduct> deliveredProducts = getDeliveredProducts(orders);

List<StoreMonthlySalesResponse> salesResponses = deliveredProducts.stream()
.filter(orderedProduct -> orderedProduct.getStoreId().equals(storeId))
.map(this::createStoreSalesResponse)
.collect(Collectors.toList());

return new PageImpl<>(salesResponses, pageable, salesResponses.size());
}

public Page<CategoryMonthlySalesResponse> getCategoryMonthlySales(LocalDate startDate, Pageable pageable) {
LocalDateTime[] dateRange = calculateDateRange(startDate, 5);
List<Order> orders = orderService.getOrdersByUpdateAtBetween(dateRange[0], dateRange[1]);

LocalDate endDate = startDate.plusMonths(5);

List<Order> orders = orderService.getOrdersByUpdateAtBetween(startDate.atStartOfDay(), endDate.atTime(23, 59, 59));
List<OrderedProduct> deliveredProducts = getDeliveredProducts(orders);

Map<Category, CategoryMonthlySalesResponse> 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<CategoryMonthlySalesResponse> responseList = new ArrayList<>(salesMap.values());

int start = (int) pageable.getOffset();
int end = Math.min((start + pageable.getPageSize()), responseList.size());
Page<CategoryMonthlySalesResponse> 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());
}
}

0 comments on commit e610adb

Please sign in to comment.