Commit e0ae13a5 authored by yanzg's avatar yanzg

消除成功接收处理

parent 44934bd2
package com.yanzuoguang.util.table; package com.yanzuoguang.util.table;
import java.util.ArrayList; import com.yanzuoguang.util.helper.StringHelper;
import java.util.LinkedHashMap;
import java.util.List; import java.util.*;
import java.util.Map;
/** /**
* 表头计算 * 表头计算
*
* @author 颜佐光 * @author 颜佐光
*/ */
public class TableHeadHelper { public class TableHeadHelper {
public static final String SPLIT = ":";
/**
* 获取父节点路径
*
* @param from 子节点
* @return 父节点路径
*/
public static String getParentPath(String from) {
if (from.lastIndexOf(SPLIT) >= 0) {
return from.substring(0, from.lastIndexOf(":"));
} else {
return StringHelper.EMPTY;
}
}
/**
* 对列头进行排序
*
* @param columns 对列头进行排序
*/
public static void sort(String[] columns) {
// 列头排序,同级元素排序
Arrays.sort(columns, new Comparator<String>() {
@Override
public int compare(String from, String to) {
String fromParent = getParentPath(from);
String toParent = getParentPath(to);
if (fromParent.length() == 0 || toParent.length() == 0) {
return 0;
}
return fromParent.compareTo(toParent);
}
});
}
/** /**
* 列头函数 * 列头函数
* *
...@@ -20,127 +56,83 @@ public class TableHeadHelper { ...@@ -20,127 +56,83 @@ public class TableHeadHelper {
public static TableHead getTableHead(String... columns) { public static TableHead getTableHead(String... columns) {
TableHead head = new TableHead(); TableHead head = new TableHead();
head.setTotalColumn(columns.length); head.setTotalColumn(columns.length);
// 节点关系映射
// 父子级别关系映射 Map<String, TableHeadItem> headMap = new LinkedHashMap<>();
Map<String, TableHeadItem> mapHead = new LinkedHashMap<String, TableHeadItem>();
// 当前节点的子节点元素 // 当前节点的子节点元素
Map<String, List<TableHeadItem>> mapSub = new LinkedHashMap<String, List<TableHeadItem>>(); Map<String, List<TableHeadItem>> headSubs = new LinkedHashMap<>();
// 最顶层的元素
Map<String, TableHeadItem> mapTop = new LinkedHashMap<String, TableHeadItem>();
for (String column : columns) {
TableHeadItem item = setHead(mapHead, mapSub, mapTop, column);
// 设置表格的最大行数
head.setTotalRow(Math.max(item.getLevel(), head.getTotalRow()));
}
// 计算合并行的数量,第几行 // 计算每个单元格的大小和占用的行、列
for (Map.Entry<String, TableHeadItem> item : mapHead.entrySet()) { for (int columnPos = 0; columnPos < columns.length; columnPos++) {
setCellRow(head, item.getValue()); String column = columns[columnPos];
int totalRow = setHead(head, headMap, headSubs, column, columnPos);
head.setTotalRow(Math.max(totalRow, head.getTotalRow()));
} }
// 计算列合并的数量,第几列 // 计算最后一行的合并数量
for (Map.Entry<String, TableHeadItem> item : mapTop.entrySet()) { for (Map.Entry<String, TableHeadItem> item : headMap.entrySet()) {
setCellColumn(head, mapSub, item.getValue()); TableHeadItem headItem = item.getValue();
if (headItem.isDataColumn()) {
headItem.setRowCell(head.getTotalRow() - headItem.getRow());
}
} }
return head; return head;
} }
private static TableHeadItem setHead(Map<String, TableHeadItem> mapHead, Map<String, List<TableHeadItem>> mapSub, /**
Map<String, TableHeadItem> mapTop, String columnFrom) { * 设置列头相关信息
String[] columns = columnFrom.split(":"); *
* @param head 列头对象集合
* @param headMap 列头对象路径映射
* @param headSubs 列头对象路径映射
* @param fullPath 来源路径
* @param columnPos 列位置
* @return
*/
private static int setHead(TableHead head,
Map<String, TableHeadItem> headMap, Map<String, List<TableHeadItem>> headSubs,
String fullPath, int columnPos) {
String[] heads = fullPath.split(":");
StringBuilder path = new StringBuilder(); StringBuilder path = new StringBuilder();
TableHeadItem last = null;
for (int i = 0; i < columns.length; i++) {
for (int rowIndex = 0; rowIndex < heads.length; rowIndex++) {
// 列名称 // 列名称
String column = columns[i]; String column = heads[rowIndex].trim();
// 父路径与当前节点路径 // 父路径与当前节点路径
String parentPath = path.toString(); String parentPath = path.toString().trim();
if (i > 0) { if (rowIndex > 0) {
path.append(":"); path.append(":");
} }
path.append(column); path.append(column);
String nowPath = path.toString();
last = new TableHeadItem();
last.setName(column);
last.setPath(nowPath);
last.setLevel(i + 1);
last.setDataColumn(columnFrom.equals(nowPath));
if (i == 0 && !mapTop.containsKey(nowPath)) { String nowPath = path.toString();
mapTop.put(nowPath, last); // 设置当前单元格属性
} if (!headMap.containsKey(nowPath)) {
TableHeadItem headItem = new TableHeadItem();
// 添加到最顶层元素中 headItem.setRow(rowIndex);
if (!mapSub.containsKey(parentPath)) { headItem.setRowCell(1);
mapSub.put(parentPath, new ArrayList<TableHeadItem>()); headItem.setColumn(columnPos);
} headItem.setColumnCell(1);
headItem.setName(column);
// 将元素添加到当前元素中 headItem.setPath(nowPath);
if (!mapHead.containsKey(nowPath)) { headItem.setDataColumn(fullPath.equals(nowPath));
mapHead.put(nowPath, last);
// 添加到父元素的子节点
// 添加到父级元素的下级元素 if (!headSubs.containsKey(parentPath)) {
mapSub.get(parentPath).add(last); headSubs.put(parentPath, new ArrayList<>());
} }
last = mapHead.get(nowPath); // 添加到当前元素对应缓存
last.setLevelMax(Math.max(last.getLevelMax(), columns.length)); head.getColumns().add(headItem);
} headMap.put(nowPath, headItem);
return last; headSubs.get(parentPath).add(headItem);
}
private static void setCellRow(TableHead head, TableHeadItem value) {
// 合并行数量
int rowCell = head.getTotalRow() - value.getLevelMax() + 1;
value.setRow(value.getLevel());
value.setRowCell(1);
// 计算第几行,合并几行
if (value.getLevel() == 1) {
// 第一行的规则
if (value.getLevelMax() == 1) {
value.setRowCell(rowCell);
}
} else if (value.getLevel() == value.getLevelMax()) {
// 最后一行的规则
if (value.getLevelMax() == 2) {
value.setRowCell(rowCell);
} else { } else {
value.setRow(head.getTotalRow()); // 当前单元格合并列增加1
} TableHeadItem headItem = headMap.get(nowPath);
} else { headItem.setColumnCell(headItem.getColumnCell() + 1);
// 其他行
if (value.getLevel() == value.getLevelMax() - 1) {
value.setRowCell(rowCell);
}
}
}
private static void setCellColumn(TableHead head, Map<String, List<TableHeadItem>> mapSub, TableHeadItem value) {
// 设置当前列号
value.setColumn(head.getDataColumn() + 1);
// 设置默认合并单元格数量
value.setColumnCell(1);
// 将列添加到当前列集合中
head.getColumns().add(value);
// 计算合并几列
if (value.isDataColumn()) {
head.setDataColumn(head.getDataColumn() + 1);
}
// 假如有子节点,则合并的单元格等于子节点的想加
List<TableHeadItem> subColumns = mapSub.containsKey(value.getPath()) ?
mapSub.get(value.getPath()) : new ArrayList<>();
if (subColumns != null && subColumns.size() > 0) {
value.setColumnCell(0);
for (TableHeadItem subColumn : subColumns) {
setCellColumn(head, mapSub, subColumn);
value.setColumnCell(subColumn.getColumnCell());
} }
} }
return heads.length;
} }
} }
...@@ -16,16 +16,6 @@ public class TableHeadItem { ...@@ -16,16 +16,6 @@ public class TableHeadItem {
*/ */
private String path; private String path;
/**
* 级别
*/
private int level;
/**
* 当前列的最大级别
*/
private int levelMax;
/** /**
* 第几行 * 第几行
*/ */
...@@ -67,22 +57,6 @@ public class TableHeadItem { ...@@ -67,22 +57,6 @@ public class TableHeadItem {
this.path = path; this.path = path;
} }
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getLevelMax() {
return levelMax;
}
public void setLevelMax(int levelMax) {
this.levelMax = levelMax;
}
public int getRow() { public int getRow() {
return row; return row;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment