From 6dbe90994c4b12935d09a2adaa1a95b9ded96dc7 Mon Sep 17 00:00:00 2001 From: OTTO <731554297@qq.com> Date: Tue, 20 Aug 2024 09:41:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=88=B6=E5=93=81=E5=BA=93=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=BC=80=E5=8F=91):=20=E4=BA=A7=E5=93=81=E5=BA=93?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=93=E5=8C=85=E4=B8=8B=E8=BD=BD=E4=BA=A7?= =?UTF-8?q?=E5=93=81=E5=BA=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 异步处理文件打包逻辑: 1. 打包期间锁定产品库,防止产品库文件发生变化导致最终结果不一致 2. 由于产品库实际为Raw类型,且每个产品库下的制品都会在数据库存储一份,所以通过数据库获取该产品库下所有制品文件 3. 检查制品文件是否存在对应的文件标识,若不存在则从Nexus下载并上传至文件微服务并获取文件标识 4. 构建制品在产品库中的相对路径以及制品文件标识的Map对象,调用文件微服务打包文件接口对产品库进行打包 --- .../PmsProductLibraryAsyncServiceImpl.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryAsyncServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryAsyncServiceImpl.java index d4237e301..f5fb6d434 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryAsyncServiceImpl.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryAsyncServiceImpl.java @@ -1,6 +1,9 @@ package com.microservices.pms.productLibrary.service.impl; +import com.microservices.common.core.constant.SecurityConstants; +import com.microservices.common.core.exception.ServiceException; import com.microservices.common.core.utils.StringUtils; +import com.microservices.common.core.utils.file.FileUtils; import com.microservices.common.core.utils.html.EscapeUtil; import com.microservices.pms.productLibrary.domain.PmsProductLibraryComponent; import com.microservices.pms.productLibrary.domain.PmsProductLibraryRepositories; @@ -13,7 +16,10 @@ import com.microservices.pms.utils.CustomExecutorFactory; import com.microservices.pms.utils.NexusRequestHelper; import com.microservices.pms.utils.NexusRequestUrl; import com.microservices.pms.utils.PythonUtil; +import com.microservices.system.api.RemoteFileService; +import com.microservices.system.api.domain.SysFile; import com.microservices.system.api.domain.SysFileBytes; +import com.microservices.system.api.utils.FeignUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -23,6 +29,7 @@ import org.springframework.stereotype.Service; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; @@ -41,6 +48,8 @@ public class PmsProductLibraryAsyncServiceImpl implements IPmsProductLibraryAsyn @Autowired @Lazy private IPmsProductLibraryRepositoriesService pmsProductLibraryRepositoriesService; + @Autowired + private RemoteFileService remoteFileService; @Override public void syncNexusMavenRepoComponent(PmsProductLibraryRepositories repo, List pmsProductLibraryComponents) { @@ -192,7 +201,44 @@ public class PmsProductLibraryAsyncServiceImpl implements IPmsProductLibraryAsyn CustomExecutorFactory.threadPoolExecutor.execute(() -> { pmsProductLibraryRepositoriesService.lockRepository(productRepo.getPmsEnterpriseIdentifier(), productRepo.getId()); try { + List componentList = + pmsProductLibraryComponentService.selectPmsProductLibraryComponentListByDatabase(productRepo.getId()); + HashMap packagedStructure = new HashMap<>(); + for (PmsProductLibraryComponent component : componentList) { + String fileIdentifier = component.getFileIdentifier(); + if (StringUtils.isEmpty(fileIdentifier)) { + String nexusFileUrl = EscapeUtil.removeExtraSlashOfUrl(productRepo.getUrl() + "/" + component.getPath()); + SysFileBytes fileBytes = nexusRequestHelper.getFileBytes(nexusFileUrl); + if (fileBytes == null) { + throw new ServiceException("制品文件不存在(制品路径[%s])", component.getPath()); + } + try { + SysFile assetFile = + FeignUtils.getReturnData( + remoteFileService.upload( + FileUtils.createMultipartFile(fileBytes.getFileInputStream(), fileBytes.getFileName()), + "pms", + productRepo.getPmsEnterpriseIdentifier() + "/productLibrary", + SecurityConstants.INNER)); + if (assetFile == null) { + throw new ServiceException("制品文件不存在(制品路径[%s])", component.getPath()); + } + fileIdentifier = assetFile.getFileIdentifier(); + } catch (IOException e) { + throw new ServiceException("制品文件上传至文件微服务失败:%s", e.getMessage()); + } + + } + packagedStructure.put(component.getGroup(), fileIdentifier); + } + String fileIdentifier = FeignUtils.getReturnData( + remoteFileService.packagedFile( + packagedStructure, + "pms", + productRepo.getPmsEnterpriseIdentifier() + "/productLibrary", + SecurityConstants.INNER) + ); } finally { pmsProductLibraryRepositoriesService.unlockRepository(productRepo.getPmsEnterpriseIdentifier(), productRepo.getId()); }