Add batch report generation
Problem Context
Finance team needs monthly reports aggregating all transactions. The batch job processes each transaction to calculate summaries. Transaction table has millions of records.
Expert
300 points
File Changes (1)
src/main/java/com/example/service/ReportService.java
MODIFIED
@@ -20 +20 @@
| 20 | 20 | @Service |
| 21 | 21 | @RequiredArgsConstructor |
| 22 | 22 | public class ReportService { |
| 23 | 23 | private final TransactionRepository transactionRepository; |
| 24 | + |
|
| 25 | + @Transactional |
|
| 26 | + public MonthlyReport generateMonthlyReport(YearMonth month) { |
|
| 27 | + LocalDateTime start = month.atDay(1).atStartOfDay(); |
|
| 28 | + LocalDateTime end = month.atEndOfMonth().atTime(23, 59, 59); |
|
| 29 | + |
|
| 30 | + // Load all transactions for the month |
|
| 31 | + List<Transaction> transactions = transactionRepository |
|
| 32 | + .findByCreatedAtBetween(start, end); |
|
| 33 | + |
|
| 34 | + BigDecimal totalRevenue = BigDecimal.ZERO; |
|
| 35 | + int orderCount = 0; |
|
| 36 | + Map<String, BigDecimal> revenueByCategory = new HashMap<>(); |
|
| 37 | + |
|
| 38 | + for (Transaction tx : transactions) { |
|
| 39 | + totalRevenue = totalRevenue.add(tx.getAmount()); |
|
| 40 | + orderCount++; |
|
| 41 | + revenueByCategory.merge(tx.getCategory(), tx.getAmount(), BigDecimal::add); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + return MonthlyReport.builder() |
|
| 45 | + .month(month) |
|
| 46 | + .totalRevenue(totalRevenue) |
|
| 47 | + .orderCount(orderCount) |
|
| 48 | + .revenueByCategory(revenueByCategory) |
|
| 49 | + .build(); |
|
| 50 | + } |
|
| 24 | 51 | } |
Login Required: You must be registered to submit reviews and receive AI feedback.
Register or
login to start reviewing!
Your Review
Review Tips
- Look for security vulnerabilities (SQL injection, XSS, etc.)
- Check for null pointer exceptions and error handling
- Consider performance implications
- Evaluate code maintainability and readability
- Check for proper resource management
- Look for logic errors or edge cases