From b9dfa11ffd1e27c527c9e36c5b9eefa78fc2d340 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期二, 12 五月 2026 14:27:53 +0800
Subject: [PATCH] fix:采购审核新增编辑(对应库存修改)

---
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java |  100 +++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 86 insertions(+), 14 deletions(-)

diff --git a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
index d7ecd95..e3ad55a 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -17,6 +17,7 @@
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.basic.pojo.SupplierManage;
 import com.ruoyi.common.enums.FileNameType;
+import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
@@ -27,6 +28,7 @@
 import com.ruoyi.other.pojo.TempFile;
 import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
 import com.ruoyi.procurementrecord.pojo.ProcurementRecordStorage;
+import com.ruoyi.procurementrecord.utils.StockUtils;
 import com.ruoyi.project.system.domain.SysUser;
 import com.ruoyi.project.system.mapper.SysUserMapper;
 import com.ruoyi.purchase.dto.PurchaseLedgerDto;
@@ -52,6 +54,8 @@
 import com.ruoyi.sales.pojo.SalesLedger;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
+import com.ruoyi.stock.pojo.StockInRecord;
+import com.ruoyi.stock.service.StockInRecordService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.BeanUtils;
@@ -150,6 +154,10 @@
     private  PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper;
     @Autowired
     private  SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
+    @Autowired
+    private StockUtils stockUtils;
+    @Autowired
+    private StockInRecordService stockInRecordService;
     @Value("${file.upload-dir}")
     private String uploadDir;
 
@@ -225,28 +233,36 @@
         purchaseLedger.setRecorderName(sysUser.getNickName());
         purchaseLedger.setPhoneNumber(sysUser.getPhonenumber());
         purchaseLedger.setApprovalStatus(1);
+
+        List<SalesLedgerProduct> productList = Optional.ofNullable(purchaseLedgerDto.getProductData()).orElse(Collections.emptyList());
         // 3. 鏂板鎴栨洿鏂颁富琛�
-        if (purchaseLedger.getId() == null) {
+        boolean isUpdate = purchaseLedger.getId() != null;
+        if (!isUpdate) {
             purchaseLedgerMapper.insert(purchaseLedger);
         } else {
-            // 鍒犻櫎閲囪喘瀹℃牳锛岄噸鏂版彁浜�
-            ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
-                    .eq(ApproveProcess::getApproveType, 5)
-                    .eq(ApproveProcess::getApproveReason, purchaseLedger.getPurchaseContractNumber())
-                    .eq(ApproveProcess::getApproveDelete, 0)
-                    .last("limit 1"));
-            if (one != null) {
-                approveProcessService.delByIds(Collections.singletonList(one.getId()));
+            PurchaseLedger oldPurchase = purchaseLedgerMapper.selectById(purchaseLedger.getId());
+            if (oldPurchase == null) {
+                throw new BaseException("閲囪喘鍙拌处涓嶅瓨鍦�");
             }
+            if (CollectionUtils.isEmpty(productList)) {
+                throw new BaseException("浜у搧淇℃伅涓嶅瓨鍦�");
+            }
+            // 缂栬緫鍓嶅厛鍥炴粴鏃х殑搴撳瓨涓庤川妫�鍓綔鐢�
+            clearPurchaseSideEffectsBeforeEdit(oldPurchase.getId(), oldPurchase.getPurchaseContractNumber());
             purchaseLedgerMapper.updateById(purchaseLedger);
         }
-        // 6.閲囪喘瀹℃牳鏂板
-//        addApproveByPurchase(loginUser, purchaseLedger);
-
         // 4. 澶勭悊瀛愯〃鏁版嵁
-        List<SalesLedgerProduct> productList = purchaseLedgerDto.getProductData();
-        if (productList != null && !productList.isEmpty()) {
+        if (!productList.isEmpty()) {
             handleSalesLedgerProducts(purchaseLedger.getId(), productList, purchaseLedgerDto.getType());
+        }
+        for (SalesLedgerProduct salesLedgerProduct : productList) {
+            // 璐ㄦ
+            if (Boolean.TRUE.equals(salesLedgerProduct.getIsChecked())) {
+                addQualityInspect(purchaseLedger, salesLedgerProduct);
+            } else {
+                //鐩存帴鍏ュ簱
+                stockUtils.addStock(salesLedgerProduct.getProductModelId(), salesLedgerProduct.getQuantity(), StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode(), purchaseLedger.getId());
+            }
         }
         //鏂板鍘熸潗鏂欐楠�  瀹℃壒涔嬪悗鎵嶇敓鎴愭楠�
 //        if (productList != null) {
@@ -291,6 +307,61 @@
                         qualityInspectParamMapper.insert(param);
                     });
         }
