wangting
2024-11-14 c162f50f97a418ec10f0dd48d9e4c34e392c4281
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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;  
    }
 
    
}