|
@@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
@@ -39,7 +40,7 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Transactional(readOnly = true)
|
|
@Transactional(readOnly = true)
|
|
|
@Component
|
|
@Component
|
|
|
-public class ExcelImportUtilService {
|
|
|
|
|
|
|
+public class ExcelImportUtilService<T extends Serializable, R extends Comparable<? super R>> {
|
|
|
|
|
|
|
|
@Value("${jeecg.path.upload}")
|
|
@Value("${jeecg.path.upload}")
|
|
|
private String upLoadPath;
|
|
private String upLoadPath;
|
|
@@ -49,19 +50,16 @@ public class ExcelImportUtilService {
|
|
|
* @param clazz
|
|
* @param clazz
|
|
|
* @param duplicateRm 去重因子
|
|
* @param duplicateRm 去重因子
|
|
|
* @param dbExistsListService 根据要导入的数据,查询当前库里是否有已存在
|
|
* @param dbExistsListService 根据要导入的数据,查询当前库里是否有已存在
|
|
|
- * @param extractUnion 取出并集,交集(用于更新) T参数为import,U参数为数据库已有的参数
|
|
|
|
|
- * @param extractDiffSet 取出差集,用于新增 T参数为import,U参数为数据库已有的参数
|
|
|
|
|
|
|
+ * @param fillUpdateField 遇到要更新的数据时,填充更新字段
|
|
|
* @param updateService 更新服务
|
|
* @param updateService 更新服务
|
|
|
* @param insertService 新增服务
|
|
* @param insertService 新增服务
|
|
|
* @return
|
|
* @return
|
|
|
- * @param <T>
|
|
|
|
|
*/
|
|
*/
|
|
|
@Transactional(readOnly = false)
|
|
@Transactional(readOnly = false)
|
|
|
- public <T extends Serializable> Result<?> importExcel(HttpServletRequest request, Class<T> clazz,
|
|
|
|
|
- Function<T, String> duplicateRm,
|
|
|
|
|
|
|
+ public Result<?> importExcel(HttpServletRequest request,
|
|
|
|
|
+ Class<T> clazz, Function<T, R> duplicateRm,
|
|
|
BiFunction<List<T>, List<T>> dbExistsListService,
|
|
BiFunction<List<T>, List<T>> dbExistsListService,
|
|
|
- com.alibaba.fastjson.util.BiFunction<List<T>,List<T>, List<T>> extractUnion,
|
|
|
|
|
- com.alibaba.fastjson.util.BiFunction<List<T>, List<T>, List<T>> extractDiffSet,
|
|
|
|
|
|
|
+ com.alibaba.fastjson.util.BiFunction<T,T,T> fillUpdateField,
|
|
|
com.alibaba.fastjson.util.BiFunction<List<T>, Integer,Boolean> updateService,
|
|
com.alibaba.fastjson.util.BiFunction<List<T>, Integer,Boolean> updateService,
|
|
|
com.alibaba.fastjson.util.BiFunction<List<T>, Integer,Boolean> insertService
|
|
com.alibaba.fastjson.util.BiFunction<List<T>, Integer,Boolean> insertService
|
|
|
) {
|
|
) {
|
|
@@ -82,14 +80,15 @@ public class ExcelImportUtilService {
|
|
|
if (list.size() > 10000) {
|
|
if (list.size() > 10000) {
|
|
|
return Result.error("文件导入失败:不可一次性导入超过10000条数据!");
|
|
return Result.error("文件导入失败:不可一次性导入超过10000条数据!");
|
|
|
}
|
|
}
|
|
|
- // 去重
|
|
|
|
|
- list = list.stream().collect(
|
|
|
|
|
|
|
+ // 去重 暂不去重
|
|
|
|
|
+/* list = list.stream().collect(
|
|
|
Collectors.collectingAndThen(
|
|
Collectors.collectingAndThen(
|
|
|
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
|
|
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
|
|
|
duplicateRm
|
|
duplicateRm
|
|
|
))), ArrayList::new
|
|
))), ArrayList::new
|
|
|
)
|
|
)
|
|
|
- );
|
|
|
|
|
|
|
+ );*/
|
|
|
|
|
+
|
|
|
//检查是否有更新的数据
|
|
//检查是否有更新的数据
|
|
|
List<T> dbExistsList = dbExistsListService.apply(list);
|
|
List<T> dbExistsList = dbExistsListService.apply(list);
|
|
|
// 更新列表
|
|
// 更新列表
|
|
@@ -98,14 +97,16 @@ public class ExcelImportUtilService {
|
|
|
List<T> instalList = list;
|
|
List<T> instalList = list;
|
|
|
if (!CollectionUtils.isEmpty(dbExistsList)) { // 不为空时, 需要将存在的做更新操作
|
|
if (!CollectionUtils.isEmpty(dbExistsList)) { // 不为空时, 需要将存在的做更新操作
|
|
|
// 取出交集
|
|
// 取出交集
|
|
|
- updateList = extractUnion.apply(list,dbExistsList);
|
|
|
|
|
|
|
+ updateList = this.getUpdateDatas(list, dbExistsList, duplicateRm, fillUpdateField);//
|
|
|
|
|
+// extractUnion.apply(list,dbExistsList);
|
|
|
// 如果有则需要更新
|
|
// 如果有则需要更新
|
|
|
if (!CollectionUtils.isEmpty(updateList)) {
|
|
if (!CollectionUtils.isEmpty(updateList)) {
|
|
|
// this.updateBatchById(updateList, 500);
|
|
// this.updateBatchById(updateList, 500);
|
|
|
updateService.apply(updateList, 500);
|
|
updateService.apply(updateList, 500);
|
|
|
}
|
|
}
|
|
|
// 取出差集
|
|
// 取出差集
|
|
|
- instalList = extractDiffSet.apply(list, dbExistsList);
|
|
|
|
|
|
|
+ instalList = this.getInsertDatas(list, dbExistsList, duplicateRm);
|
|
|
|
|
+ //extractDiffSet.apply(list, dbExistsList);
|
|
|
}
|
|
}
|
|
|
//update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
//update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
|
long start = System.currentTimeMillis();
|
|
long start = System.currentTimeMillis();
|
|
@@ -140,6 +141,37 @@ public class ExcelImportUtilService {
|
|
|
return Result.error("文件导入失败!");
|
|
return Result.error("文件导入失败!");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private List<T> getInsertDatas(List<T> list, List<T> dbExistsList, Function<T,R> duplicateRm) {
|
|
|
|
|
+ return list.stream().filter(
|
|
|
|
|
+ excelData -> dbExistsList.stream().map(duplicateRm).noneMatch(medicineCode ->
|
|
|
|
|
+ ObjectUtils.nullSafeEquals(duplicateRm.apply(excelData), medicineCode))
|
|
|
|
|
+ ).collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public List<T> getUpdateDatas(
|
|
|
|
|
+ List<T> list, List<T> dbExistsList,
|
|
|
|
|
+ Function<? super T, ? extends R> mapper, com.alibaba.fastjson.util.BiFunction<T, T, T> process) {
|
|
|
|
|
+ List<T> collect = list.stream().filter(
|
|
|
|
|
+ a -> dbExistsList.stream().map(mapper)
|
|
|
|
|
+ .anyMatch(
|
|
|
|
|
+ medicineCode -> ObjectUtils.nullSafeEquals(mapper.apply(a), medicineCode)
|
|
|
|
|
+ )
|
|
|
|
|
+ ).collect(Collectors.toList());
|
|
|
|
|
+ //取出交集之后,要将相同数据的数据库id给新数据
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(collect)) {
|
|
|
|
|
+ collect = collect.stream().map(item -> {
|
|
|
|
|
+ Optional<T> first = dbExistsList.stream().filter(o -> ObjectUtils.nullSafeEquals(mapper, mapper)).findFirst();
|
|
|
|
|
+ if (first.isPresent()) {
|
|
|
|
|
+ T t = first.get();
|
|
|
|
|
+ item = process.apply(item, t);
|
|
|
|
|
+ }
|
|
|
|
|
+ return item;
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+ return collect;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public <T extends Serializable> ModelAndView getModelAndView(HttpServletRequest request, T object, Class<T> clazz, String title) {
|
|
public <T extends Serializable> ModelAndView getModelAndView(HttpServletRequest request, T object, Class<T> clazz, String title) {
|
|
|
QueryWrapper<T> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
|
|
QueryWrapper<T> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
|
|
|
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
|
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|