From 8097a2a4383a3785e11990170d61c59ae6b48888 Mon Sep 17 00:00:00 2001
From: chenhj <1263187585@qq.com>
Date: 星期五, 20 六月 2025 18:02:53 +0800
Subject: [PATCH] 库存明细数据处理

---
 main-business/src/main/java/com/ruoyi/business/service/impl/InventorySummaryServiceImpl.java     |   25 ++++++++
 main-business/src/main/java/com/ruoyi/business/service/impl/InputInventoryRecordServiceImpl.java |   66 ++++++++++++---------
 main-business/src/main/java/com/ruoyi/business/utils/InventoryUtils.java                         |   73 ++++++++++++++++++++++++
 main-business/src/main/java/com/ruoyi/business/service/InventorySummaryService.java              |   11 +++
 4 files changed, 145 insertions(+), 30 deletions(-)

diff --git a/main-business/src/main/java/com/ruoyi/business/service/InventorySummaryService.java b/main-business/src/main/java/com/ruoyi/business/service/InventorySummaryService.java
index 82d8d24..89e8e69 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/InventorySummaryService.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/InventorySummaryService.java
@@ -1,9 +1,11 @@
 package com.ruoyi.business.service;
 
+import com.baomidou.mybatisplus.extension.service.IService;
 import com.ruoyi.business.dto.OfficialInventoryDto;
 import com.ruoyi.business.dto.PendingInventoryDto;
 import com.ruoyi.business.entity.InventorySummary;
-import com.baomidou.mybatisplus.extension.service.IService;
+
+import java.util.List;
 
 /**
  * <p>
@@ -21,4 +23,11 @@
      * @param officialInventoryDto 姝e紡搴撳瓨
      */
     int updateInventory(PendingInventoryDto pendingInventoryDto, OfficialInventoryDto officialInventoryDto);
+
+    /**
+     * 鎵归噺鏇存柊搴撳瓨
+     * @param ids 搴撳瓨id
+     * @return
+     */
+    int updateInventorySummary(List<Long> ids);
 }
diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/InputInventoryRecordServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/InputInventoryRecordServiceImpl.java
index 7370557..1ddb4c2 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/InputInventoryRecordServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/InputInventoryRecordServiceImpl.java
@@ -11,6 +11,7 @@
 import com.ruoyi.business.mapper.InventorySummaryMapper;
 import com.ruoyi.business.service.InputInventoryRecordService;
 import com.ruoyi.business.service.InventorySummaryService;
+import com.ruoyi.business.utils.InventoryUtils;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 
@@ -19,6 +20,7 @@
 import java.util.Comparator;
 import java.util.List;
 import java.util.Objects;
+import java.util.stream.Collectors;
 
 import static com.ruoyi.business.constant.InventoryRecordConstant.OFFICIAL_INVENTORY;
 import static com.ruoyi.business.constant.InventoryRecordConstant.PENDING_INVENTORY;
@@ -37,6 +39,7 @@
     private final InputInventoryRecordMapper inputInventoryRecordMapper;
     private final InventorySummaryService inventorySummaryService;
     private final InventorySummaryMapper inventorySummaryMapper;
