FormExecelUtils.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package jnpf.base.util;
  2. import jnpf.constant.MsgCode;
  3. import jnpf.exception.WorkFlowException;
  4. import jnpf.util.visiual.JnpfKeyConsts;
  5. import org.apache.commons.collections4.MapUtils;
  6. import java.util.*;
  7. import java.util.stream.Collectors;
  8. public class FormExecelUtils {
  9. /**
  10. * 合并子表数据
  11. *
  12. * @param excelDataList
  13. * @param selectKey
  14. * @return
  15. * @throws WorkFlowException
  16. */
  17. public static List<Map<String, Object>> dataMergeChildTable(List<Map> excelDataList, List<String> selectKey) throws WorkFlowException {
  18. List<Map<String, Object>> results = new ArrayList<>();
  19. Set<String> tablefield1 = selectKey.stream().filter(s -> s.toLowerCase().startsWith(JnpfKeyConsts.CHILD_TABLE_PREFIX))
  20. .map(s -> s.substring(0, s.indexOf("-"))).collect(Collectors.toSet());
  21. List<Map<String, Object>> allDataList = new ArrayList<>();
  22. for (int z = 0; z < excelDataList.size(); z++) {
  23. Map<String, Object> dataMap = new HashMap<>(16);
  24. Map m = excelDataList.get(z);
  25. List excelEntrySet = new ArrayList<>(m.entrySet());
  26. //取出的数据最后一行 不带行标签
  27. int resultsize = z == excelDataList.size() - 1 ? excelEntrySet.size() : m.containsKey("excelRowNum") ? excelEntrySet.size() - 1 : excelEntrySet.size();
  28. if (resultsize < selectKey.size()) {
  29. throw new WorkFlowException(MsgCode.VS407.get());
  30. }
  31. for (int e = 0; e < resultsize; e++) {
  32. Map.Entry o = (Map.Entry) excelEntrySet.get(e);
  33. String entryKey = o.getKey().toString();
  34. String substring = entryKey.substring(entryKey.lastIndexOf("(") + 1, entryKey.lastIndexOf(")"));
  35. boolean contains = selectKey.contains(substring);
  36. if (!contains) {
  37. throw new WorkFlowException(MsgCode.VS407.get());
  38. }
  39. dataMap.put(substring, o.getValue());
  40. }
  41. allDataList.add(dataMap);
  42. }
  43. //存放在主表数据的下标位置
  44. List<Map<String, List<Map<String, Object>>>> IndexMap = new ArrayList<>();
  45. // Map<Integer, Map<String, List<Map<String, Object>>>> IndexMap = new TreeMap<>();
  46. Map<String, List<Map<String, Object>>> childrenTabMap = new HashMap<>();
  47. for (String tab : tablefield1) {
  48. childrenTabMap.put(tab, new ArrayList<>());
  49. }
  50. for (int t = 0; t < allDataList.size(); t++) {
  51. boolean isLast = t == allDataList.size() - 1;
  52. //是否需要合并
  53. boolean needTogether = true;
  54. //这条数据是否需要添加
  55. boolean needAdd = true;
  56. Map<String, Object> dataMap = allDataList.get(t);
  57. //首条数据不合并
  58. if (t > 0) {
  59. List<Map.Entry<String, Object>> tablefield2 = dataMap.entrySet().stream().filter(e ->
  60. !e.getKey().toLowerCase().startsWith(JnpfKeyConsts.CHILD_TABLE_PREFIX)).collect(Collectors.toList());
  61. //如果除子表外都为空 则需要合并
  62. Map.Entry<String, Object> entry = tablefield2.stream().filter(ta -> ta.getValue() != null).findFirst().orElse(null);
  63. if (entry == null) {
  64. needTogether = false;
  65. needAdd = false;
  66. }
  67. }
  68. //合并子表里的字段
  69. for (String tab : tablefield1) {
  70. Map<String, Object> childObjMap = new HashMap<>(16);
  71. //该条数据下的子表字段
  72. List<Map.Entry<String, Object>> childList = dataMap.entrySet().stream().filter(e -> e.getKey().startsWith(tab)).collect(Collectors.toList());
  73. for (Map.Entry<String, Object> entry : childList) {
  74. String childFieldName = entry.getKey().replace(tab + "-", "");
  75. childObjMap.put(childFieldName, entry.getValue());
  76. }
  77. List<Map<String, Object>> mapList = childrenTabMap.get(tab);
  78. if (MapUtils.isNotEmpty(childObjMap)) {
  79. boolean b = childObjMap.values().stream().anyMatch(v -> v != null);
  80. if (b) {
  81. mapList.add(childObjMap);
  82. }
  83. }
  84. }
  85. if (needTogether && t != 0) {
  86. Map<String, List<Map<String, Object>>> c = new HashMap<>(childrenTabMap);
  87. Map<String, List<Map<String, Object>>> b = new HashMap<>();
  88. for (String tab : tablefield1) {
  89. //去掉最后一个 放到下条数据里
  90. List<Map<String, Object>> mapList = c.get(tab);
  91. Map<String, Object> map = mapList.get(mapList.size() - 1);
  92. List<Map<String, Object>> aList = new ArrayList<>();
  93. aList.add(map);
  94. mapList.remove(mapList.size() - 1);
  95. childrenTabMap.put(tab, aList);
  96. b.put(tab, mapList);
  97. }
  98. IndexMap.add(b);
  99. if (isLast) {
  100. IndexMap.add(childrenTabMap);
  101. }
  102. } else {
  103. if (isLast) {
  104. IndexMap.add(childrenTabMap);
  105. }
  106. }
  107. if (needAdd) {
  108. Map<String, Object> m = new HashMap<>();
  109. for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
  110. if (!entry.getKey().contains("-")) {
  111. m.put(entry.getKey(), entry.getValue());
  112. }
  113. }
  114. results.add(m);
  115. }
  116. }
  117. //处理结果
  118. for (int r = 0; r < results.size(); r++) {
  119. Map<String, List<Map<String, Object>>> entry = IndexMap.get(r);
  120. Map<String, Object> map = results.get(r);
  121. for (Map.Entry<String, List<Map<String, Object>>> entry1 : entry.entrySet()) {
  122. String tableField = entry1.getKey();
  123. Object tableField1 = map.get(tableField);
  124. List<Map<String, Object>> value1 = entry1.getValue();
  125. if (tableField1 != null) {
  126. List<Map<String, Object>> tfMap = (List<Map<String, Object>>) tableField1;
  127. value1.addAll(tfMap);
  128. }
  129. map.put(tableField, value1);
  130. }
  131. results.set(r, map);
  132. }
  133. return results;
  134. }
  135. }