|
|
@@ -0,0 +1,115 @@
|
|
|
+package com.uas.report.controller;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+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.ResponseBody;
|
|
|
+
|
|
|
+import com.uas.report.service.FileService;
|
|
|
+import com.uas.report.service.PrintService;
|
|
|
+import com.uas.report.util.ArrayUtils;
|
|
|
+import com.uas.report.util.CollectionUtils;
|
|
|
+import com.uas.report.util.FileUtils;
|
|
|
+import com.uas.report.util.Platform;
|
|
|
+import com.uas.report.util.ReportConstants;
|
|
|
+import com.uas.report.util.ReportUtils;
|
|
|
+
|
|
|
+import net.sf.jasperreports.engine.JRException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取pdf
|
|
|
+ *
|
|
|
+ * @author sunyj
|
|
|
+ * @since 2017年8月10日 下午7:17:38
|
|
|
+ */
|
|
|
+@Controller
|
|
|
+@RequestMapping("/pdf")
|
|
|
+public class PdfController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PrintService printService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileService fileService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param u
|
|
|
+ * 不为null;当前账套名称
|
|
|
+ * @param p
|
|
|
+ * 可选(UAS等系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
|
|
|
+ * @param r
|
|
|
+ * 不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
|
|
|
+ * @param w
|
|
|
+ * 可为null;where之后的条件(包括where)
|
|
|
+ * @param o
|
|
|
+ * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
|
|
|
+ * JSON格式,数据为键值对
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return JSON格式
|
|
|
+ *
|
|
|
+ * <table border=1 cellpadding=5 cellspacing=0 summary=
|
|
|
+ * "result">
|
|
|
+ * <tr>
|
|
|
+ * <th>key</th>
|
|
|
+ * <th>description</th>
|
|
|
+ * </tr>
|
|
|
+ * <tr>
|
|
|
+ * <td>pdf</td>
|
|
|
+ * <td>pdf相对路径</td>
|
|
|
+ * </tr>
|
|
|
+ * <tr>
|
|
|
+ * <td>pageSize</td>
|
|
|
+ * <td>pdf页数</td>
|
|
|
+ * </tr>
|
|
|
+ * <tr>
|
|
|
+ * <td>overload</td>
|
|
|
+ * <td>数据量是否过大</td>
|
|
|
+ * </tr>
|
|
|
+ * </table>
|
|
|
+ * @throws JRException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws DocumentException
|
|
|
+ * @throws SQLException
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/path")
|
|
|
+ @ResponseBody
|
|
|
+ public Map<String, Object> getPath(String u, String p, String r, String w, String o, HttpServletRequest request,
|
|
|
+ HttpServletResponse response) throws JRException, IOException, DocumentException, SQLException {
|
|
|
+ u = u == null ? null : u.toUpperCase();
|
|
|
+ ReportUtils.checkParameters(u, r);
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ // 判断是否过载
|
|
|
+ if (printService.overload(u, p, r, w, o, Platform.PHONE)) {
|
|
|
+ result.put("path", "");
|
|
|
+ result.put("pageSize", 0);
|
|
|
+ result.put("overload", true);
|
|
|
+ } else {
|
|
|
+ result = printService.preview(u, p, r, w, o, null);
|
|
|
+ if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
|
|
|
+ throw new IllegalStateException("pdf生成失败:" + u + "/" + r);
|
|
|
+ }
|
|
|
+ byte[] data = (byte[]) result.remove("data");
|
|
|
+ // 相对路径
|
|
|
+ String pdfPath = ReportConstants.GENERATED_FILES_PATH + r + "/"
|
|
|
+ + fileService.generateFileName(u, p, w, o, ReportConstants.FILE_TYPE_PDF) + "."
|
|
|
+ + ReportConstants.FILE_TYPE_PDF;
|
|
|
+ File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
|
|
|
+ FileUtils.write(file.getPath(), data);
|
|
|
+ result.put("path", pdfPath);
|
|
|
+ result.put("overload", false);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|