实现报表列表过滤原创
380次浏览
未经作者许可,禁止转载编辑于2024年12月24日 18:25:03
在我们开发的过程中经常会遇到这样的需求,要在报表列表实现和单据列表一样的过滤。
要实现这个需求我们大致分为以下步骤
1、取数插件返回DataSet前,需要调用编写的工具类将结果集和过滤条件处理(工具类放在文章的最后(仅供参考),有需要的自取)。 dataset = FilterUtils.HeaderFilter(dataset, filterInfo); 2、报表的单据插件重写setSortAndFilter方法,下边是报表所有的字段都可以过滤,也可以只加需要过滤的字段 @Override public void setSortAndFilter(List<SortAndFilterEvent> list) { super.setSortAndFilter(list); for(SortAndFilterEvent event : list){ event.setFilter(true); event.setSort(true); } } /** *实现报表列表过滤的方法 */ public static DataSet headerFilter(DataSet resultDataSet, FilterInfo filterInfo) { List<QFilter> headFilters = filterInfo.getHeadFilters(); List<String> cpList = Arrays.asList("is not null,is null".split(",")); List<String> dataTypeList = Arrays.asList("BigDecimal,Long,Timestamp".split(",")); //表头添加过滤 if (headFilters != null && headFilters.size() > 0) { QFilter qFilter = new QFilter("1", QCP.equals, 1); for (int i = 0; i < headFilters.size(); i++) { QFilter filter = headFilters.get(i); String property = filter.getProperty(); String cp = filter.getCP(); Object value = filter.getValue(); List<String> list = new ArrayList<>(Arrays.asList(resultDataSet.getRowMeta().getFieldNames())); for (String field : list) { DataType dataType = resultDataSet.getRowMeta().getField(field).getDataType(); if (property.equals(field)) { if (cpList.contains(cp) || (value != null && dataTypeList.contains(dataType.getName()))) { //校验操作符 if ("is not null".equals(cp)) { qFilter.and(new QFilter(property, QCP.is_notnull, null)) .and(new QFilter(property, QCP.not_equals, "")); } else if (StringUtils.equals("is null", cp)) { QFilter orQFilter = new QFilter(property, QCP.is_null, null) .or(new QFilter(property, QCP.equals, "")); qFilter.and(orQFilter); } //校验值 if (dataType.getName().equals("BigDecimal")) { String val = value.toString(); BigDecimal bigDecimal = new BigDecimal(val); qFilter.and(new QFilter(property, cp, bigDecimal)); } if (dataType.getName().equals("Long")) { String val = value.toString(); Long bigDecimal = Long.valueOf(val); qFilter.and(new QFilter(property, cp, bigDecimal)); } if (dataType.getName().equals("Timestamp")) { String val = value.toString(); resultDataSet = MyReportGeneralOpHelper.changFiledDataType(resultDataSet, property, String.class); if (cp.equals("=")) { QFilter dateFilter = new QFilter(property, QCP.like, "%" + val + "%"); resultDataSet = resultDataSet.filter(dateFilter.toString()); }else { String startStr = filter.toString().split(">=")[1].split("AND")[0]; String endStr = filter.toString().split("<")[1]; Date startDate = getDate(startStr); Date endDate = getDate(endStr); QFilter dateFilter = new QFilter(property,QCP.large_equals,startDate) .and(new QFilter(property,QCP.less_than,endDate)); resultDataSet = resultDataSet.filter(dateFilter.toString()); } resultDataSet = MyReportGeneralOpHelper.changFiledDataType(resultDataSet, property, Timestamp.class); } } else { qFilter.and(filter); } } } } resultDataSet = resultDataSet.filter(qFilter.toString()); } return resultDataSet; } /** * 日期时间字符串转为Date * * @param date * @return */ private static Date getDate(String date) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { if (date.contains("'")){ date = date.replaceAll("'","").trim(); } return simpleDateFormat.parse(date); } catch (ParseException e) { e.printStackTrace(); } return null; }
public class MyReportGeneralOpHelper { public static String[] getDataSetFieldArray(DataSet dataSet) { Set<String> fileds = getDataSetField(dataSet); return (String[])fileds.toArray(new String[fileds.size()]); } public static Set<String> getDataSetField(DataSet dataSet) { return getDataSetField(dataSet, true); } public static Set<String> getDataSetField(DataSet dataSet, boolean isLinked) { Field[] fields = dataSet.getRowMeta().getFields(); Set<String> fs = isLinked ? new LinkedHashSet(fields.length) : new TreeSet(); for(int i = 0; i < fields.length; ++i) { String fieldName = fields[i].getName(); ((Set)fs).add(fieldName); } return (Set)fs; } public static <T> DataSet changFiledDataType(DataSet dataSet, String filedName, Class<T> T) { dataSet = dataSet.select(arraysToString(addFileds(removeFiled(getDataSetFieldArray(dataSet), filedName), "cast(" + filedName + " as " + T.getSimpleName() + ")as " + filedName + ""))); return dataSet; } public static String[] removeFiled(String[] source, String... removeFileds) { Set<String> removeFiledSet = new HashSet(removeFileds.length); String[] var3 = removeFileds; int var4 = removeFileds.length; int var5; for(var5 = 0; var5 < var4; ++var5) { String removeFiled = var3[var5]; removeFiledSet.add(removeFiled); } List<String> fileds = new ArrayList(source.length); String[] var9 = source; var5 = source.length; for(int var10 = 0; var10 < var5; ++var10) { String string = var9[var10]; if (!removeFiledSet.contains(string)) { fileds.add(string); } } return (String[])fileds.toArray(new String[fileds.size()]); } public static String arraysToString(String[] sourceFileds) { String arrayS = Arrays.toString(sourceFileds); return arrayS.substring(1, arrayS.length() - 1); } public static String[] addFileds(String[] source, String... addFiled) { return (String[])((String[]) ArrayUtils.addAll(source, addFiled)); } }
赞 0
0人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读