zhizhijie
2026-10-06 bee9e18c3af284ff432a4472f1d409f5a6dc09a7
traffic-audit-server/src/main/java/com/trafficaudit/holiday/controller/HolidayStatController.java
New file
@@ -0,0 +1,98 @@
package com.trafficaudit.holiday.controller;
import com.trafficaudit.common.Result;
import com.trafficaudit.holiday.service.HolidayBulletinService;
import com.trafficaudit.holiday.service.HolidayTrendService;
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;
    @Resource
    private HolidayTrendService trendService;
    @GetMapping("/overview")
    public Result<Map<String, Object>> 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<Map<String, Object>> 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<byte[]> 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<byte[]> 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);
    }
    @GetMapping("/trend")
    public ResponseEntity<byte[]> trend(@RequestParam("year") int year,
                                        @RequestParam("holidayType") String holidayType) throws Exception {
        byte[] data = trendService.generate(year, holidayType);
        String fileName = year + "年" + holidayTypeFileLabel(holidayType) + "假期全省综合运输客流量走势图.docx";
        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 "十一";
    }
}