| | |
| | | import jakarta.servlet.ServletOutputStream; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springblade.core.mp.base.BizServiceImpl; |
| | | import org.springblade.core.oss.OssTemplate; |
| | | import org.springblade.core.tool.utils.DateUtil; |
| | | import org.springblade.mdm.basesetting.machine.entity.Machine; |
| | | import org.springblade.mdm.basesetting.machine.service.MachineService; |
| | | import org.springblade.mdm.machinefile.entity.MachineAcceptedFile; |
| | | import org.springblade.mdm.machinefile.entity.MachineFile; |
| | | import org.springblade.mdm.program.entity.NcProgramExchange; |
| | | import org.springblade.mdm.program.mapper.NcProgramExchangeMapper; |
| | | import org.springblade.mdm.program.service.NcNodeService; |
| | | import org.springblade.mdm.program.service.NcProgramService; |
| | | import org.springblade.mdm.program.service.ProgramAnnotationService; |
| | | import org.springblade.mdm.program.service.programannotation.*; |
| | | import org.springblade.mdm.utils.FileContentUtil; |
| | | import org.springblade.mdm.utils.ProgramFileNameParser; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.*; |
| | |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class NcProgramExportInnerService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> { |
| | | private final MachineAcceptedFileService machineBackFileService; |
| | | private final NcProgramService progService; |
| | | private final MachineFileService machineFileService; |
| | | private final NcNodeService ncNodeService; |
| | | private final MachineAcceptedFileService machineAcceptedService; |
| | | private final OssTemplate ossTemplate; |
| | | |
| | | public static final String PROGRAM_JSON_FILE = "exp_mdm_nc_program.json"; |
| | | public static final String NODE_JSON_FILE = "exp_mdm_nc_node.json"; |
| | | public static final String APPROVE_RECORD_JSON_FILE = "exp_mdm_approve_record.json"; |
| | | |
| | | |
| | | private final MachineFileService machineFileService; |
| | | private final MachineService machineService; |
| | | private final MachineAnnotationConfig annoConfig; |
| | | private final ProgramAnnotationService programAnnotationService; |
| | | /** |
| | | * 导出到涉密网 |
| | | * @param machineFileIds |
| | | * @param acceptedFileIds |
| | | * @param os |
| | | * @throws IOException |
| | | */ |
| | | public void exportToInner(List<Long> machineFileIds, ServletOutputStream os) throws IOException { |
| | | //FileOutputStream fos = new FileOutputStream("d:/exportDnc.zip"); |
| | | @Transactional |
| | | public void exportToInner(List<Long> acceptedFileIds, ServletOutputStream os) throws IOException { |
| | | |
| | | try (ZipOutputStream zipOut = new ZipOutputStream(os);) {//os |
| | | |
| | | List<MachineFile> machineFiles = machineFileService.lambdaQuery().in(MachineFile::getId,machineFileIds).list(); |
| | | Map<String,List<MachineFile>> map = machineFiles.stream() |
| | | List<MachineAcceptedFile> acceptedFiles = machineAcceptedService.lambdaQuery().in(MachineAcceptedFile::getId,acceptedFileIds).list(); |
| | | Map<String,List<MachineAcceptedFile>> map = acceptedFiles.stream() |
| | | .collect(Collectors.groupingBy(s -> ProgramFileNameParser.parseProgramName(s.getName()).logicProgramName())); |
| | | |
| | | for (Map.Entry<String, List<MachineFile>> entry : map.entrySet()) { |
| | | for (Map.Entry<String, List<MachineAcceptedFile>> entry : map.entrySet()) { |
| | | String programName = entry.getKey(); |
| | | List<MachineFile> progMachineFiles = entry.getValue(); |
| | | for (MachineFile mf : progMachineFiles) { |
| | | try(InputStream ins = machineFileService.getInputStream(mf)) { |
| | | addInputStreamToZip(zipOut,ins , programName + "/" + mf.getName()); |
| | | List<MachineAcceptedFile> acceptedFileList = entry.getValue(); |
| | | for (MachineAcceptedFile acceptedFile : acceptedFileList) { |
| | | try(InputStream ins = ossTemplate.statFileStream(acceptedFile.getOssName()) ) { |
| | | MachineFile machineFile = machineFileService.getById(acceptedFile.getMachineFileId()); |
| | | addInputStreamToZip(zipOut,ins , programName + "/" + acceptedFile.getName(),acceptedFile.getName(),machineFile.getMachineCode()); |
| | | } |
| | | acceptedFile.setExportTime(DateUtil.now()); |
| | | } |
| | | |
| | | } |
| | | |
| | | machineAcceptedService.updateBatchById(acceptedFiles); |
| | | } |
| | | |
| | | os.close(); |
| | | } |
| | | |
| | | |
| | | public void addInputStreamToZip(ZipOutputStream zipOut, InputStream inputStream, String entryName) |
| | | public void addInputStreamToZip(ZipOutputStream zipOut, InputStream inputStream, String entryName,String filename,String machineCode) |
| | | throws IOException { |
| | | // 创建新的 ZIP 条目 |
| | | ZipEntry zipEntry = new ZipEntry(entryName); |
| | | zipOut.putNextEntry(zipEntry); |
| | | |
| | | InputStream insAdded = setAnnotations(inputStream,filename,machineCode); |
| | | // 将输入流写入 ZIP 输出流 |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | while ((length = inputStream.read(buffer)) >= 0) { |
| | | while ((length = insAdded.read(buffer)) >= 0) { |
| | | zipOut.write(buffer, 0, length); |
| | | } |
| | | |
| | | // 关闭当前条目 |
| | | zipOut.closeEntry(); |
| | | } |
| | | |
| | | /** |
| | | * 给导出文件增加注释,涉密网要用 |
| | | * @param inputStream |
| | | * @param filename |
| | | * @param machineCode |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | InputStream setAnnotations(InputStream inputStream,String filename,String machineCode) throws IOException { |
| | | Machine machine = this.machineService.getByCode(machineCode); |
| | | AnnotationProcessor annoProcessor = ProcessorHelper.getProcessor(machine.getControlSystem(),annoConfig); |
| | | |
| | | AnnotationData annoData = new AnnotationData(); |
| | | annoData.setFilename(filename); |
| | | annoData.setSendPath(machine.getProgSendDir()); |
| | | |
| | | AnnotationProperties annoProps = annoProcessor.getAnnotationProperties(); |
| | | |
| | | ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.toByteArray(inputStream)); |
| | | String statusLine = FileContentUtil.readLineAt(bais,annoProps.getStatusLineIndex()); |
| | | String text = programAnnotationService.removeAnnotation(machine.getControlSystem(),statusLine); |
| | | |
| | | if(AnnotationUtil.isStatusContent(text)){ |
| | | //是3种状态之一 |
| | | annoData.setProgramStatus(text); |
| | | }else{ |
| | | //没有按试切处理 |
| | | annoData.setProgramStatus(AnnotationUtil.SQ); |
| | | } |
| | | bais.reset(); |
| | | // |
| | | return annoProcessor.putAnnotation(annoData,bais); |
| | | } |
| | | } |
| | | |