Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class PopularMenuDto {
private Long menuId;
private String menuName;
private Long soldCount;
private String imageUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.nowait.common.enums.Role;
import com.nowait.domainadminrdb.statistic.exception.StatisticViewUnauthorizedException;
import com.nowait.domaincorerdb.menu.entity.Menu;
import com.nowait.domaincorerdb.menu.entity.MenuImage;
import com.nowait.domaincorerdb.menu.repository.MenuImageRepository;
import com.nowait.domaincorerdb.menu.repository.MenuRepository;
import com.nowait.domaincorerdb.user.entity.MemberDetails;
import com.nowait.domaincorerdb.user.entity.User;
Expand All @@ -29,15 +31,16 @@ public class PopularMenuRedisServiceImpl implements PopularMenuRedisService {
private final MenuCounterService menuCounterService;
private final MenuRepository menuRepository;
private final UserRepository userRepository;
private final MenuImageRepository menuImageRepository;

@Override
public List<PopularMenuDto> getTodayTop5(MemberDetails memberDetails) {

User user = userRepository.findById(memberDetails.getId()).orElseThrow(UserNotFoundException::new);
Long storeId = user.getStoreId();

if (!Role.SUPER_ADMIN.equals(user.getRole()) && !user.getStoreId().equals(storeId)) {
throw new StatisticViewUnauthorizedException();
if (!Role.SUPER_ADMIN.equals(user.getRole())) {
if (storeId == null) throw new StatisticViewUnauthorizedException();
}

Set<ZSetOperations.TypedTuple<String>> tuples = menuCounterService.getTopMenus(storeId, 5);
Expand All @@ -53,13 +56,22 @@ public List<PopularMenuDto> getTodayTop5(MemberDetails memberDetails) {
Menu::getName
));

var firstImageByMenu = menuImageRepository.findByMenuIdInOrderByIdAsc(menuIds).stream()
.collect(Collectors.toMap(
menuImage -> menuImage.getMenu().getId(),
MenuImage::getImageUrl,
(existing, replacement) -> existing
));


return tuples.stream()
.map(tuple -> {
Long menuId = Long.parseLong(tuple.getValue());
String menuName = menuIdToNameMap.getOrDefault(menuId, "Unknown Menu");
String imageUrl = firstImageByMenu.get(menuId);
long sold = tuple.getScore().longValue();

return new PopularMenuDto(menuId, menuName, tuple.getScore().longValue());
return new PopularMenuDto(menuId, menuName, sold, imageUrl);
})
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nowait.domainadminrdb.statistic.repository;

import static com.nowait.domaincorerdb.order.entity.OrderStatus.*;
import static com.nowait.domaincorerdb.store.entity.QStore.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -50,6 +49,8 @@ public OrderSalesSumDetail findSalesSumByStoreId(Long storeId, LocalDate date) {
LocalDateTime start = date.atStartOfDay(); // 해당 날짜의 자정
LocalDateTime end = date.plusDays(1).atStartOfDay();

LocalDateTime yesterDayStart = start.minusDays(1); // 어제 00:00

// 2) target 날짜 해당하는 매출 합산
Integer targetSum = queryFactory
.select(u.totalPrice.sum())
Expand All @@ -68,8 +69,8 @@ public OrderSalesSumDetail findSalesSumByStoreId(Long storeId, LocalDate date) {
.from(u)
.where(
u.store.storeId.eq(storeId),
u.createdAt.goe(start),
u.createdAt.lt(start.minusDays(1)),
u.createdAt.goe(yesterDayStart),
u.createdAt.lt(start),
u.status.eq(COOKED)
)
.fetchOne();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
public interface MenuImageRepository extends JpaRepository<MenuImage, Long> {
List<MenuImage> findByMenu(Menu menu);
Optional<MenuImage> findByMenuId(Long menuId);
List<MenuImage> findByMenuIdInOrderByIdAsc(List<Long> menuIds);
}