PrintController.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.uas.report.controller;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.Map;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.commons.lang.ArrayUtils;
  10. import org.apache.log4j.Logger;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.util.StringUtils;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.ResponseBody;
  16. import com.uas.report.core.exception.SystemError;
  17. import com.uas.report.service.PrintService;
  18. import com.uas.report.util.PathUtils;
  19. import com.uas.report.util.ReportConstants;
  20. /**
  21. * 报表打印
  22. *
  23. * @author sunyj
  24. * @since 2016年8月16日 下午3:49:02
  25. */
  26. @Controller
  27. @RequestMapping("/print")
  28. public class PrintController {
  29. private static Logger logger = Logger.getLogger(PrintController.class);
  30. @Autowired
  31. private PrintService printService;
  32. /**
  33. * 为UAS系统打印提供服务, 根据printType进行预览、打印、下载pdf、下载纯数据excel等操作
  34. *
  35. * @param userName
  36. * 不为null;当前账套用户名
  37. * @param reportName
  38. * 不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
  39. * @param whereCondition
  40. * 可为null;where之后的条件(包括where)
  41. * @param otherParameters
  42. * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
  43. * JSON格式,数据为键值对
  44. * @param printType
  45. * 打印类型,可为PREVIEW_PRINT_TYPE、PRINT_PRINT_TYPE、PDF_PRINT_TYPE、
  46. * EXCEL_PRINT_TYPE
  47. * @param request
  48. * @param response
  49. */
  50. @RequestMapping("")
  51. public void print(String userName, String reportName, String whereCondition, String otherParameters,
  52. String printType, HttpServletRequest request, HttpServletResponse response) {
  53. // printType为空,默认进入预览页
  54. if (StringUtils.isEmpty(printType)) {
  55. printType = ReportConstants.PREVIEW_PRINT_TYPE;
  56. }
  57. // 预览或打印
  58. if (printType.equals(ReportConstants.PREVIEW_PRINT_TYPE)
  59. || printType.equals(ReportConstants.PRINT_PRINT_TYPE)) {
  60. try {
  61. request.getRequestDispatcher("preview2").forward(request, response);
  62. } catch (IOException | ServletException e) {
  63. e.printStackTrace();
  64. throw new SystemError(e.getMessage());
  65. }
  66. }
  67. // 下载pdf、纯数据excel
  68. else if (printType.equals(ReportConstants.PDF_PRINT_TYPE)
  69. || printType.equals(ReportConstants.EXCEL_PRINT_TYPE)) {
  70. String exportFileType = "pdf";
  71. if (printType.equals(ReportConstants.EXCEL_PRINT_TYPE)) {
  72. exportFileType = "xls_with_only_data";
  73. }
  74. export(userName, reportName, whereCondition, otherParameters, exportFileType, response);
  75. } else {
  76. throw new SystemError("printType不合法");
  77. }
  78. }
  79. /**
  80. * 导出报表
  81. *
  82. * @param userName
  83. * 不为null;当前账套用户名
  84. * @param reportName
  85. * 不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
  86. * @param whereCondition
  87. * 可为null;where之后的条件(包括where)
  88. * @param otherParameters
  89. * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
  90. * JSON格式,数据为键值对
  91. * @param exportFileType
  92. * 报表导出的格式,默认为pdf
  93. * @param response
  94. * @return
  95. */
  96. @RequestMapping("/export")
  97. @ResponseBody
  98. public void export(String userName, String reportName, String whereCondition, String otherParameters,
  99. String exportFileType, HttpServletResponse response) {
  100. checkParameters(userName, reportName);
  101. logger.info("开始导出报表:" + reportName);
  102. if (StringUtils.isEmpty(exportFileType)) {
  103. exportFileType = "pdf";
  104. }
  105. byte[] data = printService.export(userName, reportName, whereCondition, otherParameters, exportFileType);
  106. String message = "";
  107. if (ArrayUtils.isEmpty(data)) {
  108. message = "报表导出失败:" + reportName;
  109. logger.error(message);
  110. throw new SystemError(message);
  111. }
  112. try {
  113. String exportFileName = reportName;
  114. if (exportFileType.equals("xls_with_only_data")) {
  115. exportFileName += ".xls";
  116. } else {
  117. exportFileName += "." + exportFileType;
  118. }
  119. response.setHeader("Content-Disposition", "attachment;filename=" + exportFileName);
  120. OutputStream outputStream = response.getOutputStream();
  121. outputStream.write(data);
  122. outputStream.flush();
  123. outputStream.close();
  124. } catch (IOException e) {
  125. logger.error("浏览器重复请求!");
  126. }
  127. logger.info("报表导出完成:" + reportName);
  128. }
  129. /**
  130. * 报表预览时获取pdf流
  131. *
  132. * @param userName
  133. * 不为null;当前账套用户名
  134. * @param reportName
  135. * 不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
  136. * @param whereCondition
  137. * 可为null;where之后的条件(包括where)
  138. * @param otherParameters
  139. * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
  140. * JSON格式,数据为键值对
  141. * @param pageIndex
  142. * 分页展示,当前页码,从0开始
  143. * @return
  144. */
  145. @RequestMapping(value = "/loadPdfData")
  146. @ResponseBody
  147. public Map<String, Object> loadPdfData(String userName, String reportName, String whereCondition,
  148. String otherParameters, Integer pageIndex, HttpServletResponse response) {
  149. checkParameters(userName, reportName);
  150. logger.info("开始预览报表:" + reportName);
  151. Map<String, Object> result = printService.preview(userName, reportName, whereCondition, otherParameters,
  152. pageIndex);
  153. byte[] data = null;
  154. if (result != null && result.containsKey("data")) {
  155. data = (byte[]) result.remove("data");
  156. }
  157. String message = "";
  158. if (data == null) {
  159. message = "获取预览数据失败";
  160. logger.error(message);
  161. throw new SystemError(message);
  162. }
  163. String pdfPath = ReportConstants.GENERATED_FILES_PATH
  164. + printService.generateFileName(userName, reportName, whereCondition, otherParameters) + ".pdf";
  165. File file = new File(PathUtils.getAppPath() + pdfPath);
  166. // 文件过期或不存在,重新创建pdf文件
  167. if (printService.isFileExpired(file)) {
  168. printService.writePdfFile(file.getPath(), data);
  169. }
  170. result.put("pdfPath", pdfPath);
  171. logger.info("预览报表成功:" + reportName);
  172. return result;
  173. }
  174. /**
  175. * 检查userName、reportName参数是否有效,无效则抛出异常
  176. *
  177. * @param userName
  178. * 账套名
  179. * @param reportName
  180. * 报表名
  181. */
  182. private void checkParameters(String userName, String reportName) {
  183. String message = "";
  184. if (StringUtils.isEmpty(userName)) {
  185. message = "未传入当前账套用户名!";
  186. logger.error(message);
  187. throw new SystemError(message);
  188. }
  189. if (StringUtils.isEmpty(reportName)) {
  190. message = "未传入报表名称!";
  191. logger.error(message);
  192. throw new SystemError(message);
  193. }
  194. }
  195. }