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/StockInRecordServiceImpl.java |  267 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 236 insertions(+), 31 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
index ad80bd5..8784706 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -1,16 +1,23 @@
 package com.ruoyi.stock.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.mapper.ProductModelMapper;
+import com.ruoyi.basic.pojo.ProductModel;
+import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
 import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.EnumUtil;
 import com.ruoyi.common.utils.OrderUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
-import com.ruoyi.staff.pojo.StaffOnJob;
+import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.stock.dto.StockInRecordDto;
 import com.ruoyi.stock.dto.StockInventoryDto;
 import com.ruoyi.stock.dto.StockUninventoryDto;
+import com.ruoyi.stock.execl.StockInRecordExportData;
 import com.ruoyi.stock.mapper.StockInRecordMapper;
 import com.ruoyi.stock.mapper.StockInventoryMapper;
 import com.ruoyi.stock.mapper.StockUninventoryMapper;
@@ -18,20 +25,29 @@
 import com.ruoyi.stock.pojo.StockInventory;
 import com.ruoyi.stock.pojo.StockUninventory;
 import com.ruoyi.stock.service.StockInRecordService;
+import com.ruoyi.stock.word.WeighbridgeDocGenerator;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.http.HttpServletResponse;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.List;
 
 @Service
 public class StockInRecordServiceImpl extends ServiceImpl<StockInRecordMapper, StockInRecord> implements StockInRecordService {
+
     @Autowired
     private StockInRecordMapper stockInRecordMapper;
     @Autowired
     private StockInventoryMapper stockInventoryMapper;
     @Autowired
     private StockUninventoryMapper stockUninventoryMapper;
+    @Autowired
+    private WeighbridgeDocGenerator weighbridgeDocGenerator;
+    @Autowired
+    private ProductModelMapper productModelMapper;
 
     @Override
     public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
@@ -51,45 +67,234 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public int update(Long id, StockInRecordDto stockInRecordDto) {
-        // 鍒ゆ柇瀵硅薄鏄惁瀛樺湪
-        StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
-        if (stockInRecord == null){
-            throw new BaseException("璇ュ叆搴撹褰曚笉瀛樺湪,鏃犳硶鏇存柊!!!");
-        }
-
-        String[] ignoreProperties = {"id", "inbound_batches"};//鎺掗櫎id灞炴��
-        BeanUtils.copyProperties(stockInRecordDto, stockInRecord, ignoreProperties);
-        return stockInRecordMapper.updateById(stockInRecord);
-    }
-
-    @Override
-    @Transactional(rollbackFor = Exception.class)
     public int batchDelete(List<Long> ids) {
         for (Long id : ids) {
             StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
-            if (stockInRecord.getType().equals("0")) {
-                StockInventory stockInventory = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>().eq(StockInventory::getProductModelId, stockInRecord.getProductModelId()));
+            if (stockInRecord == null) {
+                throw new BaseException("鍏ュ簱璁板綍涓嶅瓨鍦紝ID锛�" + id);
+            }
+
+            // 鏌ヨ浜у搧鍨嬪彿锛堢敤浜庡崟浣嶈浆鎹級
+            ProductModel productModel = productModelMapper.selectById(stockInRecord.getProductModelId());
+            if (productModel == null) {
+                throw new BaseException("浜у搧鍨嬪彿涓嶅瓨鍦紝鏃犳硶鍒犻櫎");
+            }
+
+            // 鑾峰彇鍏ュ簱鏁伴噺鍙婂叾鍗曚綅锛岃浆鎹负搴撳瓨鍗曚綅
+            BigDecimal stockInNum = stockInRecord.getStockInNum();  // 鍏ュ簱璁板綍涓瓨鍌ㄧ殑鏁伴噺
+            String sourceUnit = stockInRecord.getUnit();  // 鍏ュ簱璁板綍鐨勫崟浣�
+            String stockUnit = productModel.getUnit();  // 搴撳瓨鍗曚綅
+
+            // 灏嗗叆搴撴暟閲忚浆鎹负搴撳瓨鍗曚綅
+            BigDecimal convertedQuantity = convertQuantityToStockUnit(stockInNum, sourceUnit, stockUnit);
+
+            // 鏍规嵁鍏ュ簱绫诲瀷澶勭悊涓嶅悓鐨勫簱瀛樿〃
+            if ("0".equals(stockInRecord.getType())) {
+                // 姝e父鍏ュ簱锛屽垹闄ゆ椂鎵e噺搴撳瓨
+                StockInventory stockInventory = stockInventoryMapper.selectOne(
+                        new LambdaQueryWrapper<StockInventory>()
+                                .eq(StockInventory::getProductModelId, stockInRecord.getProductModelId())
+                );
+
                 if (stockInventory == null) {
-                    throw new BaseException("搴撳瓨璁板綍涓病鏈夊搴旂殑浜у搧,鏃犳硶鍒犻櫎!!!");
-                }else {
-                    StockInventoryDto stockInRecordDto = new StockInventoryDto();
-                    stockInRecordDto.setProductModelId(stockInventory.getProductModelId());
-                    stockInRecordDto.setQualitity(stockInRecord.getStockInNum());
-                    stockInventoryMapper.updateAddStockInventory(stockInRecordDto);
+                    throw new BaseException("搴撳瓨璁板綍涓病鏈夊搴旂殑浜у搧锛屾棤娉曞垹闄�");
                 }
-            }else if (stockInRecord.getType().equals("1")) {
-                StockUninventory stockUninventory = stockUninventoryMapper.selectOne(new LambdaQueryWrapper<StockUninventory>().eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()));
+
+                StockInventoryDto inventoryDto = new StockInventoryDto();
+                inventoryDto.setProductModelId(stockInventory.getProductModelId());
+                inventoryDto.setQualitity(convertedQuantity);
+
+                // 鎵e噺搴撳瓨锛堝垹闄ゅ叆搴撹褰曠浉褰撲簬鍑忓皯搴撳瓨锛�
+                int affectedRows = stockInventoryMapper.updateSubtractStockInventory(inventoryDto);
+                if (affectedRows == 0) {
+                    throw new BaseException("鎵e噺搴撳瓨澶辫触锛屽簱瀛樺彲鑳戒笉瓒�");
+                }
+
+            } else if ("1".equals(stockInRecord.getType())) {
+                // 闈炴甯稿叆搴擄紝鍒犻櫎鏃舵墸鍑忛潪姝e父搴撳瓨
+                StockUninventory stockUninventory = stockUninventoryMapper.selectOne(
+                        new LambdaQueryWrapper<StockUninventory>()
+                                .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId())
+                );
+
                 if (stockUninventory == null) {
-                    throw new BaseException("搴撳瓨璁板綍涓病鏈夊搴旂殑浜у搧,鏃犳硶鍒犻櫎!!!");
-                }else {
-                    StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
-                    stockUninventoryDto.setProductModelId(stockUninventory.getProductModelId());
-                    stockUninventoryDto.setQualitity(stockInRecord.getStockInNum());
-                    stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
+                    throw new BaseException("闈炴甯稿簱瀛樿褰曚腑娌℃湁瀵瑰簲鐨勪骇鍝侊紝鏃犳硶鍒犻櫎");
+                }
+
+                // 鍒涘缓闈炴甯稿簱瀛樻洿鏂癉TO
+                StockUninventoryDto uninventoryDto = new StockUninventoryDto();
+                uninventoryDto.setProductModelId(stockUninventory.getProductModelId());
+                uninventoryDto.setQualitity(convertedQuantity);
+
+                // 鎵e噺闈炴甯稿簱瀛�
+                int affectedRows = stockUninventoryMapper.updateSubtractStockUnInventory(uninventoryDto);
+                if (affectedRows == 0) {
+                    throw new BaseException("鎵e噺闈炴甯稿簱瀛樺け璐�");
                 }
             }
         }
