Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…king-system-server into PBS-65-Dashboard
  • Loading branch information
Hoang-Nguyen-Huy committed Oct 30, 2024
2 parents 8b3724a + 49e4d9f commit f367de2
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 61 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
<sonar.organization>swp-pod-booking-system-server</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
<sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/site/jacoco/jacoco.xml
</sonar.coverage.jacoco.xmlReportPaths>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -137,8 +138,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>22</source>
<target>22</target>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ ApiResponse<BuildingResponse> updateBuilding(@PathVariable("buildingId") int bui
@RequestBody BuildingCreationRequest request) {
return ApiResponse.<BuildingResponse>builder()
.data(buildingService.updateBuilding(buildingId, request))
.message("Update building successfully")
.message("Cập nhật chi nhánh thành công")
.build();
}

Expand Down
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,7 @@
public class OrderUpdateRequest {
String id;
OrderStatus status;
String cancelReason;
Account orderHandler;
List<OrderDetailUpdateRoomRequest> orderDetails;
}
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 @@ -16,7 +16,8 @@ public interface BuildingRepository extends JpaRepository<Building, Integer> {
List<Building> searchByKeyword(String keyword);

@Query("SELECT b FROM Building b " +
"WHERE (b.address LIKE %:address%)")
"WHERE (b.address LIKE %:address%)" +
"ORDER BY b.createdAt DESC")
Page<Building> findFilteredBuildings(@Param("address") String address,
Pageable pageable);
}
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 @@ -90,13 +90,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 @@ -340,24 +368,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 All @@ -367,6 +395,9 @@ public void updateOrderDetail(OrderUpdateRequest request) {
Account orderHandler = accountService.getAccountById(request.getOrderHandler().getId());
od.setOrderHandler(orderHandler);
}
if (request.getCancelReason() != null) {
od.setCancelReason(request.getCancelReason());
}
if (request.getOrderDetails() != null && !request.getOrderDetails().isEmpty()) {
for (OrderDetailUpdateRoomRequest odr : request.getOrderDetails()) {
if (odr.getId().equals(od.getId())) {
Expand Down
Loading

0 comments on commit f367de2

Please sign in to comment.