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

[PBS-65][HoangHN] refactor: get revenue view by month or else 0 #121

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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