Skip to content

Commit

Permalink
Merge pull request #118 from Hoang-Nguyen-Huy/PBS-65-Dashboard
Browse files Browse the repository at this point in the history
Pbs 65 dashboard
  • Loading branch information
nguyenhcp2004 authored Oct 30, 2024
2 parents 49e4d9f + f367de2 commit adc33c4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ ApiResponse<Integer> countCurrentCustomer() {
ApiResponse<Integer> countCustomer(@RequestParam(required = false) String startTime,
@RequestParam(required = false) String endTime) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy'T'hh:mm'T'a");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy'T'HH:mm");
LocalDateTime start = startTime != null ? LocalDateTime.parse(startTime, formatter) : null;
LocalDateTime end = endTime != null ? LocalDateTime.parse(endTime, formatter) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ ApiResponse<Integer> countOrder(
@RequestParam(required = false) String startTime,
@RequestParam(required = false) String endTime) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy'T'hh:mm'T'a");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy'T'HH:mm");
LocalDateTime start = startTime != null ? LocalDateTime.parse(startTime, formatter) : null;
LocalDateTime end = endTime != null ? LocalDateTime.parse(endTime, formatter) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.swp.PodBookingSystem.dto.respone.Order.NumberOrderByBuildingDto;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailFullInfoResponse;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailResponse;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueByMonthDto;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueChartDto;
import com.swp.PodBookingSystem.dto.respone.PaginationResponse;
import com.swp.PodBookingSystem.service.OrderDetailService;
import lombok.AccessLevel;
Expand All @@ -14,7 +14,6 @@
import com.swp.PodBookingSystem.dto.respone.ApiResponse;

import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -96,7 +95,7 @@ ApiResponse<Double> getRevenueCurrentDay() {
ApiResponse<Double> getRevenue(@RequestParam(required = false) String startTime,
@RequestParam(required = false) String endTime) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy'T'hh:mm'T'a");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy'T'HH:mm");
LocalDateTime start = startTime != null ? LocalDateTime.parse(startTime, formatter) : null;
LocalDateTime end = endTime != null ? LocalDateTime.parse(endTime, formatter) : null;

Expand All @@ -106,11 +105,18 @@ ApiResponse<Double> getRevenue(@RequestParam(required = false) String startTime,
.build();
}

