controller
@ApiOperation("导入员工列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "file", value = "导入文件", dataType = "java.io.File", required = true),
})
@Log(title = "员工管理", businessType = BusinessType.IMPORT)
@PostMapping("/importData")
public R<Void> importData(MultipartFile file, boolean updateSupport, String departId) throws Exception {
updateSupport=true;
List<String> names=iEntStaffService.readExcel(file);
ExcelResult<EntStaffImportVo> result = ExcelUtil.importExcel(file.getInputStream(), EntStaffImportVo.class, new EntStaffImportListener(updateSupport,departId,names));
return R.ok(result.getAnalysis());
}
service
public List<String> readExcel(MultipartFile file) throws IOException {
List<String> names=new ArrayList<>();
// 获取输入流,将 Excel 文件读取出来
InputStream inputStream = file.getInputStream();
List<EntStaffImportVo> entStaffImportVoList = EasyExcel.read(inputStream)
// 设置与Excel表映射的类
.head(EntStaffImportVo.class)
// 设置sheet,默认读取第一个
.sheet()
// 设置标题所在行数
.headRowNumber(1)
// 异步读取
.doReadSync();
if(entStaffImportVoList!=null&&entStaffImportVoList.size()>0){
List<EntStaffImportVo> resList=new ArrayList<>();
List<EntStaffImportVo> results = HashMultiset.create(entStaffImportVoList).entrySet().stream()
.filter(w -> w.getCount() > 1)
.map(Multiset.Entry::getElement)
.collect(Collectors.toList());
// 取出来重复身份证号的
List<String> idcardList = entStaffImportVoList.stream().collect(Collectors.groupingBy(EntStaffImportVo::getIdcard, Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey).collect(Collectors.toList());
// 取出来重复手机号的
List<String> phoneList = entStaffImportVoList.stream().collect(Collectors.groupingBy(EntStaffImportVo::getPhone, Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey).collect(Collectors.toList());
for(EntStaffImportVo entStaffImportVo:entStaffImportVoList){
if(idcardList.contains(entStaffImportVo.getIdcard())||phoneList.contains(entStaffImportVo.getPhone())){
resList.add(entStaffImportVo);
}
}
//根据身份号码和手机号去重
ArrayList<EntStaffImportVo> paperRecordList = resList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(item->item.getIdcard()+"_"+item.getPhone()+"_"+item.getStaffName()))), ArrayList::new)
);
if(paperRecordList!=null&&paperRecordList.size()>0){
for (EntStaffImportVo entStaffImportVo:paperRecordList){
names.add(entStaffImportVo.getStaffName());
}
}
}
return names;
}