Commit b849c9e2 authored by 赵啸非's avatar 赵啸非

重构部分功能模块

parent 6339e40f
Pipeline #2356 canceled with stages
package com.mortals.xhx.common.plugin;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.exception.RenderException;
import com.deepoove.poi.policy.RenderPolicy;
import com.deepoove.poi.template.ElementTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.*;
import java.util.Iterator;
import java.util.List;
/**
* 单行数据填充,不需要定义列元素
*
* @author:
* @date: 2022/11/30 13:34
*/
public class LoopSingleRowTableRenderPolicy implements RenderPolicy {
@Override
public void render(ElementTemplate eleTemplate, Object data, XWPFTemplate template) {
RunTemplate runTemplate = (RunTemplate) eleTemplate;
XWPFRun run = runTemplate.getRun();
try {
if (!TableTools.isInsideTable(run)) {
throw new IllegalStateException(
"The template tag " + runTemplate.getSource() + " must be inside a table");
}
//获取设置标志的单元
XWPFTableCell tagCell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
XWPFTable table = tagCell.getTableRow().getTable();
XWPFTableRow templateRow=tagCell.getTableRow();
//重置
run.setText("", 0);
//获取模板列index
int templateColIndex = getTemplateColIndex(tagCell) + 1;
if (null != data && data instanceof Iterable) {
//data数据渲染
Iterator<?> iterator = ((Iterable<?>) data).iterator();
boolean hasNext = iterator.hasNext();
while (hasNext) {
Object root = iterator.next();
hasNext = iterator.hasNext();
//填充当前列元素
setTableRowCol(templateRow, root.toString(), templateColIndex);
templateColIndex++;
}
}
afterloop(table, data);
} catch (Exception e) {
e.printStackTrace();
throw new RenderException("HackLoopTable for " + eleTemplate + "error: " + e.getMessage(), e);
}
}
private int getTemplateColIndex(XWPFTableCell tagCell) {
String text = tagCell.getText();
int col = -1;
XWPFTableRow tagRow = tagCell.getTableRow();
List<XWPFTableCell> tableCells = tagRow.getTableCells();
for (int i = 0; i < tableCells.size(); i++) {
if (tableCells.get(i).getText().equalsIgnoreCase(text)) {
return i;
}
}
return col;
}
protected void afterloop(XWPFTable table, Object data) {
}
private void setTableRowCol(XWPFTableRow templateRow, String str, int pos) {
int limit = templateRow.getTableCells().size();
if (pos > limit) {
return;
}
XWPFTableCell cell = templateRow.getCell(pos);
if (cell != null) {
cell.setText(str);
}
}
}
......@@ -35,6 +35,7 @@ import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.key.ParamKey;
import com.mortals.xhx.common.pdu.Col;
import com.mortals.xhx.common.pdu.gen.component.ComponentCons;
import com.mortals.xhx.common.plugin.LoopSingleRowTableRenderPolicy;
import com.mortals.xhx.common.utils.ExportDocUtil;
import com.mortals.xhx.common.utils.WordUtil;
import com.mortals.xhx.module.matter.dao.MatterDao;
......@@ -330,39 +331,13 @@ public class MatterDatumServiceImpl extends AbstractCRUDServiceImpl<MatterDatumD
if (entry.getKey().indexOf("dt_") != -1) {
builder.bind(entry.getKey(), new MultipleRowTableRenderPolicy());
}
//绑定转换list list_1_身份证_18_num
//绑定转换list list_1_身份证
if (entry.getKey().indexOf("list_") != -1) {
List<String> keys = StrSplitter.split(entry.getKey(), "_", true, true);
Integer lens = 18;
if (keys.size() > 3) {
lens = DataUtil.converStr2Int(keys.get(3), 18);
}
String value = entry.getValue().toString();
SplitIter splitIter = new SplitIter(value, new LengthFinder(1), -1, false);
List<String> valueList = splitIter.toList(true);
if (valueList.size() < lens) {
int sub = lens - valueList.size();
for (int i = 0; i < sub; i++) {
valueList.add("");
}
}
//StrUtil
List<Col> colList = valueList.stream().map(item -> {
Col col = new Col();
col.setCol(item);
return col;
/*
Map<String, String> map = new HashMap<>();
String colName = "col";
if (keys.size() > 4) {
colName = keys.get(4);
}
map.put(colName, item);
return map;*/
}).collect(Collectors.toList());
builder.bind(entry.getKey(), new LoopColumnTableRenderPolicy());
data.put(entry.getKey(), colList);
builder.bind(entry.getKey(), new LoopSingleRowTableRenderPolicy());
data.put(entry.getKey(), valueList);
}
});
if (addMap.size() > 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment