From 9b2a32367466c73fc080736c8f1ed1fb30192f1b Mon Sep 17 00:00:00 2001 From: OTTO <731554297@qq.com> Date: Mon, 26 Aug 2024 17:18:00 +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=AE=8C=E5=96=84):=20=E4=BA=A7=E5=93=81=E5=BA=93?= =?UTF-8?q?=E4=B8=8D=E6=8F=90=E4=BE=9B=E7=9B=B4=E6=8E=A5=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E4=BA=A7=E5=93=81=E6=93=8D=E4=BD=9C=EF=BC=8C=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E4=BB=8E=E7=9F=A5=E8=AF=86=E5=BA=93=E4=B8=AD?= =?UTF-8?q?=E9=80=89=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 产品库上传文档逻辑优化:支持选择文档类型的文档 1. 创建临时文件,文件名为文档名称并添加后缀.md,并将文档内容输入到文件中 2. 获取文件流作为产品库制品上传 3. 删除临时文件 --- .../impl/PmsProductLibraryServiceImpl.java | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryServiceImpl.java index b5a33a5a4..9522ecec4 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryServiceImpl.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/productLibrary/service/impl/PmsProductLibraryServiceImpl.java @@ -28,6 +28,7 @@ import com.microservices.pms.utils.NexusRequestHelper; import com.microservices.pms.utils.NexusRequestUrl; import com.microservices.pms.utils.PmsUtils; import com.microservices.system.api.RemoteFileService; +import com.microservices.system.api.domain.SysFileBytes; import com.microservices.system.api.domain.SysFileInfo; import com.microservices.system.api.utils.FeignUtils; import org.slf4j.Logger; @@ -36,6 +37,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -176,7 +181,7 @@ public class PmsProductLibraryServiceImpl implements IPmsProductLibraryService { PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseByIdentifier(productComponentInputVo.getEnterpriseIdentifier()); for (Long docId : productComponentInputVo.getDocList()) { PmsDocumentDetailVo pmsDocument = pmsDocumentService.selectPmsDocumentDetailVoByIdAndEnterpriseId(docId, pmsEnterprise.getId()); - if (Objects.equals(pmsDocument.getDocType(), 1) || Objects.equals(pmsDocument.getDocType(), 4)) { + if (!Objects.equals(pmsDocument.getDocType(), 2) && !Objects.equals(pmsDocument.getDocType(), 3)) { throw new ServiceException("产品库文档类型仅允许为文档类型或附件类型(文档Id[%s])", pmsDocument.getId()); } PmsProductLibraryComponent pmsProductLibraryComponent = @@ -185,9 +190,36 @@ public class PmsProductLibraryServiceImpl implements IPmsProductLibraryService { throw new ServiceException("产品库已存在该文档,请勿重复上传(文档文件名称[%s])", pmsDocument.getName()); } AssetRawInputVo assetRawInputVo = new AssetRawInputVo(); - assetRawInputVo.setFilename(pmsDocument.getName()); assetRawInputVo.setAssetIdentifier(String.valueOf(pmsDocument.getId())); - assetRawInputVo.setFileIdentifier(pmsDocument.getAttachmentIdentifier()); + if (Objects.equals(pmsDocument.getDocType(), 3)) { + assetRawInputVo.setFilename(pmsDocument.getName()); + assetRawInputVo.setFileIdentifier(pmsDocument.getAttachmentIdentifier()); + } else if (Objects.equals(pmsDocument.getDocType(), 2)) { + Path tempFilePath = null; + try { + String docFileName = pmsDocument.getName() + ".md"; + assetRawInputVo.setFilename(docFileName); + // 创建临时文件 + tempFilePath = Files.createTempFile(pmsDocument.getName(), ".md"); + Files.write(tempFilePath, pmsDocument.getContent().getBytes(StandardCharsets.UTF_8)); + SysFileBytes sysFileBytes = new SysFileBytes(); + sysFileBytes.setFileName(docFileName); + sysFileBytes.setFileInputStream(Files.newInputStream(tempFilePath)); + assetRawInputVo.setFileBytes(sysFileBytes); + } catch (IOException e) { + log.error("文档上传失败:{}", e.getMessage()); + throw new ServiceException("文档上传失败"); + } finally { + if (tempFilePath != null) { + try { + Files.deleteIfExists(tempFilePath); + } catch (IOException e) { + log.error("删除临时文件失败:{}", e.getMessage()); + } + } + } + } + assetList.add(assetRawInputVo); } return pmsProductLibraryComponentService.insertRawComponent(productComponentInputVo.toComponentRawInputVo(assetList));