package com.rongyichuang.player.api;
|
|
import com.rongyichuang.player.service.PlayerApplicationService;
|
import jakarta.servlet.http.HttpServletResponse;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
import java.nio.charset.StandardCharsets;
|
|
@RestController
|
@RequestMapping("/player/export")
|
public class PlayerExportController {
|
|
private static final Logger log = LoggerFactory.getLogger(PlayerExportController.class);
|
|
private final PlayerApplicationService playerApplicationService;
|
|
public PlayerExportController(PlayerApplicationService playerApplicationService) {
|
this.playerApplicationService = playerApplicationService;
|
}
|
|
/**
|
* 导出比赛报名人员Excel
|
*/
|
@GetMapping("/applications")
|
public void exportApplications(
|
@RequestParam(required = false) String name,
|
@RequestParam(required = false) Long activityId,
|
@RequestParam(required = false) Integer state,
|
HttpServletResponse response) {
|
try {
|
log.info("导出比赛报名人员Excel, name: {}, activityId: {}, state: {}", name, activityId, state);
|
|
// 获取Excel数据
|
byte[] excelData = playerApplicationService.exportApplicationsToExcel(name, activityId, state);
|
|
// 设置响应头
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
String fileName = "报名人员_" + System.currentTimeMillis() + ".xlsx";
|
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
|
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + encodedFileName);
|
response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION);
|
response.setContentLength(excelData.length);
|
|
// 写入响应
|
response.getOutputStream().write(excelData);
|
response.getOutputStream().flush();
|
} catch (Exception e) {
|
log.error("导出比赛报名人员Excel失败", e);
|
try {
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
response.getWriter().write("导出失败: " + e.getMessage());
|
} catch (IOException ioException) {
|
log.error("写入错误响应失败", ioException);
|
}
|
}
|
}
|
}
|