xyc
7 小时以前 fbacc121df810f01e5b6ed43e6431dea6df270fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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<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);
    }
 
    private String holidayTypeFileLabel(String holidayType) {
        if ("SPRING_FESTIVAL".equals(holidayType)) return "春节";
        if ("LABOR_DAY".equals(holidayType)) return "五一";
        return "十一";
    }
}