+    }
+
+    private void clearPurchaseSideEffectsBeforeEdit(Long purchaseLedgerId, String purchaseContractNumber) {
+        clearPurchaseApproveProcess(purchaseContractNumber);
+        clearPurchaseQualityInspect(purchaseLedgerId);
+        clearPurchaseStockInRecord(purchaseLedgerId);
+    }
+
+    private void clearPurchaseApproveProcess(String purchaseContractNumber) {
+        ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
+                .eq(ApproveProcess::getApproveType, 5)
+                .eq(ApproveProcess::getApproveReason, purchaseContractNumber)
+                .eq(ApproveProcess::getApproveDelete, 0)
+                .last("limit 1"));
+        if (one != null) {
+            approveProcessService.delByIds(Collections.singletonList(one.getId()));
+        }
+    }
+
+    private void clearPurchaseQualityInspect(Long purchaseLedgerId) {
+        LambdaQueryWrapper<QualityInspect> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(QualityInspect::getPurchaseLedgerId, purchaseLedgerId)
+                .eq(QualityInspect::getInspectType, 0);
+        List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(queryWrapper);
+        if (CollectionUtils.isEmpty(qualityInspects)) {
+            return;
+        }
+
+        boolean hasSubmittedInspect = qualityInspects.stream()
+                .anyMatch(item -> ObjectUtils.isNotEmpty(item.getInspectState()) && item.getInspectState().equals(1));
+        if (hasSubmittedInspect) {
+            throw new BaseException("瀛樺湪宸叉彁浜ょ殑璐ㄦ鍗曪紝涓嶅厑璁哥紪杈戦噰璐槑缁�");
+        }
+
+        List<Long> inspectIds = qualityInspects.stream()
+                .map(QualityInspect::getId)
+                .collect(Collectors.toList());
+        if (CollectionUtils.isNotEmpty(inspectIds)) {
+            qualityInspectParamMapper.delete(new LambdaQueryWrapper<QualityInspectParam>()
+                    .in(QualityInspectParam::getInspectId, inspectIds));
+        }
+        qualityInspectMapper.delete(queryWrapper);
+    }
+
+    private void clearPurchaseStockInRecord(Long purchaseLedgerId) {
+        List<StockInRecord> stockInRecords = stockInRecordService.list(new LambdaQueryWrapper<StockInRecord>()
+                .eq(StockInRecord::getRecordId, purchaseLedgerId)
+                .eq(StockInRecord::getRecordType, StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode())
+                .eq(StockInRecord::getType, "0"));
+        if (CollectionUtils.isEmpty(stockInRecords)) {
+            return;
+        }
+        stockInRecordService.batchDelete(stockInRecords.stream()
+                .map(StockInRecord::getId)
+                .collect(Collectors.toList()));
     }
 
     private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) {
@@ -472,6 +543,7 @@
             if (purchaseLedger.getApprovalStatus().equals(3)) {
                 throw new BaseException(purchaseLedger.getPurchaseContractNumber()+"宸茬粡瀹℃壒閫氳繃锛屼笉鍏佽鍒犻櫎");
             }
+            clearPurchaseStockInRecord(id);
         }
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐叆搴撹褰�
         LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductQueryWrapper = new LambdaQueryWrapper<>();

--
Gitblit v1.9.3