| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package com.uas.report.controller;
- import java.io.File;
- 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;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.util.StringUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- 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.PathUtils;
- import com.uas.report.util.ReportConstants;
- /**
- * 报表打印
- *
- * @author sunyj
- * @since 2016年8月16日 下午3:49:02
- */
- @Controller
- @RequestMapping("/print")
- public class PrintController {
- private static Logger logger = Logger.getLogger(PrintController.class);
- @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) {
- // 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("preview2").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不合法");
- }
- }
- /**
- * 导出报表
- *
- * @param userName
- * 不为null;当前账套用户名
- * @param reportName
- * 不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
- * @param whereCondition
- * 可为null;where之后的条件(包括where)
- * @param otherParameters
- * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
- * JSON格式,数据为键值对
- * @param exportFileType
- * 报表导出的格式,默认为pdf
- * @param response
- * @return
- */
- @RequestMapping("/export")
- @ResponseBody
- public void export(String userName, String reportName, String whereCondition, String otherParameters,
- String exportFileType, HttpServletResponse response) {
- checkParameters(userName, reportName);
- logger.info("开始导出报表:" + reportName);
- if (StringUtils.isEmpty(exportFileType)) {
- exportFileType = "pdf";
- }
- byte[] data = printService.export(userName, reportName, whereCondition, otherParameters, exportFileType);
- String message = "";
- if (ArrayUtils.isEmpty(data)) {
- message = "报表导出失败:" + reportName;
- logger.error(message);
- throw new SystemError(message);
- }
- try {
- String exportFileName = reportName;
- if (exportFileType.equals("xls_with_only_data")) {
- exportFileName += ".xls";
- } else {
- exportFileName += "." + exportFileType;
- }
- response.setHeader("Content-Disposition", "attachment;filename=" + exportFileName);
- OutputStream outputStream = response.getOutputStream();
- outputStream.write(data);
- outputStream.flush();
- outputStream.close();
- } catch (IOException e) {
- logger.error("浏览器重复请求!");
- }
- logger.info("报表导出完成:" + reportName);
- }
- /**
- * 报表预览时获取pdf流
- *
- * @param userName
- * 不为null;当前账套用户名
- * @param reportName
- * 不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
- * @param whereCondition
- * 可为null;where之后的条件(包括where)
- * @param otherParameters
- * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
- * JSON格式,数据为键值对
- * @param pageIndex
- * 分页展示,当前页码,从0开始
- * @return
- */
- @RequestMapping(value = "/loadPdfData")
- @ResponseBody
- public Map<String, Object> loadPdfData(String userName, String reportName, String whereCondition,
- String otherParameters, Integer pageIndex, HttpServletResponse response) {
- checkParameters(userName, reportName);
- logger.info("开始预览报表:" + reportName);
- Map<String, Object> result = printService.preview(userName, reportName, whereCondition, otherParameters,
- pageIndex);
- byte[] data = null;
- if (result != null && result.containsKey("data")) {
- data = (byte[]) result.remove("data");
- }
- String message = "";
- if (data == null) {
- message = "获取预览数据失败";
- logger.error(message);
- throw new SystemError(message);
- }
- String pdfPath = ReportConstants.GENERATED_FILES_PATH
- + printService.generateFileName(userName, reportName, whereCondition, otherParameters) + ".pdf";
- File file = new File(PathUtils.getAppPath() + pdfPath);
- // 文件过期或不存在,重新创建pdf文件
- if (printService.isFileExpired(file)) {
- printService.writePdfFile(file.getPath(), data);
- }
- result.put("pdfPath", pdfPath);
- 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);
- }
- }
- }
|