Merge branch 'main' of https://github.com/dg04-hi/hi-backend
This commit is contained in:
commit
8e86d913ec
@ -152,7 +152,6 @@ public class AnalyticsService implements AnalyticsUseCase {
|
|||||||
log.info("매장 통계 조회 시작: storeId={}, startDate={}, endDate={}", storeId, startDate, endDate);
|
log.info("매장 통계 조회 시작: storeId={}, startDate={}, endDate={}", storeId, startDate, endDate);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. 캐시 키 생성
|
|
||||||
// 1. 캐시 키 생성 및 확인
|
// 1. 캐시 키 생성 및 확인
|
||||||
String cacheKey = String.format("statistics:store:%d:%s:%s", storeId, startDate, endDate);
|
String cacheKey = String.format("statistics:store:%d:%s:%s", storeId, startDate, endDate);
|
||||||
var cachedResult = cachePort.getAnalyticsCache(cacheKey);
|
var cachedResult = cachePort.getAnalyticsCache(cacheKey);
|
||||||
|
|||||||
@ -16,6 +16,7 @@ public class OrderResponse {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private Long storeId;
|
private Long storeId;
|
||||||
private Long menuId;
|
private Long menuId;
|
||||||
|
private String menuName;
|
||||||
private Integer customerAge;
|
private Integer customerAge;
|
||||||
private String customerGender;
|
private String customerGender;
|
||||||
private BigDecimal orderAmount;
|
private BigDecimal orderAmount;
|
||||||
|
|||||||
@ -116,6 +116,14 @@ public class OrderDataAdapter implements OrderDataPort {
|
|||||||
// 연령대별 분포 계산
|
// 연령대별 분포 계산
|
||||||
Map<String, Integer> ageDistribution = calculateCustomerAgeDistribution(orders);
|
Map<String, Integer> ageDistribution = calculateCustomerAgeDistribution(orders);
|
||||||
|
|
||||||
|
//메뉴명 매핑
|
||||||
|
Map<Long, String> menuIdToName = orders.stream()
|
||||||
|
.collect(Collectors.toMap(
|
||||||
|
OrderResponse::getMenuId,
|
||||||
|
OrderResponse::getMenuName,
|
||||||
|
(existing, replacement) -> existing // 중복시 기존값 유지
|
||||||
|
));
|
||||||
|
|
||||||
// 인기 메뉴 계산 (메뉴ID별 주문 횟수) - 실제로는 메뉴명을 가져와야 하지만 임시로 메뉴ID 사용
|
// 인기 메뉴 계산 (메뉴ID별 주문 횟수) - 실제로는 메뉴명을 가져와야 하지만 임시로 메뉴ID 사용
|
||||||
List<String> popularMenus = orders.stream()
|
List<String> popularMenus = orders.stream()
|
||||||
.collect(Collectors.groupingBy(
|
.collect(Collectors.groupingBy(
|
||||||
@ -125,7 +133,7 @@ public class OrderDataAdapter implements OrderDataPort {
|
|||||||
.entrySet().stream()
|
.entrySet().stream()
|
||||||
.sorted(Map.Entry.<Long, Long>comparingByValue().reversed())
|
.sorted(Map.Entry.<Long, Long>comparingByValue().reversed())
|
||||||
.limit(4)
|
.limit(4)
|
||||||
.map(entry -> "메뉴" + entry.getKey()) // 실제로는 메뉴명으로 변환 필요
|
.map(entry -> menuIdToName.get(entry.getKey())) // 실제로는 메뉴명으로 변환 필요
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
return OrderStatistics.builder()
|
return OrderStatistics.builder()
|
||||||
|
|||||||
@ -14,6 +14,7 @@ public class Order {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private Long storeId;
|
private Long storeId;
|
||||||
private Long menuId;
|
private Long menuId;
|
||||||
|
private String menuName;
|
||||||
private Integer customerAge;
|
private Integer customerAge;
|
||||||
private String customerGender;
|
private String customerGender;
|
||||||
private BigDecimal orderAmount;
|
private BigDecimal orderAmount;
|
||||||
|
|||||||
@ -28,12 +28,27 @@ public class OrderRepositoryAdapter implements OrderRepositoryPort {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Order> findOrdersByStoreIdAndPeriod(Long storeId, LocalDateTime startDate, LocalDateTime endDate) {
|
public List<Order> findOrdersByStoreIdAndPeriod(Long storeId, LocalDateTime startDate, LocalDateTime endDate) {
|
||||||
return orderJpaRepository.findByStoreIdAndOrderDateBetween(storeId, startDate, endDate)
|
|
||||||
.stream()
|
// return orderJpaRepository.findByStoreIdAndOrderDateBetween(storeId, startDate, endDate)
|
||||||
.map(this::toDomain)
|
// .stream()
|
||||||
.collect(Collectors.toList());
|
// .map(this::toDomain)
|
||||||
|
// .collect(Collectors.toList());
|
||||||
|
|
||||||
|
return orderJpaRepository.findByStoreIdAndOrderDateBetweenWithMenuName(storeId, startDate, endDate)
|
||||||
|
.stream()
|
||||||
|
.map(result -> {
|
||||||
|
OrderEntity entity = (OrderEntity) result[0];
|
||||||
|
String menuName = (String) result[1];
|
||||||
|
|
||||||
|
Order order = this.toDomain(entity); // 기존 toDomain 메서드 활용
|
||||||
|
order.setMenuName(menuName); // menuName만 별도로 설정
|
||||||
|
return order;
|
||||||
|
})
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Order> findOrderById(Long orderId) {
|
public Optional<Order> findOrderById(Long orderId) {
|
||||||
return orderJpaRepository.findById(orderId)
|
return orderJpaRepository.findById(orderId)
|
||||||
|
|||||||
@ -1,12 +1,16 @@
|
|||||||
package com.ktds.hi.store.infra.gateway.repository;
|
package com.ktds.hi.store.infra.gateway.repository;
|
||||||
|
|
||||||
|
import com.ktds.hi.store.domain.Order;
|
||||||
import com.ktds.hi.store.infra.gateway.entity.OrderEntity;
|
import com.ktds.hi.store.infra.gateway.entity.OrderEntity;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.lettuce.core.dynamic.annotation.Param;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface OrderJpaRepository extends JpaRepository<OrderEntity, Long> {
|
public interface OrderJpaRepository extends JpaRepository<OrderEntity, Long> {
|
||||||
|
|
||||||
@ -17,4 +21,14 @@ public interface OrderJpaRepository extends JpaRepository<OrderEntity, Long> {
|
|||||||
LocalDateTime startDate,
|
LocalDateTime startDate,
|
||||||
LocalDateTime endDate
|
LocalDateTime endDate
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//기간 조회시, 메뉴명을 조회하기 위해서 조인하는 쿼리
|
||||||
|
@Query("SELECT o, m.menuName FROM OrderEntity o LEFT JOIN MenuEntity m ON o.menuId = m.id " +
|
||||||
|
"WHERE o.storeId = :storeId AND o.orderDate BETWEEN :startDate AND :endDate")
|
||||||
|
List<Object[]> findByStoreIdAndOrderDateBetweenWithMenuName(
|
||||||
|
@Param("storeId") Long storeId,
|
||||||
|
@Param("startDate") LocalDateTime startDate,
|
||||||
|
@Param("endDate") LocalDateTime endDate
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user