mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-17 08:46:39 +08:00
Compare commits
3 Commits
c01ed34602
...
ffc3dcaec9
Author | SHA1 | Date | |
---|---|---|---|
|
ffc3dcaec9 | ||
|
a94e474069 | ||
|
40a0e57870 |
@ -31,12 +31,89 @@ public class CellMergeHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public List<CellRangeAddress> handle(List<?> list) {
|
public List<CellRangeAddress> handle(List<?> rows) {
|
||||||
List<CellRangeAddress> cellList = new ArrayList<>();
|
// 如果入参为空集合则返回空集
|
||||||
if (CollUtil.isEmpty(list)) {
|
if (CollUtil.isEmpty(rows)) {
|
||||||
return cellList;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
Class<?> clazz = list.get(0).getClass();
|
|
||||||
|
// 获取有合并注解的字段
|
||||||
|
Map<Field, FieldColumnIndex> mergeFields = getFieldColumnIndexMap(rows.get(0).getClass());
|
||||||
|
// 如果没有需要合并的字段则返回空集
|
||||||
|
if (CollUtil.isEmpty(mergeFields)) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 结果集
|
||||||
|
List<CellRangeAddress> result = new ArrayList<>();
|
||||||
|
|
||||||
|
// 生成两两合并单元格
|
||||||
|
Map<Field, RepeatCell> rowRepeatCellMap = new HashMap<>();
|
||||||
|
for (Map.Entry<Field, FieldColumnIndex> item : mergeFields.entrySet()) {
|
||||||
|
Field field = item.getKey();
|
||||||
|
FieldColumnIndex itemValue = item.getValue();
|
||||||
|
int colNum = itemValue.colIndex();
|
||||||
|
CellMerge cellMerge = itemValue.cellMerge();
|
||||||
|
|
||||||
|
for (int i = 0; i < rows.size(); i++) {
|
||||||
|
// 当前行数据
|
||||||
|
Object currentRowObj = rows.get(i);
|
||||||
|
// 当前行数据字段值
|
||||||
|
Object currentRowObjFieldVal = ReflectUtils.invokeGetter(currentRowObj, field.getName());
|
||||||
|
|
||||||
|
// 空值跳过不处理
|
||||||
|
if (currentRowObjFieldVal == null || "".equals(currentRowObjFieldVal)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单元格合并Map是否存在数据,如果不存在则添加当前行的字段值
|
||||||
|
if (!rowRepeatCellMap.containsKey(field)) {
|
||||||
|
rowRepeatCellMap.put(field, RepeatCell.of(currentRowObjFieldVal, i));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取 单元格合并Map 中字段值
|
||||||
|
RepeatCell repeatCell = rowRepeatCellMap.get(field);
|
||||||
|
Object cellValue = repeatCell.value();
|
||||||
|
int current = repeatCell.current();
|
||||||
|
|
||||||
|
// 检查是否满足合并条件
|
||||||
|
// currentRowObj 当前行数据
|
||||||
|
// rows.get(i - 1) 上一行数据 注:由于 if (!rowRepeatCellMap.containsKey(field)) 条件的存在,所以该 i 必不可能小于1
|
||||||
|
// cellMerge 当前行字段合并注解
|
||||||
|
boolean merge = isMerge(currentRowObj, rows.get(i - 1), cellMerge);
|
||||||
|
|
||||||
|
// 是否添加到结果集
|
||||||
|
boolean isAddResult = false;
|
||||||
|
// 最新行
|
||||||
|
int lastRow = i + rowIndex - 1;
|
||||||
|
|
||||||
|
// 如果当前行字段值和缓存中的字段值不相等,或不满足合并条件,则替换
|
||||||
|
if (!currentRowObjFieldVal.equals(cellValue) || !merge) {
|
||||||
|
rowRepeatCellMap.put(field, RepeatCell.of(currentRowObjFieldVal, i));
|
||||||
|
isAddResult = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果最后一行不能合并,检查之前的数据是否需要合并;如果最后一行可以合并,则直接合并到最后
|
||||||
|
if (i == rows.size() - 1) {
|
||||||
|
isAddResult = true;
|
||||||
|
if (i > current) {
|
||||||
|
lastRow = i + rowIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isAddResult && i > current) {
|
||||||
|
result.add(new CellRangeAddress(current + rowIndex, lastRow, colNum, colNum));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取带有合并注解的字段列索引和合并注解信息Map集
|
||||||
|
*/
|
||||||
|
private Map<Field, FieldColumnIndex> getFieldColumnIndexMap(Class<?> clazz) {
|
||||||
boolean annotationPresent = clazz.isAnnotationPresent(ExcelIgnoreUnannotated.class);
|
boolean annotationPresent = clazz.isAnnotationPresent(ExcelIgnoreUnannotated.class);
|
||||||
Field[] fields = ReflectUtils.getFields(clazz, field -> {
|
Field[] fields = ReflectUtils.getFields(clazz, field -> {
|
||||||
if ("serialVersionUID".equals(field.getName())) {
|
if ("serialVersionUID".equals(field.getName())) {
|
||||||
@ -49,86 +126,57 @@ public class CellMergeHandler {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 有注解的字段
|
// 有注解的字段
|
||||||
List<Field> mergeFields = new ArrayList<>();
|
Map<Field, FieldColumnIndex> mergeFields = new HashMap<>();
|
||||||
List<Integer> mergeFieldsIndex = new ArrayList<>();
|
|
||||||
for (int i = 0; i < fields.length; i++) {
|
for (int i = 0; i < fields.length; i++) {
|
||||||
Field field = fields[i];
|
Field field = fields[i];
|
||||||
if (field.isAnnotationPresent(CellMerge.class)) {
|
if (!field.isAnnotationPresent(CellMerge.class)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
CellMerge cm = field.getAnnotation(CellMerge.class);
|
CellMerge cm = field.getAnnotation(CellMerge.class);
|
||||||
mergeFields.add(field);
|
int index = cm.index() == -1 ? i : cm.index();
|
||||||
mergeFieldsIndex.add(cm.index() == -1 ? i : cm.index());
|
mergeFields.put(field, FieldColumnIndex.of(index, cm));
|
||||||
|
|
||||||
if (hasTitle) {
|
if (hasTitle) {
|
||||||
ExcelProperty property = field.getAnnotation(ExcelProperty.class);
|
ExcelProperty property = field.getAnnotation(ExcelProperty.class);
|
||||||
rowIndex = Math.max(rowIndex, property.value().length);
|
rowIndex = Math.max(rowIndex, property.value().length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return mergeFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<Field, RepeatCell> map = new HashMap<>();
|
private boolean isMerge(Object currentRow, Object preRow, CellMerge cellMerge) {
|
||||||
// 生成两两合并单元格
|
final String[] mergeBy = cellMerge.mergeBy();
|
||||||
for (int i = 0; i < list.size(); i++) {
|
|
||||||
Object rowObj = list.get(i);
|
|
||||||
for (int j = 0; j < mergeFields.size(); j++) {
|
|
||||||
Field field = mergeFields.get(j);
|
|
||||||
Object val = ReflectUtils.invokeGetter(rowObj, field.getName());
|
|
||||||
|
|
||||||
int colNum = mergeFieldsIndex.get(j);
|
|
||||||
if (!map.containsKey(field)) {
|
|
||||||
map.put(field, new RepeatCell(val, i));
|
|
||||||
} else {
|
|
||||||
RepeatCell repeatCell = map.get(field);
|
|
||||||
Object cellValue = repeatCell.value();
|
|
||||||
if (cellValue == null || "".equals(cellValue)) {
|
|
||||||
// 空值跳过不合并
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cellValue.equals(val)) {
|
|
||||||
if ((i - repeatCell.current() > 1)) {
|
|
||||||
cellList.add(new CellRangeAddress(repeatCell.current() + rowIndex, i + rowIndex - 1, colNum, colNum));
|
|
||||||
}
|
|
||||||
map.put(field, new RepeatCell(val, i));
|
|
||||||
} else if (i == list.size() - 1) {
|
|
||||||
if (!isMerge(list, i, field)) {
|
|
||||||
// 如果最后一行不能合并,检查之前的数据是否需要合并
|
|
||||||
if (i - repeatCell.current() > 1) {
|
|
||||||
cellList.add(new CellRangeAddress(repeatCell.current() + rowIndex, i + rowIndex - 1, colNum, colNum));
|
|
||||||
}
|
|
||||||
} else if (i > repeatCell.current()) {
|
|
||||||
// 如果最后一行可以合并,则直接合并到最后
|
|
||||||
cellList.add(new CellRangeAddress(repeatCell.current() + rowIndex, i + rowIndex, colNum, colNum));
|
|
||||||
}
|
|
||||||
} else if (!isMerge(list, i, field)) {
|
|
||||||
if ((i - repeatCell.current() > 1)) {
|
|
||||||
cellList.add(new CellRangeAddress(repeatCell.current() + rowIndex, i + rowIndex - 1, colNum, colNum));
|
|
||||||
}
|
|
||||||
map.put(field, new RepeatCell(val, i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cellList;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isMerge(List<?> list, int i, Field field) {
|
|
||||||
boolean isMerge = true;
|
|
||||||
CellMerge cm = field.getAnnotation(CellMerge.class);
|
|
||||||
final String[] mergeBy = cm.mergeBy();
|
|
||||||
if (StrUtil.isAllNotBlank(mergeBy)) {
|
if (StrUtil.isAllNotBlank(mergeBy)) {
|
||||||
//比对当前list(i)和list(i - 1)的各个属性值一一比对 如果全为真 则为真
|
//比对当前行和上一行的各个属性值一一比对 如果全为真 则为真
|
||||||
for (String fieldName : mergeBy) {
|
for (String fieldName : mergeBy) {
|
||||||
final Object valCurrent = ReflectUtil.getFieldValue(list.get(i), fieldName);
|
final Object valCurrent = ReflectUtil.getFieldValue(currentRow, fieldName);
|
||||||
final Object valPre = ReflectUtil.getFieldValue(list.get(i - 1), fieldName);
|
final Object valPre = ReflectUtil.getFieldValue(preRow, fieldName);
|
||||||
if (!Objects.equals(valPre, valCurrent)) {
|
if (!Objects.equals(valPre, valCurrent)) {
|
||||||
//依赖字段如有任一不等值,则标记为不可合并
|
//依赖字段如有任一不等值,则标记为不可合并
|
||||||
isMerge = false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return isMerge;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
record RepeatCell(Object value, int current) {}
|
/**
|
||||||
|
* 单元格合并
|
||||||
|
*/
|
||||||
|
record RepeatCell(Object value, int current) {
|
||||||
|
static RepeatCell of(Object value, int current) {
|
||||||
|
return new RepeatCell(value, current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字段列索引和合并注解信息
|
||||||
|
*/
|
||||||
|
record FieldColumnIndex(int colIndex, CellMerge cellMerge) {
|
||||||
|
static FieldColumnIndex of(int colIndex, CellMerge cellMerge) {
|
||||||
|
return new FieldColumnIndex(colIndex, cellMerge);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个单元格合并处理器实例
|
* 创建一个单元格合并处理器实例
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package org.dromara.common.json.handler;
|
package org.dromara.common.json.handler;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateTime;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
|
import org.dromara.common.core.utils.ObjectUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -25,7 +27,11 @@ public class CustomDateDeserializer extends JsonDeserializer<Date> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
return DateUtil.parse(p.getText());
|
DateTime parse = DateUtil.parse(p.getText());
|
||||||
|
if (ObjectUtils.isNull(parse)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return parse.toJdkDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user