public List<StoreCheckDailyResponseDto> checkDailyMyStore(AuthUser authUser) {
checkAdmin(authUser);
User user = findUser(authUser.getUserId());
List<Store> storeList = storeRepository.findByUserAndStoreStatus(user, StoreStatus.OPEN);
List<StoreCheckDailyResponseDto> responseList = new ArrayList<>();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate currentDate = LocalDate.now(); // 현재 날짜
OrderStatus orderStatus = OrderStatus.DELIVERY_COMPLETED; // 주문 상태 설정
for (Store store : storeList) {
LocalDateTime createTime = store.getCreatedAt(); // 매장 생성 시간
LocalDate startDate = createTime.toLocalDate(); // LocalDate로 변환
// 하루 단위로 고객 수와 매출을 집계
while (!startDate.isAfter(currentDate)) {
// 하루 단위로 끝나는 시간 설정
LocalDateTime startDateTime = startDate.atStartOfDay(); // LocalDate를 LocalDateTime으로 변환
LocalDateTime nextDayTime = startDate.plusDays(1).atStartOfDay(); // 다음 날의 시작 시간
// 고객 수와 매출 조회
Object[] result = orderRepository.countCustomersAndSales(store.getId(), startDateTime, nextDayTime, orderStatus);
Long dailyCustomers = 0L; // 기본값
Long dailySales = 0L; // 기본값
// 배열의 길이를 확인
if (result != null && result.length >= 2) {
if (result[0] instanceof Number) {
dailyCustomers = ((Number) result[0]).longValue();
}
if (result[1] instanceof Number) {
dailySales = ((Number) result[1]).longValue();
}
}
// DTO 생성 및 추가
StoreCheckDailyResponseDto dto = new StoreCheckDailyResponseDto(
store.getId(),
startDate.format(dateFormatter),
dailyCustomers,
dailySales
);
responseList.add(dto);
// 다음 날로 이동
startDate = startDate.plusDays(1); // LocalDate로 이동
}
}
return responseList;
}
public List<StoreCheckMonthlyResponseDto> checkMonthlyMyStore(AuthUser authUser) {
checkAdmin(authUser);
User user = findUser(authUser.getUserId());
List<Store> storeList = storeRepository.findByUserAndStoreStatus(user, StoreStatus.OPEN);
List<StoreCheckMonthlyResponseDto> responseList = new ArrayList<>();
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
LocalDateTime currentTime = LocalDateTime.now();
OrderStatus orderStatus = OrderStatus.DELIVERY_COMPLETED;
for (Store store : storeList) {
LocalDateTime createTime = store.getCreatedAt();
LocalDateTime startDate = createTime.withDayOfMonth(1); // 매장 생성 월의 첫 날
LocalDateTime endDate = currentTime.withDayOfMonth(1); // 현재 월의 첫 날
while (startDate.isBefore(endDate) || startDate.isEqual(endDate)) {
// 월별 고객 수와 매출 조회
Object[] result = orderRepository.countMonthlyCustomersAndSales(store.getId(), startDate, orderStatus);
Long monthlyCustomers = 0L; // 기본값
Long monthlySales = 0L; // 기본값
// 배열의 길이를 확인
if (result != null && result.length >= 2) {
if (result[0] instanceof Number) {
monthlyCustomers = ((Number) result[0]).longValue();
}
if (result[1] instanceof Number) {
monthlySales = ((Number) result[1]).longValue();
}
}
// DTO 생성 및 추가
StoreCheckMonthlyResponseDto dto = new StoreCheckMonthlyResponseDto(
store.getId(),
startDate.format(monthFormatter),
monthlyCustomers,
monthlySales
);
responseList.add(dto);
// 다음 달로 이동
startDate = startDate.plusMonths(1);
}
}
return responseList;
}
@Query("SELECT COUNT(DISTINCT o.user.id), SUM(o.price) " +
"FROM Order o WHERE o.store.id = :storeId AND o.createdAt BETWEEN :startDate AND :endDate " +
"AND o.orderStatus = :orderStatus")
Object[] countCustomersAndSales(@Param("storeId") Long storeId,
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate,
@Param("orderStatus") OrderStatus orderStatus);
@Query("SELECT COUNT(DISTINCT o.user.id), SUM(o.price) " +
"FROM Order o WHERE o.store.id = :storeId AND " +
"FUNCTION('DATE_FORMAT', o.createdAt, '%Y-%m') = FUNCTION('DATE_FORMAT', :date, '%Y-%m') AND " +
"o.orderStatus = :orderStatus")
Object[] countMonthlyCustomersAndSales(@Param("storeId") Long storeId,
@Param("date") LocalDateTime date,
@Param("orderStatus") OrderStatus orderStatus);