diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java index 6be97923..4298e8d5 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java @@ -5,6 +5,7 @@ import com.swp.PodBookingSystem.dto.request.OrderDetail.OrderDetailCreationRequest; import com.swp.PodBookingSystem.dto.respone.ApiResponse; import com.swp.PodBookingSystem.dto.respone.Order.OrderManagementResponse; +import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailResponse; import com.swp.PodBookingSystem.dto.respone.OrderResponse; import com.swp.PodBookingSystem.dto.respone.PaginationResponse; import com.swp.PodBookingSystem.entity.*; @@ -43,6 +44,15 @@ public ApiResponse> getAllOrders() { .build(); } + @GetMapping("/order-info/{orderId}") + public ApiResponse getInfoOrder(@PathVariable String orderId) { + return ApiResponse.builder() + .message("Lấy thông tin đơn hàng thành công") + .data(orderService.getInfoOrder(orderId)) + .build(); + } + + @GetMapping("/page") public ApiResponse>> getOrdersByRole( @RequestHeader("Authorization") String token, @@ -71,14 +81,11 @@ public ApiResponse>> searchOrde .build(); } -// @GetMapping("/{accountId}") -// public ApiResponse> getOrdersByAccountId(@PathVariable String accountId) { -// List orders = orderService.getOrdersByAccountId(accountId); -// logOrders(orders); -// return ApiResponse.>builder() -// .data(orders) -// .build(); -// } + @GetMapping("/{accountId}") + public PaginationResponse> getOrdersByAccountId(@PathVariable String accountId, @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "5") int take, @RequestParam(defaultValue = "Successfully") String status) { + return orderService.getOrdersByAccountCustomerId(page, take, accountId, status); + } //Check room available -> yes: create order Status: Successfully // -> no: create order Status: Pending @@ -115,7 +122,7 @@ ApiResponse updateOrder(@RequestBody OrderUpdateRequest request) orderService.updateOrderUpdateAt(request.getId()); return ApiResponse.builder() .data(orderService.updateOrder(request)) - .message("Update order successfully") + .message("Cập nhật hóa đơn thành công") .build(); } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailManagementResponse.java b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailManagementResponse.java index f7cfd408..b9817176 100644 --- a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailManagementResponse.java +++ b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailManagementResponse.java @@ -17,6 +17,8 @@ public class OrderDetailManagementResponse { private String id; private int roomId; private String roomName; + private String roomImage; + private String roomTypeName; private double roomPrice; private String status; private LocalDateTime startTime; diff --git a/src/main/java/com/swp/PodBookingSystem/dto/respone/PaginationResponse.java b/src/main/java/com/swp/PodBookingSystem/dto/respone/PaginationResponse.java index ba016b01..bcc66aea 100644 --- a/src/main/java/com/swp/PodBookingSystem/dto/respone/PaginationResponse.java +++ b/src/main/java/com/swp/PodBookingSystem/dto/respone/PaginationResponse.java @@ -12,11 +12,11 @@ @JsonInclude(JsonInclude.Include.NON_NULL) public class PaginationResponse { @Builder.Default - private int code = 200; - private String message; - private T data; - private int currentPage; - private int totalPage; - private int recordPerPage; - private int totalRecord; + int code = 200; + String message; + T data; + int currentPage; + int totalPage; + int recordPerPage; + int totalRecord; } diff --git a/src/main/java/com/swp/PodBookingSystem/entity/OrderDetail.java b/src/main/java/com/swp/PodBookingSystem/entity/OrderDetail.java index 9f10f8db..99954a4b 100644 --- a/src/main/java/com/swp/PodBookingSystem/entity/OrderDetail.java +++ b/src/main/java/com/swp/PodBookingSystem/entity/OrderDetail.java @@ -69,6 +69,9 @@ public class OrderDetail { @Temporal(TemporalType.TIMESTAMP) LocalDateTime updatedAt; + @Column(name = "cancelReason") + String cancelReason; + @PrePersist protected void onCreate() { this.createdAt = LocalDateTime.now(); diff --git a/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java b/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java index 9126d648..15675716 100644 --- a/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java +++ b/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java @@ -23,6 +23,7 @@ public enum ErrorCode { INVALID_TOKEN(401, "Token không đúng", HttpStatus.UNAUTHORIZED, "accessToken"), ORDER_DETAIL_NOT_EXIST(404, "Order detail không tồn tại", HttpStatus.NOT_FOUND, "orderDetailId"), ACCOUNT_NOT_ACTIVE(403, "Tài khoản đã bị cấm", HttpStatus.FORBIDDEN, "system"), + ORDER_NOT_FOUND(404, "Order không tồn tại", HttpStatus.NOT_FOUND, "orderId"), ; ErrorCode(int code, String message, HttpStatusCode statusCode, String field) { diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java index a86ae961..308eb00d 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java @@ -26,6 +26,12 @@ public interface OrderDetailRepository extends JpaRepository findByOrderId(String orderId); + @Query("SELECT od FROM OrderDetail od " + + "WHERE (od.order.id = :orderId)" + + "AND (od.status = :status)" + + "ORDER BY od.startTime DESC") + List findByOrderIdAndStatus(@Param("orderId") String orderId, @Param("status") OrderStatus status); + List findByEndTime(LocalDateTime endTime); @Query(""" diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java index 897ec06d..8da3c831 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderRepository.java @@ -10,40 +10,52 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; + import java.time.LocalDateTime; import java.util.List; @Repository -public interface OrderRepository extends JpaRepository { +public interface OrderRepository extends JpaRepository { List findByAccountId(String accountId); + @Query(""" + SELECT DISTINCT o FROM Order o + JOIN OrderDetail od ON o.id = od.order.id + WHERE o.account.id = :accountId + AND od.status = :status + AND od.id IS NOT NULL + ORDER BY o.createdAt DESC + """) + Page findByAccountCustomerId(@Param("accountId") String accountId, @Param("status") OrderStatus status, Pageable pageable); + Page findAll(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 - """) + 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(""" - 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 -""") + 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, @@ -61,11 +73,11 @@ Page findOrdersByBuildingNumberAndTimeRange( void updateOrderUpdatedAt(String orderId, LocalDateTime updatedAt); @Query(""" - SELECT o FROM Order o - JOIN o.account a - WHERE LOWER(o.id) LIKE LOWER(CONCAT('%', :keyword, '%')) - OR LOWER(a.name) LIKE LOWER(CONCAT('%', :keyword, '%')) - """) + SELECT o FROM Order o + JOIN o.account a + WHERE LOWER(o.id) LIKE LOWER(CONCAT('%', :keyword, '%')) + OR LOWER(a.name) LIKE LOWER(CONCAT('%', :keyword, '%')) + """) Page searchByKeyword(String keyword, Pageable pageable); @Query("SELECT COUNT(DISTINCT o.id) FROM Order o " + @@ -80,5 +92,5 @@ OR LOWER(a.name) LIKE LOWER(CONCAT('%', :keyword, '%')) "AND :endTime >= od.startTime " + "AND od.status = com.swp.PodBookingSystem.enums.OrderStatus.Successfully") int countOrdersBetweenDatetime(@Param("startTime") LocalDateTime startTime, - @Param("endTime") LocalDateTime endTime); + @Param("endTime") LocalDateTime endTime); } diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index f20b2c06..dd513160 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -89,13 +89,41 @@ public OrderDetailFullInfoResponse getOrderDetailByOrderDetailId(String orderDet .build(); } + public List getOrderDetailByOrderId(String orderId, String status) { + return orderDetailRepository.findByOrderIdAndStatus(orderId, OrderStatus.valueOf(status)).stream().map(orderDetail -> { + List amenities = orderDetailAmenityService.getOrderDetailAmenitiesByOrderDetailId(orderDetail.getId()); + return OrderDetailManagementResponse.builder() + .id(orderDetail.getId()) + .roomId(orderDetail.getRoom().getId()) + .roomName(orderDetail.getRoom().getName()) + .roomPrice(orderDetail.getPriceRoom()) + .buildingAddress(orderDetail.getBuilding().getAddress()) + .buildingId(orderDetail.getBuilding().getId()) + .roomId(orderDetail.getRoom().getId()) + .orderHandler(Optional.ofNullable(orderDetail.getOrderHandler()) + .map(accountService::toAccountResponse) + .orElse(null)) + .customer(Optional.ofNullable(orderDetail.getCustomer()) + .map(accountService::toAccountResponse) + .orElse(null)) + .servicePackage(servicePackageService.toServicePackageResponse(orderDetail.getServicePackage())) + .status(orderDetail.getStatus().name()) + .startTime(orderDetail.getStartTime()) + .endTime(orderDetail.getEndTime()) + .amenities(amenities) + .build(); + }).collect(Collectors.toList()); + } + public List getOrderDetailById(String orderId) { return orderDetailRepository.findByOrderId(orderId).stream().map(orderDetail -> { List amenities = orderDetailAmenityService.getOrderDetailAmenitiesByOrderDetailId(orderDetail.getId()); return OrderDetailManagementResponse.builder() .id(orderDetail.getId()) .roomId(orderDetail.getRoom().getId()) + .roomImage(orderDetail.getRoom().getImage()) .roomName(orderDetail.getRoom().getName()) + .roomTypeName(orderDetail.getRoom().getRoomType().getName()) .roomPrice(orderDetail.getPriceRoom()) .buildingAddress(orderDetail.getBuilding().getAddress()) .buildingId(orderDetail.getBuilding().getId()) @@ -339,24 +367,24 @@ public void updateOrderDetail(OrderUpdateRequest request) { for (OrderDetail od : orderDetails) { if (request.getStatus() != null) { od.setStatus(request.getStatus()); - if(request.getStatus().equals(OrderStatus.Rejected)){ + if (request.getStatus().equals(OrderStatus.Rejected)) { double total = 0; int countService = 0; - if(od.getServicePackage().getId() == 1){ + if (od.getServicePackage().getId() == 1) { countService = 4; - } else if(od.getServicePackage().getId() == 2){ + } 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)); + 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){ + if (customer != null) { customer.setBalance(customer.getBalance() + total); accountRepository.save(customer); } diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java index 7ed378a0..a16ff4ee 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderService.java @@ -1,4 +1,5 @@ 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; @@ -9,6 +10,8 @@ import com.swp.PodBookingSystem.entity.*; import com.swp.PodBookingSystem.enums.AccountRole; import com.swp.PodBookingSystem.enums.OrderStatus; +import com.swp.PodBookingSystem.exception.AppException; +import com.swp.PodBookingSystem.exception.ErrorCode; import com.swp.PodBookingSystem.mapper.OrderMapper; import com.swp.PodBookingSystem.repository.OrderRepository; import org.slf4j.Logger; @@ -18,12 +21,11 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.*; 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; @@ -49,6 +51,21 @@ public List getAllOrders() { .collect(Collectors.toList()); } + public OrderManagementResponse getInfoOrder(String id) { + OrderManagementResponse order = new OrderManagementResponse(); + Optional orderOptional = orderRepository.findById(id); + if (orderOptional.isEmpty()) { + throw new AppException(ErrorCode.ORDER_NOT_FOUND); + } else { + order.setId(orderOptional.get().getId()); + order.setCreatedAt(orderOptional.get().getCreatedAt()); + order.setUpdatedAt(orderOptional.get().getUpdatedAt()); + } + List orderDetailDTOs = orderDetailService.getOrderDetailById(order.getId()); + order.setOrderDetails(orderDetailDTOs); + return order; + } + public List getOrdersByAccountId(String accountId) { List orders = orderRepository.findByAccountId(accountId); return orders.stream() @@ -56,6 +73,12 @@ public List getOrdersByAccountId(String accountId) { .collect(Collectors.toList()); } + public PaginationResponse> getOrdersByAccountCustomerId(int page, int take, String accountId, String status) { + Page ordersPage; + ordersPage = orderRepository.findByAccountCustomerId(accountId, OrderStatus.valueOf(status), PageRequest.of(page, take)); + return convertToPaginationResponse(ordersPage); + } + public PaginationResponse> getOrdersByRole( int page, int size, LocalDateTime startDate, LocalDateTime endDate, Account user, OrderStatus status) { Page ordersPage; @@ -106,7 +129,7 @@ public OrderResponse updateOrder(OrderUpdateRequest request) { .build(); } - public OrderResponse updateOrderHandlerWithOrder(String id, OrderUpdateStaffRequest request){ + public OrderResponse updateOrderHandlerWithOrder(String id, OrderUpdateStaffRequest request) { Order existingOrder = orderRepository.findById(id).orElseThrow(() -> new RuntimeException("Order not found with id: " + id)); if (request.getOrderHandler() == null) { throw new RuntimeException("Account handler cannot be null"); @@ -121,7 +144,7 @@ public OrderResponse updateOrderHandlerWithOrder(String id, OrderUpdateStaffRequ .build(); } - public void updateOrderUpdateAt(String orderId){ + public void updateOrderUpdateAt(String orderId) { orderRepository.updateOrderUpdatedAt(orderId, LocalDateTime.now()); } @@ -145,6 +168,7 @@ private PaginationResponse> convertToPaginationRes .build(); }).collect(Collectors.toList()); return PaginationResponse.>builder() + .message("Lấy danh sách hóa đơn thành công") .data(orderResponses) .currentPage(ordersPage.getNumber()) .totalPage(ordersPage.getTotalPages()) @@ -153,6 +177,27 @@ private PaginationResponse> convertToPaginationRes .build(); } + private PaginationResponse> convertToPaginationQueryResponse(Page ordersPage, String status) { + List orderResponses = ordersPage.getContent().stream().map(order -> { + List orderDetailDTOs = orderDetailService.getOrderDetailByOrderId(order.getId(), status); + return OrderManagementResponse.builder() + .id(order.getId()) + .createdAt(order.getCreatedAt()) + .updatedAt(order.getUpdatedAt()) + .orderDetails(orderDetailDTOs) + .build(); + }).filter(orderResponse -> !orderResponse.getOrderDetails().isEmpty()).collect(Collectors.toList()); + return PaginationResponse.>builder() + .message("Lấy danh sách hóa đơn thành công") + .data(orderResponses) + .currentPage(ordersPage.getNumber()) + .totalPage(ordersPage.getTotalPages()) + .recordPerPage(ordersPage.getSize()) + .totalRecord(orderResponses.size()) + .build(); + + } + public LocalDateTime parseDateTime(String dateTime) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"); return LocalDateTime.parse(dateTime, formatter); @@ -171,7 +216,7 @@ public String renderOrderID(OrderDetailCreationRequest request) { .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-" + 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); @@ -180,14 +225,14 @@ public String renderOrderID(OrderDetailCreationRequest request) { /* [GET]: /order/number-order-current-day */ - public int countCurrentlyOrder () { + public int countCurrentlyOrder() { return orderRepository.countCurrentlyOrder(); } /* [GET]: /order/number-order */ - public int countOrder (LocalDateTime startTime, LocalDateTime endTime) { + public int countOrder(LocalDateTime startTime, LocalDateTime endTime) { return orderRepository.countOrdersBetweenDatetime(startTime, endTime); } } \ No newline at end of file