报表表头过滤原创
16人赞赏了该文章
116次浏览
编辑于2024年12月16日 11:22:48
【需求背景】
报表动态列,表头过滤
【实现方案】
1、在AbstractReportFormPlugin表单插件中开启表头过滤
2、获取表头过滤条件,并解析
3、对query返回的Dataset做过滤
【实现过程】
1、在AbstractReportFormPlugin表单插件中开启表头过滤
public void setSortAndFilter(List<SortAndFilterEvent> allColumns) { super.setSortAndFilter(allColumns); for(SortAndFilterEvent event : allColumns){ event.setFilter(true); } }
2、封装一个AbstractReportListDataPlugin抽象类
public abstract class AbstractHeaderFilterQuery extends AbstractReportListDataPlugin { @Override public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable { DataSet reportQuery = this.reportQuery(reportQueryParam, o); if (reportQuery == null) { return null; } else { //转换dataset . -> _ 如果不替换,导出,表头过滤都会有问题 String[] fieldNames = CommonUtils.getStrReplace(reportQuery); DataSet select = reportQuery.select(fieldNames); //表头过滤 return getHeadFilters(reportQueryParam, select); } } @Override public List<AbstractReportColumn> getColumns(List<AbstractReportColumn> columns) throws Throwable { List<AbstractReportColumn> reportColumns = this.reportGetColumns(columns); updateStr(reportColumns); return columns; } private static void updateStr(List<AbstractReportColumn> reportColumns) { reportColumns.forEach(column -> { if ((column instanceof ReportColumnGroup)) { List<AbstractReportColumn> children = ((ReportColumnGroup) column).getChildren(); updateStr(children); }else { String fieldKey = ((ReportColumn) column).getFieldKey(); ((ReportColumn) column).setFieldKey(fieldKey.replace(".","_")); } }); } protected abstract DataSet reportQuery(ReportQueryParam reportQueryParam, Object o) throws Throwable; protected abstract List<AbstractReportColumn> reportGetColumns(List<AbstractReportColumn> columns) throws Throwable; }
//一些常用的表头过滤拼接
public static DataSet getHeadFilters(ReportQueryParam reportQueryParam, DataSet select) { List<QFilter> filters = reportQueryParam.getFilter().getHeadFilters(); StringBuilder sb = new StringBuilder(); RowMeta rowMeta = select.getRowMeta(); HashSet<String> numberSet = Sets.newHashSet("mdnb_age", "mdnb_exitcontroltime", "mdnb_careertenure", "mdnb_rankyears"); for (int i = 0; i < filters.size(); i++) { QFilter q = filters.get(i); String field = q.getProperty(); DataType dataType = rowMeta.getField(field).getDataType(); String qstr = q.toString(); if (dataType instanceof IntegerType || (dataType instanceof LongType && numberSet.contains(field)) || dataType instanceof BigDecimalType) { sb.append(qstr, 0, q.toString().indexOf("'")).append(q.getValue()); } else if (dataType instanceof TimestampType) { if (q.toString().contains("AND")) { String[] strs = q.toString().split("AND"); for (int j = 0; j < strs.length; j++) { String str = strs[j]; sb.append(str.replaceFirst("'", "TO_DATE('")).append(",'yyyy-MM-dd')"); if (j != strs.length - 1) { sb.append(" and "); } } } else { sb.append(qstr, 0, q.toString().indexOf("'")).append("TO_DATE('").append(q.getValue()).append("','yyyy-MM-dd')"); } } else { sb.append(qstr); } if (i != filters.size() - 1) { sb.append(" and "); } } if (sb.length() > 0) { select = select.filter(sb.toString().replaceAll("null ''", "null")); } return select; }
赞 16
16人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
2人打赏
还没有人打赏,快来当第一个打赏的人吧!