longz
2026-04-21 c4db0a4526f61758272c92396aa788d84f098102
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
package com.sinata.appointment.controller;
 
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sinata.appointment.common.Result;
import com.sinata.appointment.common.ResultUtil;
import com.sinata.appointment.dto.ExportTaskStatusVo;
import com.sinata.appointment.service.ExportApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@Tag(name = "导出")
@RestController
@RequestMapping("/api/exports")
@RequiredArgsConstructor
public class ExportController {
 
    private final ExportApplicationService exportApplicationService;
 
    @Operation(summary = "异步导出任务入队")
    @PostMapping("/enqueue")
    @SaCheckPermission("exports:enqueue")
    public Result<Map<String, String>> enqueue(@RequestBody Map<String, Object> body) {
        String type = body.get("type") != null ? body.get("type").toString() : "appointment";
        @SuppressWarnings("unchecked")
        Map<String, Object> criteria =
                body.get("criteria") instanceof Map ? (Map<String, Object>) body.get("criteria") : Map.of();
        String taskId = exportApplicationService.enqueueExport(type, criteria);
        Map<String, String> res = new HashMap<>();
        res.put("taskId", taskId);
        return ResultUtil.data(res);
    }
 
    @Operation(summary = "查询导出任务状态")
    @GetMapping("/{taskId}")
    @SaCheckPermission("exports:enqueue")
    public Result<ExportTaskStatusVo> status(@PathVariable String taskId) {
        ExportTaskStatusVo status = exportApplicationService.getTaskStatus(taskId);
        if (status == null) {
            return ResultUtil.error(404, "导出任务不存在");
        }
        return ResultUtil.data(status);
    }
 
    @Operation(summary = "下载导出文件")
    @GetMapping("/{taskId}/download")
    @SaCheckPermission("exports:enqueue")
    public ResponseEntity<byte[]> download(@PathVariable String taskId) throws IOException {
        Path filePath = exportApplicationService.getExportFilePath(taskId);
        if (filePath == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new byte[0]);
        }
        return ResponseEntity.ok()
                .contentType(
                        MediaType.parseMediaType(
                                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .header(
                        HttpHeaders.CONTENT_DISPOSITION,
                        "attachment; filename=\"" + filePath.getFileName() + "\"")
                .body(Files.readAllBytes(filePath));
    }
}