|
| 1 | +package com.vehicle.management.api.controller; |
| 2 | + |
| 3 | +import com.vehicle.management.domain.model.BorrowingRequest; |
| 4 | +import com.vehicle.management.domain.model.Vehicle; |
| 5 | +import com.vehicle.management.repository.IBorrowingRepository; |
| 6 | +import com.vehicle.management.repository.IVehicleRepository; |
| 7 | +import org.apache.poi.ss.usermodel.*; |
| 8 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 9 | +import org.springframework.format.annotation.DateTimeFormat; |
| 10 | +import org.springframework.http.HttpHeaders; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.security.core.annotation.AuthenticationPrincipal; |
| 14 | +import org.springframework.security.core.userdetails.UserDetails; |
| 15 | +import org.springframework.web.bind.annotation.*; |
| 16 | + |
| 17 | +import java.io.ByteArrayOutputStream; |
| 18 | +import java.io.IOException; |
| 19 | +import java.time.Instant; |
| 20 | +import java.time.ZoneId; |
| 21 | +import java.time.format.DateTimeFormatter; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.UUID; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +@RestController |
| 28 | +@RequestMapping("/api/reports") |
| 29 | +public class ReportController { |
| 30 | + |
| 31 | + private final IBorrowingRepository borrowingRepository; |
| 32 | + private final IVehicleRepository vehicleRepository; |
| 33 | + |
| 34 | + public ReportController(IBorrowingRepository borrowingRepository, |
| 35 | + IVehicleRepository vehicleRepository) { |
| 36 | + this.borrowingRepository = borrowingRepository; |
| 37 | + this.vehicleRepository = vehicleRepository; |
| 38 | + } |
| 39 | + |
| 40 | + @GetMapping("/borrowings") |
| 41 | + public ResponseEntity<byte[]> exportBorrowings( |
| 42 | + @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Instant from, |
| 43 | + @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Instant to, |
| 44 | + @AuthenticationPrincipal UserDetails userDetails) throws IOException { |
| 45 | + |
| 46 | + List<BorrowingRequest> requests = borrowingRepository.findAll(); |
| 47 | + |
| 48 | + // Filter by date range on createdAt if provided |
| 49 | + if (from != null) { |
| 50 | + requests = requests.stream() |
| 51 | + .filter(r -> !r.getCreatedAt().isBefore(from)) |
| 52 | + .collect(Collectors.toList()); |
| 53 | + } |
| 54 | + if (to != null) { |
| 55 | + requests = requests.stream() |
| 56 | + .filter(r -> !r.getCreatedAt().isAfter(to)) |
| 57 | + .collect(Collectors.toList()); |
| 58 | + } |
| 59 | + |
| 60 | + // Build vehicle lookup map |
| 61 | + Map<UUID, Vehicle> vehicleMap = vehicleRepository.findAll().stream() |
| 62 | + .collect(Collectors.toMap(Vehicle::getId, v -> v)); |
| 63 | + |
| 64 | + byte[] excel = buildExcel(requests, vehicleMap); |
| 65 | + |
| 66 | + HttpHeaders headers = new HttpHeaders(); |
| 67 | + headers.setContentType(MediaType.parseMediaType( |
| 68 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); |
| 69 | + headers.setContentDispositionFormData("attachment", "borrowings_export.xlsx"); |
| 70 | + |
| 71 | + return ResponseEntity.ok().headers(headers).body(excel); |
| 72 | + } |
| 73 | + |
| 74 | + private byte[] buildExcel(List<BorrowingRequest> requests, Map<UUID, Vehicle> vehicleMap) |
| 75 | + throws IOException { |
| 76 | + try (Workbook wb = new XSSFWorkbook()) { |
| 77 | + Sheet sheet = wb.createSheet("借用記錄"); |
| 78 | + |
| 79 | + // Header style |
| 80 | + CellStyle headerStyle = wb.createCellStyle(); |
| 81 | + headerStyle.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex()); |
| 82 | + headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| 83 | + Font font = wb.createFont(); |
| 84 | + font.setBold(true); |
| 85 | + font.setColor(IndexedColors.WHITE.getIndex()); |
| 86 | + headerStyle.setFont(font); |
| 87 | + |
| 88 | + // Header row |
| 89 | + String[] headers = {"編號", "申請人ID", "車牌", "車型", "借用開始", "借用結束", "用途", "狀態", "審核備註", "建立時間"}; |
| 90 | + Row headerRow = sheet.createRow(0); |
| 91 | + for (int i = 0; i < headers.length; i++) { |
| 92 | + Cell cell = headerRow.createCell(i); |
| 93 | + cell.setCellValue(headers[i]); |
| 94 | + cell.setCellStyle(headerStyle); |
| 95 | + } |
| 96 | + |
| 97 | + // Data rows |
| 98 | + DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm") |
| 99 | + .withZone(ZoneId.of("Asia/Taipei")); |
| 100 | + int rowNum = 1; |
| 101 | + for (BorrowingRequest req : requests) { |
| 102 | + Row row = sheet.createRow(rowNum++); |
| 103 | + Vehicle v = vehicleMap.get(req.getVehicleId()); |
| 104 | + row.createCell(0).setCellValue(rowNum - 1); |
| 105 | + row.createCell(1).setCellValue(req.getUserId().toString()); |
| 106 | + row.createCell(2).setCellValue(v != null ? v.getPlate() : ""); |
| 107 | + row.createCell(3).setCellValue(v != null ? v.getModel() : ""); |
| 108 | + row.createCell(4).setCellValue(fmt.format(req.getPeriodStart())); |
| 109 | + row.createCell(5).setCellValue(fmt.format(req.getPeriodEnd())); |
| 110 | + row.createCell(6).setCellValue(req.getPurpose()); |
| 111 | + row.createCell(7).setCellValue(translateState(req.getStateName())); |
| 112 | + row.createCell(8).setCellValue(req.getReviewNote() != null ? req.getReviewNote() : ""); |
| 113 | + row.createCell(9).setCellValue(fmt.format(req.getCreatedAt())); |
| 114 | + } |
| 115 | + |
| 116 | + // Auto-size columns |
| 117 | + for (int i = 0; i < headers.length; i++) { |
| 118 | + sheet.autoSizeColumn(i); |
| 119 | + } |
| 120 | + |
| 121 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 122 | + wb.write(out); |
| 123 | + return out.toByteArray(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private String translateState(String state) { |
| 128 | + return switch (state) { |
| 129 | + case "PENDING" -> "待審核"; |
| 130 | + case "APPROVED" -> "已核准"; |
| 131 | + case "REJECTED" -> "已拒絕"; |
| 132 | + case "IN_USE" -> "使用中"; |
| 133 | + case "RETURNED" -> "已還車"; |
| 134 | + default -> state; |
| 135 | + }; |
| 136 | + } |
| 137 | +} |
0 commit comments