Commit 62fa9a5a authored by qinhu's avatar qinhu

报告-导出

parent f3cce5c9
...@@ -97,7 +97,11 @@ ...@@ -97,7 +97,11 @@
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
<version>4.6.8</version> <version>4.6.8</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- JwtUtil类需要jjwt --> <!-- JwtUtil类需要jjwt -->
<dependency> <dependency>
<groupId>io.jsonwebtoken</groupId> <groupId>io.jsonwebtoken</groupId>
...@@ -163,6 +167,14 @@ ...@@ -163,6 +167,14 @@
<include>*</include> <include>*</include>
</includes> </includes>
</resource> </resource>
<resource>
<directory>src/main/resources/templates</directory>
<filtering>true</filtering>
<includes>
<include>*</include>
</includes>
<targetPath>/templates</targetPath>
</resource>
<resource> <resource>
<directory>src/main/java/cn/wise/sc/cement/business/mapper/xml</directory> <directory>src/main/java/cn/wise/sc/cement/business/mapper/xml</directory>
<filtering>true</filtering> <filtering>true</filtering>
......
package cn.wise.sc.cement.business.controller; package cn.wise.sc.cement.business.controller;
import cn.wise.sc.cement.business.entity.EntrustReport;
import cn.wise.sc.cement.business.model.BaseResponse; import cn.wise.sc.cement.business.model.BaseResponse;
import cn.wise.sc.cement.business.model.PageQuery; import cn.wise.sc.cement.business.model.PageQuery;
import cn.wise.sc.cement.business.model.ReportDetailVo; import cn.wise.sc.cement.business.model.ReportDetailVo;
import cn.wise.sc.cement.business.model.vo.EntrustVo; import cn.wise.sc.cement.business.model.vo.EntrustVo;
import cn.wise.sc.cement.business.service.IEntrustService; import cn.wise.sc.cement.business.service.IEntrustService;
import cn.wise.sc.cement.business.util.PageUtil; import cn.wise.sc.cement.business.util.WordUtil;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
...@@ -20,8 +18,10 @@ import org.springframework.web.bind.annotation.PathVariable; ...@@ -20,8 +18,10 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List; import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors; import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/** /**
* @description: 报告管理 * @description: 报告管理
...@@ -39,6 +39,7 @@ public class ReportController { ...@@ -39,6 +39,7 @@ public class ReportController {
public ReportController(IEntrustService iEntrustService) { public ReportController(IEntrustService iEntrustService) {
this.iEntrustService = iEntrustService; this.iEntrustService = iEntrustService;
} }
@ApiOperation(value = "报告分页列表") @ApiOperation(value = "报告分页列表")
...@@ -74,14 +75,48 @@ public class ReportController { ...@@ -74,14 +75,48 @@ public class ReportController {
@GetMapping("/{entrustId}") @GetMapping("/{entrustId}")
@ApiOperation("获取报告详情") @ApiOperation("获取报告详情")
public BaseResponse<ReportDetailVo> getReportDetail(@PathVariable("entrustId") Integer entrustId, PageQuery pageQuery) { public void getReportDetail(@PathVariable("entrustId") Integer entrustId,
HttpServletResponse response) {
ReportDetailVo rts = iEntrustService.getReportDetail(entrustId); ReportDetailVo rts = iEntrustService.getReportDetail(entrustId);
if (rts ==null) {
return BaseResponse.errorMsg("没找到相关数据!"); Map<String, Object> beanParams = new HashMap<>();
}else { beanParams.put("sendName", rts.getSendName());
return BaseResponse.okData(rts); beanParams.put("sender", rts.getSender());
beanParams.put("sendDate", rts.getSendDate());
beanParams.put("sampleNum", rts.getSampleNum());
beanParams.put("sampleNames", set2String(rts.getSampleNames()));
beanParams.put("teamNames", set2String(rts.getTeamNames()));
beanParams.put("methodNames", set2StringN(rts.getMethodNames()));
beanParams.put("equipmentNames", set2StringN(rts.getEquipmentNames()));
beanParams.put("projectName", rts.getProjectName());
beanParams.put("printDate", rts.getPrintDate());
WordUtil.writeWordReport(rts.getProjectName() + "(报告)", "report.ftl",
beanParams, response);
}
private String set2String(Set<String> source) {
if (source.size() == 0) {
return "";
} }
StringBuilder strBuilder = new StringBuilder();
for (String target : source) {
strBuilder.append("、").append(target);
}
return strBuilder.replace(0, 1, "").toString();
} }
private String set2StringN(Set<String> source) {
if (source.size() == 0) {
return "";
}
StringBuilder strBuilder = new StringBuilder();
for (String target : source) {
strBuilder.append("&#10;").append(target);
}
return strBuilder.replace(0,5,"").toString();
}
} }
...@@ -1917,7 +1917,7 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl ...@@ -1917,7 +1917,7 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl
for (String idStr : teamSplit) { for (String idStr : teamSplit) {
int id = Integer.parseInt(idStr); int id = Integer.parseInt(idStr);
teams.forEach(opt -> { teams.forEach(opt -> {
if (opt.getId() == id) { if (opt.getId() == id && "1".equals(opt.getQualifications())) {
if (StrUtil.isBlank(arg.getTeamName())) { if (StrUtil.isBlank(arg.getTeamName())) {
arg.setTeamName(opt.getName()); arg.setTeamName(opt.getName());
} else { } else {
...@@ -1929,9 +1929,9 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl ...@@ -1929,9 +1929,9 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl
String methodNumbers = arg.getMethodNumbers(); String methodNumbers = arg.getMethodNumbers();
String[] methodSplit = methodNumbers.split("、"); String[] methodSplit = methodNumbers.split("、");
for (String idStr : methodSplit) { for (String idStr : methodSplit) {
int id = Integer.parseInt(idStr); String id = Integer.parseInt(idStr) +"";
methods.forEach(opt -> { methods.forEach(opt -> {
if (opt.getId() == id) { if (id.equals(opt.getNumber())) {
if (StrUtil.isBlank(arg.getMethodName())) { if (StrUtil.isBlank(arg.getMethodName())) {
arg.setMethodName(opt.getName() + " " + opt.getStandard()); arg.setMethodName(opt.getName() + " " + opt.getStandard());
} else { } else {
...@@ -2146,7 +2146,7 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl ...@@ -2146,7 +2146,7 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl
} }
//关联检测依据 //关联检测依据
if (StrUtil.isNotBlank(arg.getMethodName())) { if (StrUtil.isNotBlank(arg.getMethodName())) {
String[] methodSplits = arg.getMethodNumbers().split("、"); String[] methodSplits = arg.getMethodName().split("、");
for (String methodName : methodSplits) { for (String methodName : methodSplits) {
rts.getMethodNames().add(methodName); rts.getMethodNames().add(methodName);
} }
......
package cn.wise.sc.cement.business.util;
import freemarker.template.Configuration;
import freemarker.template.Template;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* @description:
* @author: qh
* @create: 2020-10-10 15:22
**/
public class WordUtil {
/**
* 模板路径
*/
private static final String FTL_FP = "/templates/";
private static Configuration configuration;
static {
configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
configuration.setDefaultEncoding("utf-8");
//读配置文件
// path = PropertiesUtil.get("FILE_PATH")+"/";
}
public static Boolean writeWordReport(
String templeName,
String templateFileName,
Map<String, Object> beanParams,
HttpServletResponse response) {
Writer out = null;
File file = null;
try {
configuration.setClassForTemplateLoading(WordUtil.class, FTL_FP);
Template template = configuration.getTemplate(templateFileName, "UTF-8");
//将文件输出到response,返回给客户端
String filePath;
filePath = "";
File file1 = new File(filePath);
if (!file1.exists()) {
file1.mkdirs();
}
//变量替换
//输出文件
file = new File(filePath + templeName);
FileOutputStream fos = new FileOutputStream(file);
out = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
template.process(beanParams, out);
FileInputStream in = new FileInputStream(file);
byte[] buffer = new byte[in.available()];
in.read(buffer);
in.close();
response.reset();
ServletOutputStream outputStream = response.getOutputStream();
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
response.setContentType("application/octet-stream");
templeName = new String((templeName).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
response.setHeader("Content-Disposition", "attachment;filename=" + templeName + ".xls");
outputStream.write(buffer);
outputStream.flush();
outputStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (out != null) {
out.close();
file.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment