package com.mm.service.impl;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.mm.service.IReceiveFileService;
|
import com.mm.util.DateUtil;
|
import com.mm.util.FileUtil;
|
import com.mm.util.JsonMapper;
|
import com.mm.vo.NcTxtFilePathInfo;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Service;
|
|
import java.io.*;
|
import java.nio.charset.Charset;
|
import java.util.Scanner;
|
|
/**
|
* @author clown
|
* * @date 2023/12/15
|
*/
|
@Service
|
@PropertySource("classpath:/scheduled.properties")
|
public class ReceiveFileServiceImpl implements IReceiveFileService {
|
@Value("${giveServiceTo.filePath}")
|
private String filePath;
|
|
@Value("${giveServiceTo.newFilePath}")
|
private String newFilePath;
|
@Value("${fileNCPath}")
|
private String fileNCPath;
|
|
@Override
|
@Scheduled(cron = "${equipmentCron}")
|
public void analysisAndCopyFile() {
|
if (StringUtils.isBlank(filePath)) {
|
return;
|
}
|
File f3 = new File(filePath);
|
File[] files = f3.listFiles();
|
for (File fi : files){
|
InputStreamReader input =null;
|
BufferedReader bufferedReader =null;
|
String loFilePath = filePath + "\\" + fi.getName();
|
System.out.println(loFilePath);
|
try {
|
if (FileUtil.getFileSuffix(fi.getName()).equals("nc")) {
|
String line;
|
int count = 0;
|
NcTxtFilePathInfo result = new NcTxtFilePathInfo();
|
FileInputStream fis = null;
|
try {
|
fis = new FileInputStream(loFilePath);
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
}
|
Scanner sc = new Scanner(fis, "UTF-8");
|
while (count < 8 && sc.hasNext()) {
|
line = sc.nextLine();
|
if (count ==0) {
|
result.setFileTxtName(line.trim());
|
} else if (count ==1) {
|
result.setFileNcName(line.trim());
|
}else if (count ==2) {
|
result.setOrigFileName(line.trim());
|
}else if (count ==3) {
|
result.setOrigFileSuffix(line.trim());
|
}else if (count ==4) {
|
result.setFilePath(line.trim());
|
}else if (count ==5) {
|
result.setEquipmentId(line.trim());
|
}else if (count ==6) {
|
result.setFileAddOrDelete(Integer.valueOf(line.trim()));
|
}else if (count == 7) {
|
result.setFileSize(line.trim());
|
}
|
count++;
|
}
|
if (sc != null) {
|
sc.close();
|
}
|
if (bufferedReader != null) {
|
bufferedReader.close();
|
}
|
if (input != null) {
|
input.close();
|
}
|
if (result == null) {
|
continue;
|
}
|
if (result.getFileAddOrDelete() != null ) {
|
if ( result.getFileAddOrDelete()==1) {
|
String path;
|
/*if(StringUtils.isBlank(result.getOrigFileSuffix())) {
|
path = filePath + "\\" + result.getFileNcName();
|
} else {
|
path = filePath + "\\" + result.getFileNcName() + "." + result.getOrigFileSuffix();
|
}*/
|
path = filePath + "\\" + result.getFileNcName()+ ".NC";
|
if (StringUtils.isNotBlank(result.getFileNcName()) &&
|
result.getFileNcName().equals("null")) {
|
//备份数据
|
boolean b = FileUtil.copyFile(loFilePath,newFilePath + "/" + DateUtil.format(DateUtil.getNow(),DateUtil.STRDATE) + "/" +fi.getName(),null);
|
if (b) {
|
FileUtil.deleteFile(loFilePath);
|
}
|
continue;
|
}
|
File ncFile = new File(path);
|
if (ncFile == null) {
|
continue;
|
} else {
|
//已经存在文件需要备份
|
String size = String.valueOf(ncFile.length());
|
if (!size.equals(result.getFileSize())) {
|
continue;
|
}
|
boolean nc = false;
|
//FileUtil.copyFileNcToBak(result.getFilePath(),result.getOrigFileName(),result.getOrigFileSuffix());
|
if(StringUtils.isBlank(result.getOrigFileSuffix())) {
|
nc = FileUtil.copyFile(path,fileNCPath + "/" + result.getFilePath() + "/send/" +result.getOrigFileName(),
|
fileNCPath + "/" + result.getFilePath() + "/send_bak/" + DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE) + "/"
|
+result.getOrigFileName());
|
} else {
|
nc = FileUtil.copyFile(path,fileNCPath + "/" + result.getFilePath() + "/send/" +result.getOrigFileName()+ "." + result.getOrigFileSuffix()
|
,fileNCPath + "/" + result.getFilePath() + "/send_bak/" + DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE) + "/" +result.getOrigFileName()+ "." + result.getOrigFileSuffix());
|
}
|
try {
|
if (bufferedReader != null) {
|
bufferedReader.close();
|
}
|
if (input != null) {
|
input.close();
|
}
|
} catch (IOException e1) {
|
e1.printStackTrace();
|
}
|
if (nc) {
|
//备份数据
|
boolean b = FileUtil.copyFile(loFilePath,newFilePath + "/" + DateUtil.format(DateUtil.getNow(),DateUtil.STRDATE) + "/" +fi.getName(),null);
|
if (b) {
|
FileUtil.deleteFile(loFilePath);
|
}
|
//备份数据
|
boolean c = FileUtil.copyFile(path,newFilePath + "/" + DateUtil.format(DateUtil.getNow(),DateUtil.STRDATE) + "/" +result.getFileNcName() + "." + result.getOrigFileSuffix(),null);
|
if (c) {
|
FileUtil.deleteFile(path);
|
}
|
}
|
}
|
} else {
|
String path;
|
if(StringUtils.isBlank(result.getOrigFileSuffix())) {
|
path = fileNCPath + "/" + result.getFilePath() + "/send/" +result.getOrigFileName();
|
} else {
|
path = fileNCPath + "/" + result.getFilePath() + "/send/" +result.getOrigFileName()+ "." + result.getOrigFileSuffix();
|
}
|
|
File ncFile = new File(path);
|
if (ncFile == null) {
|
return;
|
} else {
|
//待优化
|
boolean deleteFile = FileUtil.copyFileNcToBak(result.getFilePath(),
|
result.getOrigFileName(), result.getOrigFileSuffix());
|
FileUtil.deleteFile(path);
|
FileUtil.deleteFile(loFilePath);
|
}
|
}
|
|
}
|
}
|
} catch (Exception e1) {
|
try {
|
if (bufferedReader != null) {
|
bufferedReader.close();
|
}
|
if (input != null) {
|
input.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
e1.printStackTrace();
|
} finally {
|
try {
|
if (bufferedReader != null) {
|
bufferedReader.close();
|
}
|
if (input != null) {
|
input.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
}
|