Procházet zdrojové kódy

增加获取文件状态的接口;预览时后台在生成pdf后生成纯数据excel,以提高下载速度;

sunyj před 9 roky
rodič
revize
6673de3525

+ 45 - 4
src/main/java/com/uas/report/controller/PrintController.java

@@ -201,11 +201,12 @@ public class PrintController {
 			// 检查第一页的pdf文件是否存在,若不存在,先生成第一页pdf,返回给前台展示,
 			// 再开线程生成后面页的pdf和总的pdf(即未分页的pdf),以备后续可能的打印、下载操作使用
 			if (pageIndex != null) {
-				Integer pageSize = printService.previewFirstPage(userName, reportName, whereCondition, otherParameters, file.getPath());
-				result=new HashMap<>();
+				Integer pageSize = printService.previewFirstPage(userName, reportName, whereCondition, otherParameters,
+						file.getPath());
+				result = new HashMap<>();
 				result.put("pageSize", pageSize);
-			} 
-			//参数pageIndex为null,表示是直接打印,需要先生成总的pdf
+			}
+			// 参数pageIndex为null,表示是直接打印,需要先生成总的pdf
 			else {
 				result = printService.preview(userName, reportName, whereCondition, otherParameters, null);
 				byte[] data = null;
@@ -228,6 +229,46 @@ public class PrintController {
 		return result;
 	}
 
+	/**
+	 * 获取生成的pdf或者xls的信息
+	 * 
+	 * @param userName
+	 *            不为null;当前账套用户名
+	 * @param reportName
+	 *            不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
+	 * @param whereCondition
+	 *            可为null;where之后的条件(包括where)
+	 * @param otherParameters
+	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
+	 *            JSON格式,数据为键值对
+	 * @param pdfOrXls
+	 *            pdf或者xls
+	 * @return 文件的信息
+	 */
+	@RequestMapping(value = "/getGeneratedPdfOrXlsInformation")
+	@ResponseBody
+	public Map<String, Object> getGeneratedPdfOrXlsInformation(String userName, String reportName,
+			String whereCondition, String otherParameters, String pdfOrXls) {
+		Map<String, Object> result = new HashMap<>();
+		if (StringUtils.isEmpty(pdfOrXls)) {
+			pdfOrXls = "pdf";
+		}
+		String filePath = ReportConstants.GENERATED_FILES_PATH + reportName + "/"
+				+ printService.generateFileName(userName, reportName, whereCondition, otherParameters);
+		File file = new File(PathUtils.getAppPath() + filePath + "." + pdfOrXls);
+		result.put("file", file.getPath());
+		if (pdfOrXls.equals("pdf")) {
+			result.put("valid", printService.isFileValid(file.getPath()));
+			result.put("size", file.length());
+		} else if (pdfOrXls.equals("xls")) {
+			result.put("valid", printService.isFileValid(file.getPath()));
+			result.put("size", file.length());
+		} else {
+			throw new SystemError("pdfOrXls只能为pdf或xls");
+		}
+		return result;
+	}
+
 	/**
 	 * 检查userName、reportName参数是否有效,无效则抛出异常
 	 * 

+ 1 - 1
src/main/java/com/uas/report/service/PrintService.java

@@ -51,7 +51,7 @@ public interface PrintService {
 	/**
 	 * 先生成第一页pdf,返回给前台展示,以提高预览速度;
 	 * 
-	 * 再开线程生成后面页的pdf和总的pdf(即未分页的pdf),以备后续可能的打印、下载操作使用
+	 * 再开线程生成后面页的pdf、总的pdf(即未分页的pdf)、纯数据excel,以备后续可能的打印、下载操作使用
 	 * 
 	 * @param userName
 	 *            不为null;当前账套用户名

+ 16 - 4
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -17,6 +17,7 @@ import java.util.Properties;
 
 import javax.sql.DataSource;
 
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -93,17 +94,28 @@ public class PrintServiceImpl implements PrintService {
 		final Integer pageSize = writePdfData(userName, reportName, whereCondition, otherParameters,
 				pdfFilePath.replace(".pdf", "_1.pdf"), 1);
 
-		// 再开线程生成后面页的pdf和总的pdf(即未分页的pdf)
+		// 再开线程生成后面页的pdf、总的pdf(即未分页的pdf)、纯数据excel
 		new Thread(new Runnable() {
 			@Override
 			public void run() {
-				// 生成之后页的pdf
-				for (int i = 2; i <= pageSize; i++) {
+				// 生成之后2~5页的pdf,防止数据量较大的情况下,卡在生成总的pdf阶段,此时查看前5页也不会卡顿
+				for (int i = 2; i <= Math.min(pageSize, 5); i++) {
 					writePdfData(userName, reportName, whereCondition, otherParameters,
 							pdfFilePath.replace(".pdf", "_" + i + ".pdf"), i);
 				}
 				// 生成总的pdf
 				writePdfData(userName, reportName, whereCondition, otherParameters, pdfFilePath, null);
+				writePagedPdfFiles(pdfFilePath);
+				// 生成纯数据excel
+				File file = new File(pdfFilePath.replace(".pdf", ".xls"));
+				// 文件无效(不存在或过期),创建
+				if (!isFileValid(file.getPath())) {
+					byte[] data = export(userName, reportName, whereCondition, otherParameters, "xls_with_only_data");
+					if (ArrayUtils.isEmpty(data)) {
+						throw new SystemError("报表导出失败:" + reportName);
+					}
+					writeDataToFile(file.getPath(), data);
+				}
 			}
 		}).start();
 
@@ -148,7 +160,7 @@ public class PrintServiceImpl implements PrintService {
 			FileOutputStream fos = new FileOutputStream(file);
 			fos.write(data);
 			fos.flush();
-			logger.info("Write file..." + file.getPath());
+			logger.info("Writed file..." + file.getPath());
 			fos.close();
 		} catch (IOException e) {
 			throw new SystemError(e);

+ 3 - 3
src/main/java/com/uas/report/service/impl/ResourceServiceImpl.java

@@ -50,14 +50,14 @@ public class ResourceServiceImpl implements ResourceService {
 	@Override
 	public List<Resource> syncResources(String userName)
 			throws ClientProtocolException, URISyntaxException, IOException {
-		logger.info("Synchronizing resources...");
+//		logger.info("Synchronizing resources...");
 		List<Resource> synchronizingResources = getSynchronizingResources(userName);
 		if (!CollectionUtils.isEmpty(synchronizingResources)) {
 			for (Resource synchronizingResource : synchronizingResources) {
 				downloadFile(synchronizingResource);
 			}
 		}
-		logger.info("Synchronized");
+//		logger.info("Synchronized");
 		return synchronizingResources;
 	}
 
@@ -283,7 +283,7 @@ public class ResourceServiceImpl implements ResourceService {
 		// 采用HTTP Basic验证
 		request.setHeader("Authorization", "Basic " + jsRestAPIConf.getAuthorization());
 		HttpResponse response = httpClient.execute(request);
-		logger.info(request.getMethod() + " " + request.getURI() + " " + response.getStatusLine());
+//		logger.info(request.getMethod() + " " + request.getURI() + " " + response.getStatusLine());
 		return response;
 	}