+
+        // 5. 鎵归噺鍒犻櫎鍏ュ簱璁板綍
         return stockInRecordMapper.deleteBatchIds(ids);
     }
+
+    @Override
+    public void exportStockInRecord(HttpServletResponse response, StockInRecordDto stockInRecordDto) {
+        List<StockInRecordExportData> list = stockInRecordMapper.listStockInRecordExportData(stockInRecordDto);
+        for (StockInRecordExportData stockInRecordExportData : list) {
+            if (stockInRecordExportData.getType().equals("0")) {
+                stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockOutQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue());
+            }else {
+                stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockInUnQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue());
+            }
+        }
+        ExcelUtil<StockInRecordExportData> util = new ExcelUtil<>(StockInRecordExportData.class);
+        util.exportExcel(response,list, "鍏ュ簱璁板綍淇℃伅");
+    }
+
+    @Override
+    public int editStockInStock(StockInRecordDto stockInRecordDto) {
+        if (stockInRecordDto.getId() == null) {
+            throw new BaseException("鍏ュ簱璁板綍ID涓嶈兘涓虹┖");
+        }
+
+        // 1. 鏌ヨ鍘熷鍏ュ簱璁板綍
+        StockInRecord originalRecord = stockInRecordMapper.selectById(stockInRecordDto.getId());
+        if (originalRecord == null) {
+            throw new BaseException("鍏ュ簱璁板綍涓嶅瓨鍦�");
+        }
+
+        // 2. 鏌ヨ浜у搧鍨嬪彿淇℃伅锛堢敤浜庡崟浣嶈浆鎹級
+        ProductModel productModel = productModelMapper.selectById(originalRecord.getProductModelId());
+        if (productModel == null) {
+            throw new BaseException("浜у搧鍨嬪彿涓嶅瓨鍦�");
+        }
+
+        // 3. 鑾峰彇鍘熷鍏ュ簱鏁伴噺鍙婂叾鍗曚綅
+        BigDecimal originalNetWeight = originalRecord.getStockInNum();  // 鍘熷璁板綍涓瓨鍌ㄧ殑鏁伴噺
+        String originalUnit = originalRecord.getUnit();  // 鍘熷璁板綍鐨勫崟浣�
+        String stockUnit = productModel.getUnit();  // 搴撳瓨鍗曚綅
+
+        // 灏嗗師濮嬪叆搴撴暟閲忚浆鎹负搴撳瓨鍗曚綅
+        BigDecimal originalNetWeightInStockUnit = convertQuantityToStockUnit(originalNetWeight, originalUnit, stockUnit);
+
+        // 4. 鑾峰彇鏂板叆搴撴暟閲忓強鍏跺崟浣�
+        BigDecimal newNetWeight = stockInRecordDto.getNetWeight();  // 鏂拌緭鍏ョ殑鏁伴噺
+        if (newNetWeight == null) {
+            throw new BaseException("鍑�閲嶄笉鑳戒负绌�");
+        }
+        String newUnit = stockInRecordDto.getUnit();  // 鏂拌緭鍏ョ殑鍗曚綅
+
+        // 灏嗘柊鍏ュ簱鏁伴噺杞崲涓哄簱瀛樺崟浣�
+        BigDecimal newNetWeightInStockUnit = convertQuantityToStockUnit(newNetWeight, newUnit, stockUnit);
+
+        // 5. 璁$畻搴撳瓨鍙樺寲閲忥紙浣跨敤缁熶竴杞崲鍚庣殑搴撳瓨鍗曚綅鍊硷級
+        // 鍏ュ簱鏁伴噺鍙樺寲閲忥紙姝f暟琛ㄧず鍏ュ簱鍙樺锛岄渶瑕佸鍔犲簱瀛橈紱璐熸暟琛ㄧず鍏ュ簱鍙樺皯锛岄渶瑕佸噺灏戝簱瀛橈級
+        BigDecimal diffQuantity = newNetWeightInStockUnit.subtract(originalNetWeightInStockUnit);
+
+        // 6. 鏇存柊鍏ュ簱璁板綍锛堜繚瀛樼敤鎴疯緭鍏ョ殑鍘熷鏁板�煎拰鍗曚綅锛�
+        stockInRecordDto.setStockInNum(stockInRecordDto.getNetWeight());
+        stockInRecordDto.setUnit(stockInRecordDto.getUnit());
+
+        // 7. 鐢熸垚纾呭崟
+        String absoluteDocPath = weighbridgeDocGenerator.generateWeighbridgeDoc(stockInRecordDto);
+        stockInRecordDto.setWeighbridgeDocPath(absoluteDocPath);
+        int updateResult = stockInRecordMapper.updateById(stockInRecordDto);
+
+        // 8. 鏇存柊搴撳瓨锛堜粎鍦ㄦ暟閲忓彂鐢熷彉鍖栨椂锛�
+        if (diffQuantity.compareTo(BigDecimal.ZERO) != 0) {
+            updateInventory(stockInRecordDto.getProductId(), diffQuantity);
+        }
+
+        return updateResult;
+    }
+
+    /**
+     * 灏嗘暟閲忚浆鎹负搴撳瓨鍗曚綅
+     * @param quantity 鍘熷鏁伴噺
+     * @param sourceUnit 鍘熷鍗曚綅
+     * @param targetUnit 鐩爣鍗曚綅锛堝簱瀛樺崟浣嶏級
+     * @return 杞崲鍚庣殑鏁伴噺
+     */
+    private BigDecimal convertQuantityToStockUnit(BigDecimal quantity, String sourceUnit, String targetUnit) {
+        if (quantity == null) {
+            throw new BaseException("鏁伴噺涓嶈兘涓虹┖");
+        }
+
+        if (sourceUnit == null || sourceUnit.isEmpty()) {
+            return quantity;
+        }
+
+        if (sourceUnit.equals(targetUnit)) {
+            return quantity;
+        }
+
+        // 鍗曚綅杞崲
+        if ("鍚�".equals(targetUnit)) {
+            // 鐩爣鍗曚綅鏄惃锛岄渶瑕佸皢婧愬崟浣嶈浆鎹负鍚�
+            if ("鍏枻".equals(sourceUnit)) {
+                // 鍏枻杞惃锛氶櫎浠�1000
+                return quantity.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
+            } else if ("鍏�".equals(sourceUnit)) {
+                // 鍏嬭浆鍚細闄や互1000000
+                return quantity.divide(BigDecimal.valueOf(1000000), 2, RoundingMode.HALF_UP);
+            }
+        } else if ("鍏枻".equals(targetUnit)) {
+            // 鐩爣鍗曚綅鏄叕鏂�
+            if ("鍚�".equals(sourceUnit)) {
+                // 鍚ㄨ浆鍏枻锛氫箻浠�1000
+                return quantity.multiply(BigDecimal.valueOf(1000));
+            } else if ("鍏�".equals(sourceUnit)) {
+                // 鍏嬭浆鍏枻锛氶櫎浠�1000
+                return quantity.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
+            }
+        } else if ("鍏�".equals(targetUnit)) {
+            // 鐩爣鍗曚綅鏄厠
+            if ("鍚�".equals(sourceUnit)) {
+                // 鍚ㄨ浆鍏嬶細涔樹互1000000
+                return quantity.multiply(BigDecimal.valueOf(1000000));
+            } else if ("鍏枻".equals(sourceUnit)) {
+                // 鍏枻杞厠锛氫箻浠�1000
+                return quantity.multiply(BigDecimal.valueOf(1000));
+            }
+        }
+
+        return quantity;
+    }
+
+    /**
+     * 鏇存柊搴撳瓨
+     * @param productId 浜у搧ID
+     * @param diffQuantity 搴撳瓨鍙樺寲閲忥紙姝f暟涓哄鍔狅紝璐熸暟涓哄噺灏戯級
+     */
+    private void updateInventory(Long productId, BigDecimal diffQuantity) {
+        // 鏌ヨ褰撳墠搴撳瓨
+        StockInventory stockInventory = stockInventoryMapper.selectOne(
+                new QueryWrapper<StockInventory>().lambda()
+                        .eq(StockInventory::getProductId, productId)
+        );
+
+        if (stockInventory == null) {
+            throw new BaseException("搴撳瓨璁板綍涓嶅瓨鍦�");
+        }
+
+        StockInventoryDto inventoryDto = new StockInventoryDto();
+        BeanUtils.copyProperties(stockInventory, inventoryDto);
+        inventoryDto.setQualitity(diffQuantity.abs());
+
+        // 鏍规嵁鍙樺寲閲忔璐熻皟鐢ㄤ笉鍚岀殑鏇存柊鏂规硶
+        if (diffQuantity.compareTo(BigDecimal.ZERO) > 0) {
+            // 澧炲姞搴撳瓨
+            int affectedRows = stockInventoryMapper.updateAddStockInventory(inventoryDto);
+            if (affectedRows == 0) {
+                throw new BaseException("鏇存柊搴撳瓨澶辫触");
+            }
+        } else {
+            // 鍑忓皯搴撳瓨
+            int affectedRows = stockInventoryMapper.updateSubtractStockInventory(inventoryDto);
+            if (affectedRows == 0) {
+                throw new BaseException("搴撳瓨涓嶈冻锛屾棤娉曟墸鍑�");
+            }
+        }
+    }
 }

--
Gitblit v1.9.3