From 76c84d95506998f546e6f3ebbf70414c0dd9da9d Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期五, 15 五月 2026 17:15:15 +0800
Subject: [PATCH] refactor(stock): 优化库存记录服务中的仓库信息处理逻辑

---
 src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java |   68 ++++++++++++++++++++++------------
 1 files changed, 44 insertions(+), 24 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 f84a9d8..94f853a 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -5,8 +5,8 @@
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.enums.ReviewStatusEnum;
+import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
 import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
-import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.EnumUtil;
 import com.ruoyi.common.utils.OrderUtils;
@@ -24,12 +24,13 @@
 import com.ruoyi.stock.pojo.StockInventory;
 import com.ruoyi.stock.pojo.StockUninventory;
 import com.ruoyi.stock.service.StockInRecordService;
+import jakarta.servlet.http.HttpServletResponse;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
 
-import jakarta.servlet.http.HttpServletResponse;
 import java.util.List;
 
 @Service
@@ -48,12 +49,13 @@
     // 鏂板鍏ュ簱
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public int add(StockInRecordDto stockInRecordDto) {
+    public Long add(StockInRecordDto stockInRecordDto) {
         String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK","inbound_batches");
         stockInRecordDto.setInboundBatches(no);
         StockInRecord stockInRecord = new StockInRecord();
         BeanUtils.copyProperties(stockInRecordDto, stockInRecord);
-        return stockInRecordMapper.insert(stockInRecord);
+        stockInRecordMapper.insert(stockInRecord);
+        return stockInRecord.getId();
     }
 
     @Override
@@ -76,13 +78,11 @@
         for (Long id : ids) {
             StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
             if (stockInRecord.getType().equals("0")) {
-                LambdaQueryWrapper<StockInventory> eq = new LambdaQueryWrapper<StockInventory>()
-                        .eq(StockInventory::getProductModelId, stockInRecord.getProductModelId());
-                if (StringUtils.isEmpty(stockInRecord.getBatchNo())) {
-                    eq.isNull(StockInventory::getBatchNo);
-                } else {
-                    eq.eq(StockInventory::getBatchNo, stockInRecord.getBatchNo());
-                }
+                LambdaQueryWrapper<StockInventory> eq = buildQualifiedInventoryQuery(
+                        stockInRecord.getProductModelId(),
+                        stockInRecord.getBatchNo(),
+                        stockInRecord.getWarehouseInfoId()
+               );
                 StockInventory stockInventory = stockInventoryMapper.selectOne(eq);
                 if (stockInventory == null) {
                     throw new BaseException("搴撳瓨璁板綍涓病鏈夊搴旂殑浜у搧,鏃犳硶鍒犻櫎!!!");
@@ -90,6 +90,7 @@
                     StockInventoryDto stockInRecordDto = new StockInventoryDto();
                     stockInRecordDto.setProductModelId(stockInventory.getProductModelId());
                     stockInRecordDto.setBatchNo(stockInventory.getBatchNo());
+                    stockInRecordDto.setWarehouseInfoId(stockInventory.getWarehouseInfoId());
                     stockInRecordDto.setQualitity(stockInRecord.getStockInNum());
                     stockInventoryMapper.updateSubtractStockInventory(stockInRecordDto);
                 }
@@ -120,25 +121,18 @@
     public void exportStockInRecord(HttpServletResponse response, StockInRecordDto stockInRecordDto) {
         List<StockInRecordExportData> list = stockInRecordMapper.listStockInRecordExportData(stockInRecordDto);
         for (StockInRecordExportData stockInRecordExportData : list) {
-            if (stockInRecordExportData.getType().equals("0")) {
+            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());
+                stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockInQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue());
             }
         }
         ExcelUtil<StockInRecordExportData> util = new ExcelUtil<>(StockInRecordExportData.class);
         util.exportExcel(response,list, "鍏ュ簱璁板綍淇℃伅");
     }
 
-    private StockInventory getStockInventory(Long productModelId, String batchNo) {
-        LambdaQueryWrapper<StockInventory> eq = new LambdaQueryWrapper<>();
-        eq.eq(StockInventory::getProductModelId, productModelId);
-        if (StringUtils.isEmpty(batchNo)) {
-            eq.isNull(StockInventory::getBatchNo);
-        } else {
-            eq.eq(StockInventory::getBatchNo, batchNo);
-        }
-        return stockInventoryMapper.selectOne(eq);
+    private StockInventory getStockInventory(Long productModelId, String batchNo, Long warehouseInfoId) {
+        return stockInventoryMapper.selectOne(buildQualifiedInventoryQuery(productModelId, batchNo, warehouseInfoId));
     }
 
     private StockUninventory getStockUninventory(Long productModelId, String batchNo) {
@@ -169,7 +163,7 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public int batchApprove(List<Long> ids, Integer approvalStatus) {
+    public int batchApprove(List<Long> ids, Integer approvalStatus,Long warehouseInfoId1) {
         if (CollectionUtils.isEmpty(ids)) {
             throw new BaseException("璇烽�夋嫨鑷冲皯涓�鏉℃暟鎹�");
         }
@@ -190,10 +184,20 @@
             if (ReviewStatusEnum.APPROVED.getCode().equals(approvalStatus)) {
                 if ("0".equals(stockInRecord.getType())) {
                     // 鍚堟牸鍏ュ簱 -> 鍏堟煡搴撳瓨锛屽瓨鍦ㄥ垯鏇存柊锛屼笉瀛樺湪鍒欐柊澧�
-                    StockInventory stockInventory = getStockInventory(stockInRecord.getProductModelId(), stockInRecord.getBatchNo());
+                    StockInventory stockInventory = getStockInventory(
+                            stockInRecord.getProductModelId(),
+                            stockInRecord.getBatchNo(),
+                            stockInRecord.getWarehouseInfoId()
+                    );
+                    if (!ObjectUtils.isEmpty(stockInventory)) {
+                        if (stockInventory.getLocked().equals( true)&&!stockInRecord.getRecordType().equals(StockInQualifiedRecordTypeEnum.INVENTORY_CHECK_STOCK_IN.getCode())) {
+                            throw new BaseException("姝e湪搴撳瓨鐩樼偣,鏃犳硶鍏ュ簱,鍏ュ簱鎵规:" + stockInRecord.getInboundBatches());
+                        }
+                    }
                     StockInventoryDto stockInventoryDto = new StockInventoryDto();
                     stockInventoryDto.setProductModelId(stockInRecord.getProductModelId());
                     stockInventoryDto.setBatchNo(stockInRecord.getBatchNo());
+                    stockInventoryDto.setWarehouseInfoId(stockInRecord.getWarehouseInfoId());
                     stockInventoryDto.setQualitity(stockInRecord.getStockInNum());
                     stockInventoryDto.setRemark(stockInRecord.getRemark());
                     if (stockInventory == null) {
@@ -202,6 +206,7 @@
                             setQualitity(stockInRecord.getStockInNum());
                             setBatchNo(stockInRecord.getBatchNo());
                             setRemark(stockInRecord.getRemark());
+                            setWarehouseInfoId(stockInRecord.getWarehouseInfoId());
                             setVersion(1);
                         }});
                     } else {
@@ -221,6 +226,7 @@
                             setQualitity(stockInRecord.getStockInNum());
                             setBatchNo(stockInRecord.getBatchNo());
                             setRemark(stockInRecord.getRemark());
+                            setWarehouseInfoId(stockInRecord.getWarehouseInfoId());
                             setVersion(1);
                         }});
                     } else {
@@ -231,4 +237,18 @@
         }
         return ids.size();
     }
+
+    private LambdaQueryWrapper<StockInventory> buildQualifiedInventoryQuery(Long productModelId, String batchNo, Long warehouseInfoId) {
+        LambdaQueryWrapper<StockInventory> wrapper = new LambdaQueryWrapper<StockInventory>()
+                .eq(StockInventory::getProductModelId, productModelId);
+        if (warehouseInfoId != null) {
+            wrapper.eq(StockInventory::getWarehouseInfoId, warehouseInfoId);
+        }
+        if (StringUtils.isEmpty(batchNo)) {
+            wrapper.isNull(StockInventory::getBatchNo);
+        } else {
+            wrapper.eq(StockInventory::getBatchNo, batchNo);
+        }
+        return wrapper;
+    }
 }

--
Gitblit v1.9.3