diff --git a/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java index 4999ee6d..28045f45 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface AssignmentRepository extends JpaRepository { @@ -22,9 +23,11 @@ public interface AssignmentRepository extends JpaRepository "WHERE a.slot = :slot " + "AND a.weekDate = :weekDate " + "AND ac.buildingNumber = :buildingId") - String findStaffForMatchingOrder(@Param("slot") String slot, - @Param("weekDate") String weekDate, - @Param("buildingId") Integer buildingId); + List findStaffIdsForMatchingOrder(@Param("slot") String slot, + @Param("weekDate") String weekDate, + @Param("buildingId") Integer buildingId); + + @Query("SELECT a FROM Assignment a " + diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java index ba594640..327e7a24 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java @@ -88,7 +88,7 @@ Page findOrdersByBuildingNumberAndTimeRange( // integer countByBuildingId(@Param("buildingId") Integer buildingId); @Query("SELECT SUM((od.priceRoom + COALESCE(amenityTotal.totalAmenityPrice, 0)) * " + - "(1 - COALESCE(od.discountPercentage, 0) / 100.0) * (1 - COALESCE(sp.discountPercentage, 0) / 100.0)) as grandTotal " + + "(1 - COALESCE(od.discountPercentage, 0) / 100.0)) as grandTotal " + "FROM OrderDetail od " + "LEFT JOIN od.servicePackage sp " + "LEFT JOIN (SELECT oda.orderDetail.id as orderDetailId, SUM(oda.price * oda.quantity) as totalAmenityPrice " + @@ -99,7 +99,7 @@ Page findOrdersByBuildingNumberAndTimeRange( Double calculateRevenueCurrentDay(); @Query("SELECT SUM((od.priceRoom + COALESCE(amenityTotal.totalAmenityPrice, 0)) * " + - "(1 - COALESCE(od.discountPercentage, 0) / 100.0) * (1 - COALESCE(sp.discountPercentage, 0) / 100.0)) as grandTotal " + + "(1 - COALESCE(od.discountPercentage, 0) / 100.0)) as grandTotal " + "FROM OrderDetail od " + "LEFT JOIN od.servicePackage sp " + "LEFT JOIN (SELECT oda.orderDetail.id as orderDetailId, SUM(oda.price * oda.quantity) as totalAmenityPrice " + @@ -113,7 +113,7 @@ Optional calculateRevenueBetweenDateTime(@Param("startTime") LocalDateTi @Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueChartDto(" + "CONCAT(YEAR(:startTime), '-', LPAD(CAST(MONTH(:startTime) AS string), 2, '0'), '-', LPAD(CAST(DAY(:startTime) AS string), 2, '0')), " + "SUM((od.priceRoom + COALESCE(amenityTotal.totalAmenityPrice, 0)) * " + - "(1 - COALESCE(od.discountPercentage, 0) / 100.0) * (1 - COALESCE(sp.discountPercentage, 0) / 100.0))) " + + "(1 - COALESCE(od.discountPercentage, 0) / 100.0))) " + "FROM OrderDetail od " + "LEFT JOIN od.servicePackage sp " + "LEFT JOIN (SELECT oda.orderDetail.id as orderDetailId, SUM(oda.price * oda.quantity) as totalAmenityPrice " + @@ -126,7 +126,7 @@ Optional calculateRevenueBetweenDateTime(@Param("startTime") LocalDateTi @Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueChartDto(" + "CONCAT(YEAR(od.startTime), '-', LPAD(CAST(MONTH(od.startTime) AS string), 2, '0'), '-', LPAD(CAST(DAY(od.startTime) AS string), 2, '0')), " + "SUM((od.priceRoom + COALESCE(amenityTotal.totalAmenityPrice, 0)) * " + - "(1 - COALESCE(od.discountPercentage, 0) / 100.0) * (1 - COALESCE(sp.discountPercentage, 0) / 100.0))) " + + "(1 - COALESCE(od.discountPercentage, 0) / 100.0))) " + "FROM OrderDetail od " + "LEFT JOIN od.servicePackage sp " + "LEFT JOIN (SELECT oda.orderDetail.id as orderDetailId, SUM(oda.price * oda.quantity) as totalAmenityPrice " + @@ -142,8 +142,8 @@ Optional calculateRevenueBetweenDateTime(@Param("startTime") LocalDateTi @Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueChartDto(" + "CONCAT(YEAR(MIN(od.startTime)), '-', LPAD(CAST(MONTH(MIN(od.startTime)) AS string), 2, '0'), '-01'), " + "SUM((od.priceRoom + COALESCE(amenityTotal.totalAmenityPrice, 0)) * " + - "(1 - COALESCE(od.discountPercentage, 0) / 100.0) * " + - "(1 - COALESCE(sp.discountPercentage, 0) / 100.0))) " + + "(1 - COALESCE(od.discountPercentage, 0) / 100.0) " + + ")) " + "FROM OrderDetail od " + "LEFT JOIN od.servicePackage sp " + "LEFT JOIN (SELECT oda.orderDetail.id as orderDetailId, SUM(oda.price * oda.quantity) as totalAmenityPrice " + @@ -172,7 +172,7 @@ Optional calculateRevenueBetweenDateTime(@Param("startTime") LocalDateTi "AND TIME(od.startTime) BETWEEN :slotStartTime AND :slotEndTime " + "AND od.building.id = :buildingNumber " + "AND od.startTime > CURRENT_TIMESTAMP " + - "AND (od.orderHandler.id != :staffId OR od.orderHandler IS NULL)") + "AND od.orderHandler IS NULL") void assignOrdersToStaff(@Param("staffId") String staffId, @Param("weekDay") int weekDay, @Param("slotStartTime") String slotStartTime, diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index 2c7b05c8..a583b419 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -364,13 +364,10 @@ public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order o DayOfWeek dayOfWeek = startTime.getDayOfWeek(); String weekDate = getDayLabel(dayOfWeek); - Account orderHandler; - String orderHandlerId = assignmentRepository.findStaffForMatchingOrder(slot, weekDate, building.getId()); - if (orderHandlerId == null){ - orderHandler = null; - } else { - orderHandler = accountRepository.getById(orderHandlerId); - } + List staffIds = assignmentRepository.findStaffIdsForMatchingOrder(slot, weekDate, building.getId()); + String orderHandlerId = staffIds.isEmpty() ? null : staffIds.get(0); + + Account orderHandler = (orderHandlerId == null) ? null : accountRepository.getById(orderHandlerId); OrderDetail response = new OrderDetail(); response.setCustomer(account);