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 |  158 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 120 insertions(+), 38 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 2f6d54c..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,7 +11,9 @@
 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;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.stock.dto.StockInRecordDto;
@@ -34,7 +36,9 @@
 
 import javax.servlet.http.HttpServletResponse;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.List;
+import java.util.Optional;
 
 /**
  * <p>
@@ -55,6 +59,7 @@
     private final WeighbridgeDocGenerator weighbridgeDocGenerator;
     private final ProductMapper productMapper;
     private final ProductModelMapper productModelMapper;
+    private final ElectricityConsumptionAreaMapper electricityConsumptionAreaMapper;
 
     @Override
     public IPage<StockInventoryDto> pagestockInventory(Page page, StockInventoryDto stockInventoryDto) {
@@ -70,10 +75,13 @@
         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());
             stockInRecordDto.setProductModelId(stockInventoryDto.getProductModelId());
-        }else {
+            modelId = stockInventoryDto.getProductModelId();
+        } else {
             Product parent = productMapper.selectOne(new LambdaQueryWrapper<Product>().eq(Product::getProductName, "鍘熸潗鏂�").last("limit 1"));
             if (parent == null) {
                 throw new RuntimeException("鍘熸潗鏂欏垎绫讳笉瀛樺湪");
@@ -120,6 +128,8 @@
             stockInRecordDto.setProductId(product.getId());
             stockInRecordDto.setProductModelId(productModelId);
             stockInventoryDto.setProductModelId(productModelId);
+
+            modelId = productModelId;
         }
         stockInRecordDto.setRemark(stockInventoryDto.getRemark());
         stockInRecordDto.setType("0");
@@ -132,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);
@@ -148,12 +159,13 @@
                 new QueryWrapper<StockInventory>().lambda()
                         .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId())
         );
+        ProductModel productModel = productModelMapper.selectById(modelId);
+        BigDecimal weight = compareUnit(stockInventoryDto, productModel);
         if (ObjectUtils.isEmpty(oldStockInventory)) {
             // 鏂板搴撳瓨
             StockInventory newStockInventory = new StockInventory();
             newStockInventory.setProductModelId(stockInventoryDto.getProductModelId());
-            newStockInventory.setQualitity(stockInventoryDto.getProductType() != null && stockInventoryDto.getProductType() == 0 ?
-                    stockInventoryDto.getNetWeight() : stockInventoryDto.getQualitity());
+            newStockInventory.setQualitity(weight);
             newStockInventory.setVersion(1);
             newStockInventory.setRemark(stockInventoryDto.getRemark());
             newStockInventory.setLockedQuantity(stockInventoryDto.getLockedQuantity());
@@ -162,8 +174,7 @@
             stockInventoryMapper.insert(newStockInventory);
         } else {
             // 鏇存柊搴撳瓨
-            stockInventoryDto.setQualitity(stockInventoryDto.getProductType() != null && stockInventoryDto.getProductType() == 0 ?
-                    stockInventoryDto.getNetWeight() : stockInventoryDto.getQualitity());
+            stockInventoryDto.setQualitity(weight);
             stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
         }
         return true;
@@ -173,41 +184,112 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean subtractStockInventory(StockInventoryDto stockInventoryDto) {
-        //  鏂板鍑哄簱璁板綍
-        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;
-        }
-        if (stockInventoryDto.getQualitity().compareTo(oldStockInventory.getQualitity().subtract(lockedQty)) > 0) {
-            throw new RuntimeException("搴撳瓨涓嶈冻鏃犳硶鍑哄簱");
+
+        // 1. 鏌ヨ浜у搧鍨嬪彿鍜屽簱瀛樹俊鎭�
+        ProductModel productModel = productModelMapper.selectById(stockInventoryDto.getProductModelId());
+        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) {
+        // 鑾峰彇鍘熷鍑哄簱鏁伴噺锛堟牴鎹骇鍝佺被鍨嬮�夋嫨鍑�閲嶆垨鏁伴噺锛�
+        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;
+        }
+
+        // 鍗曚綅杞崲
+        if ("鍚�".equals(targetUnit)) {
+            // 杞崲涓哄惃
+            return originalQuantity.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
+        } else if ("鍏枻".equals(targetUnit)) {
+            // 杞崲涓哄叕鏂�
+            return originalQuantity.multiply(BigDecimal.valueOf(1000));
+        } else {
+            // 鍗曚綅涓嶅尮閰嶆椂锛岃繑鍥炲師鍊�
+            return originalQuantity;
+        }
+    }
+
+    /**
+     * 鍒涘缓骞朵繚瀛樺嚭搴撹褰�
+     */
+    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
@@ -235,7 +317,7 @@
                 stockInventoryDto.setModel(dto.getModel());
                 stockInventoryDto.setUnit(dto.getUnit());
 
-                // 瑙e喅鏂规1锛氶�氳繃浠g悊瀵硅薄璋冪敤
+                //閫氳繃浠g悊瀵硅薄璋冪敤
                 ((StockInventoryService) AopContext.currentProxy()).addstockInventory(stockInventoryDto);
             }
             return R.ok("瀵煎叆鎴愬姛");

--
Gitblit v1.9.3