From 1eb432710b7a3a2e820be10b17c0138b377ee1b8 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期四, 20 八月 2026 16:50:12 +0800
Subject: [PATCH] 仓储物流-采购退货新增关联采购订单及带入物料功能

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pd/document/MesPdDocumentServiceImpl.java |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pd/document/MesPdDocumentServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pd/document/MesPdDocumentServiceImpl.java
index c733e24..18f02e4 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pd/document/MesPdDocumentServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pd/document/MesPdDocumentServiceImpl.java
@@ -1,17 +1,23 @@
 package cn.iocoder.yudao.module.mes.service.pd.document;
 
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
+import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
 import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentPageReqVO;
 import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentSaveReqVO;
 import cn.iocoder.yudao.module.mes.dal.dataobject.pd.document.MesPdDocumentDO;
 import cn.iocoder.yudao.module.mes.dal.mysql.pd.document.MesPdDocumentMapper;
 import cn.iocoder.yudao.module.mes.service.pd.project.MesPdProjectService;
+import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
+import cn.iocoder.yudao.module.system.api.storage.dto.StorageBlobRespDTO;
 import jakarta.annotation.Resource;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
 
+import java.time.LocalDateTime;
 import java.util.List;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -20,7 +26,7 @@
 /**
  * MES 璁捐璧勬枡 Service 瀹炵幇绫�
  *
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
  */
 @Service
 @Validated
@@ -32,6 +38,8 @@
     @Resource
     @Lazy
     private MesPdProjectService projectService;
+    @Resource
+    private StorageAttachmentApi storageAttachmentApi;
 
     @Override
     public Long createDocument(MesPdDocumentSaveReqVO createReqVO) {
@@ -39,23 +47,48 @@
         projectService.validateProjectExists(createReqVO.getProjectId());
         // 鎻掑叆
         MesPdDocumentDO document = BeanUtils.toBean(createReqVO, MesPdDocumentDO.class);
+        // 琛ュ厖涓婁紶浜恒�佷笂浼犳椂闂�
+        if (document.getUploadUserId() == null) {
+            document.setUploadUserId(SecurityFrameworkUtils.getLoginUserId());
+        }
+        if (document.getUploadTime() == null) {
+            document.setUploadTime(LocalDateTime.now());
+        }
         documentMapper.insert(document);
+        storageAttachmentApi.bindAttachments("file", "mes_pd_document",
+                document.getId(), createReqVO.getBlobIds());
+        // 鏈~鍐欐枃浠跺悕鏃讹紝浠庝笂浼犻檮浠惰嚜鍔ㄨˉ鍏�
+        fillFileNameIfAbsent(document);
         return document.getId();
     }
 
     @Override
     public void updateDocument(MesPdDocumentSaveReqVO updateReqVO) {
         // 鏍¢獙瀛樺湪
-        validateDocumentExists(updateReqVO.getId());
+        MesPdDocumentDO existing = validateDocumentExists(updateReqVO.getId());
         // 鏇存柊
         MesPdDocumentDO updateObj = BeanUtils.toBean(updateReqVO, MesPdDocumentDO.class);
+        // 淇濈暀鍘熶笂浼犱汉銆佷笂浼犳椂闂达紱鍘嗗彶鏁版嵁涓虹┖鏃惰ˉ榻�
+        if (updateObj.getUploadUserId() == null) {
+            updateObj.setUploadUserId(existing.getUploadUserId() != null
+                    ? existing.getUploadUserId() : SecurityFrameworkUtils.getLoginUserId());
+        }
+        if (updateObj.getUploadTime() == null) {
+            updateObj.setUploadTime(existing.getUploadTime() != null
+                    ? existing.getUploadTime() : LocalDateTime.now());
+        }
         documentMapper.updateById(updateObj);
+        storageAttachmentApi.updateAttachments("file", "mes_pd_document", updateReqVO.getId(), updateReqVO.getBlobIds());
+        // 鏈~鍐欐枃浠跺悕鏃讹紝浠庝笂浼犻檮浠惰嚜鍔ㄨˉ鍏�
+        fillFileNameIfAbsent(updateObj);
     }
 
     @Override
     public void deleteDocument(Long id) {
         // 鏍¢獙瀛樺湪
         validateDocumentExists(id);
+        // 鍒犻櫎闄勪欢
+        storageAttachmentApi.deleteAttachmentsByRecord("mes_pd_document", id);
         // 鍒犻櫎
         documentMapper.deleteById(id);
     }
@@ -77,6 +110,10 @@
 
     @Override
     public void deleteByProjectId(Long projectId) {
+        List<MesPdDocumentDO> documents = documentMapper.selectListByProjectId(projectId);
+        for (MesPdDocumentDO document : documents) {
+            storageAttachmentApi.deleteAttachmentsByRecord("mes_pd_document", document.getId());
+        }
         documentMapper.deleteByProjectId(projectId);
     }
 
@@ -88,4 +125,19 @@
         return document;
     }
 
+    /**
+     * 鏈~鍐欐枃浠跺悕鏃讹紝浠庝笂浼犻檮浠惰嚜鍔ㄨˉ鍏ㄦ枃浠跺悕
+     *
+     * @param document 璁捐璧勬枡
+     */
+    private void fillFileNameIfAbsent(MesPdDocumentDO document) {
+        if (StrUtil.isNotEmpty(document.getFileName())) {
+            return;
+        }
+        List<StorageBlobRespDTO> attachments = storageAttachmentApi.listAttachments("mes_pd_document", document.getId());
+        if (CollUtil.isNotEmpty(attachments)) {
+            document.setFileName(attachments.get(0).getName());
+            documentMapper.updateById(document);
+        }
+    }
 }

--
Gitblit v1.9.3