| | |
| | | |
| | | package org.springblade.mdm.program.service; |
| | | |
| | | import com.alibaba.csp.sentinel.util.StringUtil; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
| | | import org.apache.commons.compress.archivers.zip.ZipFile; |
| | | import org.apache.commons.compress.utils.SeekableInMemoryByteChannel; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.mp.base.BizEntity; |
| | | import org.springblade.core.mp.base.BizServiceImpl; |
| | | import org.springblade.core.mp.support.Condition; |
| | | import org.springblade.core.mp.support.Query; |
| | | import org.springblade.core.tool.utils.FileUtil; |
| | | import org.springblade.core.oss.OssTemplate; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.mdm.basesetting.machine.MachineService; |
| | | import org.springblade.mdm.basesetting.machine.entity.Machine; |
| | | import org.springblade.mdm.flow.entity.ApproveRecord; |
| | | import org.springblade.mdm.flow.entity.FlowProgramFile; |
| | | import org.springblade.mdm.flow.service.ApproveRecordService; |
| | | import org.springblade.mdm.flow.service.FlowProgramFileService; |
| | | import org.springblade.mdm.program.entity.NcNode; |
| | | import org.springblade.mdm.program.entity.NcProgramApproved; |
| | | import org.springblade.mdm.program.entity.NcProgramExchange; |
| | | import org.springblade.mdm.program.mapper.NcProgramExchangeMapper; |
| | | import org.springblade.mdm.program.vo.DncSendBackData; |
| | | import org.springblade.mdm.utils.FileContentUtil; |
| | | import org.springblade.system.feign.IDictClient; |
| | | import org.springblade.system.pojo.entity.Dict; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.Enumeration; |
| | | import java.util.List; |
| | | import java.io.*; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.*; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | | * 下发/回传程序统计 |
| | |
| | | @AllArgsConstructor |
| | | public class NcProgramExportDNCService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> { |
| | | |
| | | //private final NcProgramService progService; |
| | | private final IDictClient dictClient; |
| | | private final MachineService machineService; |
| | | private final NcProgramApprovedService approvedService; |
| | | private final ApproveRecordService approveRecordService; |
| | | private final NcNodeService ncNodeService; |
| | | private final OssTemplate ossTemplate; |
| | | private final FlowProgramFileService flowProgramFileService; |
| | | private final ProgramAnnotationService programAnnotationService; |
| | | //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"; |
| | | |
| | | public static final String ANNOTATION_DICT = "machine_annotation"; |
| | | |
| | | /** |
| | | * 分页查询 |
| | |
| | | * @return |
| | | */ |
| | | public IPage<DncSendBackData> dncSendBackPageQuery(Query query) { |
| | | return this.getBaseMapper().dncSendBackpageQuery(Condition.getPage(query),query); |
| | | } |
| | | |
| | | IPage<DncSendBackData> page = this.getBaseMapper().dncSendBackpageQuery(Condition.getPage(query),query); |
| | | /** |
| | | * 导出dnc压缩包 |
| | | * @param approvedIdArray 待导出审批表id数组 |
| | | */ |
| | | public void exportDnc(Long[] approvedIdArray, OutputStream os) throws IOException { |
| | | //FileOutputStream fos = new FileOutputStream("d:/exportDnc.zip"); |
| | | try (ZipOutputStream zipOut = new ZipOutputStream(os);os) {//os |
| | | |
| | | return page; |
| | | for (Long approvedId : approvedIdArray) { |
| | | NcProgramApproved approved = approvedService.getById(approvedId); |
| | | addProgramPackageToZip(zipOut,approved); |
| | | |
| | | approved.setStatus(NcProgramApproved.STATUS_EXPORTED); |
| | | approvedService.updateById(approved); |
| | | } |
| | | |
| | | }catch(Exception e){ |
| | | Throwable [] err = e.getSuppressed(); |
| | | throw new ServiceException("导出工控网错误"+e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将程序包和下属文件加入压缩包 |
| | | * @param zipOut |
| | | * @param approved |
| | | */ |
| | | private void addProgramPackageToZip(ZipOutputStream zipOut, NcProgramApproved approved) throws IOException{ |
| | | String packageFolder = approved.getProgramName()+"/"; |
| | | ZipEntry zipEntry = new ZipEntry(packageFolder);// "/"结尾表示文件夹 |
| | | zipOut.putNextEntry(zipEntry); |
| | | zipOut.closeEntry(); |
| | | |
| | | NcNode packageNode = ncNodeService.getById(approved.getNcNodeId()); |
| | | Machine machine = machineService.getByCode(packageNode.getMachineCode());//程序包节点,获取注释用 |
| | | |
| | | List<NcNode> programNodes = ncNodeService.lambdaQuery().eq(NcNode::getIsLastEdition,1).eq(NcNode::getParentId, approved.getNcNodeId()).list(); |
| | | |
| | | FlowProgramFile programFile; |
| | | NcNode programPackageNode = this.ncNodeService.getById(approved.getNcNodeId()); |
| | | String status = "SQ"; |
| | | if(programPackageNode.isDeviationProgram()){ |
| | | status = "PL"; |
| | | }else if(programPackageNode.hasCured()){ |
| | | status = "GH"; |
| | | } |
| | | |
| | | for (NcNode node : programNodes) { |
| | | String filePathInZip = packageFolder + node.getName(); |
| | | programFile = this.flowProgramFileService.getById(node.getFlowProgramFileId()); |
| | | if(programFile.isProgram()) {//程序文件,才会加入压缩包 |
| | | InputStream inputStream = ossTemplate.statFileStream(programFile.getOssName()); |
| | | //InputStream addedIns1 = addSendDirAnnotation(inputStream, machine, annotationList); |
| | | //InputStream addedIns2 = addProgramStatusAnnotation(addedIns1, status, machine, annotationList); |
| | | String sendDir = machine.getProgSendDir()==null? StringUtil.EMPTY:machine.getProgSendDir(); |
| | | InputStream addedIns2 = programAnnotationService.setSendDirAndStatusAnnotation(sendDir,status,inputStream,machine.getMachineGroupCode()); |
| | | this.addInputStreamToZip(zipOut, addedIns2, filePathInZip); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 加入机床下发路径和程序状态的注释 |
| | | * @param inputStream |
| | | * @return |
| | | */ |
| | | /* |
| | | InputStream addSendDirAnnotation(InputStream inputStream,Machine machine,List<Dict> annotationList) throws IOException { |
| | | //检测第二行是否是路径 |
| | | String annotationText = programAnnotationService.generateAnnotation(machine.getProgSendDir()==null? StringUtil.EMPTY:machine.getProgSendDir(),machine.getMachineGroupCode(),annotationList); |
| | | |
| | | //TODO 改判断行内容 |
| | | return FileContentUtil.insertLine(inputStream,1,annotationText); |
| | | }*/ |
| | | |
| | | /** |
| | | * 添加程序专改的注释 |
| | | * @param inputStream |
| | | * @param status 程序状态 |
| | | * @param machine |
| | | * @param annotationList 配置的注释字典列表 |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | /* |
| | | InputStream addProgramStatusAnnotation(InputStream inputStream,String status,Machine machine,List<Dict> annotationList) throws IOException { |
| | | String annoText= programAnnotationService.generateAnnotation(status,machine.getMachineGroupCode(),annotationList); |
| | | |
| | | return FileContentUtil.insertLine(inputStream,2,annoText); |
| | | }*/ |
| | | |
| | | /** |
| | | * 判断一行文本是否是注释 |
| | | * @param line |
| | | * @param annotationList |
| | | * @return |
| | | */ |
| | | /* |
| | | boolean isAnnotation(String line,List<Dict> annotationList){ |
| | | boolean isAnno = false; |
| | | |
| | | return isAnno; |
| | | }*/ |
| | | |
| | | |
| | | /* |
| | | String generateAnnotation(String oriTest,String machineGroupCode,List<Dict> annotationList) { |
| | | R<List<Dict>> dictsResult = dictClient.getList(ANNOTATION_DICT); |
| | | String annotation = oriTest; |
| | | String begin = "("; |
| | | String end = ")"; |
| | | |
| | | Optional<Dict> dictOpt = dictsResult.getData().stream().filter(dict -> {return dict.getDictKey().equals(machineGroupCode);}).findFirst(); |
| | | String annotationSetting = "(,)"; |
| | | if(dictOpt.isPresent()) { |
| | | annotationSetting = dictOpt.get().getDictValue(); |
| | | } |
| | | String[] arr = annotationSetting.split(","); |
| | | if(arr.length == 2){ |
| | | begin = arr[0]; |
| | | end = arr[1]; |
| | | } |
| | | |
| | | annotation = begin + oriTest + end; |
| | | return annotation; |
| | | |
| | | } |
| | | */ |
| | | |
| | | /** |
| | | * 将 输入流 中的内容写入zip |
| | | * @param zipOut zip输出流 |
| | | * @param inputStream 输入流 |
| | | * @param entryName 文件名 |
| | | * @throws IOException |
| | | */ |
| | | void addInputStreamToZip(ZipOutputStream zipOut, InputStream inputStream, String entryName) |
| | | throws IOException { |
| | | // 创建新的 ZIP 条目 |
| | | ZipEntry zipEntry = new ZipEntry(entryName); |
| | | zipOut.putNextEntry(zipEntry); |
| | | |
| | | // 将输入流写入 ZIP 输出流 |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | while ((length = inputStream.read(buffer)) >= 0) { |
| | | zipOut.write(buffer, 0, length); |
| | | } |
| | | |
| | | // 关闭当前条目 |
| | | zipOut.closeEntry(); |
| | | } |
| | | } |
| | | |