@GetMapping("/revenue-by-month")
ApiResponse<List<RevenueByMonthDto>> getRevenueByMonth() {
return ApiResponse.<List<RevenueByMonthDto>>builder()
.message("Doanh thu các tháng năm hiện tại")
.data(orderDetailService.calculateRevenueByMonth())
@GetMapping("/revenue-chart")
ApiResponse<List<RevenueChartDto>> getRevenueChart(@RequestParam(required = false) String startTime,
@RequestParam(required = false) String endTime,
@RequestParam(required = false) String viewWith) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy'T'HH:mm");
LocalDateTime start = startTime != null ? LocalDateTime.parse(startTime, formatter) : null;
LocalDateTime end = endTime != null ? LocalDateTime.parse(endTime, formatter) : null;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@AllArgsConstructor
@Builder
@FieldDefaults(level = AccessLevel.PRIVATE)
public class RevenueByMonthDto {
public class RevenueChartDto {
String date;
Double revenue;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.swp.PodBookingSystem.repository;

import com.swp.PodBookingSystem.dto.respone.Order.NumberOrderByBuildingDto;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueByMonthDto;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueChartDto;
import com.swp.PodBookingSystem.entity.OrderDetail;
import com.swp.PodBookingSystem.enums.OrderStatus;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -108,10 +108,39 @@ Page<OrderDetail> findOrdersByBuildingNumberAndTimeRange(
"ON od.id = amenityTotal.orderDetailId " +
"WHERE od.startTime >= :startTime AND od.endTime <= :endTime " +
"AND od.status = com.swp.PodBookingSystem.enums.OrderStatus.Successfully")
Double calculateRevenueBetweenDateTime(@Param("startTime") LocalDateTime startTime,
Optional<Double> calculateRevenueBetweenDateTime(@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime);

@Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueByMonthDto(" +
@Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueChartDto(" +
"CONCAT(YEAR(:startTime), '-', LPAD(CAST(MONTH(:startTime) AS string), 2, '0'), '-', LPAD(CAST(DAY(:startTime) AS string), 2, '0')), " +
"SUM((od.priceRoom + COALESCE(amenityTotal.totalAmenityPrice, 0)) * " +
"(1 - COALESCE(od.discountPercentage, 0) / 100.0) * (1 - COALESCE(sp.discountPercentage, 0) / 100.0))) " +
"FROM OrderDetail od " +
"LEFT JOIN od.servicePackage sp " +
"LEFT JOIN (SELECT oda.orderDetail.id as orderDetailId, SUM(oda.price * oda.quantity) as totalAmenityPrice " +
" FROM OrderDetailAmenity oda GROUP BY oda.orderDetail.id) amenityTotal " +
"ON od.id = amenityTotal.orderDetailId " +
"WHERE DATE(od.startTime) = DATE(:startTime) " +
"AND od.status = com.swp.PodBookingSystem.enums.OrderStatus.Successfully")
RevenueChartDto calculateRevenueForSingleDay(@Param("startTime") LocalDateTime startTime);

@Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueChartDto(" +
"CONCAT(YEAR(od.startTime), '-', LPAD(CAST(MONTH(od.startTime) AS string), 2, '0'), '-', LPAD(CAST(DAY(od.startTime) AS string), 2, '0')), " +
"SUM((od.priceRoom + COALESCE(amenityTotal.totalAmenityPrice, 0)) * " +
"(1 - COALESCE(od.discountPercentage, 0) / 100.0) * (1 - COALESCE(sp.discountPercentage, 0) / 100.0))) " +
"FROM OrderDetail od " +
"LEFT JOIN od.servicePackage sp " +
"LEFT JOIN (SELECT oda.orderDetail.id as orderDetailId, SUM(oda.price * oda.quantity) as totalAmenityPrice " +
" FROM OrderDetailAmenity oda GROUP BY oda.orderDetail.id) amenityTotal " +
"ON od.id = amenityTotal.orderDetailId " +
"WHERE od.startTime BETWEEN :startTime AND :endTime " +
"AND od.status = com.swp.PodBookingSystem.enums.OrderStatus.Successfully " +
"GROUP BY CONCAT(YEAR(od.startTime), '-', LPAD(CAST(MONTH(od.startTime) AS string), 2, '0'), '-', LPAD(CAST(DAY(od.startTime) AS string), 2, '0')) " +
"ORDER BY CONCAT(YEAR(od.startTime), '-', LPAD(CAST(MONTH(od.startTime) AS string), 2, '0'), '-', LPAD(CAST(DAY(od.startTime) AS string), 2, '0'))")
List<RevenueChartDto> calculateRevenueByMonth(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);


@Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueChartDto(" +
"CONCAT(YEAR(MIN(od.startTime)), '-', LPAD(CAST(MONTH(MIN(od.startTime)) AS string), 2, '0'), '-01'), " +
"SUM((od.priceRoom + COALESCE(amenityTotal.totalAmenityPrice, 0)) * " +
"(1 - COALESCE(od.discountPercentage, 0) / 100.0) * " +
Expand All @@ -121,10 +150,11 @@ Double calculateRevenueBetweenDateTime(@Param("startTime") LocalDateTime startTi
"LEFT JOIN (SELECT oda.orderDetail.id as orderDetailId, SUM(oda.price * oda.quantity) as totalAmenityPrice " +
" FROM OrderDetailAmenity oda GROUP BY oda.orderDetail.id) amenityTotal " +
"ON od.id = amenityTotal.orderDetailId " +
"WHERE FUNCTION('YEAR', od.startTime) = FUNCTION('YEAR', CURRENT_DATE) " +
"WHERE od.startTime BETWEEN :startTime AND :endTime " +
"AND od.status = com.swp.PodBookingSystem.enums.OrderStatus.Successfully " +
"GROUP BY YEAR(od.startTime), MONTH(od.startTime)")
List<RevenueByMonthDto> calculateRevenueByMonthForCurrentYear();
List<RevenueChartDto> calculateRevenueByQuarter(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);


@Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.Order.NumberOrderByBuildingDto(" +
"od.building.id, od.building.address, COUNT(DISTINCT od.order.id)" +
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.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -461,20 +462,32 @@ public double calculateRevenueCurrentDay() {
[GET]: /order-detail/revenue?
*/
public double calculateRevenue(LocalDateTime startTime, LocalDateTime endTime) {
return orderDetailRepository.calculateRevenueBetweenDateTime(startTime, endTime).orElse(0.0);
}

/*
[GET]: /order-detail/revenue-chart
*/
public List<RevenueChartDto> calculateRevenueByMonth(LocalDateTime startTime, LocalDateTime endTime, String viewWith) {
if (startTime == null) {
startTime = LocalDate.now().atStartOfDay();
}
if (endTime == null) {
endTime = LocalDate.now().atTime(LocalTime.MAX);
}
return orderDetailRepository.calculateRevenueBetweenDateTime(startTime, endTime);
}

/*
[GET]: /order-detail/revenue-by-month
*/
public List<RevenueByMonthDto> calculateRevenueByMonth() {
return orderDetailRepository.calculateRevenueByMonthForCurrentYear();
if (viewWith == null) {
return Collections.singletonList(orderDetailRepository.calculateRevenueForSingleDay(startTime));
}
switch (viewWith.toLowerCase()) {
case "day":
return Collections.singletonList(orderDetailRepository.calculateRevenueForSingleDay(startTime));
case "month":
return orderDetailRepository.calculateRevenueByMonth(startTime, endTime);
case "quarter":
return orderDetailRepository.calculateRevenueByQuarter(startTime, endTime);
default:
return Collections.singletonList(orderDetailRepository.calculateRevenueForSingleDay(startTime));
}
}

/*
Expand Down

0 comments on commit adc33c4

Please sign in to comment.