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(); } }