//代码块 public class MergeSubEntryPlugin extends AbstractPrintPlugin {
//单据体(行程信息) private static final String ENTRY_ENTITY = "entryentity";
//子单据体(费用项目) private static final String SUB_ENTRY_ENTITY = "subentryentity";
//差旅项目 private static final String EXPENSE_ITEM = "expenseitem.name";
//报销金额 private static final String TRIP_AMOUNT = "tripamount";
//备注 private static final String REMARK = "remark";
//序号 private static final String SEQ = "seq";
@Override public void afterLoadData(AfterLoadDataEvent evt) { for (DataRowSet rowSet : evt.getDataRowSets()) { //获取单据体数据 List<DataRowSet> entryRows = rowSet.getCollectionField(ENTRY_ENTITY).getValue(); for (DataRowSet entryRow : entryRows) { List<DataRowSet> subEntryRows = entryRow.getCollectionField(SUB_ENTRY_ENTITY).getValue(); //子单据体数据分组 Map<String, List<DataRowSet>> groupMap = subEntryRows .stream() .filter(f -> f.getField(EXPENSE_ITEM) != null) .collect(Collectors.groupingBy(m -> m.getField(EXPENSE_ITEM).getValue().toString())); List<DataRowSet> resultRows = new ArrayList<>(groupMap.size()); groupMap.forEach((key, values) -> { if (!CollectionUtils.isEmpty(values)) { //报销金额累计求和 double sum = values.stream() .filter(f -> f.getField(TRIP_AMOUNT) != null) .mapToDouble(m -> ((DecimalField) m.getField(TRIP_AMOUNT)).getValue().longValue()) .sum(); //备注字段通过分号拼接 String remarks = values.stream() .filter(f -> f.getField(REMARK) != null) .map(m -> m.getField(REMARK).toString()) .collect(Collectors.joining(";\n")); //字段合并后,合并结果保存于当前分组的第一行数据 DataRowSet firstRow = values.get(0); Field tripAmount = firstRow.getField(TRIP_AMOUNT); tripAmount.setValue(new BigDecimal(sum)); firstRow.put(TRIP_AMOUNT, tripAmount); firstRow.put(REMARK, new TextField(remarks)); //合并后仅仅保留第一条数据 resultRows.add(firstRow); } }); //子单据体数据清空 subEntryRows.clear(); //根据序号重排序,并重新编号 AtomicInteger seq = new AtomicInteger(1); resultRows.stream() .filter(f -> f.getField(SEQ) != null) .sorted(Comparator.comparing(r -> ((IntegerField) r.getField(SEQ)).getValue())) .collect(Collectors.toList()) .forEach(row -> { row.getField(SEQ).setValue(seq.getAndIncrement()); //保存结果 subEntryRows.add(row); }); } } } } |