package com.lxzn.base.service.impl; import com.lxzn.base.service.IReceiveFileService; import com.lxzn.base.service.ISingleDictionaryService; import com.lxzn.framework.domain.base.SingleDictionary; import com.lxzn.framework.domain.base.ext.NcTxtFilePathInfo; import com.lxzn.framework.utils.date.DateUtil; import com.lxzn.framework.utils.file.FileUtil; import com.lxzn.nc.service.IDocInfoService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; 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.util.Scanner; /** * @author clown * * @date 2023/12/15 */ @Service @PropertySource("classpath:/scheduled.properties") public class ReceiveFileServiceImpl implements IReceiveFileService { @Autowired private IDocInfoService docInfoService; @Autowired private ISingleDictionaryService dictionaryService; @Value("${serviceIntranet.filePath}") private String filePath; @Value("${serviceIntranet.newFilePath}") private String newFilePath; @Value("${fileNCPath}") private String fileNCPath; @Override @Scheduled(cron = "${ncFileRec}") public void analysisAndCopyFileRec() { 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 = ""; SingleDictionary singleDictionary=dictionaryService.findListFromTypeCode("virtual_equipment_id").get(0); String docEquipmentId = null; if (singleDictionary != null) { docEquipmentId= String.valueOf(singleDictionary.getDicValue()); } if (result.getEquipmentId().equals(docEquipmentId)) { //虚拟设备处理 path = filePath + result.getOrigFileName(); if (StringUtils.isNotBlank(result.getOrigFileName()) && result.getOrigFileName().equals("null")) { //文件待处理 //备份数据 FileUtil.deleteFileNewRec(loFilePath); continue; } }else { path = filePath + result.getFileNcName()+ ".NC"; if (StringUtils.isNotBlank(result.getFileNcName()) && result.getFileNcName().equals("null")) { //文件待处理 //备份数据 FileUtil.deleteFileNewRec(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 bool = docInfoService.addDocInfoRecService(result.getEquipmentId(), ncFile,result.getOrigFileSuffix(), FileUtil.getFilenameNonSuffix(result.getOrigFileName()) ,result.getFilePath()); if (bool) { FileUtil.deleteFileNewRec(path); FileUtil.deleteFileNewRec(loFilePath); } } } 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(); } } } } }