From 770361cf5d70c7893ffea4f1dacac297628aea1d Mon Sep 17 00:00:00 2001
From: chenhj <1263187585@qq.com>
Date: 星期五, 20 六月 2025 18:03:00 +0800
Subject: [PATCH] Merge branch 'master' of http://114.132.189.42:9002/r/zd-after

---
 main-business/src/main/java/com/ruoyi/business/utils/InventoryUtils.java |   73 ++++++++++++++++++++++++++++++++++++
 1 files changed, 73 insertions(+), 0 deletions(-)

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