“mitao”
2026-01-21 adf2dbafcd4b1c049017371bdcfcf1cad6017abe
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
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);
        }
    }
 
    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();
    }
 
}