Skip to content

Commit

Permalink
Merge pull request #121 from Hoang-Nguyen-Huy/PBS-65-Dashboard
Browse files Browse the repository at this point in the history
[PBS-65][HoangHN] refactor: get revenue view by month or else 0
  • Loading branch information
nguyenhcp2004 authored Oct 31, 2024
2 parents adc33c4 + 0f6ad7f commit eb95c42
Showing 1 changed file with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
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;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -482,14 +480,46 @@ public List<RevenueChartDto> calculateRevenueByMonth(LocalDateTime startTime, Lo
case "day":
return Collections.singletonList(orderDetailRepository.calculateRevenueForSingleDay(startTime));
case "month":
return orderDetailRepository.calculateRevenueByMonth(startTime, endTime);
return calculateRevenueByMonth(startTime, endTime);
case "quarter":
return orderDetailRepository.calculateRevenueByQuarter(startTime, endTime);
default:
return Collections.singletonList(orderDetailRepository.calculateRevenueForSingleDay(startTime));
}
}

public List<RevenueChartDto> calculateRevenueByMonth(LocalDateTime startTime, LocalDateTime endTime) {
if (startTime == null) {
startTime = LocalDate.now().atStartOfDay();
}
if (endTime == null) {
endTime = LocalDate.now().atTime(LocalTime.MAX);
}

// Step 1: Generate all dates from startTime to endTime
List<LocalDate> dateRange = startTime.toLocalDate()
.datesUntil(endTime.toLocalDate().plusDays(1))
.collect(Collectors.toList());

// Step 2: Fetch actual revenue data from the repository
List<RevenueChartDto> actualRevenueData = orderDetailRepository.calculateRevenueByMonth(startTime, endTime);

// Step 3: Convert actual revenue data to a map for fast lookup
Map<String, Double> revenueMap = actualRevenueData.stream()
.collect(Collectors.toMap(RevenueChartDto::getDate, RevenueChartDto::getRevenue));

// Step 4: Populate the result with all dates in range, setting missing data to zero
List<RevenueChartDto> result = dateRange.stream()
.map(date -> {
String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
return new RevenueChartDto(formattedDate, revenueMap.getOrDefault(formattedDate, 0.0));
})
.collect(Collectors.toList());

return result;
}


/*
[GET]: /order-detail/number-order-by-building
*/
Expand Down

0 comments on commit eb95c42

Please sign in to comment.