public class Row2ColPlugin extends AbstractPrintPlugin {
//需动态添加的列的标题 private List<String> addCols = new ArrayList<>(10);
//单据体(行程信息) private static final String ENTRY_ENTITY = "entryentity";
//子单据体(费用项目) private static final String SUB_ENTRY_ENTITY = "subentryentity";
//差旅项目名称 private static final String EXPENSE_ITEM_NAME = "expenseitem.name";
//报销金额 private static final String TRIP_AMOUNT = "tripamount";
//子单据体数据源标识 private static final String DS_SUB_ENTRY = "er_tripreimbursebill2.subentryentity";
//字段拼接符 private static final String FIELD_JOIN_MARK = "_";
//被转换的行所在列的列下标 private static final int deleteColIndex = 3;
//被转换的行所在列的宽度 300 = 30mm private static final int deletedColWidth = 300;
@Override public void afterLoadData(AfterLoadDataEvent evt) { //获取子单据体记录行 List<DataRowSet> subRows = evt.getDataRowSets().stream() .flatMap(fMap -> fMap.getCollectionField(ENTRY_ENTITY).getValue().stream()) .flatMap(fMap -> fMap.getCollectionField(SUB_ENTRY_ENTITY).getValue().stream()) .collect(Collectors.toList()); //获取需动态添加的列的标题 addCols = subRows.stream() .map(m -> m.getField(EXPENSE_ITEM_NAME).getValue().toString()) .distinct() .collect(Collectors.toList()); for (DataRowSet subRow : subRows) { //为新加入的列添加字段绑定 Field tripAmount = subRow.getField(TRIP_AMOUNT); for (String col : addCols) { if (col.equals(subRow.getField(EXPENSE_ITEM_NAME).getValue().toString())) { subRow.put(EXPENSE_ITEM_NAME + FIELD_JOIN_MARK + col, tripAmount); } else { //当前行没有该差旅项目,则报销金额为0 final Field copyField = tripAmount.copy(); copyField.setValue(BigDecimal.ZERO); subRow.put(EXPENSE_ITEM_NAME + FIELD_JOIN_MARK + col, copyField); } } }
}
@Override public void beforeOutputWidget(BeforeOutputWidgetEvent evt) { if (evt instanceof BeforeOutputGridEvent) { BeforeOutputGridEvent gridEvent = (BeforeOutputGridEvent) evt; //计算新增列的列宽,新增列宽 = (空余范围宽度 + 被删除列宽度)/ 新增列数 int avgWidth = (gridEvent.getRightSpace() + deletedColWidth) / addCols.size(); //删除差旅项目名称列 gridEvent.delColumn(deleteColIndex); //新增的列从被删除列位置开始插入 int index = deleteColIndex; for (String addCol : addCols) { final List<PWGridCellBo> cellBos = gridEvent.insertColumn(index++, avgWidth); //获取新增列的标题行对应单元格,赋值为列标题名称 cellBos.get(0).setCellValue(addCol); //获取新增列数据行对应的单元格,并绑定afterLoadData中添加的字段 cellBos.get(1).setCellValue(DS_SUB_ENTRY, EXPENSE_ITEM_NAME + FIELD_JOIN_MARK + addCol); } } } } |