+    private final InventoryUtils inventoryUtils;
 
     @Override
     public int insertInputInventoryRecord(PendingInventoryDto pendingInventoryDto, OfficialInventoryDto officialInventoryDto, BigDecimal quantity) {
@@ -62,36 +65,41 @@
 
     @Override
     public int deleteInputInventoryRecord(List<Long> ids) {
-        if (CollectionUtils.isNotEmpty(ids)) {
-            List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordMapper.selectBatchIds(ids);
-            // 鏍规嵁id杩涜闄嶅簭鎺掑簭
-            List<InputInventoryRecord> inputInventoryRecordList = inputInventoryRecords.stream()
-                    .sorted(Comparator.comparing(InputInventoryRecord::getId).reversed())
-                    .toList();
-
-            String InventoryType = inputInventoryRecords.get(0).getInventoryType();
-            List<Long> inventoryIds = inputInventoryRecords.stream().map(InputInventoryRecord::getInventoryId).toList();
-            List<InventorySummary> inventorySummaries = inventorySummaryMapper.selectList(new LambdaQueryWrapper<InventorySummary>()
-                    .eq(InventorySummary::getInventoryType, InventoryType)
-                    .in(InventorySummary::getInventoryId, inventoryIds));
-            if (CollectionUtils.isNotEmpty(inventorySummaries)) {
-                List<InventorySummary> updates = new ArrayList<>();
-                for (InventorySummary inventorySummary : inventorySummaries) {
-                    for (InputInventoryRecord inputInventoryRecord : inputInventoryRecords) {
-                        // 濡傛灉鑺傜偣涓婄殑鍏ュ簱璁板綍id澶т簬鍙樻洿鐨刬d锛岃鏄庢id鍓嶆墍鏈夊叆搴撹褰曢兘瑕侀噸鏂拌绠�
-                        if (Objects.equals(inventorySummary.getInventoryId(), inputInventoryRecord.getInventoryId()) && inventorySummary.getInputEndRecordId() > inputInventoryRecord.getId()) {
-                            inventorySummary.setInputEndRecordId(inputInventoryRecord.getId());
-                            updates.add(inventorySummary);
-                        }
-                    }
-
-                }
-                // 閲嶇疆浠栦滑鐨勮妭鐐规渶缁坕d
-                inventorySummaryMapper.updateById(updates);
-            }
+        if (CollectionUtils.isEmpty(ids)) {
+            throw new RuntimeException("璇蜂紶鍏ヨ鍒犻櫎鐨刬d璁板綍");
         }
-        // todo 閲嶆柊璁$畻鑺傜偣搴撳瓨
-        // todo 鏇存柊搴撳瓨瀹炴柦鏁版嵁
+        List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordMapper.selectBatchIds(ids);
+        // 鏍规嵁id杩涜闄嶅簭鎺掑簭
+        List<InputInventoryRecord> inputInventoryRecordList = inputInventoryRecords.stream()
+                .sorted(Comparator.comparing(InputInventoryRecord::getId).reversed())
+                .toList();
+
+        String InventoryType = inputInventoryRecords.get(0).getInventoryType();
+        List<Long> inventoryIds = inputInventoryRecords.stream().map(InputInventoryRecord::getInventoryId).toList().stream().distinct().collect(Collectors.toList());
+        List<InventorySummary> inventorySummaries = inventorySummaryMapper.selectList(new LambdaQueryWrapper<InventorySummary>()
+                .eq(InventorySummary::getInventoryType, InventoryType)
+                .in(InventorySummary::getInventoryId, inventoryIds));
+        if (CollectionUtils.isNotEmpty(inventorySummaries)) {
+            List<InventorySummary> updates = new ArrayList<>();
+            for (InventorySummary inventorySummary : inventorySummaries) {
+                for (InputInventoryRecord inputInventoryRecord : inputInventoryRecordList) {
+                    // 濡傛灉鑺傜偣涓婄殑鍏ュ簱璁板綍id澶т簬鍙樻洿鐨刬d锛岃鏄庢id鍓嶆墍鏈夊叆搴撹褰曢兘瑕侀噸鏂拌绠�
+                    if (Objects.equals(inventorySummary.getInventoryId(), inputInventoryRecord.getInventoryId()) && inventorySummary.getInputEndRecordId() > inputInventoryRecord.getId()) {
+                        inventorySummary.setInputEndRecordId(inputInventoryRecord.getId());
+                        updates.add(inventorySummary);
+                    }
+                }
+
+            }
+            // 閲嶇疆鑺傜偣鏈�缁坕d
+            inventorySummaryMapper.updateById(updates);
+        }
+
+        // 閲嶆柊璁$畻鑺傜偣搴撳瓨
+        inventorySummaryService.updateInventorySummary(ids);
+
+        // 鏇存柊搴撳瓨瀹炴椂鏁版嵁
+        inventoryUtils.updateInventoryByIds(inventoryIds, inputInventoryRecords.get(0).getInventoryType());
 
 
         // 鍒犻櫎鍏ュ簱璁板綍
diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/InventorySummaryServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/InventorySummaryServiceImpl.java
index 9892611..2669ab6 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/InventorySummaryServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/InventorySummaryServiceImpl.java
@@ -87,4 +87,29 @@
             return officialInventoryMapper.updateById(officialInventory);
         }
     }
+
+    @Override
+    public int updateInventorySummary(List<Long> ids) {
+        List<InventorySummary> inventorySummaries = inventorySummaryMapper.selectBatchIds(ids);
+        for (InventorySummary inventorySummary : inventorySummaries) {
+            // 鏌ヨ鑺傜偣涔嬪墠鎵�鏈夊叆搴撹褰�
+            List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordMapper.selectList(new LambdaQueryWrapper<InputInventoryRecord>()
+                    .eq(InputInventoryRecord::getInventoryId, inventorySummary.getInventoryId())
+                    .eq(InputInventoryRecord::getInventoryType, inventorySummary.getInventoryType())
+                    .lt(InputInventoryRecord::getId, inventorySummary.getInputEndRecordId()));
+
+            // 鏌ヨ鑺傜偣涔嬪墠鎵�鏈夊嚭搴撹褰�
+            List<OutputInventoryRecord> outputInventoryRecords = outputInventoryRecordMapper.selectList(new LambdaQueryWrapper<OutputInventoryRecord>()
+                    .eq(OutputInventoryRecord::getInventoryId, inventorySummary.getInventoryId())
+                    .eq(OutputInventoryRecord::getInventoryType, inventorySummary.getInventoryType())
+                    .lt(OutputInventoryRecord::getId, inventorySummary.getOutputEndRecordId()));
+
+            // 璁$畻搴撳瓨鏁伴噺
+            BigDecimal inputQuantity = inputInventoryRecords.stream().map(InputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
+            BigDecimal outputQuantity = outputInventoryRecords.stream().map(OutputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
+            inventorySummary.setInventoryQuantity(inputQuantity.subtract(outputQuantity));
+        }
+
+        return inventorySummaryMapper.updateById(inventorySummaries).size();
+    }
 }
diff --git a/main-business/src/main/java/com/ruoyi/business/utils/InventoryUtils.java b/main-business/src/main/java/com/ruoyi/business/utils/InventoryUtils.java
new file mode 100644
index 0000000..0b17601
--- /dev/null
+++ b/main-business/src/main/java/com/ruoyi/business/utils/InventoryUtils.java
@@ -0,0 +1,73 @@
+package com.ruoyi.business.utils;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.ruoyi.business.constant.InventoryRecordConstant;
+import com.ruoyi.business.entity.*;
+import com.ruoyi.business.mapper.*;
+import com.ruoyi.common.utils.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+@Component
+public class InventoryUtils {
+
+    @Autowired
+    private InventorySummaryMapper inventorySummaryMapper;
+    @Autowired
+    private InputInventoryRecordMapper inputInventoryRecordMapper;
+    @Autowired
+    private OutputInventoryRecordMapper outputInventoryRecordMapper;
+    @Autowired
+    private OfficialInventoryMapper officialInventoryMapper;
+    @Autowired
+    private PendingInventoryMapper pendingInventoryMapper;
+
+    /**
+     * 鏇存柊搴撳瓨
+     */
+    public void updateInventoryByIds(List<Long> ids, String type) {
+        if (StringUtils.isEmpty(type)) {
+            throw new RuntimeException("璇锋寚瀹氬簱瀛樼被鍨�");
+        }
+
+        for (Long id : ids) {
+            // 鑾峰彇搴撳瓨鑺傜偣
+            InventorySummary inventorySummary = inventorySummaryMapper.selectOne(new LambdaQueryWrapper<InventorySummary>()
+                    .eq(InventorySummary::getInventoryId, id)
+                    .eq(InventorySummary::getInventoryType, type));
+            Long inputEndRecordId = inventorySummary != null ? inventorySummary.getInputEndRecordId() : 0L;
+            Long outputEndRecordId = inventorySummary != null ? inventorySummary.getOutputEndRecordId() : 0L;
+            List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordMapper.selectList(new LambdaQueryWrapper<InputInventoryRecord>()
+                    .eq(InputInventoryRecord::getInventoryId, id)
+                    .eq(InputInventoryRecord::getInventoryType, type)
+                    .gt(InputInventoryRecord::getId, inputEndRecordId));
+
+            List<OutputInventoryRecord> outputInventoryRecords = outputInventoryRecordMapper.selectList(new LambdaQueryWrapper<OutputInventoryRecord>()
+                    .eq(OutputInventoryRecord::getInventoryId, id)
+                    .eq(OutputInventoryRecord::getInventoryType, type)
+                    .gt(OutputInventoryRecord::getId, outputEndRecordId));
+
+            // 鑾峰彇搴撳瓨鏁版嵁
+            BigDecimal inputQuantity = inputInventoryRecords.stream().map(InputInventoryRecord::getQuantity).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
+            BigDecimal outputQuantity = outputInventoryRecords.stream().map(OutputInventoryRecord::getQuantity).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
+            // 璁$畻杩欐搴撳瓨鏁版嵁
+            BigDecimal quantity = inventorySummary != null ? inventorySummary.getInventoryQuantity().add(inputQuantity.subtract(outputQuantity)) : inputQuantity.subtract(outputQuantity);
+            if (inventorySummary != null) {
+                if (type.equals(InventoryRecordConstant.OFFICIAL_INVENTORY)) {
+                    OfficialInventory update = new OfficialInventory();
+                    update.setInventoryQuantity(quantity);
+                    update.setId(id);
+                    officialInventoryMapper.updateById(update);
+                } else {
+                    PendingInventory update = new PendingInventory();
+                    update.setInventoryQuantity(quantity);
+                    update.setId(id);
+                    pendingInventoryMapper.updateById(update);
+                }
+            }
+        }
+    }
+}

--
Gitblit v1.9.3