From 3bf81c083883d4861d53380423ee8a44c899b5f6 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期四, 26 三月 2026 18:04:46 +0800
Subject: [PATCH] fix:1.仓储库存单位转换记录和库存关联更新 2.原材料和非原材料的数量区分 3.入库和出库记录删除单位转换库存更新 4.过磅单单位转换更新

---
 src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java |  151 +++++++++++++++++++++++++++++++++-----------------
 1 files changed, 99 insertions(+), 52 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
index ca4e84d..9f7029d 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
@@ -11,6 +11,7 @@
 import com.ruoyi.basic.pojo.Product;
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
+import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.equipmentenergyconsumption.mapper.ElectricityConsumptionAreaMapper;
 import com.ruoyi.framework.web.domain.R;
@@ -37,6 +38,7 @@
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.List;
+import java.util.Optional;
 
 /**
  * <p>
@@ -73,6 +75,7 @@
         stockInRecordDto.setRecordId(stockInventoryDto.getRecordId());
         stockInRecordDto.setRecordType(stockInventoryDto.getRecordType());
         stockInRecordDto.setWeighingOperator(stockInventoryDto.getWeighingOperator());
+        stockInRecordDto.setUnit(stockInventoryDto.getUnit());
         Long modelId;
         if (stockInventoryDto.getProductId() != null) {
             stockInRecordDto.setProductId(stockInventoryDto.getProductId());
@@ -139,6 +142,7 @@
             stockInRecordDto.setGrossWeight(stockInventoryDto.getGrossWeight());
             stockInRecordDto.setTareWeight(stockInventoryDto.getTareWeight());
             stockInRecordDto.setLicensePlateNo(stockInventoryDto.getLicensePlateNo());
+            stockInRecordDto.setUnit(stockInventoryDto.getUnit());
             // 鐢熸垚纾呭崟
             String absoluteDocPath = weighbridgeDocGenerator.generateWeighbridgeDoc(stockInRecordDto);
             stockInRecordDto.setWeighbridgeDocPath(absoluteDocPath);
@@ -181,68 +185,111 @@
     @Transactional(rollbackFor = Exception.class)
     public Boolean subtractStockInventory(StockInventoryDto stockInventoryDto) {
 
+        // 1. 鏌ヨ浜у搧鍨嬪彿鍜屽簱瀛樹俊鎭�
         ProductModel productModel = productModelMapper.selectById(stockInventoryDto.getProductModelId());
-        BigDecimal weight = compareUnit(stockInventoryDto, productModel);
-        //  鏂板鍑哄簱璁板綍
-        StockOutRecordDto stockOutRecordDto = new StockOutRecordDto();
-        stockOutRecordDto.setRecordId(stockInventoryDto.getRecordId());
-        stockOutRecordDto.setRecordType(stockInventoryDto.getRecordType());
-        stockOutRecordDto.setWeighingDate(stockInventoryDto.getWeighingDate());
-        stockOutRecordDto.setStockOutNum(stockInventoryDto.getNetWeight());
-        stockOutRecordDto.setNetWeight(stockInventoryDto.getNetWeight());
-        stockOutRecordDto.setGrossWeight(stockInventoryDto.getGrossWeight());
-        stockOutRecordDto.setTareWeight(stockInventoryDto.getTareWeight());
-        stockOutRecordDto.setWeighingOperator(stockInventoryDto.getWeighingOperator());
-        stockOutRecordDto.setProductModelId(stockInventoryDto.getProductModelId());
-        stockOutRecordDto.setLicensePlateNo(stockInventoryDto.getLicensePlateNo());
-        stockOutRecordDto.setProductId(stockInventoryDto.getProductId());
-        stockOutRecordDto.setRemark(stockInventoryDto.getRemark());
-        stockOutRecordDto.setType("0");
-        //鐢熸垚纾呭崟
-        StockInRecordDto stockInRecordDto = new StockInRecordDto();
-        BeanUtils.copyProperties(stockOutRecordDto, stockInRecordDto);
-        String absoluteDocPath = weighbridgeDocGenerator.generateWeighbridgeDoc(stockInRecordDto);
-        stockOutRecordDto.setWeighbridgeDocPath(absoluteDocPath);
-        stockOutRecordService.add(stockOutRecordDto);
-        StockInventory oldStockInventory = stockInventoryMapper.selectOne(new QueryWrapper<StockInventory>().lambda().eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId()));
-        if (ObjectUtils.isEmpty(oldStockInventory)) {
-            throw new RuntimeException("浜у搧搴撳瓨涓嶅瓨鍦�");
-        }
-        BigDecimal lockedQty = oldStockInventory.getLockedQuantity();
-        if (lockedQty == null) {
-            lockedQty = BigDecimal.ZERO;
-        }
-        stockInventoryDto.setQualitity(weight);
-        if (stockInventoryDto.getQualitity().compareTo(oldStockInventory.getQualitity().subtract(lockedQty)) > 0) {
-            throw new RuntimeException("搴撳瓨涓嶈冻鏃犳硶鍑哄簱");
+        if (productModel == null) {
+            throw new BaseException("浜у搧鍨嬪彿涓嶅瓨鍦�");
         }
 
-        stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
+        StockInventory oldStockInventory = stockInventoryMapper.selectOne(
+                new QueryWrapper<StockInventory>().lambda()
+                        .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId())
+        );
+        if (oldStockInventory == null) {
+            throw new BaseException("浜у搧搴撳瓨涓嶅瓨鍦�");
+        }
+
+        // 2. 璁$畻骞惰浆鎹㈠嚭搴撴暟閲�
+        BigDecimal outQuantity = compareUnit(stockInventoryDto, productModel);
+
+        // 3. 妫�鏌ュ簱瀛樻槸鍚﹀厖瓒筹紙鑰冭檻閿佸畾搴撳瓨锛�
+        BigDecimal availableQuantity = oldStockInventory.getQualitity()
+                .subtract(Optional.ofNullable(oldStockInventory.getLockedQuantity()).orElse(BigDecimal.ZERO));
+        if (outQuantity.compareTo(availableQuantity) > 0) {
+            throw new BaseException("搴撳瓨涓嶈冻锛屽綋鍓嶅彲鐢ㄥ簱瀛橈細" + availableQuantity);
+        }
+
+        // 4. 鎵e噺搴撳瓨
+        stockInventoryDto.setQualitity(outQuantity);
+        int affectedRows = stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
+        if (affectedRows == 0) {
+            throw new BaseException("鎵e噺搴撳瓨澶辫触锛屽彲鑳藉簱瀛樹笉瓒虫垨鏁版嵁宸茶淇敼");
+        }
+
+        // 5. 鍒涘缓骞朵繚瀛樺嚭搴撹褰�
+        createAndSaveStockOutRecord(stockInventoryDto, productModel);
+
         return true;
     }
 
+    /**
+     * 璁$畻骞惰浆鎹㈠嚭搴撴暟閲忥紙缁熶竴杞崲涓哄簱瀛樺崟浣嶏級
+     */
     private BigDecimal compareUnit(StockInventoryDto stockInventoryDto, ProductModel productModel) {
-        String unit = "";
-        if (productModel != null) {
-            unit = productModel.getUnit();
+        // 鑾峰彇鍘熷鍑哄簱鏁伴噺锛堟牴鎹骇鍝佺被鍨嬮�夋嫨鍑�閲嶆垨鏁伴噺锛�
+        BigDecimal originalQuantity = (stockInventoryDto.getProductType() != null && stockInventoryDto.getProductType() == 0)
+                ? stockInventoryDto.getNetWeight()
+                : stockInventoryDto.getQualitity();
+
+        String sourceUnit = stockInventoryDto.getUnit();
+        String targetUnit = productModel.getUnit();
+
+        // 鍗曚綅鐩稿悓锛岀洿鎺ヨ繑鍥�
+        if (sourceUnit.equals(targetUnit)) {
+            return originalQuantity;
         }
-        BigDecimal weight;
-        if (stockInventoryDto.getUnit().equals(unit)) {
-            weight = stockInventoryDto.getProductType() != null && stockInventoryDto.getProductType() == 0 ?
-                    stockInventoryDto.getNetWeight() : stockInventoryDto.getQualitity();
+
+        // 鍗曚綅杞崲
+        if ("鍚�".equals(targetUnit)) {
+            // 杞崲涓哄惃
+            return originalQuantity.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
+        } else if ("鍏枻".equals(targetUnit)) {
+            // 杞崲涓哄叕鏂�
+            return originalQuantity.multiply(BigDecimal.valueOf(1000));
         } else {
-            if ("鍚�".equals(unit)) {
-                weight =stockInventoryDto.getProductType() != null && stockInventoryDto.getProductType() == 0 ?
-                        stockInventoryDto.getNetWeight().divide(BigDecimal.valueOf(1000),2, RoundingMode.HALF_UP) : stockInventoryDto.getQualitity().divide(BigDecimal.valueOf(1000),2,RoundingMode.HALF_UP);
-            } else if ("鍏枻".equals(unit)) {
-                weight = stockInventoryDto.getProductType() != null && stockInventoryDto.getProductType() == 0 ?
-                        stockInventoryDto.getNetWeight().multiply(BigDecimal.valueOf(1000)) : stockInventoryDto.getQualitity().multiply(BigDecimal.valueOf(1000));
-            }else {
-                weight =stockInventoryDto.getProductType() != null && stockInventoryDto.getProductType() == 0 ?
-                        stockInventoryDto.getNetWeight() : stockInventoryDto.getQualitity();
-            }
+            // 鍗曚綅涓嶅尮閰嶆椂锛岃繑鍥炲師鍊�
+            return originalQuantity;
         }
-        return weight;
+    }
+
+    /**
+     * 鍒涘缓骞朵繚瀛樺嚭搴撹褰�
+     */
+    private void createAndSaveStockOutRecord(StockInventoryDto stockInventoryDto, ProductModel productModel) {
+        // 鍒涘缓鍑哄簱璁板綍DTO
+        StockOutRecordDto stockOutRecordDto = new StockOutRecordDto();
+        if (stockInventoryDto.getProductType() != null && stockInventoryDto.getProductType() == 0) {
+            stockOutRecordDto.setRecordId(stockInventoryDto.getRecordId());
+            stockOutRecordDto.setRecordType(stockInventoryDto.getRecordType());
+            stockOutRecordDto.setWeighingDate(stockInventoryDto.getWeighingDate());
+            stockOutRecordDto.setStockOutNum(stockInventoryDto.getNetWeight());
+            stockOutRecordDto.setNetWeight(stockInventoryDto.getNetWeight());
+            stockOutRecordDto.setGrossWeight(stockInventoryDto.getGrossWeight());
+            stockOutRecordDto.setTareWeight(stockInventoryDto.getTareWeight());
+            stockOutRecordDto.setWeighingOperator(stockInventoryDto.getWeighingOperator());
+            stockOutRecordDto.setProductModelId(stockInventoryDto.getProductModelId());
+            stockOutRecordDto.setLicensePlateNo(stockInventoryDto.getLicensePlateNo());
+            stockOutRecordDto.setProductId(stockInventoryDto.getProductId());
+            stockOutRecordDto.setRemark(stockInventoryDto.getRemark());
+            stockOutRecordDto.setUnit(stockInventoryDto.getUnit());
+            stockOutRecordDto.setType("0");
+            // 鐢熸垚纾呭崟
+            StockInRecordDto stockInRecordDto = new StockInRecordDto();
+            BeanUtils.copyProperties(stockOutRecordDto, stockInRecordDto);
+            String absoluteDocPath = weighbridgeDocGenerator.generateWeighbridgeDoc(stockInRecordDto);
+            stockOutRecordDto.setWeighbridgeDocPath(absoluteDocPath);
+        } else {
+            stockOutRecordDto.setRecordId(stockInventoryDto.getRecordId());
+            stockOutRecordDto.setRecordType(stockInventoryDto.getRecordType());
+            stockOutRecordDto.setStockOutNum(stockInventoryDto.getQualitity());
+            stockOutRecordDto.setNetWeight(stockInventoryDto.getQualitity());
+            stockOutRecordDto.setProductModelId(stockInventoryDto.getProductModelId());
+            stockOutRecordDto.setProductId(stockInventoryDto.getProductId());
+            stockOutRecordDto.setRemark(stockInventoryDto.getRemark());
+            stockOutRecordDto.setUnit(stockInventoryDto.getUnit());
+            stockOutRecordDto.setType("0");
+        }
+        stockOutRecordService.add(stockOutRecordDto);
     }
 
     @Override

--
Gitblit v1.9.3