| | |
| | | |
| | | package org.springblade.mdm.program.service; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import io.netty.util.internal.StringUtil; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | 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.oss.OssTemplate; |
| | | import org.springblade.core.oss.model.BladeFile; |
| | |
| | | import org.springblade.core.tool.utils.DateUtil; |
| | | import org.springblade.core.tool.utils.FileUtil; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.mdm.basesetting.machine.entity.Machine; |
| | | import org.springblade.mdm.basesetting.machine.service.MachineService; |
| | | import org.springblade.mdm.basesetting.producedivision.entity.QinzheFgb; |
| | | import org.springblade.mdm.basesetting.producedivision.service.QinzheFgbService; |
| | | import org.springblade.mdm.commons.contants.RegExpConstants; |
| | | import org.springblade.mdm.commons.service.ParamService; |
| | | import org.springblade.mdm.flow.entity.FlowProgramFile; |
| | | import org.springblade.mdm.flow.service.CureFlowService; |
| | | import org.springblade.mdm.flow.service.FlowCommonService; |
| | | import org.springblade.mdm.flow.service.FlowProgramFileService; |
| | | import org.springblade.mdm.program.entity.DncBackFile; |
| | | import org.springblade.mdm.program.entity.NcNode; |
| | | import org.springblade.mdm.program.entity.NcProgramExchange; |
| | | import org.springblade.mdm.program.mapper.NcProgramExchangeMapper; |
| | | import org.springblade.mdm.program.service.programannotation.*; |
| | | import org.springblade.mdm.program.vo.DncSendBackData; |
| | | import org.springblade.mdm.program.vo.DncSendBackFile; |
| | | import org.springblade.mdm.utils.EntityUtil; |
| | | import org.springblade.mdm.program.vo.ProgramAnnotation; |
| | | import org.springblade.mdm.utils.FileContentUtil; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springblade.mdm.utils.ProgramFileNameParser; |
| | | import org.springblade.mdm.utils.ZipTextFileContentUtil; |
| | | import org.springblade.system.pojo.entity.DictBiz; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | |
| | | import java.nio.file.StandardOpenOption; |
| | | import java.time.Duration; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | | |
| | |
| | | @AllArgsConstructor |
| | | public class DNCSendBackService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> { |
| | | private final CureFlowService cureFlowService; |
| | | private final FlowProgramFileService flowProgramFileService; |
| | | private final QinzheFgbService qinzheFbgService; |
| | | private final NcNodeService ncNodeService; |
| | | private final OssTemplate ossTemplate; |
| | | private final BladeRedis bladeRedis; |
| | | private final FlowCommonService flowCommonService; |
| | | private final DncBackFileService dncBackFileService; |
| | | private final ProgramFlowStatusQueryService programFlowStatusQueryService; |
| | | private final MachineService machineService; |
| | | private final ParamService paramService; |
| | | |
| | | /** |
| | | * 偏离单文件末尾的模式:P+数字 |
| | | */ |
| | | private static final String P_NUMBER_PATTERN = "(?i)P\\d+"; |
| | | |
| | | private String getFileKey(){ |
| | | return "dncimpfile-"+ AuthUtil.getUserId(); |
| | | } |
| | |
| | | bladeRedis.setEx(getFileKey(),bfile.getName(), Duration.ofHours(2)); |
| | | |
| | | try(InputStream zipFileInputStream = ossTemplate.statFileStream(bfile.getName());) { |
| | | //InputStream zipFileInputStream = file.getInputStream();//test |
| | | list = parseProgramListFromZip(zipFileInputStream); |
| | | } |
| | | } catch (IOException e) { |
| | |
| | | Path tempZipFile = createTempFile(inputStream); |
| | | |
| | | List<String> fileEntryNameList = new ArrayList<>(); |
| | | List<String> dirEntryNameList = new ArrayList<>(); |
| | | List<String> dirEntryNameList = new ArrayList<>();//程序包名+工序版次 作为文件夹名 |
| | | try (ZipFile zipFile = new ZipFile(tempZipFile.toFile())) { |
| | | |
| | | Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); |
| | |
| | | for(String entryName : dirEntryNameList){ |
| | | DncSendBackData progData = new DncSendBackData(); |
| | | String packageName = StringUtils.removeEnd(entryName,"/"); |
| | | progData.setProgramName(packageName); |
| | | String statusLine = ""; |
| | | Optional<String> optFilename = fileEntryNameList.stream().filter(n -> n.startsWith(packageName)).findFirst(); |
| | | |
| | | Optional<String> optFilename = fileEntryNameList.stream().filter(n -> n.startsWith(entryName)).findFirst(); |
| | | if(optFilename.isPresent()){ |
| | | entry = zipFile.getEntry(optFilename.get()); |
| | | InputStream ins = zipFile.getInputStream(entry); |
| | | ByteArrayInputStream byteArrayIns = new ByteArrayInputStream(IOUtils.toByteArray(ins)); |
| | | |
| | | //解析机床 |
| | | progData.setFileBackTime(DateUtil.fromInstant(entry.getLastModifiedTime().toInstant())); |
| | | statusLine = FileContentUtil.readLineAt(ins,2); |
| | | if(statusLine.contains("SQ")){ |
| | | |
| | | AnnotationProperties defAnnoProperties =AnnotationProperties.getDefault(); |
| | | String statusLine = FileContentUtil.readLineAt(byteArrayIns,defAnnoProperties.getStatusLineIndex());//状态注释行 |
| | | byteArrayIns.reset(); |
| | | String sendPathLine = FileContentUtil.readLineAt(byteArrayIns,defAnnoProperties.getSendPathLineIndex());//状态注释行 |
| | | byteArrayIns.reset(); |
| | | |
| | | if(statusLine.contains(AnnotationUtil.GH)){ |
| | | //固化,不应回传,忽略 |
| | | log.warn("状态{},不应回传,忽略",statusLine); |
| | | continue; |
| | | } |
| | | |
| | | Machine machine = this.machineService.getMachineBySendPathAnnotation(sendPathLine); |
| | | if(machine == null){ |
| | | throw new ServiceException("根据下发路径未找到程序对应的机床:"+sendPathLine); |
| | | } |
| | | |
| | | progData.setProgramName(packageName); |
| | | |
| | | if(statusLine.contains(AnnotationUtil.SQ)){ |
| | | //试切 |
| | | programPackageNode = ncNodeService.getLastEditionTryingProgramPackage(packageName); |
| | | }else if(statusLine.contains("GH")){ |
| | | //固化 |
| | | programPackageNode = ncNodeService.getLastEditionCuredProgramPackage(packageName); |
| | | }else if(statusLine.contains("PL")){ |
| | | //偏离 |
| | | programPackageNode = ncNodeService.getLastEditionTryingProgramPackage(packageName);//TODO 还需根据机床组(如何获取?根据下发路径获取机床,进而获取),,processEdition |
| | | }else if(statusLine.contains(AnnotationUtil.LG)){ |
| | | //临时更改单 |
| | | programPackageNode =ncNodeService.getLastEditionDeviationProgramPackage(packageName); |
| | | }else{ |
| | | throw new ServiceException("未找到程序文件中的状态注释"); |
| | | throw new ServiceException("状态注释不在范围内:"+statusLine+",仅试切、临时更改单可以回传"); |
| | | } |
| | | |
| | | if(programPackageNode != null) { |
| | | progData.setId(programPackageNode.getId()); |
| | | progData.setProgramNo(programPackageNode.getProgramNo()); |
| | |
| | | progData.setFiles(programFiles); |
| | | list.add(progData); |
| | | }else{ |
| | | throw new ServiceException("找不到程序包名:"+packageName+statusLine); |
| | | throw new ServiceException("找不到程序包名:"+packageName); |
| | | } |
| | | }else{ |
| | | throw new ServiceException(entryName+"包下未找到文件"+statusLine); |
| | | throw new ServiceException(entryName+"包下未找到文件"); |
| | | } |
| | | |
| | | } |
| | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 修复程序包名,fanuc不识别下划线,下发时转换为了-,这里需要确认。应该是从导出记录中查找修改后的包名,但是现场编制的没有咋办? |
| | | * @param packageNameInZip |
| | | * @return |
| | | */ |
| | | /* |
| | | private String fixProgramPackageName(String packageNameInZip) { |
| | | Matcher matcher = RegExpConstants.PROGRAM_PACKAGE_PATTERN.matcher(packageNameInZip); |
| | | |
| | | String drawingNo = null; |
| | | if(matcher.find()) { |
| | | drawingNo = matcher.group(1); |
| | | } |
| | | String processNo = null; |
| | | if(matcher.find()) { |
| | | processNo = matcher.group(2); |
| | | } |
| | | |
| | | String processEdition = null; |
| | | if(matcher.find()) { |
| | | processEdition = matcher.group(3); |
| | | } |
| | | if(drawingNo != null && processNo != null && processEdition != null) { |
| | | if(drawingNo.contains("_")) { |
| | | |
| | | } |
| | | } |
| | | return packageNameInZip; |
| | | } |
| | | */ |
| | | |
| | | /** |
| | | * 入库回传文件,并启动固化流程 |
| | |
| | | String zipFileName = bladeRedis.get(filekey); |
| | | log.info("filekey={},文件名={}",filekey,zipFileName); |
| | | |
| | | //Map<Long,List<NcNode>> programPackageSubMap = new HashMap<>(); |
| | | |
| | | Map<Long,List<FlowProgramFile>> pkgIdFileMap = dealWithBackFile(zipFileName,acceptIdList); |
| | | |
| | | ///List<NcNode> newProgramPackageList = updateNodeDataByDNCBackData(pkgFileName,idList,programPackageSubMap); |
| | | //updateNodeDataByDNCBackData(pkgFileName,idList,programPackageSubMap); |
| | | /* |
| | | log.info("需要启动固化流程的程序包名数量:{}",newProgramPackageList.size()); |
| | | for(NcNode pkgNode :newProgramPackageList){ |
| | | exchange = new NcProgramExchange(); |
| | | exchange.setName(pkgNode.getName()); |
| | | exchange.setExchangeType(2);//回传 |
| | | exchange.setNcNodeId(pkgNode.getId()); |
| | | |
| | | this.save(exchange); |
| | | }*/ |
| | | //bladeRedis.del(filekey); |
| | | //this.ossTemplate.removeFile(zipFileName); |
| | | //log.info("删除oss文件:{}",zipFileName); |
| | | |
| | | //cureFlowService.startCure(newProgramPackageList,programPackageSubMap); |
| | | cureFlowService.startCureNew(pkgIdFileMap); |
| | | |
| | | } |
| | |
| | | List<String> dirList = entryNameList.stream().filter(s -> s.endsWith("/")).toList(); |
| | | for(String dir : dirList){ |
| | | String programPackageName = StringUtils.removeEnd(dir,"/"); |
| | | |
| | | Optional<NcNode> optPackageNode = allAcceptPackages.stream().filter(node -> StringUtils.equals(node.getName(),programPackageName)).findFirst(); |
| | | |
| | | if(optPackageNode.isEmpty()){ |
| | | throw new ServiceException("找不到程序"+programPackageName); |
| | | } |
| | | |
| | | NcNode packageNode = optPackageNode.get(); |
| | | if(packageNode.hasCured()) { |
| | | throw new ServiceException(programPackageName + "已经固化,请勿重复入库。"); |
| | | } |
| | | |
| | | //偏离程序判断是否重复回传 |
| | | if(packageNode.isDeviationProgram() && packageNode.hasLocked()) { |
| | | throw new ServiceException(programPackageName + "已锁定的程序不可以再次回传。"); |
| | | } |
| | | //检查是否在审批过程中 |
| | | //根据节点信息查询流程 |
| | | boolean active = flowCommonService.isProcessInstanceActive(packageNode.getProcessInstanceId()); |
| | | if(active){ |
| | | throw new ServiceException(programPackageName+"正在审批中,请勿等待审批完成。"); |
| | |
| | | if(!entryName.endsWith("/")){ |
| | | //实际的文件 |
| | | String fileName = StringUtils.removeStart(entryName,dir);//去除文件名路径部分 |
| | | |
| | | fileName = removeDeviationPart(fileName); |
| | | try { |
| | | FlowProgramFile newFlowFile = new FlowProgramFile(); |
| | | newFlowFile.setProgramName(packageNode.getName()); |
| | | newFlowFile.setProcessInstanceId(null);//先置为空,启动流程后设置该值 |
| | | newFlowFile.setFileType("program"); |
| | | newFlowFile.setFileType(FlowProgramFile.TYPE_PROGRAM); |
| | | newFlowFile.setName(fileName); |
| | | InputStream ins = zipFile.getInputStream(zipFile.getEntry(entryName)); |
| | | BladeFile newOssFile = ossTemplate.putFile("mdm",fileName,ins); |
| | |
| | | |
| | | pkgIdFileMap.put(packageNode.getId(),flowFiles); |
| | | } |
| | | |
| | | } |
| | | |
| | | return pkgIdFileMap; |
| | | } |
| | | |
| | | /** |
| | | * 更新节点,主要是创建 程序包名 的新版本。 |
| | | * @param pkgFileName zip文件名 |
| | | * @param programPackageIdList 程序包名 节点的id列表 |
| | | * @param programPackageSubMap 新的 程序包节点id -> =文件列表 map,用于回传数据 |
| | | * @throws IOException 访问文件异常 |
| | | */ |
| | | /* |
| | | List<NcNode> updateNodeDataByDNCBackData(String pkgFileName, List<Long> programPackageIdList,Map<Long,List<NcNode>> programPackageSubMap) throws IOException { |
| | | InputStream inputStream = this.ossTemplate.statFileStream(pkgFileName); |
| | | Path tempZipFile = createTempFile(inputStream); |
| | | List<NcNode> newProgramPackageNodeList = new ArrayList<>(); |
| | | List<String> entryNameList = new ArrayList<>(); |
| | | |
| | | ZipEntry entry; |
| | | try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) { |
| | | Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
| | | while(entries.hasMoreElements()) { |
| | | entry = entries.nextElement(); |
| | | entryNameList.add(entry.getName()); |
| | | String removeDeviationPart(String filename){ |
| | | String finalFilename = filename; |
| | | //去掉文件名中可能带有的偏离单部分:-P[序号] |
| | | String ext = FilenameUtils.getExtension(filename); |
| | | String dotExt = StringUtils.isNotBlank(ext)?"."+ext:ext;//带点的扩展名 |
| | | String notExtName = StringUtils.removeEnd(filename,dotExt); |
| | | int idx = notExtName.lastIndexOf("-"); |
| | | if(idx != -1){ |
| | | String endPart = notExtName.substring(idx+1); |
| | | //Pattern.CASE_INSENSITIVE |
| | | boolean containsPld = endPart.matches(P_NUMBER_PATTERN); |
| | | if(containsPld){ |
| | | finalFilename = notExtName.substring(0, idx)+dotExt; |
| | | } |
| | | log.info("allentrynames:{}",entryNameList); |
| | | |
| | | List<NcNode> allAcceptPackages = this.ncNodeService.lambdaQuery().in(NcNode::getId,programPackageIdList).list(); |
| | | //根据内部文件,读取和分析程序包和程序文件数据 |
| | | List<String> dirList = entryNameList.stream().filter(s -> s.endsWith("/")).toList(); |
| | | for(String dir : dirList){ |
| | | String programPackageName = StringUtils.removeEnd(dir,"/"); |
| | | Optional<NcNode> optPackageNode = allAcceptPackages.stream().filter(node -> StringUtils.equals(node.getName(),programPackageName)).findFirst(); |
| | | |
| | | if(optPackageNode.isEmpty()){ |
| | | throw new ServiceException("找不到程序"+programPackageName); |
| | | } |
| | | |
| | | NcNode packageNode = optPackageNode.get(); |
| | | if(packageNode.hasCured()) { |
| | | throw new ServiceException(programPackageName + "已经固化,请勿重复入库。"); |
| | | } |
| | | |
| | | //检查是否在审批过程中 |
| | | //根据节点信息查询流程 |
| | | boolean active = flowCommonService.isProcessInstanceActive(packageNode.getProcessInstanceId()); |
| | | if(active){ |
| | | throw new ServiceException(programPackageName+"正在审批中,请勿重复入库。"); |
| | | } |
| | | |
| | | NcNode newProgramPkg = new NcNode(); |
| | | BeanUtils.copyProperties(packageNode, newProgramPkg); |
| | | EntityUtil.clearBaseProperties(newProgramPkg); |
| | | newProgramPkg.setIsLastEdition(1); |
| | | ncNodeService.save(newProgramPkg); |
| | | newProgramPackageNodeList.add(newProgramPkg); |
| | | |
| | | //旧数据更新为老版本 |
| | | packageNode.setIsLocked(1);//旧版自动锁定 |
| | | packageNode.setIsLastEdition(0);; |
| | | ncNodeService.updateById(packageNode); |
| | | |
| | | //List<FlowProgramFile> newFlowFiles = new ArrayList<>(); |
| | | List<NcNode> newProgramNodes = new ArrayList<>(); |
| | | //查找包下的文件数据, |
| | | entryNameList.stream().filter(s -> s.startsWith(dir)).forEach(entryName -> { |
| | | log.info("{}下的文件:{}",dir,entryName); |
| | | if(!entryName.endsWith("/")){ |
| | | //实际的文件 |
| | | String fileName = StringUtils.removeStart(entryName,dir);//去除文件名路径部分 |
| | | NcNode oldProgramNode = this.ncNodeService.getLastEditionProgramFile(fileName,packageNode.getId()); |
| | | if(oldProgramNode == null){ |
| | | log.info("{}找不到程序文件",entryName); |
| | | throw new ServiceException(programPackageName+"下找不到程序文件"+fileName); |
| | | } |
| | | |
| | | //创建新版本的程序节点 |
| | | NcNode newProgramNode = new NcNode(); |
| | | BeanUtils.copyProperties(oldProgramNode, newProgramNode); |
| | | EntityUtil.clearBaseProperties(newProgramNode); |
| | | newProgramNode.setIsLastEdition(1); |
| | | newProgramNode.setParentId(newProgramPkg.getId()); |
| | | newProgramNode.setParentIds(newProgramPkg.getParentIds()+","+newProgramPkg.getId()); |
| | | |
| | | FlowProgramFile oldFlowFile = flowProgramFileService.getById(newProgramNode.getFlowProgramFileId()); |
| | | |
| | | FlowProgramFile newFlowFile = new FlowProgramFile(); |
| | | BeanUtils.copyProperties(oldFlowFile, newFlowFile); |
| | | newFlowFile.setProcessInstanceId(null);//先置为空,启动流程后设置该值 |
| | | EntityUtil.clearBaseProperties(newFlowFile); |
| | | |
| | | try { |
| | | InputStream ins = zipFile.getInputStream(zipFile.getEntry(entryName)); |
| | | BladeFile newOssFile = ossTemplate.putFile("mdm",fileName,ins); |
| | | newFlowFile.setOssName(newOssFile.getName()); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | newProgramNode.setFlowProgramFile(newFlowFile); |
| | | newProgramNode.setVersionNumber(oldProgramNode.genNewVersionNumber()); |
| | | newProgramNodes.add(newProgramNode); |
| | | //旧节点处理,咋办?如果导出工控网 重复导出呢?,isLastEdition不用设置了,因为 程序包节点 是新的 |
| | | } |
| | | }); |
| | | |
| | | programPackageSubMap.put(newProgramPkg.getId(),newProgramNodes); |
| | | } |
| | | |
| | | } |
| | | |
| | | return newProgramPackageNodeList; |
| | | return finalFilename; |
| | | } |
| | | */ |
| | | /** |
| | | * 创建一个临时zip文件 |
| | | * @param inputStream 文件的输入流 |
| | |
| | | public String getEntryFileContent(String entryName) throws IOException { |
| | | String result = ""; |
| | | String zipFileName = bladeRedis.get(getFileKey()); |
| | | try(InputStream inputStream = this.ossTemplate.statFileStream(zipFileName);){ |
| | | Path tempZipFile = createTempFile(inputStream); |
| | | return ZipTextFileContentUtil.getTextContent(this.ossTemplate.statFileStream(zipFileName),entryName); |
| | | |
| | | ZipEntry entry; |
| | | try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) { |
| | | Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
| | | while (entries.hasMoreElements()) { |
| | | entry = entries.nextElement(); |
| | | if (!entryName.equals(entry.getName())) { |
| | | continue; |
| | | } |
| | | try (InputStream fileIns = zipFile.getInputStream(zipFile.getEntry(entryName))) { |
| | | ByteArrayInputStream bos = new ByteArrayInputStream(fileIns.readAllBytes()); |
| | | boolean isText = StringUtils.endsWithIgnoreCase(entryName,".txt") || StringUtils.endsWithIgnoreCase(entryName,".nc")|| StringUtils.endsWithIgnoreCase(entryName,".xml"); |
| | | if(!isText) { |
| | | isText = FileContentUtil.isTextFile(bos); |
| | | } |
| | | if (isText) { |
| | | bos.reset(); |
| | | result = FileContentUtil.getContentFromStream(bos); |
| | | } else { |
| | | result = "<非文本文件>"; |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | } |
| | | |
| | | @Data |
| | | class PackageAndProcessEdition{ |
| | | private String programPackageName; |
| | | private String processEdition; |
| | | |
| | | public String programName(){ |
| | | return programPackageName+"-"+processEdition; |
| | | } |
| | | } |
| | | |