ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
package jnpf.base.util;
 
import jnpf.constant.MsgCode;
import jnpf.exception.WorkFlowException;
import jnpf.util.visiual.JnpfKeyConsts;
import org.apache.commons.collections4.MapUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
public class FormExecelUtils {
    /**
     * 合并子表数据
     *
     * @param excelDataList
     * @param selectKey
     * @return
     * @throws WorkFlowException
     */
    public static List<Map<String, Object>> dataMergeChildTable(List<Map> excelDataList, List<String> selectKey) throws WorkFlowException {
        List<Map<String, Object>> results = new ArrayList<>();
        Set<String> tablefield1 = selectKey.stream().filter(s -> s.toLowerCase().startsWith(JnpfKeyConsts.CHILD_TABLE_PREFIX))
                .map(s -> s.substring(0, s.indexOf("-"))).collect(Collectors.toSet());
        List<Map<String, Object>> allDataList = new ArrayList<>();
 
        for (int z = 0; z < excelDataList.size(); z++) {
            Map<String, Object> dataMap = new HashMap<>(16);
            Map m = excelDataList.get(z);
            List excelEntrySet = new ArrayList<>(m.entrySet());
            //取出的数据最后一行 不带行标签
            int resultsize = z == excelDataList.size() - 1 ? excelEntrySet.size() : m.containsKey("excelRowNum") ? excelEntrySet.size() - 1 : excelEntrySet.size();
            if (resultsize < selectKey.size()) {
                throw new WorkFlowException(MsgCode.VS407.get());
            }
            for (int e = 0; e < resultsize; e++) {
                Map.Entry o = (Map.Entry) excelEntrySet.get(e);
                String entryKey = o.getKey().toString();
                String substring = entryKey.substring(entryKey.lastIndexOf("(") + 1, entryKey.lastIndexOf(")"));
                boolean contains = selectKey.contains(substring);
                if (!contains) {
                    throw new WorkFlowException(MsgCode.VS407.get());
                }
                dataMap.put(substring, o.getValue());
            }
            allDataList.add(dataMap);
        }
 
        //存放在主表数据的下标位置
        List<Map<String, List<Map<String, Object>>>> IndexMap = new ArrayList<>();
//            Map<Integer, Map<String, List<Map<String, Object>>>> IndexMap = new TreeMap<>();
        Map<String, List<Map<String, Object>>> childrenTabMap = new HashMap<>();
        for (String tab : tablefield1) {
            childrenTabMap.put(tab, new ArrayList<>());
        }
 
        for (int t = 0; t < allDataList.size(); t++) {
            boolean isLast = t == allDataList.size() - 1;
            //是否需要合并
            boolean needTogether = true;
            //这条数据是否需要添加
            boolean needAdd = true;
            Map<String, Object> dataMap = allDataList.get(t);
            //首条数据不合并
            if (t > 0) {
                List<Map.Entry<String, Object>> tablefield2 = dataMap.entrySet().stream().filter(e ->
                        !e.getKey().toLowerCase().startsWith(JnpfKeyConsts.CHILD_TABLE_PREFIX)).collect(Collectors.toList());
                //如果除子表外都为空 则需要合并
                Map.Entry<String, Object> entry = tablefield2.stream().filter(ta -> ta.getValue() != null).findFirst().orElse(null);
                if (entry == null) {
                    needTogether = false;
                    needAdd = false;
                }
            }
 
            //合并子表里的字段
            for (String tab : tablefield1) {
                Map<String, Object> childObjMap = new HashMap<>(16);
                //该条数据下的子表字段
                List<Map.Entry<String, Object>> childList = dataMap.entrySet().stream().filter(e -> e.getKey().startsWith(tab)).collect(Collectors.toList());
                for (Map.Entry<String, Object> entry : childList) {
                    String childFieldName = entry.getKey().replace(tab + "-", "");
                    childObjMap.put(childFieldName, entry.getValue());
                }
                List<Map<String, Object>> mapList = childrenTabMap.get(tab);
                if (MapUtils.isNotEmpty(childObjMap)) {
                    boolean b = childObjMap.values().stream().anyMatch(v -> v != null);
                    if (b) {
                        mapList.add(childObjMap);
                    }
                }
            }
            if (needTogether && t != 0) {
                Map<String, List<Map<String, Object>>> c = new HashMap<>(childrenTabMap);
                Map<String, List<Map<String, Object>>> b = new HashMap<>();
 
                for (String tab : tablefield1) {
                    //去掉最后一个 放到下条数据里
                    List<Map<String, Object>> mapList = c.get(tab);
                    Map<String, Object> map = mapList.get(mapList.size() - 1);
                    List<Map<String, Object>> aList = new ArrayList<>();
                    aList.add(map);
                    mapList.remove(mapList.size() - 1);
                    childrenTabMap.put(tab, aList);
                    b.put(tab, mapList);
                }
                IndexMap.add(b);
                if (isLast) {
                    IndexMap.add(childrenTabMap);
                }
            } else {
                if (isLast) {
                    IndexMap.add(childrenTabMap);
                }
            }
            if (needAdd) {
                Map<String, Object> m = new HashMap<>();
                for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
                    if (!entry.getKey().contains("-")) {
                        m.put(entry.getKey(), entry.getValue());
                    }
                }
                results.add(m);
            }
        }
 
        //处理结果
        for (int r = 0; r < results.size(); r++) {
            Map<String, List<Map<String, Object>>> entry = IndexMap.get(r);
            Map<String, Object> map = results.get(r);
            for (Map.Entry<String, List<Map<String, Object>>> entry1 : entry.entrySet()) {
                String tableField = entry1.getKey();
                Object tableField1 = map.get(tableField);
                List<Map<String, Object>> value1 = entry1.getValue();
                if (tableField1 != null) {
                    List<Map<String, Object>> tfMap = (List<Map<String, Object>>) tableField1;
                    value1.addAll(tfMap);
                }
                map.put(tableField, value1);
            }
            results.set(r, map);
        }
        return results;
    }
}