Skip to content

Commit

Permalink
Merge pull request #116 from Hoang-Nguyen-Huy/PBS-84-Manage-Order-Ame…
Browse files Browse the repository at this point in the history
…nities

Pbs 84 manage order amenities
  • Loading branch information
nguyenhcp2004 authored Oct 28, 2024
2 parents 812fa33 + 30e4354 commit 25374bd
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,14 +49,15 @@ public ApiResponse<PaginationResponse<List<OrderManagementResponse>>> 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.<PaginationResponse<List<OrderManagementResponse>>>builder()
.data(orderService.getOrdersByRole(page, size, startDateTime, endDateTime, user))
.data(orderService.getOrdersByRole(page, size, startDateTime, endDateTime, user, status))
.message("get paging order successfully")
.build();
}
Expand Down Expand Up @@ -87,7 +89,7 @@ public ApiResponse<String> 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";
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/swp/PodBookingSystem/entity/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@Table(name = "`order`")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
String id;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,39 @@ public interface OrderDetailRepository extends JpaRepository<OrderDetail, String

List<OrderDetail> findByEndTime(LocalDateTime endTime);

@Query(value = "SELECT od FROM OrderDetail od WHERE od.createdAt BETWEEN :startTime AND :endTime ORDER BY od.createdAt DESC")
Page<OrderDetail> 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("""
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<OrderDetail> 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<OrderDetail> findOrdersByBuildingNumberAndTimeRange(
@Param("buildingNumber") int buildingNumber,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime,
Pageable pageable);
Pageable pageable
);

Page<OrderDetail> findAll(Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,16 +19,37 @@ public interface OrderRepository extends JpaRepository<Order, String> {

Page<Order> 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<Order> 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<Order> 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<Order> 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<Order> findOrdersByBuildingNumberAndTimeRange(
@Param("buildingNumber") int buildingNumber,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime,
@Param("status") OrderStatus status,
Pageable pageable);

@Modifying
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
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;
import com.swp.PodBookingSystem.enums.AmenityType;
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;
Expand All @@ -36,6 +38,7 @@ public class OrderDetailAmenityService {
OrderDetailRepository orderDetailRepository;
OrderDetailAmenityMapper orderDetailAmenityMapper;
AmenityMapper amenityMapper;
private final AccountRepository accountRepository;

//GET:
public List<AmenityManagementResponse> getOrderDetailAmenitiesByOrderDetailId(String orderDetailId) {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<OrderDetailResponse> getAllOrders() {
Expand Down Expand Up @@ -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 <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){
customer.setBalance(customer.getBalance() + total);
accountRepository.save(customer);
}
}
}
if (request.getOrderHandler() != null) {
Account orderHandler = accountService.getAccountById(request.getOrderHandler().getId());
Expand Down
38 changes: 31 additions & 7 deletions src/main/java/com/swp/PodBookingSystem/service/OrderService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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;
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;
Expand All @@ -15,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
Expand Down Expand Up @@ -51,15 +57,15 @@ public List<OrderResponse> getOrdersByAccountId(String accountId) {
}

public PaginationResponse<List<OrderManagementResponse>> getOrdersByRole(
int page, int size, LocalDateTime startDate, LocalDateTime endDate, Account user) {
int page, int size, LocalDateTime startDate, LocalDateTime endDate, Account user, OrderStatus status) {
Page<Order> 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.");
}
Expand All @@ -73,14 +79,14 @@ public PaginationResponse<List<OrderManagementResponse>> 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);
Expand All @@ -92,7 +98,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())
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 25374bd

Please sign in to comment.