liujie
2026-01-21 2d368b1f587edfe4e1c87e9dc1d5345cd43b8841
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.estate.expert.admin.controller.stat;
 
import com.deepoove.poi.XWPFTemplate;
import com.estate.expert.admin.enums.BizResultCode;
import com.estate.expert.admin.service.CheckTaskService;
import com.estate.expert.admin.service.ReportService;
import com.estate.expert.admin.service.ReportTemplateService;
import com.estate.expert.common.auth.IgnoreAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
 
@Api(tags = "管理后台/统计/报告下载")
@Slf4j
@CrossOrigin
@Controller
@RequestMapping("/admin/stat/report")
@Validated
public class ReportController {
 
    @Resource
    private ReportService reportService;
 
    @Resource
    private CheckTaskService checkTaskService;
 
    @Resource
    private ReportTemplateService reportTemplateService;
 
    @ApiOperation("模糊评价报告下载")
    @GetMapping("/fuzzyReport")
    @IgnoreAuth
    public void fuzzyReport(@ApiParam("检查任务ID") @RequestParam(value = "checkTaskId") Integer checkTaskId,
                          @ApiParam("报告模版ID") @RequestParam(value = "templateId") Integer templateId,
                          HttpServletResponse response) throws IOException {
        try (var doc = reportService.fuzzyReport(checkTaskId, templateId)) {
            download(doc, response, "模糊评价", checkTaskId, templateId);
        }
    }
 
    @ApiOperation("打分评价报告下载")
    @GetMapping("/scoreReport")
    @IgnoreAuth
    public void scoreReport(@ApiParam("检查任务ID") @RequestParam(value = "checkTaskId") Integer checkTaskId,
                          @ApiParam("报告模版ID") @RequestParam(value = "templateId") Integer templateId,
                          HttpServletResponse response) throws IOException {
        try (var doc = reportService.scoreReport(checkTaskId, templateId)) {
            download(doc, response, "打分评价", checkTaskId, templateId);
        }
    }
 
    @ApiOperation("模版示例下载")
    @GetMapping("/example")
    @IgnoreAuth
    public void example(@ApiParam("报告模版ID") @RequestParam(value = "templateId") Integer templateId,
                          HttpServletResponse response) throws IOException {
        try (var doc = reportService.fuzzyReport(1, templateId)) {
            download(doc, response, "示例",1, templateId);
        }
    }
 
    @ApiOperation("模糊评价报告PDF下载")
    @GetMapping("/fuzzyReportPdf")
    @IgnoreAuth
    public void fuzzyReportPdf(@ApiParam("检查任务ID") @RequestParam(value = "checkTaskId") Integer checkTaskId,
                               @ApiParam("报告模版ID") @RequestParam(value = "templateId") Integer templateId,
                               HttpServletResponse response) throws IOException {
        downloadPdf(checkTaskId, templateId, response, "模糊评价", true);
    }
 
    @ApiOperation("打分评价报告PDF下载")
    @GetMapping("/scoreReportPdf")
    @IgnoreAuth
    public void scoreReportPdf(@ApiParam("检查任务ID") @RequestParam(value = "checkTaskId") Integer checkTaskId,
                               @ApiParam("报告模版ID") @RequestParam(value = "templateId") Integer templateId,
                               HttpServletResponse response) throws IOException {
        downloadPdf(checkTaskId, templateId, response, "打分评价", false);
    }
 
    private void download(XWPFTemplate doc, HttpServletResponse response, String typeName,
                          Integer checkTaskId, Integer templateId) throws IOException {
        OutputStream out;
        BufferedOutputStream bos;
 
        var checkTask = checkTaskService.get(checkTaskId, true);
        var reportTemplate = reportTemplateService.get(templateId);
        BizResultCode.TASK_NOT_FOUND.check(checkTask == null);
 
        var docName = checkTask.getProject().getName()+ "_" + reportTemplate.getName() + "_" + typeName ;
        String encodedFileName = URLEncoder.encode(docName, "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=\"" + encodedFileName + ".docx\"");
 
        // HttpServletResponse response
        out = response.getOutputStream();
        bos = new BufferedOutputStream(out);
        doc.write(bos);
        bos.flush();
        out.flush();
    }
 
    private void downloadPdf(Integer checkTaskId, Integer templateId, HttpServletResponse response,
                             String typeName, boolean isFuzzy) throws IOException {
        OutputStream out;
        BufferedOutputStream bos;
 
        var checkTask = checkTaskService.get(checkTaskId, true);
        var reportTemplate = reportTemplateService.get(templateId);
        BizResultCode.TASK_NOT_FOUND.check(checkTask == null);
 
        var docName = checkTask.getProject().getName() + "_" + reportTemplate.getName() + "_" + typeName;
        String encodedFileName = URLEncoder.encode(docName, "UTF-8");
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "attachment;filename=\"" + encodedFileName + ".pdf\"");
 
        out = response.getOutputStream();
        bos = new BufferedOutputStream(out);
 
        if (isFuzzy) {
            reportService.fuzzyReportPdf(checkTaskId, templateId, bos);
        } else {
            reportService.scoreReportPdf(checkTaskId, templateId, bos);
        }
 
        bos.flush();
        out.flush();
    }
 
}