package com.trafficaudit.holiday.controller; import com.trafficaudit.common.Result; import com.trafficaudit.holiday.service.HolidayBulletinService; import com.trafficaudit.holiday.service.HolidayStatService; 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.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.net.URLEncoder; import java.time.LocalDate; import java.util.Map; @RestController @RequestMapping("/api/holiday") public class HolidayStatController { @Resource private HolidayStatService statService; @Resource private HolidayBulletinService bulletinService; @GetMapping("/overview") public Result> overview(@RequestParam("year") int year, @RequestParam("holidayType") String holidayType) { try { return Result.ok(statService.overview(year, holidayType)); } catch (Exception e) { return Result.error(400, e.getMessage()); } } @PostMapping("/import") public Result> importWorkbook(@RequestParam(value = "file", required = false) MultipartFile file, @RequestParam("year") int year, @RequestParam("holidayType") String holidayType, @RequestParam(value = "allowHistoricalOverwrite", defaultValue = "false") boolean allowHistoricalOverwrite) { try { return Result.ok(statService.importWorkbook(file, year, holidayType, allowHistoricalOverwrite)); } catch (Exception e) { return Result.error(400, e.getMessage()); } } @GetMapping("/export") public ResponseEntity exportWorkbook(@RequestParam("year") int year, @RequestParam("holidayType") String holidayType) throws Exception { byte[] data = statService.exportWorkbook(year, holidayType); String fileName = year + "年" + holidayTypeFileLabel(holidayType) + "数据.xlsx"; 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); } @GetMapping("/bulletin") public ResponseEntity bulletin(@RequestParam(value = "date", required = false) String date) throws Exception { LocalDate targetDate = (date == null || date.trim().isEmpty()) ? LocalDate.now().minusDays(1) : LocalDate.parse(date); byte[] data = bulletinService.generate(targetDate); String fileName = String.format("湖北省国庆假期综合运输客流快报(10月%d日).docx", targetDate.getDayOfMonth()); 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.wordprocessingml.document")) .body(data); } private String holidayTypeFileLabel(String holidayType) { if ("SPRING_FESTIVAL".equals(holidayType)) return "春节"; if ("LABOR_DAY".equals(holidayType)) return "五一"; return "十一"; } }