From 8fd4304cb5a408afcc39186405439e98a35c0b82 Mon Sep 17 00:00:00 2001 From: toki-ai Date: Mon, 28 Oct 2024 17:15:33 +0700 Subject: [PATCH 1/6] [PBS-84][Anh Dao] fix: query by startTime and endTime --- .../PodBookingSystem/repository/OrderDetailRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java index 54950050..a744066e 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java @@ -28,12 +28,12 @@ public interface OrderDetailRepository extends JpaRepository findByEndTime(LocalDateTime endTime); - @Query(value = "SELECT od FROM OrderDetail od WHERE od.createdAt BETWEEN :startTime AND :endTime ORDER BY od.createdAt DESC") + @Query(value = "SELECT od FROM OrderDetail od WHERE od.startTime >= :startTime AND od.endTime <= :endTime ORDER BY od.updatedAt DESC") Page findAllWithTimeRange(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, Pageable pageable); - @Query(value = "SELECT od FROM OrderDetail od WHERE od.createdAt BETWEEN :startTime AND :endTime AND od.building.id = :buildingNumber ORDER BY od.createdAt DESC") + @Query(value = "SELECT od FROM OrderDetail od WHERE od.startTime >= :startTime AND od.endTime <= :endTime AND od.building.id = :buildingNumber ORDER BY od.updatedAt DESC") Page findOrdersByBuildingNumberAndTimeRange( @Param("buildingNumber") int buildingNumber, @Param("startTime") LocalDateTime startTime, From 2d968213bfe9a43d8c6f46e6d840b13f7ad9d672 Mon Sep 17 00:00:00 2001 From: toki-ai Date: Mon, 28 Oct 2024 17:20:30 +0700 Subject: [PATCH 2/6] [PBS-84][Anh Dao] fix: update balance if cancel order detail menity --- .../service/OrderDetailAmenityService.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java index effcfb4b..da36ab9a 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java @@ -5,6 +5,7 @@ import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityUpdateReq; import com.swp.PodBookingSystem.dto.respone.Amenity.AmenityManagementResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse; +import com.swp.PodBookingSystem.entity.Account; import com.swp.PodBookingSystem.entity.Amenity; import com.swp.PodBookingSystem.entity.OrderDetail; import com.swp.PodBookingSystem.entity.OrderDetailAmenity; @@ -12,6 +13,7 @@ import com.swp.PodBookingSystem.enums.OrderDetailAmenityStatus; import com.swp.PodBookingSystem.mapper.AmenityMapper; import com.swp.PodBookingSystem.mapper.OrderDetailAmenityMapper; +import com.swp.PodBookingSystem.repository.AccountRepository; import com.swp.PodBookingSystem.repository.AmenityRepository; import com.swp.PodBookingSystem.repository.OrderDetailAmenityRepository; import com.swp.PodBookingSystem.repository.OrderDetailRepository; @@ -36,6 +38,7 @@ public class OrderDetailAmenityService { OrderDetailRepository orderDetailRepository; OrderDetailAmenityMapper orderDetailAmenityMapper; AmenityMapper amenityMapper; + private final AccountRepository accountRepository; //GET: public List getOrderDetailAmenitiesByOrderDetailId(String orderDetailId) { @@ -125,6 +128,14 @@ public void updateOrderDetailAmenityStatus(OrderDetailAmenityUpdateReq request) throw new RuntimeException("Order detail amenity not found"); } OrderDetailAmenity updateOrderDetailAmenity = orderDetailAmenity.get(); + OrderDetail od = updateOrderDetailAmenity.getOrderDetail(); + od.setUpdatedAt(LocalDateTime.now()); + orderDetailRepository.save(od); + if(request.getStatus() == OrderDetailAmenityStatus.Canceled){ + Account customer = od.getCustomer(); + customer.setBalance(customer.getBalance() + updateOrderDetailAmenity.getPrice() * updateOrderDetailAmenity.getQuantity()); + accountRepository.save(customer); + } updateOrderDetailAmenity.setStatus(request.getStatus()); orderDetailAmenityRepository.save(updateOrderDetailAmenity); } From 33e7cee17272b37ff137cb89ec94d9205083100b Mon Sep 17 00:00:00 2001 From: toki-ai Date: Mon, 28 Oct 2024 17:29:01 +0700 Subject: [PATCH 3/6] [PBS-84][Anh Dao] fix: only get order detail exists order detail amenity --- .../repository/OrderDetailRepository.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java index a744066e..a86ae961 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java @@ -28,17 +28,39 @@ public interface OrderDetailRepository extends JpaRepository findByEndTime(LocalDateTime endTime); - @Query(value = "SELECT od FROM OrderDetail od WHERE od.startTime >= :startTime AND od.endTime <= :endTime ORDER BY od.updatedAt DESC") - Page findAllWithTimeRange(@Param("startTime") LocalDateTime startTime, - @Param("endTime") LocalDateTime endTime, - Pageable pageable); - - @Query(value = "SELECT od FROM OrderDetail od WHERE od.startTime >= :startTime AND od.endTime <= :endTime AND od.building.id = :buildingNumber ORDER BY od.updatedAt DESC") + @Query(""" + SELECT od FROM OrderDetail od + WHERE od.startTime >= :startTime + AND od.endTime <= :endTime + AND EXISTS ( + SELECT 1 FROM OrderDetailAmenity oda + WHERE oda.orderDetail.id = od.id + ) + ORDER BY od.updatedAt DESC +""") + Page findAllWithTimeRange( + @Param("startTime") LocalDateTime startTime, + @Param("endTime") LocalDateTime endTime, + Pageable pageable + ); + + @Query(""" + SELECT od FROM OrderDetail od + WHERE od.startTime >= :startTime + AND od.endTime <= :endTime + AND od.building.id = :buildingNumber + AND EXISTS ( + SELECT 1 FROM OrderDetailAmenity oda + WHERE oda.orderDetail.id = od.id + ) + ORDER BY od.updatedAt DESC +""") Page findOrdersByBuildingNumberAndTimeRange( @Param("buildingNumber") int buildingNumber, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, - Pageable pageable); + Pageable pageable + ); Page findAll(Pageable pageable); From 874dc5d00c3960d0af0ddc19315f1500f7dfc198 Mon Sep 17 00:00:00 2001 From: toki-ai Date: Mon, 28 Oct 2024 19:44:18 +0700 Subject: [PATCH 4/6] [PBS-84][Anh Dao] refactor: add status into query get page order --- .../controller/OrderController.java | 6 ++- .../repository/OrderRepository.java | 40 ++++++++++++++----- .../service/OrderService.java | 7 ++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java index 4a7c6375..347e34b1 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java @@ -8,6 +8,7 @@ import com.swp.PodBookingSystem.dto.respone.OrderResponse; import com.swp.PodBookingSystem.dto.respone.PaginationResponse; import com.swp.PodBookingSystem.entity.*; +import com.swp.PodBookingSystem.enums.OrderStatus; import com.swp.PodBookingSystem.service.*; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; @@ -48,14 +49,15 @@ public ApiResponse>> getOrdersB @RequestParam String startDate, @RequestParam String endDate, @RequestParam(defaultValue = "0") int page, - @RequestParam(defaultValue = "10") int size) { + @RequestParam(defaultValue = "10") int size, + @RequestParam(required = false) OrderStatus status) { String accountId = accountService.extractAccountIdFromToken(token); Account user = accountService.getAccountById(accountId); LocalDateTime startDateTime = orderService.parseDateTime(startDate); LocalDateTime endDateTime = orderService.parseDateTime(endDate); return ApiResponse.>>builder() - .data(orderService.getOrdersByRole(page, size, startDateTime, endDateTime, user)) + .data(orderService.getOrdersByRole(page, size, startDateTime, endDateTime, user, status)) .message("get paging order successfully") .build(); } diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java index 8b5343a2..897ec06d 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java @@ -1,6 +1,7 @@ package com.swp.PodBookingSystem.repository; import com.swp.PodBookingSystem.entity.Order; +import com.swp.PodBookingSystem.enums.OrderStatus; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; @@ -18,16 +19,37 @@ public interface OrderRepository extends JpaRepository { Page findAll(Pageable pageable); - @Query(value = "SELECT DISTINCT o FROM Order o JOIN OrderDetail od ON o.id = od.order.id WHERE o.createdAt >= :startTime AND o.createdAt <= :endTime ORDER BY o.createdAt DESC") - Page findAllWithTimeRange(@Param("startTime") LocalDateTime startTime, - @Param("endTime") LocalDateTime endTime, - Pageable pageable); + @Query(""" + SELECT DISTINCT o FROM Order o + JOIN OrderDetail od ON o.id = od.order.id + WHERE o.createdAt >= :startTime + AND o.createdAt <= :endTime + AND (:status IS NULL OR od.status = :status) + AND od.id IS NOT NULL + ORDER BY o.createdAt DESC + """) + Page findAllWithTimeRange( + @Param("startTime") LocalDateTime startTime, + @Param("endTime") LocalDateTime endTime, + @Param("status") OrderStatus status, + Pageable pageable); + ; - @Query(value = "SELECT DISTINCT o FROM Order o JOIN OrderDetail od ON o.id = od.order.id WHERE od.building.id = :buildingNumber AND o.createdAt >= :startTime AND o.createdAt <= :endTime ORDER BY o.createdAt DESC") - Page findOrdersByBuildingNumberAndTimeRange(@Param("buildingNumber") int buildingNumber, - @Param("startTime") LocalDateTime startTime, - @Param("endTime") LocalDateTime endTime, - Pageable pageable); + @Query(""" + SELECT DISTINCT o FROM Order o + JOIN OrderDetail od ON o.id = od.order.id + WHERE od.building.id = :buildingNumber + AND o.createdAt >= :startTime + AND o.createdAt <= :endTime + AND (:status IS NULL OR od.status = :status) + ORDER BY o.createdAt DESC +""") + Page findOrdersByBuildingNumberAndTimeRange( + @Param("buildingNumber") int buildingNumber, + @Param("startTime") LocalDateTime startTime, + @Param("endTime") LocalDateTime endTime, + @Param("status") OrderStatus status, + Pageable pageable); @Modifying @Transactional diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java index aeaff2b7..4c5ac19b 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java @@ -7,6 +7,7 @@ import com.swp.PodBookingSystem.dto.respone.PaginationResponse; import com.swp.PodBookingSystem.entity.*; import com.swp.PodBookingSystem.enums.AccountRole; +import com.swp.PodBookingSystem.enums.OrderStatus; import com.swp.PodBookingSystem.mapper.OrderMapper; import com.swp.PodBookingSystem.repository.OrderRepository; import org.slf4j.Logger; @@ -51,15 +52,15 @@ public List getOrdersByAccountId(String accountId) { } public PaginationResponse> getOrdersByRole( - int page, int size, LocalDateTime startDate, LocalDateTime endDate, Account user) { + int page, int size, LocalDateTime startDate, LocalDateTime endDate, Account user, OrderStatus status) { Page ordersPage; if (user.getRole().equals(AccountRole.Admin)) { ordersPage = orderRepository.findAllWithTimeRange( - startDate, endDate, PageRequest.of(page, size)); + startDate, endDate, status, PageRequest.of(page, size)); } else if (user.getRole().equals(AccountRole.Staff) || user.getRole().equals(AccountRole.Manager)) { int buildingNumber = user.getBuildingNumber(); ordersPage = orderRepository.findOrdersByBuildingNumberAndTimeRange( - buildingNumber, startDate, endDate, PageRequest.of(page, size)); + buildingNumber, startDate, endDate, status, PageRequest.of(page, size)); } else { throw new IllegalArgumentException("User role not authorized to access orders."); } From 2a4422ac194e1203ef71b5fbce97dc3e991ff7a0 Mon Sep 17 00:00:00 2001 From: toki-ai Date: Mon, 28 Oct 2024 20:07:05 +0700 Subject: [PATCH 5/6] [PBS-84][Anh Dao] fix: update balance and order detail amenity when update order --- .../service/OrderDetailService.java | 24 +++++++++++++++++++ .../service/OrderService.java | 1 - 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index e0823860..f20b2c06 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -3,6 +3,7 @@ import com.swp.PodBookingSystem.dto.request.Order.OrderUpdateRequest; import com.swp.PodBookingSystem.dto.request.OrderDetail.OrderDetailCreationRequest; import com.swp.PodBookingSystem.dto.request.OrderDetail.OrderDetailUpdateRoomRequest; +import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityUpdateReq; import com.swp.PodBookingSystem.dto.request.Room.RoomWithAmenitiesDTO; import com.swp.PodBookingSystem.dto.respone.Amenity.AmenityManagementResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetail.*; @@ -50,6 +51,7 @@ public class OrderDetailService { private final ServicePackageService servicePackageService; private final RoomService roomService; private final RoomRepository roomRepository; + private final OrderDetailAmenityRepository orderDetailAmenityRepository; //GET: public List getAllOrders() { @@ -337,6 +339,28 @@ public void updateOrderDetail(OrderUpdateRequest request) { for (OrderDetail od : orderDetails) { if (request.getStatus() != null) { od.setStatus(request.getStatus()); + if(request.getStatus().equals(OrderStatus.Rejected)){ + double total = 0; + int countService = 0; + if(od.getServicePackage().getId() == 1){ + countService = 4; + } else if(od.getServicePackage().getId() == 2){ + countService = 30; + } else { + countService = 1; + } + total += od.getPriceRoom() * (100- od.getDiscountPercentage()) * countService/ 100; + List listOda = orderDetailAmenityRepository.findByOrderDetailId(od.getId()); + for(OrderDetailAmenity oda : listOda){ + total += oda.getPrice() * oda.getQuantity() * (100- od.getDiscountPercentage()) * countService/ 100; + orderDetailAmenityService.updateOrderDetailAmenityStatus( new OrderDetailAmenityUpdateReq(oda.getId(), OrderDetailAmenityStatus.Canceled)); + } + Account customer = od.getCustomer(); + if(customer != null){ + customer.setBalance(customer.getBalance() + total); + accountRepository.save(customer); + } + } } if (request.getOrderHandler() != null) { Account orderHandler = accountService.getAccountById(request.getOrderHandler().getId()); diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java index 4c5ac19b..c49af589 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java @@ -93,7 +93,6 @@ public Order createOrderByRequest(Account account) { public OrderResponse updateOrder(OrderUpdateRequest request) { Order existingOrder = orderRepository.findById(request.getId()).orElseThrow(() -> new RuntimeException("Order not found with id: " + request.getId())); orderDetailService.updateOrderDetail(request); - updateOrderUpdateAt(request.getId()); return OrderResponse.builder() .id(existingOrder.getId()) .accountId(existingOrder.getAccount().getId()) From 30e435482a97a2253e99c742d792e9b96845ff66 Mon Sep 17 00:00:00 2001 From: toki-ai Date: Mon, 28 Oct 2024 21:15:06 +0700 Subject: [PATCH 6/6] [PBS-84][Anh Dao] refactor: format order id --- .../controller/OrderController.java | 2 +- .../swp/PodBookingSystem/entity/Order.java | 1 - .../service/OrderService.java | 30 +++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java index 347e34b1..6be97923 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java @@ -89,7 +89,7 @@ public ApiResponse createOrderByRequest( try { String accountId = accountService.extractAccountIdFromToken(token); Account account = accountService.getAccountById(accountId); - Order orderCreated = orderService.createOrderByRequest(account); + Order orderCreated = orderService.createOrderByRequest(account, request); boolean isSomeRoomWasBook = orderDetailService.processOrderDetails(request, orderCreated, account); String status = isSomeRoomWasBook ? "Pending" : "Successfully"; diff --git a/src/main/java/com/swp/PodBookingSystem/entity/Order.java b/src/main/java/com/swp/PodBookingSystem/entity/Order.java index 405871a8..bd8793ad 100644 --- a/src/main/java/com/swp/PodBookingSystem/entity/Order.java +++ b/src/main/java/com/swp/PodBookingSystem/entity/Order.java @@ -17,7 +17,6 @@ @Table(name = "`order`") public class Order { @Id - @GeneratedValue(strategy = GenerationType.UUID) String id; @ManyToOne(fetch = FetchType.LAZY) diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java index c49af589..7ed378a0 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java @@ -1,6 +1,7 @@ package com.swp.PodBookingSystem.service; import com.swp.PodBookingSystem.dto.request.Order.OrderUpdateRequest; import com.swp.PodBookingSystem.dto.request.Order.OrderUpdateStaffRequest; +import com.swp.PodBookingSystem.dto.request.OrderDetail.OrderDetailCreationRequest; import com.swp.PodBookingSystem.dto.respone.Order.OrderManagementResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailManagementResponse; import com.swp.PodBookingSystem.dto.respone.OrderResponse; @@ -16,10 +17,14 @@ import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; + +import java.text.Normalizer; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; +import java.util.Locale; import java.util.UUID; +import java.util.regex.Pattern; import java.util.stream.Collectors; @Service @@ -74,14 +79,14 @@ public PaginationResponse> searchOrdersByKeyword(i } //CREATE: - public Order createOrderByRequest(Account account) { + public Order createOrderByRequest(Account account, OrderDetailCreationRequest request) { try { Order order = new Order(); order.setAccount(account); - order.setId(UUID.randomUUID().toString()); + order.setId(renderOrderID(request)); order.setCreatedAt(LocalDateTime.now()); order.setUpdatedAt(LocalDateTime.now()); - order = orderRepository.save(order); + orderRepository.save(order); return order; } catch (Exception e) { log.error("Error creating order: ", e); @@ -153,6 +158,25 @@ public LocalDateTime parseDateTime(String dateTime) { return LocalDateTime.parse(dateTime, formatter); } + public static String removeDiacritics(String input) { + String normalized = Normalizer.normalize(input, Normalizer.Form.NFD); + Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+"); + return pattern.matcher(normalized).replaceAll("").replaceAll("[^\\p{L}]", ""); + } + + public String renderOrderID(OrderDetailCreationRequest request) { + String customerName = removeDiacritics(request.getCustomer().getName()); + String roomNames = request.getSelectedRooms() + .stream() + .map(room -> room.getName().replace(" ", "")) + .collect(Collectors.joining("-")); + String uuid = UUID.randomUUID().toString(); + return "OD-" + roomNames.toLowerCase() + "-CUS-" + customerName.replace(" ","").toString().substring(0,3).toLowerCase() + "-D-" + + request.getStartTime().getFirst().getDayOfMonth() + "-" + + request.getStartTime().getFirst().getMonthValue() + "-" + + uuid.substring(0, 6); + } + /* [GET]: /order/number-order-current-day */