From 1ec3b6f73b1ee8b81d8c6583c38ffdf5ea52ed56 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期二, 29 九月 2026 14:29:08 +0800
Subject: [PATCH] 新疆-融邦 1.协同办公-协同审批:采购及销售审批进入销售流程后也可预览在台账上传的附件

---
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java |   81 +++++++++++++++++++++++++++++++++++-----
 1 files changed, 71 insertions(+), 10 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 a81fca7..0043d81 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -14,6 +14,7 @@
 import com.ruoyi.approve.pojo.ApproveProcess;
 import com.ruoyi.approve.service.ApprovalInstanceService;
 import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl;
+import com.ruoyi.basic.dto.StorageBlobVO;
 import com.ruoyi.basic.enums.ApplicationTypeEnum;
 import com.ruoyi.basic.enums.RecordTypeEnum;
 import com.ruoyi.basic.mapper.OilDepotMapper;
@@ -104,6 +105,11 @@
      * 鏄庣粏绫诲瀷锛氶噰璐紙sales_ledger_product.type锛夈�備粎閲囪喘鏄庣粏鎵嶅洖鍐欏浠跺簱瀛�
      */
     private static final Integer LEDGER_TYPE_PURCHASE = 2;
+
+    /**
+     * 澶囦欢娴佹按鏉ユ簮锛氶噰璐叆搴撱�傝 spare_parts_record.source_type
+     */
+    private static final Integer SPARE_PARTS_SOURCE_PURCHASE = 1;
 
     private final PurchaseLedgerMapper purchaseLedgerMapper;
     private final SalesLedgerMapper salesLedgerMapper;
@@ -719,10 +725,13 @@
                 salesLedgerProduct.setRegisterDate(localDateTime);
                 salesLedgerProductMapper.insert(salesLedgerProduct);
             }
-            // 浠呮柊澧炴槑缁嗗洖鍐欏浠讹紝缂栬緫璧� update 鍒嗘敮澶╃劧涓嶉噸澶嶈鏁�
-            if (LEDGER_TYPE_PURCHASE.equals(ledgerType)) {
-                recordSparePartsInbound(insertList, purchaseLedger);
-            }
+        }
+        // 閲囪喘鏄庣粏鍥炲啓澶囦欢锛氬厛鎸夊彴璐﹀洖婊氫笂涓�娆$殑娴佹按锛屽啀鎸夊綋鍓嶆槑缁嗘暣浣撻噸寤恒��
+        // 缂栬緫鏃舵槑缁嗚鍙兘琚Щ闄わ紝鍙鏂板琛屼細婕忔帀绉婚櫎鐨勯偅浜涳紱鏁翠綋閲嶅缓澶╃劧涓庡綋鍓嶆槑缁嗗榻愩��
+        // 娉ㄦ剰杩欐蹇呴』鍦� insertList 涔嬪锛屽惁鍒欍�屽彧鍒犳槑缁嗐�佷笉鏂板銆嶇殑缂栬緫涓嶄細瑙﹀彂鍥炴粴
+        if (LEDGER_TYPE_PURCHASE.equals(ledgerType)) {
+            rollbackSparePartsInbound(salesLedgerId);
+            recordSparePartsInbound(products, purchaseLedger);
         }
 
         // 璁$畻鎬诲惈绋庨噾棰�
@@ -739,11 +748,54 @@
     }
 
     /**
-     * 閲囪喘鍙拌处鏂板鏄庣粏鎸夎鏍煎尮閰嶅浠讹細绱姞澶囦欢搴撳瓨骞惰涓�鏉″叆搴撴祦姘淬��
-     * 鏈淮鎶よ鏍肩殑澶囦欢涓嶅弬涓庯紝闈欓粯璺宠繃銆�
+     * 鎸夐噰璐彴璐﹀洖婊氬浠讹細鎶婅鍙拌处浜х敓鐨勫叆搴撴祦姘撮�愬浠跺噺鍥炲簱瀛橈紝鐒跺悗鍒犻櫎杩欎簺娴佹按銆�
+     *
+     * <p>澶囦欢鍏堢敱閲囪喘鍏ヨ处銆佸悗缁缁翠慨/淇濆吇棰嗙敤鍑鸿处锛岃嫢鍏ヨ处閲忓凡缁忚棰嗙敤鎺夛紝鍑忓洖浼氬嚭鐜拌礋搴撳瓨锛�
+     * 姝ゆ椂鐩存帴鎷︿綇涓嶈鍒狅紝閬垮厤鎶婂浠惰处鍋氳姳銆傚垹闄ゅ彴璐︿笌缂栬緫閲嶅缓閮借蛋杩欓噷锛屽彛寰勭粺涓�銆�</p>
      */
