Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor calculating revenue api #124

Merged
merged 6 commits into from
Oct 31, 2024
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,21 @@ 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);
if (amenityResponses.isEmpty()){
throw new AppException(ErrorCode.AMENITY_NOT_EXIST);
}
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 @@ -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,6 +5,7 @@
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;
Expand All @@ -14,4 +15,9 @@ public interface AmenityRepository extends JpaRepository<Amenity, Integer> {
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 @@ -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