Skip to content

Commit

Permalink
Merge pull request #117 from Hoang-Nguyen-Huy/feat/socket
Browse files Browse the repository at this point in the history
[PBS-85][NguyenHCP] feat: get list order of customer
  • Loading branch information
nguyenhcp2004 authored Oct 29, 2024
2 parents 25374bd + 38c94fc commit c33297a
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -43,6 +44,15 @@ public ApiResponse<List<OrderResponse>> getAllOrders() {
.build();
}

@GetMapping("/order-info/{orderId}")
public ApiResponse<OrderManagementResponse> getInfoOrder(@PathVariable String orderId) {
return ApiResponse.<OrderManagementResponse>builder()
.message("Lấy thông tin đơn hàng thành công")
.data(orderService.getInfoOrder(orderId))
.build();
}


@GetMapping("/page")
public ApiResponse<PaginationResponse<List<OrderManagementResponse>>> getOrdersByRole(
@RequestHeader("Authorization") String token,
Expand Down Expand Up @@ -71,14 +81,11 @@ public ApiResponse<PaginationResponse<List<OrderManagementResponse>>> searchOrde
.build();
}

// @GetMapping("/{accountId}")
// public ApiResponse<List<OrderResponse>> getOrdersByAccountId(@PathVariable String accountId) {
// List<OrderResponse> orders = orderService.getOrdersByAccountId(accountId);
// logOrders(orders);
// return ApiResponse.<List<OrderResponse>>builder()
// .data(orders)
// .build();
// }
@GetMapping("/{accountId}")
public PaginationResponse<List<OrderManagementResponse>> 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
Expand Down Expand Up @@ -115,7 +122,7 @@ ApiResponse<OrderResponse> updateOrder(@RequestBody OrderUpdateRequest request)
orderService.updateOrderUpdateAt(request.getId());
return ApiResponse.<OrderResponse>builder()
.data(orderService.updateOrder(request))
.message("Update order successfully")
.message("Cập nhật hóa đơn thành công")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PaginationResponse<T> {
@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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public interface OrderDetailRepository extends JpaRepository<OrderDetail, String

List<OrderDetail> findByOrderId(String orderId);

@Query("SELECT od FROM OrderDetail od " +
"WHERE (od.order.id = :orderId)" +
"AND (od.status = :status)" +
"ORDER BY od.startTime DESC")
List<OrderDetail> findByOrderIdAndStatus(@Param("orderId") String orderId, @Param("status") OrderStatus status);

List<OrderDetail> findByEndTime(LocalDateTime endTime);

@Query("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Order, String> {
public interface OrderRepository extends JpaRepository<Order, String> {
List<Order> 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<Order> findByAccountCustomerId(@Param("accountId") String accountId, @Param("status") OrderStatus status, Pageable pageable);

Page<Order> 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<Order> 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<Order> findOrdersByBuildingNumberAndTimeRange(
@Param("buildingNumber") int buildingNumber,
@Param("startTime") LocalDateTime startTime,
Expand All @@ -61,11 +73,11 @@ Page<Order> 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<Order> searchByKeyword(String keyword, Pageable pageable);

@Query("SELECT COUNT(DISTINCT o.id) FROM Order o " +
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,41 @@ public OrderDetailFullInfoResponse getOrderDetailByOrderDetailId(String orderDet
.build();
}

public List<OrderDetailManagementResponse> getOrderDetailByOrderId(String orderId, String status) {
return orderDetailRepository.findByOrderIdAndStatus(orderId, OrderStatus.valueOf(status)).stream().map(orderDetail -> {
List<AmenityManagementResponse> 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<OrderDetailManagementResponse> getOrderDetailById(String orderId) {
return orderDetailRepository.findByOrderId(orderId).stream().map(orderDetail -> {
List<AmenityManagementResponse> 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())
Expand Down Expand Up @@ -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 <OrderDetailAmenity> 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<OrderDetailAmenity> 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);
}
Expand Down
Loading

0 comments on commit c33297a

Please sign in to comment.