-    private void recordSparePartsInbound(List<SalesLedgerProduct> insertList, PurchaseLedger purchaseLedger) {
-        List<Long> modelIds = insertList.stream()
+    private void rollbackSparePartsInbound(Long ledgerId) {
+        List<SparePartsRecord> records = sparePartsRecordService.list(new LambdaQueryWrapper<SparePartsRecord>()
+                .eq(SparePartsRecord::getSourceType, SPARE_PARTS_SOURCE_PURCHASE)
+                .eq(SparePartsRecord::getSourceId, ledgerId));
+        if (CollectionUtils.isEmpty(records)) {
+            return;
+        }
+        Map<Long, BigDecimal> rollbackBySparePartsId = new HashMap<>();
+        for (SparePartsRecord record : records) {
+            BigDecimal quantity = record.getQuantity() == null ? BigDecimal.ZERO : record.getQuantity();
+            rollbackBySparePartsId.merge(record.getSparePartsId(), quantity, BigDecimal::add);
+        }
+        for (Map.Entry<Long, BigDecimal> entry : rollbackBySparePartsId.entrySet()) {
+            if (entry.getKey() == null) {
+                continue;
+            }
+            SpareParts spareParts = sparePartsMapper.selectById(entry.getKey());
+            if (spareParts == null) {
+                continue;
+            }
+            BigDecimal current = spareParts.getQuantity() == null ? BigDecimal.ZERO : spareParts.getQuantity();
+            if (current.compareTo(entry.getValue()) < 0) {
+                throw new BaseException("澶囦欢銆�" + spareParts.getName() + "銆嶅綋鍓嶅簱瀛� " + current
+                        + "锛屼笉瓒充互鍥炴粴鏈閲囪喘鍏ュ簱鐨� " + entry.getValue() + "锛岃鍏堝鐞嗛鐢ㄨ褰�");
+            }
+            spareParts.setQuantity(current.subtract(entry.getValue()));
+            sparePartsMapper.updateById(spareParts);
+        }
+        sparePartsRecordService.removeByIds(records.stream()
+                .map(SparePartsRecord::getId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList()));
+    }
+
+    /**
+     * 閲囪喘鍙拌处鏄庣粏鎸夎鏍煎尮閰嶅浠讹細绱姞澶囦欢搴撳瓨骞惰涓�鏉″叆搴撴祦姘淬��
+     * 鏈淮鎶よ鏍肩殑澶囦欢涓嶅弬涓庯紝闈欓粯璺宠繃銆�
+     *
+     * <p>璋冪敤鍓嶅繀椤诲厛 {@link #rollbackSparePartsInbound(Long)}锛屼袱鑰呮垚瀵瑰嚭鐜帮細
+     * 鍥炴粴鏃ф祦姘� + 鎸夊綋鍓嶆槑缁嗛噸寤猴紝鎵嶈兘淇濊瘉缂栬緫鍙拌处鍚庡浠舵暟閲忎笌鏄庣粏涓�鑷淬��</p>
+     */
+    private void recordSparePartsInbound(List<SalesLedgerProduct> products, PurchaseLedger purchaseLedger) {
+        List<Long> modelIds = products.stream()
                 .map(SalesLedgerProduct::getProductModelId)
                 .filter(Objects::nonNull)
                 .distinct()
@@ -758,7 +810,7 @@
         }
         Map<Long, SpareParts> sparePartsByModelId = sparePartsList.stream()
                 .collect(Collectors.toMap(SpareParts::getProductModelId, s -> s, (a, b) -> a));
-        for (SalesLedgerProduct product : insertList) {
+        for (SalesLedgerProduct product : products) {
             SpareParts spareParts = sparePartsByModelId.get(product.getProductModelId());
             if (spareParts == null || product.getQuantity() == null
                     || product.getQuantity().compareTo(BigDecimal.ZERO) <= 0) {
@@ -790,6 +842,9 @@
             if (purchaseLedger.getApprovalStatus().equals(3)) {
                 throw new BaseException(purchaseLedger.getPurchaseContractNumber()+"宸茬粡瀹℃壒閫氳繃锛屼笉鍏佽鍒犻櫎");
             }
+            // 鍙拌处鍒犻櫎瑕佹妸璇ュ彴璐﹂噰璐叆搴撴椂绱姞鍒板浠朵笂鐨勬暟閲忓噺鍥炲幓锛屽苟鍒犳帀瀵瑰簲娴佹按锛�
+            // 鍚﹀垯澶囦欢搴撳瓨铏氶珮銆佹祦姘存畫鐣欍�傚浠跺凡棰嗙敤瀵艰嚧鍑忓洖涓鸿礋鏃朵細鍦ㄨ繖閲屾嫤浣�
+            rollbackSparePartsInbound(id);
         }
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐叆搴撹褰�
         LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductQueryWrapper = new LambdaQueryWrapper<>();
@@ -888,8 +943,8 @@
         if (!products.isEmpty()) {
             resultDto.setHasChildren(true);
             resultDto.setProductData(products);
-            resultDto.setStorageBlobVOS(fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.FILE, RecordTypeEnum.PURCHASE_LEDGER, purchaseLedger.getId()));
         }
+        resultDto.setStorageBlobVOS(fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.FILE, RecordTypeEnum.PURCHASE_LEDGER, purchaseLedger.getId()));
         return resultDto;
     }
 
@@ -1124,6 +1179,10 @@
             }
         }
 
+        // 闄勪欢鎸夊彴璐� id 瀹炴煡锛屽鎵逛汉棰勮鏃跺缁堟槸鏈�鏂颁笂浼犵殑闄勪欢
+        List<StorageBlobVO> attachments = fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(
+                ApplicationTypeEnum.FILE, RecordTypeEnum.PURCHASE_LEDGER, purchaseLedger.getId());
+
         // 3. 鏌ヨ褰撳墠閲囪喘鍗曞搴旂殑瀹℃壒蹇収
         if (approvalSnapshotInstance == null) {
             approvalSnapshotInstance = approvalInstanceService.getOne(new LambdaQueryWrapper<ApprovalInstance>()
@@ -1174,6 +1233,7 @@
                     }
                     snapshot.setProductData(snapshot.getProductData() == null ? Collections.emptyList() : snapshot.getProductData());
                     snapshot.setHasChildren(CollectionUtils.isNotEmpty(snapshot.getProductData()));
+                    snapshot.setStorageBlobVOS(attachments);
                     return snapshot;
                 }
             } catch (Exception e) {
@@ -1194,6 +1254,7 @@
             resultDto.setHasChildren(true);
             resultDto.setProductData(products);
         }
+        resultDto.setStorageBlobVOS(attachments);
         return resultDto;
     }
 

--
Gitblit v1.9.3