package com.vci.starter.word.bo;
|
|
import com.aspose.words.IMailMergeDataSource;
|
import com.aspose.words.ref.Ref;
|
import com.vci.starter.web.util.VciBaseUtil;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
/**
|
* 用于mergeField的表格形式的数据写入对象,因为aspose.word不支持HashMap
|
* @author weidy,dangsn
|
*/
|
public class WordMergeListDataSource implements IMailMergeDataSource {
|
/**
|
* 数据对象
|
*/
|
private List<Map<String, Object>> dataList;
|
|
/**
|
* 索引
|
*/
|
private int index;
|
|
/**
|
* 表格名称
|
*/
|
private String tableName;
|
|
/**
|
* 子表格的名称
|
*/
|
private String childTableName;
|
|
/**
|
* @param dataList 数据集 ,数据集中Map的key,需要跟TableStart,TableEnd包含的名称对应
|
* @param tableName 与模板中的TableStart,TableEnd对应
|
*/
|
public WordMergeListDataSource(List<Map<String, Object>> dataList, String tableName) {
|
this.dataList = dataList;
|
this.tableName = tableName;
|
index = -1;
|
this.setChildTableName(listChildTableNameFromData(this.dataList));
|
}
|
|
/**
|
* @param data 单个数据集
|
* @param tableName 与模板中的Name对应
|
*/
|
public WordMergeListDataSource(Map<String, Object> data, String tableName) {
|
if(this.dataList == null) {
|
this.dataList = new ArrayList<Map<String,Object>>();
|
this.dataList.add(data);
|
}
|
this.tableName = tableName;
|
index = -1;
|
this.setChildTableName(listChildTableNameFromData(this.dataList));
|
}
|
|
/**
|
* 获取子表名称
|
* @return 子表名称,多个会以逗号分隔
|
*/
|
public String getChildTableName() {
|
return childTableName;
|
}
|
|
/**
|
* 设置子表名称
|
* @param childTableName 子表名称,多个以逗号分隔
|
*/
|
public void setChildTableName(String childTableName) {
|
this.childTableName = childTableName;
|
}
|
|
/**
|
* 获取结果集总数
|
* @return 个数
|
*/
|
public int getCount() {
|
return this.dataList.size();
|
}
|
|
/**
|
* 获取数据列表
|
* @return 数据列表
|
*/
|
public List<Map<String, Object>> getDataList(){
|
return this.dataList;
|
}
|
|
/**
|
* 获取表格名称
|
* @return 表格的名称
|
*/
|
public String getTableName() {
|
return this.tableName;
|
}
|
|
/**
|
* 获取具体的值,接口所需要的
|
* @param key 字段名称
|
* @param ref 值
|
* @return 是否有值
|
* @throws Exception 获取的时候能出错
|
*/
|
@Override
|
public boolean getValue(String key, Ref<Object> ref) throws Exception {
|
if(index < 0 || index >= this.getCount()){
|
return false;
|
}
|
if(ref != null){
|
ref.set(this.dataList.get(index).get(key));
|
return true;
|
}else{
|
return false;
|
}
|
}
|
|
/**
|
* 从数据中找到list的作为子表的对象
|
* @param dataList 数据列表
|
* @return 包含子表的名称的连接字符串
|
*/
|
private String listChildTableNameFromData(List<Map<String,Object>> dataList ){
|
List<String> nextChildNameList = new ArrayList<>();
|
if(!CollectionUtils.isEmpty(dataList)){
|
dataList.get(0).forEach((k,v) ->{
|
if(v != null && v instanceof List){
|
nextChildNameList.add(k);
|
}
|
});
|
}
|
return nextChildNameList.stream().collect(Collectors.joining(","));
|
}
|
|
/**
|
* 实现的接口
|
* @param tableName 子表的名称
|
* @return 子表对象,不符合的会返回null
|
* @throws Exception 获取出错的时候会抛出异常
|
*/
|
@Override
|
public IMailMergeDataSource getChildDataSource(String tableName) throws Exception {
|
if(StringUtils.isNotBlank(getChildTableName())){
|
List<String> childrenTableName = VciBaseUtil.str2List(getChildTableName());
|
if(childrenTableName.contains(tableName)){
|
Object childData = this.dataList.get(index).get(tableName);
|
if(childData != null && childData instanceof List) {
|
List<Map<String,Object>> childDataList = (List<Map<String,Object>>)childData;
|
if(!CollectionUtils.isEmpty(childDataList)) {
|
WordMergeListDataSource childDataSource = new WordMergeListDataSource(childDataList, tableName);
|
childDataSource.setChildTableName(listChildTableNameFromData(childDataList));
|
return childDataSource;
|
}
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 实现接口
|
* 判断是否还有下一条记录
|
* @return true移动到下一条
|
*/
|
@Override
|
public boolean moveNext() {
|
index += 1;
|
if(index >= this.getCount()) {
|
return false;
|
}
|
return true;
|
}
|
|
|
}
|