Przeglądaj źródła

controller增加接口,用于对打印、预览、下载pdf、下载纯数据excel进行统一处理

sunyj 9 lat temu
rodzic
commit
61944b36bc

+ 80 - 22
src/main/java/com/uas/report/controller/PrintController.java

@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Map;
 
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.lang.ArrayUtils;
@@ -16,6 +18,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 
 import com.uas.report.core.exception.SystemError;
 import com.uas.report.service.PrintService;
+import com.uas.report.util.ReportConstants;
 
 /**
  * 报表打印
@@ -33,6 +36,57 @@ public class PrintController {
 	@Autowired
 	private PrintService printService;
 
+	/**
+	 * 为UAS系统打印提供服务, 根据printType进行预览、打印、下载pdf、下载纯数据excel等操作
+	 * 
+	 * @param userName
+	 *            不为null;当前账套用户名
+	 * @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 request
+	 * @param response
+	 */
+	@RequestMapping("")
+	public void print(String userName, String reportName, String whereCondition, String otherParameters,
+			String printType, HttpServletRequest request, HttpServletResponse response) {
+		checkParameters(userName, reportName);
+
+		// printType为空,默认进入预览页
+		if (StringUtils.isEmpty(printType)) {
+			printType = ReportConstants.PREVIEW_PRINT_TYPE;
+		}
+
+		// 预览或打印
+		if (printType.equals(ReportConstants.PREVIEW_PRINT_TYPE)
+				|| printType.equals(ReportConstants.PRINT_PRINT_TYPE)) {
+			try {
+				request.getRequestDispatcher("preview").forward(request, response);
+			} catch (IOException | ServletException e) {
+				e.printStackTrace();
+				throw new SystemError(e.getMessage());
+			}
+		}
+		// 下载pdf、纯数据excel
+		else if (printType.equals(ReportConstants.PDF_PRINT_TYPE)
+				|| printType.equals(ReportConstants.EXCEL_PRINT_TYPE)) {
+			String exportFileType = "pdf";
+			if (printType.equals(ReportConstants.EXCEL_PRINT_TYPE)) {
+				exportFileType = "xls_with_only_data";
+			}
+			export(userName, reportName, whereCondition, otherParameters, exportFileType, response);
+		} else {
+			throw new SystemError("printType不合法");
+		}
+	}
+
 	/**
 	 * 导出报表
 	 * 
@@ -54,17 +108,7 @@ public class PrintController {
 	@ResponseBody
 	public void export(String userName, String reportName, String whereCondition, String otherParameters,
 			String exportFileType, HttpServletResponse response) {
-		String message = "";
-		if (StringUtils.isEmpty(userName)) {
-			message = "未传入当前账套用户名!";
-			logger.error(message);
-			throw new SystemError(message);
-		}
-		if (StringUtils.isEmpty(reportName)) {
-			message = "报表名称无效!";
-			logger.error(message);
-			throw new SystemError(message);
-		}
+		checkParameters(userName, reportName);
 
 		logger.info("开始导出报表:" + reportName);
 		if (StringUtils.isEmpty(exportFileType)) {
@@ -72,6 +116,7 @@ public class PrintController {
 		}
 
 		byte[] data = printService.export(userName, reportName, whereCondition, otherParameters, exportFileType);
+		String message = "";
 		if (ArrayUtils.isEmpty(data)) {
 			message = "报表导出失败:" + reportName;
 			logger.error(message);
@@ -116,17 +161,7 @@ public class PrintController {
 	@ResponseBody
 	public Map<String, Object> loadPdfData(String userName, String reportName, String whereCondition,
 			String otherParameters, Integer pageIndex, HttpServletResponse response) {
-		String message = "";
-		if (StringUtils.isEmpty(userName)) {
-			message = "未传入当前账套用户名!";
-			logger.error(message);
-			throw new SystemError(message);
-		}
-		if (StringUtils.isEmpty(reportName)) {
-			message = "报表名称无效!";
-			logger.error(message);
-			throw new SystemError(message);
-		}
+		checkParameters(userName, reportName);
 
 		logger.info("开始预览报表:" + reportName);
 		Map<String, Object> result = printService.preview(userName, reportName, whereCondition, otherParameters,
@@ -135,6 +170,7 @@ public class PrintController {
 		if (result != null && result.get("data") != null) {
 			data = (byte[]) result.get("data");
 		}
+		String message = "";
 		if (data == null) {
 			message = "获取预览数据失败";
 			logger.error(message);
@@ -145,4 +181,26 @@ public class PrintController {
 		logger.info("预览报表成功:" + reportName);
 		return result;
 	}
+
+	/**
+	 * 检查userName、reportName参数是否有效,无效则抛出异常
+	 * 
+	 * @param userName
+	 *            账套名
+	 * @param reportName
+	 *            报表名
+	 */
+	private void checkParameters(String userName, String reportName) {
+		String message = "";
+		if (StringUtils.isEmpty(userName)) {
+			message = "未传入当前账套用户名!";
+			logger.error(message);
+			throw new SystemError(message);
+		}
+		if (StringUtils.isEmpty(reportName)) {
+			message = "未传入报表名称!";
+			logger.error(message);
+			throw new SystemError(message);
+		}
+	}
 }

+ 8 - 0
src/main/java/com/uas/report/util/ReportConstants.java

@@ -17,4 +17,12 @@ public class ReportConstants {
 	 * 报表参数 - REPORT_DIR,报表的路径
 	 */
 	public static final String PARAMETER_REPORT_DIR = "REPORT_DIR";
+
+	/**
+	 * 请求类型,判断是预览、打印、下载pdf还是下载纯数据excel
+	 */
+	public static final String PREVIEW_PRINT_TYPE = "PREVIEW";
+	public static final String PRINT_PRINT_TYPE = "PRINT";
+	public static final String PDF_PRINT_TYPE = "PDF";
+	public static final String EXCEL_PRINT_TYPE = "EXCEL";
 }

+ 0 - 1
src/main/webapp/WEB-INF/webmvc.xml

@@ -33,6 +33,5 @@
 	<mvc:view-controller path="/" view-name="index" />
 	<mvc:view-controller path="/fileUpload" view-name="fileUpload" />
 	<mvc:view-controller path="/prevnext" view-name="prevnext" />
-	<mvc:view-controller path="/print" view-name="print" />
 	<mvc:view-controller path="preview" view-name="preview" />
 </beans>