package com.trafficaudit.reportexport.controller; import com.trafficaudit.common.Result; import com.trafficaudit.reportexport.service.ReportExportService; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @Slf4j @RestController @RequestMapping("/api/report") public class ReportExportController { @Resource private ReportExportService reportService; /** 生成_货运量分市州明细.xlsx */ @GetMapping("/export/cityDetail") public ResponseEntity exportCityDetail(@RequestParam("period") String period) throws Exception { return buildResponse(reportService.exportCityDetail(period), "生成_货运量分市州明细.xlsx"); } /** 生成_货运量排名.xlsx */ @GetMapping("/export/freightRank") public ResponseEntity exportFreightRank(@RequestParam("period") String period) throws Exception { return buildResponse(reportService.exportFreightRank(period), "生成_货运量排名.xlsx"); } /** 生成_周转量排名.xlsx */ @GetMapping("/export/turnoverRank") public ResponseEntity exportTurnoverRank(@RequestParam("period") String period) throws Exception { return buildResponse(reportService.exportTurnoverRank(period), "生成_周转量排名.xlsx"); } private ResponseEntity buildResponse(byte[] data, String fileName) throws Exception { String encoded = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20"); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encoded) .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) .body(data); } }