diff --git a/src/main/java/com/swp/PodBookingSystem/controller/AmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/AmenityController.java index a911ae6f..25c169aa 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/AmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/AmenityController.java @@ -8,12 +8,15 @@ import com.swp.PodBookingSystem.entity.Account; import com.swp.PodBookingSystem.entity.Amenity; import com.swp.PodBookingSystem.enums.AmenityType; +import com.swp.PodBookingSystem.exception.AppException; +import com.swp.PodBookingSystem.exception.ErrorCode; import com.swp.PodBookingSystem.service.AccountService; import com.swp.PodBookingSystem.service.AmenityService; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; import lombok.extern.slf4j.Slf4j; +import org.apache.coyote.BadRequestException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; @@ -49,6 +52,18 @@ public ApiResponse> getAmenitiesByType(@RequestParam(defau .build(); } + @GetMapping("/available-amenity") + public ApiResponse> getAvailableAmenitiesByBuildingId(@RequestParam (required = false) Integer buildingId){ + if (buildingId == null){ + throw new AppException(ErrorCode.BUILDING_ID_REQUIRED); + } + List amenityResponses = amenityService.getAvailableAmenitiesByBuildingId(buildingId); + return ApiResponse.>builder() + .message("Lấy danh sách tiện ích của chi nhánh thứ " + buildingId) + .data(amenityResponses) + .build(); + } + @PostMapping ApiResponse createAmenity(@RequestBody AmenityCreationRequest request){ return ApiResponse.builder() diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailController.java index a3898ef7..1668861d 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailController.java @@ -116,7 +116,7 @@ ApiResponse> getRevenueChart(@RequestParam(required = fals return ApiResponse.>builder() .message("Doanh thu theo " + (viewWith != null ? viewWith : "ngày")) - .data(orderDetailService.calculateRevenueByMonth(start, end, viewWith)) + .data(orderDetailService.calculateRevenueChart(start, end, viewWith)) .build(); } diff --git a/src/main/java/com/swp/PodBookingSystem/controller/RoomTypeController.java b/src/main/java/com/swp/PodBookingSystem/controller/RoomTypeController.java index e8fb3174..2575c21b 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/RoomTypeController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/RoomTypeController.java @@ -74,6 +74,15 @@ PaginationResponse> getFilteredRoomType(@RequestParam(required = .build(); } + @GetMapping("/room-type-within-address") + ApiResponse> getRoomTypeByBuildingAddress(@RequestParam (required = false) String address) { + List roomTypeResponse = roomTypeService.getRoomTypeByBuildingAddress(address); + return ApiResponse.>builder() + .message("Lấy loại phòng theo địa chỉ thành công") + .data(roomTypeResponse) + .build(); + } + @PutMapping("/{roomTypeId}") ApiResponse updatedRoomType(@PathVariable("roomTypeId") int roomTypeId, @RequestBody RoomTypeCreationRequest request) { diff --git a/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java b/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java index 15675716..083d4f02 100644 --- a/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java +++ b/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java @@ -24,6 +24,8 @@ public enum ErrorCode { 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"), + BUILDING_ID_REQUIRED(400, "Yêu cầu số chi nhánh", HttpStatus.BAD_REQUEST, "buildingId"), + AMENITY_NOT_EXIST(404, "Không tìm thấy tiện tích", HttpStatus.NOT_FOUND, "amenity") ; ErrorCode(int code, String message, HttpStatusCode statusCode, String field) { diff --git a/src/main/java/com/swp/PodBookingSystem/repository/AmenityRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/AmenityRepository.java index 4f44033f..82453885 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/AmenityRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/AmenityRepository.java @@ -5,13 +5,22 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface AmenityRepository extends JpaRepository { + @Query("SELECT a " + + "FROM Amenity a " + + "WHERE a.type = :type AND a.isDeleted = 0 AND a.quantity > 0") List findAllByType(AmenityType type); Page findAllByBuildingId(Integer buildingId, Pageable pageable); + + @Query("SELECT a " + + "FROM Amenity a " + + "WHERE a.building.id = :buildingId AND a.isDeleted = 0 AND a.quantity > 0") + List findAllAvailableByBuildingId(Integer buildingId); } diff --git a/src/main/java/com/swp/PodBookingSystem/repository/RoomTypeRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/RoomTypeRepository.java index 0969f2c4..cbd43809 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/RoomTypeRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/RoomTypeRepository.java @@ -8,6 +8,7 @@ import org.springframework.data.repository.query.Param; import java.time.LocalDateTime; +import java.util.List; public interface RoomTypeRepository extends JpaRepository { @Query("SELECT rt FROM RoomType rt " + @@ -29,4 +30,9 @@ Page findFilteredRoomTypes(@Param("address") String address, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, Pageable pageable); + + @Query("SELECT rt " + + "FROM RoomType rt " + + "WHERE rt.building.address = :buildingAddress") + List findByBuildingAddress(String buildingAddress); } diff --git a/src/main/java/com/swp/PodBookingSystem/service/AmenityService.java b/src/main/java/com/swp/PodBookingSystem/service/AmenityService.java index ecfc35a2..f4bd6164 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/AmenityService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/AmenityService.java @@ -44,6 +44,13 @@ public List getAmenitiesByType(AmenityType amenityType) { .collect(Collectors.toList()); } + public List getAvailableAmenitiesByBuildingId(Integer buildingId) { + List amenities = amenityRepository.findAllAvailableByBuildingId(buildingId); + return amenities.stream() + .map(amenityMapper::toAmenityResponse) + .collect(Collectors.toList()); + } + public AmenityResponse createAmenity(AmenityCreationRequest request){ Amenity newAmenity = amenityMapper.toAmenity(request); return amenityMapper.toAmenityResponse(amenityRepository.save(newAmenity)); diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index 12aa1c77..6ec186e3 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -31,6 +31,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @@ -466,7 +467,7 @@ public double calculateRevenue(LocalDateTime startTime, LocalDateTime endTime) { /* [GET]: /order-detail/revenue-chart */ - public List calculateRevenueByMonth(LocalDateTime startTime, LocalDateTime endTime, String viewWith) { + public List calculateRevenueChart(LocalDateTime startTime, LocalDateTime endTime, String viewWith) { if (startTime == null) { startTime = LocalDate.now().atStartOfDay(); } @@ -482,7 +483,7 @@ public List calculateRevenueByMonth(LocalDateTime startTime, Lo case "month": return calculateRevenueByMonth(startTime, endTime); case "quarter": - return orderDetailRepository.calculateRevenueByQuarter(startTime, endTime); + return calculateRevenueByQuarter(startTime, endTime); default: return Collections.singletonList(orderDetailRepository.calculateRevenueForSingleDay(startTime)); } @@ -519,6 +520,25 @@ public List calculateRevenueByMonth(LocalDateTime startTime, Lo return result; } + public List calculateRevenueByQuarter(LocalDateTime startTime, LocalDateTime endTime) { + List revenueData = orderDetailRepository.calculateRevenueByQuarter(startTime, endTime); + + Map revenueMap = revenueData.stream() + .collect(Collectors.toMap(RevenueChartDto::getDate, RevenueChartDto::getRevenue)); + + List result = new ArrayList<>(); + YearMonth start = YearMonth.from(startTime); + YearMonth end = YearMonth.from(endTime); + + while(!start.isAfter(end)) { + String monthKey = start.toString() + "-01"; + double revenue = revenueMap.getOrDefault(monthKey, 0.0); + result.add(new RevenueChartDto(monthKey, revenue)); + start = start.plusMonths(1); + } + return result; + } + /* [GET]: /order-detail/number-order-by-building diff --git a/src/main/java/com/swp/PodBookingSystem/service/RoomTypeService.java b/src/main/java/com/swp/PodBookingSystem/service/RoomTypeService.java index f7ae3a4f..5d2ebfdb 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/RoomTypeService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/RoomTypeService.java @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service; import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; @Service @@ -59,6 +60,17 @@ public Page getFilteredRoomTypes(String address, Integer capacity, Loc return roomTypeRepository.findFilteredRoomTypes(address, capacity, startTime, endTime, pageable); } + /* + [GET]: /room-types/room-type-within-address + */ + public List getRoomTypeByBuildingAddress(String buildingAddress) { + System.out.println("building address la: " + buildingAddress); + if (buildingAddress == null || buildingAddress.equals("")) { + return roomTypeRepository.findAll(); + } + return roomTypeRepository.findByBuildingAddress(buildingAddress); + } + /* [PUT]: /room-types/roomTypeId */