Browse Source

判断行数时,需替换动态参数

sunyj 8 years ago
parent
commit
539e311a8c

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

@@ -135,7 +135,7 @@ public class PrintController {
 			String otherParameters, String exportFileType, Boolean flush, String title, HttpServletRequest request,
 			HttpServletResponse response) {
 		ReportUtils.checkParameters(userName, reportName);
-		if (printService.overload(userName, profile, reportName, whereCondition)) {
+		if (printService.overload(userName, profile, reportName, whereCondition, otherParameters)) {
 			throw new ReportException("数据量过大,无法提供服务");
 		}
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
@@ -230,7 +230,7 @@ public class PrintController {
 			final String whereCondition, final String otherParameters, Integer pageIndex, Boolean flush,
 			HttpServletRequest request, HttpServletResponse response) {
 		ReportUtils.checkParameters(userName, reportName);
-		if (printService.overload(userName, profile, reportName, whereCondition)) {
+		if (printService.overload(userName, profile, reportName, whereCondition, otherParameters)) {
 			throw new ReportException("数据量过大,无法提供服务");
 		}
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
@@ -310,7 +310,7 @@ public class PrintController {
 		ReportUtils.checkParameters(userName, reportName);
 		Map<String, Object> result = new HashMap<>();
 		// 判断是否过载
-		if (printService.overload(userName, profile, reportName, whereCondition)) {
+		if (printService.overload(userName, profile, reportName, whereCondition, otherParameters)) {
 			result.put("data", "");
 			result.put("pageSize", 0);
 			result.put("overload", true);

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

@@ -100,8 +100,12 @@ public interface PrintService {
 	 *            不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
 	 * @param whereCondition
 	 *            可为null;where之后的条件(包括where)
+	 * @param otherParameters
+	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
+	 *            JSON格式,数据为键值对
 	 * @return 结果数目超出限制,返回true
 	 */
-	public boolean overload(String userName, String profile, String reportName, String whereCondition);
+	public boolean overload(String userName, String profile, String reportName, String whereCondition,
+			String otherParameters);
 
 }

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

@@ -638,7 +638,8 @@ public class PrintServiceImpl implements PrintService {
 	}
 
 	@Override
-	public boolean overload(String userName, String profile, String reportName, String whereCondition) {
+	public boolean overload(String userName, String profile, String reportName, String whereCondition,
+			String otherParameters) {
 		DataSource dataSource = MasterManager.getDataSource(userName, profile);
 		if (dataSource == null) {
 			throw new ReportException("获取数据源失败");
@@ -660,7 +661,7 @@ public class PrintServiceImpl implements PrintService {
 			}
 
 			// 因为子报表数据量较小,而且其参数来自主报表,无法简单地进行判断,所以不判断子报表是否过载
-			int count = getCount(jrxmlFilePath, whereCondition, connection);
+			int count = getCount(jrxmlFilePath, whereCondition, otherParameters, connection);
 			logger.info("count... " + count);
 			return count >= MAX_RECODR_SIZE;
 		} catch (Exception e) {
@@ -681,9 +682,10 @@ public class PrintServiceImpl implements PrintService {
 	 * 
 	 * @param jrxmlFilePath
 	 * @param whereCondition
+	 * @param otherParameters
 	 * @param connection
 	 */
-	private int getCount(String jrxmlFilePath, String whereCondition, Connection connection) {
+	private int getCount(String jrxmlFilePath, String whereCondition, String otherParameters, Connection connection) {
 		XMLWriter xmlWriter = null;
 		try {
 			SAXReader saxReader = new SAXReader();
@@ -697,6 +699,29 @@ public class PrintServiceImpl implements PrintService {
 			if (queryString.contains("$P!{WHERE_CONDITION}") && !StringUtils.isEmpty(whereCondition)) {
 				queryString = queryString.replace("$P!{WHERE_CONDITION}", whereCondition);
 			}
+			// 替换其他参数
+			if (queryString.contains("$")) {
+				if (StringUtils.isEmpty(otherParameters)) {
+					throw new ReportException("未传入动态参数");
+				}
+				JSONObject parameters = JSONObject.parseObject(otherParameters);
+				Pattern pattern = Pattern.compile("\\$P\\{([^\\$]*)\\}");
+				Matcher matcher = pattern.matcher(queryString);
+				while (matcher.find()) {
+					String match = matcher.group();
+					String parameterName = match.substring("$P{".length(), match.length() - 1);
+					Object value = parameters.get(parameterName);
+					if (value == null) {
+						throw new ReportException("未传入动态参数:" + parameterName);
+					}
+					// 如果不是数字,需以单引号括起来
+					if (value instanceof Number) {
+						queryString = queryString.replace(match, value.toString());
+					} else {
+						queryString = queryString.replace(match, "'" + value.toString() + "'");
+					}
+				}
+			}
 			return getCount(connection, queryString);
 		} catch (DocumentException | SQLException e) {
 			throw new ReportException(e).setDetailedMessage(e);