| | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.mp.base.BizServiceImpl; |
| | |
| | | import java.nio.file.StandardOpenOption; |
| | | import java.time.Duration; |
| | | import java.util.*; |
| | | import java.util.regex.Pattern; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | | |
| | |
| | | private final FlowCommonService flowCommonService; |
| | | private final DncBackFileService dncBackFileService; |
| | | private final ProgramFlowStatusQueryService programFlowStatusQueryService; |
| | | |
| | | /** |
| | | * 偏离单文件末尾的模式:P+数字 |
| | | */ |
| | | private static final String P_NUMBER_PATTERN = "(?i)P\\d+"; |
| | | |
| | | private String getFileKey(){ |
| | | return "dncimpfile-"+ AuthUtil.getUserId(); |
| | | } |
| | |
| | | //根据内部文件,读取和分析程序包和程序文件数据 |
| | | List<String> dirList = entryNameList.stream().filter(s -> s.endsWith("/")).toList(); |
| | | for(String dir : dirList){ |
| | | String programPackageName1 = StringUtils.removeEnd(dir,"/"); |
| | | //String programPackageName1 = StringUtils.removeEnd(dir,"/"); |
| | | String folderName = StringUtils.removeEnd(dir,"/"); |
| | | |
| | | PackageAndProcessEdition pkgAndEdition = parseProgramPackageFromFolderName(folderName); |
| | |
| | | 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){ |
| | |
| | | if(!entryName.endsWith("/")){ |
| | | //实际的文件 |
| | | String fileName = StringUtils.removeStart(entryName,dir);//去除文件名路径部分 |
| | | |
| | | fileName = removeDeviationPart(fileName); |
| | | try { |
| | | FlowProgramFile newFlowFile = new FlowProgramFile(); |
| | | newFlowFile.setProgramName(packageNode.getName()); |
| | |
| | | |
| | | 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 文件的输入流 |