Finish Task #8394 Cost:8h 【PC端】新增海外订单排产需求

This commit is contained in:
时间淡忘一切 2023-11-06 09:16:31 +08:00
parent b491b9a30b
commit f52b5ef762
2 changed files with 97 additions and 0 deletions

View File

@ -229,6 +229,13 @@ public class ContractService {
return desc;
}
/**
* 返回 pdf 文件
* @param templateName
* @param dataMap
* @return
* @throws Exception
*/
public File download(String templateName, Map<String, Object> dataMap) throws Exception {
OutputStream os = null;
@ -269,4 +276,49 @@ public class ContractService {
}
}
}
/**
* 返回 word 文件
* @param templateName
* @param dataMap
* @return
* @throws Exception
*/
public File downloadWord(String templateName, Map<String, Object> dataMap) throws Exception {
OutputStream os = null;
try {
File outFile = new File(Consts.uploadAbsDir());
if (!outFile.exists()) {
outFile.mkdirs();
}
String templatePath = Consts.getTemplatePath();
String abTemplatePath = templatePath + File.separator + templateName + ".docx";
//判断模板是否存在
File file = new File(abTemplatePath);
if(!file.exists()){
throw new KkFileException("请先上传模板!");
}
XWPFDocument xwpfDocument = WordExportUtil.exportWord07(abTemplatePath, dataMap);
//如果是签收单则需要合并单元格
if (Consts.RECEIPT_FORM.equals(templateName)){
List<Object> invoiceResList = (List<Object>) MapUtils.getObject(dataMap, "invoiceResList");
mergeCell(xwpfDocument,0,7,7+invoiceResList.size()-1,Arrays.asList(5,6));
}
String docxFile = "/"+ UUID.randomUUID().toString().replace("-", "") + ".docx";
//通过填参 生产文件 并放到 upload 文件夹中
String path = Consts.uploadAbsDir();
String pathFile = path + docxFile;
os = new FileOutputStream(pathFile);
xwpfDocument.write(os);
//返回word
return new File(pathFile);
} finally {
if (os != null) {
os.close();
}
}
}
}

View File

@ -155,4 +155,49 @@ public class ContractController {
return new ServiceResponse<>(null, "fail", ServiceResponse.RESULT_SERVER_ERR_CODE);
}
}
/**
* 下载 word 文件
*/
@ApiParam("下载word文件")
@PostMapping("/downloadWord/{templateName}")
public ServiceResponse downloadWord(
@PathVariable String templateName,
@RequestBody Map<String, Object> dataMap,
HttpServletResponse response
) {
try {
log.error(JSON.toJSONString(dataMap));
OutputStream outstream = null;
File file = contractService.downloadWord(templateName, dataMap);
outstream = response.getOutputStream();
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
String filename = dataMap.get("conCode") + ".docx";
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
// 刷新缓冲
response.flushBuffer();
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] b = new byte[bufferedInputStream.available()];
bufferedInputStream.read(b);
outstream.write(b);
bufferedInputStream.close();
outstream.flush();
outstream.close();
return new ServiceResponse<>("success",
ServiceResponse.RESULT_CODE_SUCCESS_CODE);
} catch (Exception e) {
log.error("生成错误:", e);
if (e instanceof KkFileException) {
KkFileException kkFileException = (KkFileException) e;
return new ServiceResponse<>(null, kkFileException.getMessage(), ServiceResponse.RESULT_CODE_ERROR_CODE);
}
return new ServiceResponse<>(null, "fail", ServiceResponse.RESULT_SERVER_ERR_CODE);
}
}
}