|
|
@@ -3,9 +3,11 @@ package com.uas.report.controller;
|
|
|
import com.uas.report.SystemProperties;
|
|
|
import com.uas.report.model.ExportType;
|
|
|
import com.uas.report.model.Platform;
|
|
|
+import com.uas.report.model.PrintParameter;
|
|
|
import com.uas.report.model.PrintType;
|
|
|
import com.uas.report.service.FileService;
|
|
|
import com.uas.report.service.PrintService;
|
|
|
+import com.uas.report.util.Assert;
|
|
|
import com.uas.report.util.ReportUtils;
|
|
|
import com.uas.report.util.StringUtils;
|
|
|
import net.sf.jasperreports.engine.JRException;
|
|
|
@@ -13,6 +15,7 @@ import org.dom4j.DocumentException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
@@ -23,6 +26,9 @@ import java.io.IOException;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.ConcurrentMap;
|
|
|
|
|
|
/**
|
|
|
* 报表打印
|
|
|
@@ -47,108 +53,130 @@ public class PrintController {
|
|
|
@Autowired
|
|
|
private PdfController pdfController;
|
|
|
|
|
|
+ /**
|
|
|
+ * 缓存的打印参数
|
|
|
+ */
|
|
|
+ private ConcurrentMap<String, PrintParameter> printParameters = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 预打印,用于缓存参数,以便之后通过 GET 方式调用打印接口(GET 下参数不能过长)
|
|
|
+ *
|
|
|
+ * @param userName 不为null;当前账套名称
|
|
|
+ * @param profile 可选(UAS等系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
|
|
|
+ * @param reportName 不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
|
|
|
+ * @param whereCondition 可为null;where之后的条件(包括where)
|
|
|
+ * @param otherParameters 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
|
|
|
+ * JSON格式,数据为键值对
|
|
|
+ * @param printType 打印类型,可为PREVIEW_PRINT_TYPE、PRINT_PRINT_TYPE、PDF_PRINT_TYPE、
|
|
|
+ * EXCEL_PRINT_TYPE
|
|
|
+ * @param title 导出为文件时的名称
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return 本次请求的 id
|
|
|
+ * @throws SQLException
|
|
|
+ * @throws DocumentException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws JRException
|
|
|
+ * @throws ServletException
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/pre")
|
|
|
+ @ResponseBody
|
|
|
+ public String preprint(String userName, String profile, String reportName, String whereCondition, String otherParameters,
|
|
|
+ String printType, String title, HttpServletRequest request, HttpServletResponse response)
|
|
|
+ throws JRException, IOException, DocumentException, SQLException, ServletException {
|
|
|
+ userName = userName == null ? null : userName.toUpperCase();
|
|
|
+ // printType为空,默认进入预览页
|
|
|
+ PrintType type = StringUtils.isEmpty(printType) ? PrintType.PREVIEW : PrintType.checkType(printType);
|
|
|
+ String requestId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ printParameters.put(requestId, new PrintParameter(userName, profile, reportName, whereCondition, otherParameters, type, title));
|
|
|
+ return requestId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取缓存的打印参数
|
|
|
+ *
|
|
|
+ * @param id 预打印时获取的 id
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return 缓存的打印参数
|
|
|
+ */
|
|
|
+ @RequestMapping("/parameter")
|
|
|
+ @ResponseBody
|
|
|
+ public PrintParameter getPrintParameter(@RequestParam String id, HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ PrintParameter printParameter = printParameters.get(id);
|
|
|
+ Assert.notNull(printParameter, "id 不存在");
|
|
|
+ return printParameter;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 为UAS系统打印提供服务, 根据printType进行预览、打印、下载pdf、下载纯数据excel等操作
|
|
|
*
|
|
|
- * @param userName
|
|
|
- * 不为null;当前账套名称
|
|
|
- * @param profile
|
|
|
- * 可选(UAS等系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
|
|
|
- * @param reportName
|
|
|
- * 不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
|
|
|
- * @param whereCondition
|
|
|
- * 可为null;where之后的条件(包括where)
|
|
|
- * @param otherParameters
|
|
|
- * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
|
|
|
- * JSON格式,数据为键值对
|
|
|
- * @param printType
|
|
|
- * 打印类型,可为PREVIEW_PRINT_TYPE、PRINT_PRINT_TYPE、PDF_PRINT_TYPE、
|
|
|
- * EXCEL_PRINT_TYPE
|
|
|
- * @param title
|
|
|
- * 导出为文件时的名称
|
|
|
- * @param request
|
|
|
- * @param response
|
|
|
- * @throws SQLException
|
|
|
+ * @param id 预打印时获取的 id
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @throws SQLException
|
|
|
* @throws DocumentException
|
|
|
* @throws IOException
|
|
|
* @throws JRException
|
|
|
- * @throws ServletException
|
|
|
+ * @throws ServletException
|
|
|
*/
|
|
|
- @RequestMapping()
|
|
|
- public void print(String userName, String profile, String reportName, String whereCondition, String otherParameters,
|
|
|
- String printType, String title, HttpServletRequest request, HttpServletResponse response)
|
|
|
+ @RequestMapping
|
|
|
+ public void print(@RequestParam String id, HttpServletRequest request, HttpServletResponse response)
|
|
|
throws JRException, IOException, DocumentException, SQLException, ServletException {
|
|
|
- userName = userName == null ? null : userName.toUpperCase();
|
|
|
- // printType为空,默认进入预览页
|
|
|
- PrintType type = StringUtils.isEmpty(printType) ? PrintType.PREVIEW : PrintType.checkType(printType);
|
|
|
-
|
|
|
- switch (type) {
|
|
|
+ switch (getPrintParameter(id, request, response).getPrintType()) {
|
|
|
// 预览或打印
|
|
|
case PREVIEW:
|
|
|
case PRINT:
|
|
|
- request.getRequestDispatcher("preview").forward(request, response);
|
|
|
+ request.getRequestDispatcher("preview").forward(request, response);
|
|
|
break;
|
|
|
case PDF:
|
|
|
- export(userName, profile, reportName, whereCondition, otherParameters, ExportType.PDF.name(), true, title,
|
|
|
- request, response);
|
|
|
+ export(id, ExportType.PDF.name(), true, request, response);
|
|
|
break;
|
|
|
// 该下载接口供 UAS 系统使用,应其要求,printType 为{@link PrintType.EXCEL}时,下载纯数据的 excel
|
|
|
case EXCEL:
|
|
|
if (systemProperties.isUseXlsx()) {
|
|
|
- export(userName, profile, reportName, whereCondition, otherParameters, ExportType.XLSX_DATA.name(),
|
|
|
- true, title, request, response);
|
|
|
+ export(id, ExportType.XLSX_DATA.name(), true, request, response);
|
|
|
} else {
|
|
|
- export(userName, profile, reportName, whereCondition, otherParameters, ExportType.XLS_DATA.name(), true,
|
|
|
- title, request, response);
|
|
|
+ export(id, ExportType.XLS_DATA.name(), true, request, response);
|
|
|
}
|
|
|
break;
|
|
|
case WORD:
|
|
|
- export(userName, profile, reportName, whereCondition, otherParameters, ExportType.DOC.name(), true, title,
|
|
|
- request, response);
|
|
|
+ export(id, ExportType.DOC.name(), true, request, response);
|
|
|
break;
|
|
|
case TEXT:
|
|
|
- export(userName, profile, reportName, whereCondition, otherParameters, ExportType.TXT.name(), true, title,
|
|
|
- request, response);
|
|
|
+ export(id, ExportType.TXT.name(), true, request, response);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 导出报表
|
|
|
- *
|
|
|
- * @param userName
|
|
|
- * 不为null;当前账套名称
|
|
|
- * @param profile
|
|
|
- * 可选(UAS等系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
|
|
|
- * @param reportName
|
|
|
- * 不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
|
|
|
- * @param whereCondition
|
|
|
- * 可为null;where之后的条件(包括where)
|
|
|
- * @param otherParameters
|
|
|
- * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
|
|
|
- * JSON格式,数据为键值对
|
|
|
- * @param exportFileType
|
|
|
- * 报表导出的格式,默认为pdf
|
|
|
- * @param flush
|
|
|
- * 是否强制刷新pdf、xls
|
|
|
- * @param title
|
|
|
- * 导出为文件时的名称
|
|
|
- * @param request
|
|
|
- * @param response
|
|
|
- * @throws SQLException
|
|
|
- * @throws DocumentException
|
|
|
- * @throws IOException
|
|
|
- * @throws JRException
|
|
|
- */
|
|
|
- @RequestMapping("/export")
|
|
|
- @ResponseBody
|
|
|
- public void export(String userName, String profile, String reportName, String whereCondition,
|
|
|
- String otherParameters, String exportFileType, Boolean flush, String title, HttpServletRequest request,
|
|
|
+ /**
|
|
|
+ * 导出报表
|
|
|
+ *
|
|
|
+ * @param id 预打印时获取的 id
|
|
|
+ * @param exportFileType 报表导出的格式,默认为pdf
|
|
|
+ * @param flush 是否强制刷新pdf、xls
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @throws SQLException
|
|
|
+ * @throws DocumentException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws JRException
|
|
|
+ */
|
|
|
+ @RequestMapping("/export")
|
|
|
+ @ResponseBody
|
|
|
+ public void export(@RequestParam String id, String exportFileType, Boolean flush, HttpServletRequest request,
|
|
|
HttpServletResponse response) throws JRException, IOException, DocumentException, SQLException {
|
|
|
// TODO show download process
|
|
|
- userName = userName == null ? null : userName.toUpperCase();
|
|
|
- ReportUtils.checkParameters(userName, reportName);
|
|
|
- String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
|
|
|
+ PrintParameter printParameter = getPrintParameter(id, request, response);
|
|
|
+ String userName = printParameter.getUserName();
|
|
|
+ String profile = printParameter.getProfile();
|
|
|
+ String reportName = printParameter.getReportName();
|
|
|
+ String whereCondition = printParameter.getWhereCondition();
|
|
|
+ String otherParameters = printParameter.getOtherParameters();
|
|
|
+ String title = printParameter.getTitle();
|
|
|
+ ReportUtils.checkParameters(userName, reportName);
|
|
|
+
|
|
|
+ String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
|
|
|
ExportType exportType = StringUtils.isEmpty(exportFileType) ? ExportType.PDF
|
|
|
: ExportType.checkType(exportFileType);
|
|
|
|
|
|
@@ -170,39 +198,31 @@ public class PrintController {
|
|
|
fileService.rangeDownload(file, exportFileName, request, response);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 报表预览时获取pdf相对路径
|
|
|
- *
|
|
|
- * @param userName
|
|
|
- * 不为null;当前账套名称
|
|
|
- * @param profile
|
|
|
- * 可选(UAS等系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
|
|
|
- * @param reportName
|
|
|
- * 不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
|
|
|
- * @param whereCondition
|
|
|
- * 可为null;where之后的条件(包括where)
|
|
|
- * @param otherParameters
|
|
|
- * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
|
|
|
- * JSON格式,数据为键值对
|
|
|
- * @param flush
|
|
|
- * 是否强制刷新pdf、xls
|
|
|
- * @param request
|
|
|
- * @param response
|
|
|
- * @return pdf相对路径
|
|
|
- * @throws SQLException
|
|
|
- * @throws DocumentException
|
|
|
- * @throws IOException
|
|
|
- * @throws JRException
|
|
|
- */
|
|
|
- @RequestMapping(value = "/pdfPath")
|
|
|
+ /**
|
|
|
+ * 报表预览时获取pdf相对路径
|
|
|
+ *
|
|
|
+ * @param id 预打印时获取的 id
|
|
|
+ * @param flush 是否强制刷新pdf、xls
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return pdf相对路径
|
|
|
+ * @throws SQLException
|
|
|
+ * @throws DocumentException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws JRException
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/pdfPath")
|
|
|
@ResponseBody
|
|
|
- public String getPdfPath(String userName, final String profile, final String reportName,
|
|
|
- final String whereCondition, final String otherParameters, Boolean flush, HttpServletRequest request,
|
|
|
- HttpServletResponse response) throws JRException, IOException, DocumentException, SQLException {
|
|
|
- userName = userName == null ? null : userName.toUpperCase();
|
|
|
+ public String getPdfPath(@RequestParam String id, Boolean flush, HttpServletRequest request, HttpServletResponse response) throws JRException, IOException, DocumentException, SQLException {
|
|
|
+ PrintParameter printParameter = getPrintParameter(id, request, response);
|
|
|
+ String userName = printParameter.getUserName();
|
|
|
+ String profile = printParameter.getProfile();
|
|
|
+ String reportName = printParameter.getReportName();
|
|
|
+ String whereCondition = printParameter.getWhereCondition();
|
|
|
+ String otherParameters = printParameter.getOtherParameters();
|
|
|
ReportUtils.checkParameters(userName, reportName);
|
|
|
- String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
|
|
|
|
|
|
+ String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
|
|
|
String pdfPath = reportName + "/"
|
|
|
+ fileService.generateFileName(userName, profile, whereCondition, otherParameters, ExportType.PDF.getQualifier())
|
|
|
+ "." + ExportType.PDF.getQualifier();
|