|
|
|
@ -15,7 +15,11 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileReader; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
@ -34,24 +38,31 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void saveByExcel() { |
|
|
|
public void saveByExcel() { |
|
|
|
//获取每一个文件夹路径 |
|
|
|
List<EquipmentFolderLocation> locationList = equipmentFolderLocationService.list(); |
|
|
|
if(CollectionUtils.isEmpty(locationList)){ |
|
|
|
if (CollectionUtils.isEmpty(locationList)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
//创建线程池 |
|
|
|
ExecutorService executorService = Executors.newFixedThreadPool(locationList.size());//线程池的最大数量为文件夹的个数 |
|
|
|
for (EquipmentFolderLocation excel : locationList) { |
|
|
|
String folderFiler = excel.getFolderPath(); |
|
|
|
executorService.submit(()->{ |
|
|
|
saveInformation(folderFiler,excel); |
|
|
|
|
|
|
|
executorService.submit(() -> { |
|
|
|
try { |
|
|
|
//对文件夹下的文件进行读取并保存 |
|
|
|
saveInformation(excel); |
|
|
|
}catch (Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
//关闭线程 |
|
|
|
executorService.shutdown(); |
|
|
|
try { |
|
|
|
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // 等待所有任务完成 |
|
|
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
Thread.currentThread().interrupt(); |
|
|
|
log.info("线程池中断异常"); |
|
|
|
@ -59,54 +70,248 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void saveInformation(String folderFiler,EquipmentFolderLocation excel) { |
|
|
|
private void saveInformation( EquipmentFolderLocation excel) throws Exception { |
|
|
|
String folderFiler = excel.getFolderPath(); |
|
|
|
//获取文件夹下所有文件 |
|
|
|
List<File> files=getExcelFiles(folderFiler); |
|
|
|
if(CollectionUtils.isEmpty(files)){ |
|
|
|
List<File> files = getExcelFiles(folderFiler); |
|
|
|
if (CollectionUtils.isEmpty(files)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
//判断这个批次号应该是多少 |
|
|
|
LambdaQueryWrapper<EquipmentDataDetail> queryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
queryWrapper.eq(EquipmentDataDetail::getBuNo,excel.getBuNo()); |
|
|
|
queryWrapper.eq(EquipmentDataDetail::getSite,excel.getSite()); |
|
|
|
queryWrapper.eq(EquipmentDataDetail::getFileNo,excel.getFileNo()); |
|
|
|
queryWrapper.eq(EquipmentDataDetail::getBuNo, excel.getBuNo()); |
|
|
|
queryWrapper.eq(EquipmentDataDetail::getSite, excel.getSite()); |
|
|
|
queryWrapper.eq(EquipmentDataDetail::getFileNo, excel.getFileNo()); |
|
|
|
List<EquipmentDataDetail> equipments = this.list(queryWrapper); |
|
|
|
//定义批次号 |
|
|
|
Integer batchNo; |
|
|
|
if (CollectionUtils.isEmpty(equipments)){ |
|
|
|
batchNo=1; |
|
|
|
}else { |
|
|
|
batchNo=equipments.get(equipments.size()-1).getBatchNo()+1; |
|
|
|
if (CollectionUtils.isEmpty(equipments)) { |
|
|
|
batchNo = 1; |
|
|
|
} else { |
|
|
|
batchNo = equipments.get(equipments.size() - 1).getBatchNo() + 1; |
|
|
|
} |
|
|
|
List<EquipmentDataDetail> equipmentDataDetails =new ArrayList<>(); |
|
|
|
|
|
|
|
//创建一个list用来存储所有的的excel文件路径 |
|
|
|
List<String> fileName = new ArrayList<>(); |
|
|
|
//创建一个list用来存储所有的txt文件路径 |
|
|
|
List<String> txtName = new ArrayList<>(); |
|
|
|
for (File file : files) { |
|
|
|
//找到.的索引 |
|
|
|
int dotIndex = file.getName().lastIndexOf("."); |
|
|
|
String s = file.getName().substring(dotIndex + 1); |
|
|
|
|
|
|
|
if (s.equals("csv")){ |
|
|
|
fileName.add(folderFiler+"\\"+file.getName()); |
|
|
|
if (s.equals("csv")) { |
|
|
|
fileName.add(folderFiler + "\\" + file.getName()); |
|
|
|
} |
|
|
|
if (s.equals("tff")) { |
|
|
|
txtName.add(folderFiler + "\\" + file.getName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//excel(csv)不为空就做新增 |
|
|
|
if (CollectionUtils.isNotEmpty(fileName)) { |
|
|
|
List<EquipmentDataDetail> equipmentDataDetails = saveExcel(fileName, excel, batchNo); |
|
|
|
//批量新增 |
|
|
|
getInformationForExcelMapper.saveByExcels(equipmentDataDetails); |
|
|
|
for (String s : fileName) { |
|
|
|
deleteBypath(s, excel.getBackupFolderPath()); |
|
|
|
} |
|
|
|
} |
|
|
|
//txt(tff)不为空就做新增 |
|
|
|
if (CollectionUtils.isNotEmpty(txtName)) { |
|
|
|
List<EquipmentDataDetail> equipmentDataDetails = saveTxt(txtName, excel, batchNo); |
|
|
|
// 创建一个固定大小的线程池,这里假设有10个线程 |
|
|
|
ExecutorService executor = Executors.newFixedThreadPool(equipmentDataDetails.size()/100); |
|
|
|
int startList =0; |
|
|
|
int endList=100; |
|
|
|
for (int i = startList; i < equipmentDataDetails.size(); i= startList) { |
|
|
|
if (endList>equipmentDataDetails.size()){ |
|
|
|
endList = equipmentDataDetails.size(); |
|
|
|
} |
|
|
|
List<EquipmentDataDetail> details = equipmentDataDetails.subList(startList, endList); |
|
|
|
startList=startList+endList; |
|
|
|
endList=endList+endList; |
|
|
|
executor.execute(()->{ |
|
|
|
//批量新增 |
|
|
|
getInformationForExcelMapper.saveByExcels(details); |
|
|
|
}); |
|
|
|
} |
|
|
|
// 关闭线程池 |
|
|
|
executor.shutdown(); |
|
|
|
for (String s : txtName) { |
|
|
|
deleteBypath( s, excel.getBackupFolderPath()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//新增完之后把原路径的文件复制到另一个文件并删除原路径的下的文件 |
|
|
|
private static void deleteBypath(String endPath,String suffixName) throws Exception{ |
|
|
|
// 创建源文件对象和目标文件夹对象 |
|
|
|
File sourceFile = new File(endPath); |
|
|
|
File destinationDir = new File(suffixName); |
|
|
|
|
|
|
|
// 确保源文件存在且是文件 |
|
|
|
if (sourceFile.exists() && sourceFile.isFile()) { |
|
|
|
// 确保目标文件夹存在且是目录 |
|
|
|
if (!destinationDir.exists()) { |
|
|
|
destinationDir.mkdirs(); // 创建目标文件夹及其父目录 |
|
|
|
} |
|
|
|
|
|
|
|
// 构建目标文件路径 |
|
|
|
String destinationPath = suffixName + File.separator + sourceFile.getName(); |
|
|
|
File destinationFile = new File(destinationPath); |
|
|
|
|
|
|
|
try { |
|
|
|
// 复制文件到目标文件夹 |
|
|
|
Files.copy(sourceFile.toPath(), destinationFile.toPath()); |
|
|
|
System.out.println("文件复制成功!"); |
|
|
|
|
|
|
|
// 删除源文件 |
|
|
|
if (sourceFile.delete()) { |
|
|
|
System.out.println("源文件删除成功!"); |
|
|
|
} else { |
|
|
|
System.out.println("源文件删除失败!"); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
System.out.println("文件操作出错:" + e.getMessage()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
System.out.println("源文件不存在或不是文件。"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args)throws Exception { |
|
|
|
// 源文件路径 |
|
|
|
String sourcePath = "E:\\file\\cs.tff"; |
|
|
|
// 目标文件夹路径 |
|
|
|
String destinationFolder = "E:\\info"; |
|
|
|
|
|
|
|
// 创建源文件对象和目标文件夹对象 |
|
|
|
File sourceFile = new File(sourcePath); |
|
|
|
File destinationDir = new File(destinationFolder); |
|
|
|
|
|
|
|
// 确保源文件存在且是文件 |
|
|
|
if (sourceFile.exists() && sourceFile.isFile()) { |
|
|
|
// 确保目标文件夹存在且是目录 |
|
|
|
if (!destinationDir.exists()) { |
|
|
|
destinationDir.mkdirs(); // 创建目标文件夹及其父目录 |
|
|
|
} |
|
|
|
|
|
|
|
// 构建目标文件路径 |
|
|
|
String destinationPath = destinationFolder + File.separator + sourceFile.getName(); |
|
|
|
File destinationFile = new File(destinationPath); |
|
|
|
|
|
|
|
try { |
|
|
|
// 复制文件到目标文件夹 |
|
|
|
Files.copy(sourceFile.toPath(), destinationFile.toPath()); |
|
|
|
System.out.println("文件复制成功!"); |
|
|
|
|
|
|
|
// 删除源文件 |
|
|
|
if (sourceFile.delete()) { |
|
|
|
System.out.println("源文件删除成功!"); |
|
|
|
} else { |
|
|
|
System.out.println("源文件删除失败!"); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
System.out.println("文件操作出错:" + e.getMessage()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
System.out.println("源文件不存在或不是文件。"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//对tff文件进行操作 |
|
|
|
private static List<EquipmentDataDetail> saveTxt(List<String> fileName, EquipmentFolderLocation excel, Integer batchNo) { |
|
|
|
List<EquipmentDataDetail> equipmentDataDetails = new ArrayList<>(); |
|
|
|
for (String s : fileName) { |
|
|
|
int startLine = 15; // 第一次读取开始的行数 |
|
|
|
int firstReadLines = 41; |
|
|
|
int columnInterval = 11; |
|
|
|
try { |
|
|
|
BufferedReader bufferedReader = new BufferedReader(new FileReader(s)); |
|
|
|
String line; |
|
|
|
List<String> list = new ArrayList<>(); |
|
|
|
// 第一次读取从第 15 行开始的 41 行数据 |
|
|
|
for (int i = 1; i < startLine; i++) { |
|
|
|
bufferedReader.readLine(); // 跳过前面的行数 |
|
|
|
} |
|
|
|
for (int i = 0; i < firstReadLines; i++) { |
|
|
|
line = bufferedReader.readLine(); |
|
|
|
if (line != null) { |
|
|
|
// 处理当前行数据 |
|
|
|
list.add(line); |
|
|
|
} |
|
|
|
} |
|
|
|
// 移动到下一列 |
|
|
|
for (int i = 0; i < columnInterval; i++) { |
|
|
|
line = bufferedReader.readLine(); |
|
|
|
} |
|
|
|
|
|
|
|
// 每隔 11 行读取往下 41 行的数据 |
|
|
|
while ((line = bufferedReader.readLine()) != null) { |
|
|
|
// 从当前行开始读取指定行数 |
|
|
|
for (int i = 0; i < firstReadLines; i++) { |
|
|
|
if (line != null) { |
|
|
|
// 处理当前行数据 |
|
|
|
list.add(line); |
|
|
|
} |
|
|
|
line = bufferedReader.readLine(); |
|
|
|
} |
|
|
|
// 移动到下一列 |
|
|
|
for (int i = 0; i < columnInterval-1; i++) { |
|
|
|
line = bufferedReader.readLine(); |
|
|
|
} |
|
|
|
} |
|
|
|
//对数据进行切割封装 |
|
|
|
for (String info : list) { |
|
|
|
String[] data = info.split("\t"); |
|
|
|
EquipmentDataDetail equipmentDataDetail = new EquipmentDataDetail(); |
|
|
|
equipmentDataDetail.setEquipmentNo(excel.getEquipmentNo()); |
|
|
|
equipmentDataDetail.setBuNo(excel.getBuNo()); |
|
|
|
equipmentDataDetail.setSite(excel.getSite()); |
|
|
|
equipmentDataDetail.setBatchNo(batchNo); |
|
|
|
equipmentDataDetail.setItemNo(excel.getItemNo()); |
|
|
|
equipmentDataDetail.setFileNo(excel.getFileNo()); |
|
|
|
equipmentDataDetails.add(equipmentDataDetail); |
|
|
|
equipmentDataDetail.setValue0(data[0]); |
|
|
|
equipmentDataDetail.setValue1(data[1]); |
|
|
|
equipmentDataDetail.setValue2(data[2]); |
|
|
|
equipmentDataDetail.setValue3(data[3]); |
|
|
|
equipmentDataDetail.setValue4(data[4]); |
|
|
|
equipmentDataDetail.setValue5(data[5]); |
|
|
|
equipmentDataDetail.setValue6(data[6]); |
|
|
|
equipmentDataDetail.setValue7(data[7]); |
|
|
|
equipmentDataDetail.setValue8(data[8]); |
|
|
|
equipmentDataDetail.setValue9(data[9]); |
|
|
|
equipmentDataDetails.add(equipmentDataDetail); |
|
|
|
} |
|
|
|
bufferedReader.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
return equipmentDataDetails; |
|
|
|
} |
|
|
|
|
|
|
|
//对excel(csv)文件进行操作 |
|
|
|
private static List<EquipmentDataDetail> saveExcel(List<String> fileName, EquipmentFolderLocation excel, Integer batchNo) { |
|
|
|
List<EquipmentDataDetail> equipmentDataDetails = new ArrayList<>(); |
|
|
|
//获取每个excel下的所有关于Test的两个值并保存 |
|
|
|
for (String s : fileName) { |
|
|
|
File excelFile = new File(s); |
|
|
|
List<Map<Integer,String>> list=new ArrayList<>(); |
|
|
|
try{ |
|
|
|
List<Map<Integer, String>> list = new ArrayList<>(); |
|
|
|
try { |
|
|
|
list = EasyExcel.read(excelFile) |
|
|
|
.excelType(ExcelTypeEnum.XLSX) |
|
|
|
.headRowNumber(0) |
|
|
|
.sheet() |
|
|
|
.doReadSync(); |
|
|
|
}catch (Exception e){ |
|
|
|
log.info("读取文件失败,文件为:"+s+"失败日志:"+e.getMessage()); |
|
|
|
} catch (Exception e) { |
|
|
|
log.info("读取文件失败,文件为:" + s + "失败日志:" + e.getMessage()); |
|
|
|
} |
|
|
|
//获取TEST后面的两个值 |
|
|
|
for (int i=0;i<list.size();i++) { |
|
|
|
if (list.get(i).get(0).equals("TEST")){ |
|
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
|
if (list.get(i).get(0).equals("TEST")) { |
|
|
|
EquipmentDataDetail equipmentDataDetail = new EquipmentDataDetail(); |
|
|
|
equipmentDataDetail.setEquipmentNo(excel.getEquipmentNo()); |
|
|
|
equipmentDataDetail.setBuNo(excel.getBuNo()); |
|
|
|
@ -120,19 +325,18 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//批量新增 |
|
|
|
getInformationForExcelMapper.saveByExcels(equipmentDataDetails); |
|
|
|
return equipmentDataDetails; |
|
|
|
} |
|
|
|
|
|
|
|
//获取文件夹下的所有文件 |
|
|
|
private static List<File> getExcelFiles(String folderFiler) { |
|
|
|
//创建本地文件夹路径 |
|
|
|
File file = new File(folderFiler); |
|
|
|
//获取本地文件夹下所有excel表格 |
|
|
|
//获取本地文件夹下所有文件 |
|
|
|
File[] files = file.listFiles(); |
|
|
|
List<File> excelFiles=new ArrayList<>(); |
|
|
|
if (file !=null ){ |
|
|
|
List<File> excelFiles = new ArrayList<>(); |
|
|
|
if (file != null) { |
|
|
|
for (File file1 : files) { |
|
|
|
if (file1.isFile()){ |
|
|
|
if (file1.isFile()) { |
|
|
|
excelFiles.add(file1); |
|
|
|
} |
|
|
|
} |
|
|
|
|