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> dataMergeChildTable(List excelDataList, List selectKey) throws WorkFlowException { List> results = new ArrayList<>(); Set tablefield1 = selectKey.stream().filter(s -> s.toLowerCase().startsWith(JnpfKeyConsts.CHILD_TABLE_PREFIX)) .map(s -> s.substring(0, s.indexOf("-"))).collect(Collectors.toSet()); List> allDataList = new ArrayList<>(); for (int z = 0; z < excelDataList.size(); z++) { Map 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>>> IndexMap = new ArrayList<>(); // Map>>> IndexMap = new TreeMap<>(); Map>> 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 dataMap = allDataList.get(t); //首条数据不合并 if (t > 0) { List> tablefield2 = dataMap.entrySet().stream().filter(e -> !e.getKey().toLowerCase().startsWith(JnpfKeyConsts.CHILD_TABLE_PREFIX)).collect(Collectors.toList()); //如果除子表外都为空 则需要合并 Map.Entry entry = tablefield2.stream().filter(ta -> ta.getValue() != null).findFirst().orElse(null); if (entry == null) { needTogether = false; needAdd = false; } } //合并子表里的字段 for (String tab : tablefield1) { Map childObjMap = new HashMap<>(16); //该条数据下的子表字段 List> childList = dataMap.entrySet().stream().filter(e -> e.getKey().startsWith(tab)).collect(Collectors.toList()); for (Map.Entry entry : childList) { String childFieldName = entry.getKey().replace(tab + "-", ""); childObjMap.put(childFieldName, entry.getValue()); } List> 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>> c = new HashMap<>(childrenTabMap); Map>> b = new HashMap<>(); for (String tab : tablefield1) { //去掉最后一个 放到下条数据里 List> mapList = c.get(tab); Map map = mapList.get(mapList.size() - 1); List> 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 m = new HashMap<>(); for (Map.Entry 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>> entry = IndexMap.get(r); Map map = results.get(r); for (Map.Entry>> entry1 : entry.entrySet()) { String tableField = entry1.getKey(); Object tableField1 = map.get(tableField); List> value1 = entry1.getValue(); if (tableField1 != null) { List> tfMap = (List>) tableField1; value1.addAll(tfMap); } map.put(tableField, value1); } results.set(r, map); } return results; } }