package com.ruoyi.interchange.service.impl; import com.alibaba.fastjson2.JSON; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.uuid.UUID; import com.ruoyi.framework.config.ActiveManager; import com.ruoyi.interchange.domain.TbDataRules; import com.ruoyi.interchange.domain.TbDataSendRecords; import com.ruoyi.interchange.service.ITbDataSendRecordsService; import com.ruoyi.interchange.service.PlatformService; import com.ruoyi.utils.DESUtil; import com.ruoyi.utils.FileUtils; import com.ruoyi.utils.FtpUtils; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.jms.Destination; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @Service public class PlatformServiceImpl implements PlatformService { @Autowired public ITbDataSendRecordsService tbDataSendRecordsService; @Autowired private ActiveManager activeManager; /** * 将文件夹下的文件校验,发送 * * @param rule 校验规则对象 * @param list 接收文件列表 */ public void checkFileName(TbDataRules rule, List list){ try { Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String path=rule.getReceiveUrl(); FtpUtils ftpUtil=new FtpUtils(); String ftpPath= RuoYiConfig.getFtpPath()+"/"+path; //接收文件路径 String sendPath= RuoYiConfig.getFtpPath()+"/send/"+path; //待发送路径 String bakPath=RuoYiConfig.getFtpPath()+"/bak/"+path; //备份路径 TbDataSendRecords records=new TbDataSendRecords(); //接收文件记录 if(StringUtils.isNotEmpty(rule.getCoalMineCode())){ records.setCoalMineId(Long.parseLong(rule.getCoalMineCode())); } UUID uuid=UUID.randomUUID(); records.setId(uuid.toString()); records.setSystemId(rule.getSystemId()); records.setRuleId(rule.getId()); records.setDetectionTime(new Date()); String dataHeader=rule.getCheckHead()+"0;"+sdf.format(date); records.setDataHeader(dataHeader); if(list==null||list.size()==0){ //生成校验记录未接收到文件,已经生成空白文件 String fileName= FileUtils.createFtpFile(path,rule.getCheckTitle(),rule.getCheckHead()); records.setFileName(fileName); records.setDataNumber(0l); records.setSendFileUrl(sendPath); records.setDetectionResult("0"); records.setCheckResult("1"); fileName=fileName.substring(0,fileName.length()-4)+".tmp"; String fileRoot=sendPath+"/"+fileName; boolean storeFlag=ftpUtil.uploadFile(path,"",fileName,fileRoot); if(storeFlag){ records.setSendResult("0"); }else{ records.setSendResult("1"); } tbDataSendRecordsService.recordSaveMongo(records); Boolean flag= FileUtils.deleteFile(fileRoot); System.out.println("是否成功删除=====================================================================》"+flag); }else{ boolean noHave = true; for(File file:list){ if(file.getName().contains(rule.getCheckTitle())){ //生成校验记录已接收到文件,文件标题校验通过 records.setFileName(file.getName()); records.setSendFileUrl(sendPath); records.setDetectionResult("0"); records.setCheckResult("0"); tbDataSendRecordsService.recordSaveMongo(records); //文件名符合规范 //文件发送到send文件夹下 FileUtils.copyFile(new File(ftpPath+"/"+file.getName()),new File(sendPath+"/"+file.getName())); //文件发送bak文件夹下 FileUtils.copyFile(new File(ftpPath+"/"+file.getName()),new File(bakPath+"/"+file.getName())); //删除201目录下的文件 file.delete(); //校验与加密队列 Destination destination = new ActiveMQQueue("encryption");//P2P模式单个消费者 rule.setSendPath(sendPath+"/"+file.getName()); activeManager.send(destination,JSON.toJSONString(rule)); noHave = false; } } if (noHave){ String fileName=FileUtils.createFtpFile(path,rule.getCheckTitle(),rule.getCheckHead()); records.setFileName(fileName); records.setDataNumber(0l); records.setSendFileUrl(sendPath); records.setDetectionResult("0"); records.setCheckResult("1"); fileName=fileName.substring(0,fileName.length()-4)+".tmp"; String fileRoot=sendPath+"/"+fileName; boolean storeFlag=ftpUtil.uploadFile(path,"",fileName,fileRoot); if(storeFlag){ records.setSendResult("0"); }else{ records.setSendResult("1"); } tbDataSendRecordsService.recordSaveMongo(records); } } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 将发送失败的文件重新发送 * */ public void waitFileSend(String path){ try { String filePath=RuoYiConfig.getFtpPath()+"/wait"+"/"+path; //接收文件路径 FtpUtils ftpUtils=new FtpUtils(); Listlist= FileUtils.getAllFile(filePath); if(list!=null&&list.size()>0){ for(File file:list){ Boolean flag=ftpUtils.uploadFile(path,"",file.getName(),file.getPath()); if(flag){ Files.delete(Paths.get(file.getPath())); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void fileEncrypt(String path,String tmpPath) throws Exception { FileOutputStream fot=null; OutputStreamWriter ops=null; File tmpFile=new File(tmpPath); tmpFile.createNewFile(); try { //文件加密 Listlist=FileUtils.readFile(path); byte []fileByte=new byte[list.size()]; fot=new FileOutputStream(tmpFile); ops=new OutputStreamWriter(fot); for(int i=0;ilist=FileUtils.readFile(path); byte []fileByte=new byte[list.size()]; fot=new FileOutputStream(file); ops=new OutputStreamWriter(fot); for(int i=0;ilist=FileUtils.readFile(path); byte []fileByte=new byte[list.size()]; fot=new FileOutputStream(file); ops=new OutputStreamWriter(fot); for(int i=0;i