Commit e0ae13a5 authored by yanzg's avatar yanzg

消除成功接收处理

parent 44934bd2
package com.yanzuoguang.util.table;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.*;
/**
* 表头计算
*
* @author 颜佐光
*/
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 {
public static TableHead getTableHead(String... columns) {
TableHead head = new TableHead();
head.setTotalColumn(columns.length);
// 父子级别关系映射
Map<String, TableHeadItem> mapHead = new LinkedHashMap<String, TableHeadItem>();
// 节点关系映射
Map<String, TableHeadItem> headMap = new LinkedHashMap<>();
// 当前节点的子节点元素
Map<String, List<TableHeadItem>> mapSub = new LinkedHashMap<String, List<TableHeadItem>>();
// 最顶层的元素
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()));
}
Map<String, List<TableHeadItem>> headSubs = new LinkedHashMap<>();
// 计算合并行的数量,第几行
for (Map.Entry<String, TableHeadItem> item : mapHead.entrySet()) {
setCellRow(head, item.getValue());
// 计算每个单元格的大小和占用的行、列
for (int columnPos = 0; columnPos < columns.length; columnPos++) {
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()) {
setCellColumn(head, mapSub, item.getValue());
// 计算最后一行的合并数量
for (Map.Entry<String, TableHeadItem> item : headMap.entrySet()) {
TableHeadItem headItem = item.getValue();
if (headItem.isDataColumn()) {
headItem.setRowCell(head.getTotalRow() - headItem.getRow());
}
}
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();
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();
if (i > 0) {
String parentPath = path.toString().trim();
if (rowIndex > 0) {
path.append(":");
}
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)) {
mapTop.put(nowPath, last);
}
// 添加到最顶层元素中
if (!mapSub.containsKey(parentPath)) {
mapSub.put(parentPath, new ArrayList<TableHeadItem>());
}
// 将元素添加到当前元素中
if (!mapHead.containsKey(nowPath)) {
mapHead.put(nowPath, last);
// 添加到父级元素的下级元素
mapSub.get(parentPath).add(last);
}
last = mapHead.get(nowPath);
last.setLevelMax(Math.max(last.getLevelMax(), columns.length));
}
return last;
}
String nowPath = path.toString();
// 设置当前单元格属性
if (!headMap.containsKey(nowPath)) {
TableHeadItem headItem = new TableHeadItem();
headItem.setRow(rowIndex);
headItem.setRowCell(1);
headItem.setColumn(columnPos);
headItem.setColumnCell(1);
headItem.setName(column);
headItem.setPath(nowPath);
headItem.setDataColumn(fullPath.equals(nowPath));
// 添加到父元素的子节点
if (!headSubs.containsKey(parentPath)) {
headSubs.put(parentPath, new ArrayList<>());
}
// 添加到当前元素对应缓存
head.getColumns().add(headItem);
headMap.put(nowPath, headItem);
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 {
value.setRow(head.getTotalRow());
}
} else {
// 其他行
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());
// 当前单元格合并列增加1
TableHeadItem headItem = headMap.get(nowPath);
headItem.setColumnCell(headItem.getColumnCell() + 1);
}
}
return heads.length;
}
}
......@@ -16,16 +16,6 @@ public class TableHeadItem {
*/
private String path;
/**
* 级别
*/
private int level;
/**
* 当前列的最大级别
*/
private int levelMax;
/**
* 第几行
*/
......@@ -67,22 +57,6 @@ public class TableHeadItem {
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() {
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