-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/poomasi/domain/admin/statistics/controller/StatisticsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package poomasi.domain.admin.statistics.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import poomasi.domain.admin.statistics.dto.response.CategoryMonthlySalesResponse; | ||
import poomasi.domain.admin.statistics.dto.response.StoreMonthlySalesResponse; | ||
import poomasi.domain.admin.statistics.service.StatisticsService; | ||
import poomasi.domain.order.service.OrderService; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/admin/statistics") | ||
public class StatisticsController { | ||
|
||
private final StatisticsService statisticsService; | ||
private final OrderService orderService; | ||
|
||
@GetMapping("/stores/{storeId}/monthly-sales") | ||
public ResponseEntity<Page<StoreMonthlySalesResponse>> getMonthlyStoreSales( | ||
@PathVariable Long storeId, | ||
@RequestParam String startMonth, | ||
@RequestParam String endMonth, | ||
Pageable pageable) { | ||
|
||
Page<StoreMonthlySalesResponse> salesResponses = statisticsService.getMonthlyStoreSales(storeId, startMonth, endMonth, pageable); | ||
return ResponseEntity.ok(salesResponses); | ||
} | ||
|
||
@GetMapping("/categories/monthly-sales") | ||
public ResponseEntity<Page<CategoryMonthlySalesResponse>> getCategoryMonthlySales( | ||
@RequestParam Long categoryId, | ||
@RequestParam String startMonth, | ||
@RequestParam String endMonth, | ||
Pageable pageable | ||
) { | ||
Page<CategoryMonthlySalesResponse> sales = statisticsService.getCategoryMonthlySales(categoryId, startMonth, endMonth, pageable); | ||
return ResponseEntity.ok(sales); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/poomasi/domain/admin/statistics/dto/response/CategoryMonthlySalesResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package poomasi.domain.admin.statistics.dto.response; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public record CategoryMonthlySalesResponse( | ||
String categoryName, | ||
int count, | ||
BigDecimal totalSales | ||
) { } |
10 changes: 10 additions & 0 deletions
10
src/main/java/poomasi/domain/admin/statistics/dto/response/StoreMonthlySalesResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package poomasi.domain.admin.statistics.dto.response; | ||
|
||
import poomasi.domain.store.entity.Store; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public record StoreMonthlySalesResponse( | ||
Store store, | ||
BigDecimal totalSales | ||
) {} |
60 changes: 60 additions & 0 deletions
60
src/main/java/poomasi/domain/admin/statistics/service/StatisticsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package poomasi.domain.admin.statistics.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import poomasi.domain.admin.statistics.dto.response.StoreMonthlySalesResponse; | ||
import poomasi.domain.order.entity.Order; | ||
import poomasi.domain.order.service.OrderService; | ||
import poomasi.domain.admin.statistics.dto.response.CategoryMonthlySalesResponse; | ||
|
||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Service | ||
public class StatisticsService { | ||
private final OrderService orderService; | ||
|
||
@Transactional(readOnly = true) | ||
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()); | ||
|
||
LocalDateTime startDate = start.atStartOfDay(); | ||
LocalDateTime endDate = end.atTime(23, 59, 59); | ||
|
||
List<Order> orders = orderService.getOrdersByUpdateAtBetween(startDate, endDate); | ||
|
||
// 주문 목록에서 각 OrderedProduct를 순회하여 storeId와 일치하는 주문 품목의 매출을 계산 | ||
List<StoreMonthlySalesResponse> salesResponses = orders.stream() | ||
.flatMap(order -> order.getOrderedProducts().stream()) | ||
.filter(orderedProduct -> orderedProduct.getProduct().getStore().getId().equals(storeId)) | ||
.map(orderedProduct -> { | ||
BigDecimal totalSales = orderedProduct.getPrice().multiply(BigDecimal.valueOf(orderedProduct.getCount())); | ||
return new StoreMonthlySalesResponse(orderedProduct.getProduct().getStore(), totalSales); | ||
}) | ||
.collect(Collectors.toList()); | ||
|
||
return new PageImpl<>(salesResponses, pageable, salesResponses.size()); | ||
} | ||
|
||
public Page<CategoryMonthlySalesResponse> getCategoryMonthlySales(Long categoryId, String startMonth, String endMonth, Pageable pageable) { | ||
LocalDate start = LocalDate.parse(startMonth + "-01"); | ||
LocalDate end = LocalDate.parse(endMonth + "-01"); | ||
end = end.withDayOfMonth(end.lengthOfMonth()); | ||
|
||
return orderService.getCategoryMonthlySales(categoryId, start.atStartOfDay(), end.atTime(LocalTime.MAX), pageable); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters