Skip to content

Commit

Permalink
Merge pull request #124 from Hoang-Nguyen-Huy/PBS-65-Dashboard
Browse files Browse the repository at this point in the history
refactor calculating revenue api
  • Loading branch information
nguyenhcp2004 authored Oct 31, 2024
2 parents 64e7d18 + 5055443 commit dc1e664
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -49,6 +52,18 @@ public ApiResponse<List<AmenityResponse>> getAmenitiesByType(@RequestParam(defau
.build();
}

@GetMapping("/available-amenity")
public ApiResponse<List<AmenityResponse>> getAvailableAmenitiesByBuildingId(@RequestParam (required = false) Integer buildingId){
if (buildingId == null){
throw new AppException(ErrorCode.BUILDING_ID_REQUIRED);
}
List<AmenityResponse> amenityResponses = amenityService.getAvailableAmenitiesByBuildingId(buildingId);
return ApiResponse.<List<AmenityResponse>>builder()
.message("Lấy danh sách tiện ích của chi nhánh thứ " + buildingId)
.data(amenityResponses)
.build();
}

@PostMapping
ApiResponse<AmenityResponse> createAmenity(@RequestBody AmenityCreationRequest request){
return ApiResponse.<AmenityResponse>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ ApiResponse<List<RevenueChartDto>> getRevenueChart(@RequestParam(required = fals

return ApiResponse.<List<RevenueChartDto>>builder()
.message("Doanh thu theo " + (viewWith != null ? viewWith : "ngày"))
.data(orderDetailService.calculateRevenueByMonth(start, end, viewWith))
.data(orderDetailService.calculateRevenueChart(start, end, viewWith))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ PaginationResponse<List<RoomType>> getFilteredRoomType(@RequestParam(required =
.build();
}

@GetMapping("/room-type-within-address")
ApiResponse<List<RoomType>> getRoomTypeByBuildingAddress(@RequestParam (required = false) String address) {
List<RoomType> roomTypeResponse = roomTypeService.getRoomTypeByBuildingAddress(address);
return ApiResponse.<List<RoomType>>builder()
.message("Lấy loại phòng theo địa chỉ thành công")
.data(roomTypeResponse)
.build();
}

@PutMapping("/{roomTypeId}")
ApiResponse<RoomTypeResponse> updatedRoomType(@PathVariable("roomTypeId") int roomTypeId,
@RequestBody RoomTypeCreationRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Amenity, Integer> {
@Query("SELECT a " +
"FROM Amenity a " +
"WHERE a.type = :type AND a.isDeleted = 0 AND a.quantity > 0")
List<Amenity> findAllByType(AmenityType type);

Page<Amenity> 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<Amenity> findAllAvailableByBuildingId(Integer buildingId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;

public interface RoomTypeRepository extends JpaRepository<RoomType, Integer> {
@Query("SELECT rt FROM RoomType rt " +
Expand All @@ -29,4 +30,9 @@ Page<RoomType> 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<RoomType> findByBuildingAddress(String buildingAddress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public List<AmenityResponse> getAmenitiesByType(AmenityType amenityType) {
.collect(Collectors.toList());
}

public List<AmenityResponse> getAvailableAmenitiesByBuildingId(Integer buildingId) {
List<Amenity> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -466,7 +467,7 @@ public double calculateRevenue(LocalDateTime startTime, LocalDateTime endTime) {
/*
[GET]: /order-detail/revenue-chart
*/
public List<RevenueChartDto> calculateRevenueByMonth(LocalDateTime startTime, LocalDateTime endTime, String viewWith) {
public List<RevenueChartDto> calculateRevenueChart(LocalDateTime startTime, LocalDateTime endTime, String viewWith) {
if (startTime == null) {
startTime = LocalDate.now().atStartOfDay();
}
Expand All @@ -482,7 +483,7 @@ public List<RevenueChartDto> 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));
}
Expand Down Expand Up @@ -519,6 +520,25 @@ public List<RevenueChartDto> calculateRevenueByMonth(LocalDateTime startTime, Lo
return result;
}

public List<RevenueChartDto> calculateRevenueByQuarter(LocalDateTime startTime, LocalDateTime endTime) {
List<RevenueChartDto> revenueData = orderDetailRepository.calculateRevenueByQuarter(startTime, endTime);

Map<String, Double> revenueMap = revenueData.stream()
.collect(Collectors.toMap(RevenueChartDto::getDate, RevenueChartDto::getRevenue));

List<RevenueChartDto> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
Expand Down Expand Up @@ -59,6 +60,17 @@ public Page<RoomType> getFilteredRoomTypes(String address, Integer capacity, Loc
return roomTypeRepository.findFilteredRoomTypes(address, capacity, startTime, endTime, pageable);
}

/*
[GET]: /room-types/room-type-within-address
*/
public List<RoomType> 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
*/
Expand Down

0 comments on commit dc1e664

Please sign in to comment.