public static <T> List<T> excelSortLoanNo(List<T> list) {
Predicate<List<T>> singleOrNot = equalsList -> equalsList.size() > 1;
//可针对三种情况导出列表,<T>对导出的借据号重复的放在一起(并提示与哪一行重复),为null的排除
List listVisiable = list.stream()
.filter(exl -> {
try {
Field field = exl.getClass().getDeclaredField(LOAN_NUMBER);
field.setAccessible(true);
return field.get(exl) != null;
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
})
.sorted((in1, in2) -> {
Field field1 = null;
Field field2 = null;
try {
field1 = in1.getClass().getDeclaredField(LOAN_NUMBER);
field2 = in2.getClass().getDeclaredField(LOAN_NUMBER);
field1.setAccessible(true);
field2.setAccessible(true);
} catch (NoSuchFieldException e) {
log.error("excelSortLoanNo字段不存在");
}
return field1.hashCode() - field2.hashCode();
})
.collect(Collectors.groupingBy(exl -> {
try {
Field declaredField = exl.getClass().getDeclaredField(LOAN_NUMBER);
declaredField.setAccessible(true);
return declaredField.get(exl);
} catch (IllegalAccessException e) {
log.error("checkOutListCondition非法访问");
throw new RuntimeException("提取数据异常");
} catch (NoSuchFieldException e) {
log.error("checkOutListCondition字段不存在");
throw new RuntimeException("提取数据异常");
}
}))
.values().stream()
.filter(loanNoDuplicateList -> singleOrNot.test(loanNoDuplicateList))
.map(oneGroup -> {
List<Object> sequenceIds = oneGroup.stream().map(one -> {
try {
Field declaredField = one.getClass().getDeclaredField(SEQUENCE_NAME);
declaredField.setAccessible(true);
return declaredField.get(one);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
return oneGroup.stream()
.peek(one -> {
try {
Field failedReasons = one.getClass().getDeclaredField(FAILED_REASONS);
failedReasons.setAccessible(true);
Object reasonsOld = failedReasons.get(one);
Field sequenceField = one.getClass().getDeclaredField(SEQUENCE_NAME);
sequenceField.setAccessible(true);
Object thisOneSequenceId = sequenceField.get(one);
List sequenceIdList = sequenceIds.stream()
.filter(se -> !se.equals(thisOneSequenceId)).collect(Collectors.toList());
Object[] sequenceIdArr = sequenceIdList.stream().toArray();
String sequenceIdArrStr = StringUtils.join(sequenceIdArr, ",");
String failedReasonsFresh = reasonsOld.toString()
.replace("excel内借据号重复", "excel内借据号重复,与序号" + sequenceIdArrStr + "条重复");
failedReasons.set(one, failedReasonsFresh);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
})
.collect(Collectors.toList());
})
.reduce((l1, l2) -> {
l1.addAll(l2);
return l1;
})
.filter(list1 -> !CollectionUtils.isEmpty(list1))
.orElse(Collections.EMPTY_LIST);
list.removeAll(listVisiable);
listVisiable.addAll(list);
return listVisiable;
}
其中重要的几步都已经高亮展示
当时的注释:
- excel排序根据LoanNo
- 先进行整个list列表的序号填充
- 获取loanNo借据号不为空的进行排序,相同借据号的排序到一起
- 将相同借据号的数据提示,[与第2,3条借据号重复][与第1,3条借据号重复][与1,2条借据号重复]
- 可针对三种情况导出列表,
对导出的借据号重复的放在一起(并提示与哪一行重复),为null的排除 - TODO 因为现在导出列表时最后也有写入序号,所以在执行这个方法时只能取消导出时的写入序号操作