From f16bf325804fb3bc6ab883693a96a374b3c63188 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期四, 18 六月 2026 17:41:50 +0800
Subject: [PATCH] 不合格库存批号自动生成

---
 src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java |  272 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 230 insertions(+), 42 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
index 5ed894f..d738db1 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
@@ -4,30 +4,38 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 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.exception.ServiceException;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
+import com.ruoyi.stock.enums.StockInventorySourceEnum;
 import com.ruoyi.stock.dto.StockInRecordDto;
 import com.ruoyi.stock.dto.StockInventoryDto;
 import com.ruoyi.stock.dto.StockOutRecordDto;
 import com.ruoyi.stock.dto.StockUninventoryDto;
 import com.ruoyi.stock.execl.StockUnInventoryExportData;
+import com.ruoyi.stock.mapper.StockInventoryMapper;
 import com.ruoyi.stock.mapper.StockUninventoryMapper;
+import com.ruoyi.stock.pojo.StockInRecord;
 import com.ruoyi.stock.pojo.StockInventory;
 import com.ruoyi.stock.pojo.StockUninventory;
 import com.ruoyi.stock.service.StockInRecordService;
 import com.ruoyi.stock.service.StockOutRecordService;
 import com.ruoyi.stock.service.StockUninventoryService;
-import lombok.AllArgsConstructor;
-import org.springframework.beans.factory.annotation.Autowired;
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-
-import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
 import java.util.List;
 
 /**
@@ -39,14 +47,15 @@
  * @since 2026-01-22 10:17:45
  */
 @Service
+@RequiredArgsConstructor
 public class StockUninventoryServiceImpl extends ServiceImpl<StockUninventoryMapper, StockUninventory> implements StockUninventoryService {
 
-    @Autowired
-    private StockUninventoryMapper stockUninventoryMapper;
-    @Autowired
-    private StockOutRecordService stockOutRecordService;
-    @Autowired
-    private StockInRecordService stockInRecordService;
+    private static final String UNQUALIFIED_TYPE = "unqualified";
+    private final StockUninventoryMapper stockUninventoryMapper;
+    private final StockOutRecordService stockOutRecordService;
+    private final StockInRecordService stockInRecordService;
+    private final ProductModelMapper productModelMapper;
+    private final StockInventoryMapper stockInventoryMapper;
 
     @Override
     public IPage<StockUninventoryDto> pageStockUninventory(Page page, StockUninventoryDto stockUninventoryDto) {
@@ -54,46 +63,71 @@
     }
 
     @Override
+    public IPage<StockUninventoryDto> pageWasteQuery(Page page, StockUninventoryDto stockUninventoryDto) {
+        IPage<StockUninventoryDto> resultPage = stockUninventoryMapper.pageWasteQuery(page, stockUninventoryDto);
+        resultPage.getRecords().forEach(dto -> {
+            if (StringUtils.isNotEmpty(dto.getSource())) {
+                StockInventorySourceEnum sourceEnum = StockInventorySourceEnum.fromCode(dto.getSource());
+                if (sourceEnum != null) {
+                    dto.setSourceText(sourceEnum.getValue());
+                }
+            }
+        });
+        return resultPage;
+    }
+
+    @Override
+    public IPage<StockUninventoryDto> getWasteBatchNoQty(Page page, StockInventoryDto stockInventoryDto) {
+        IPage<StockUninventoryDto> resultPage = stockUninventoryMapper.getWasteBatchNoQty(page, stockInventoryDto);
+        resultPage.getRecords().forEach(dto -> {
+            if (StringUtils.isNotEmpty(dto.getSource())) {
+                StockInventorySourceEnum unQualifiedSourceEnum = StockInventorySourceEnum.fromCode(dto.getSource());
+                if (unQualifiedSourceEnum != null) {
+                    dto.setSourceText(unQualifiedSourceEnum.getValue());
+                }
+            }
+        });
+        return resultPage;
+    }
+
+    @Override
+    public List<StockUninventoryDto> getWasteByModelId(Long productModelId) {
+        return stockUninventoryMapper.getWasteByModelId(productModelId);
+    }
+
+    @Override
     @Transactional(rollbackFor = Exception.class)
     public Integer addStockUninventory(StockUninventoryDto stockUninventoryDto) {
-        LambdaQueryWrapper<StockUninventory> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId());
-        if (StringUtils.isEmpty(stockUninventoryDto.getBatchNo())) {
-            stockUninventoryDto.setBatchNo(null);
-            wrapper.isNull(StockUninventory::getBatchNo);
-        } else {
-            wrapper.eq(StockUninventory::getBatchNo, stockUninventoryDto.getBatchNo());
-        }
+        String inventoryType = resolveInventoryType(stockUninventoryDto);
+        stockUninventoryDto.setType(inventoryType);
+        String batchNo = resolveOrGenerateBatchNo(stockUninventoryDto.getBatchNo(), stockUninventoryDto.getProductModelId());
+        stockUninventoryDto.setBatchNo(batchNo);
         //鏂板鍏ュ簱璁板綍鍐嶆坊鍔犲簱瀛�
         StockInRecordDto stockInRecordDto = new StockInRecordDto();
         stockInRecordDto.setRecordId(stockUninventoryDto.getRecordId());
         stockInRecordDto.setRecordType(stockUninventoryDto.getRecordType());
         stockInRecordDto.setStockInNum(stockUninventoryDto.getQualitity());
-        stockInRecordDto.setBatchNo(stockUninventoryDto.getBatchNo());
+        stockInRecordDto.setBatchNo(batchNo);
         stockInRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
-        stockInRecordDto.setType("1");
-        stockInRecordService.add(stockInRecordDto);
-        //鍐嶈繘琛屾柊澧炲簱瀛樻暟閲忓簱瀛�
-        //鍏堟煡璇㈠簱瀛樿〃涓殑浜у搧鏄惁瀛樺湪锛屼笉瀛樺湪鏂板锛屽瓨鍦ㄦ洿鏂�
-        StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne(wrapper);
-        if (ObjectUtils.isEmpty(oldStockUnInventory)) {
-            StockUninventory newStockUnInventory = new StockUninventory();
-            newStockUnInventory.setProductModelId(stockUninventoryDto.getProductModelId());
-            newStockUnInventory.setQualitity(stockUninventoryDto.getQualitity());
-            newStockUnInventory.setLockedQuantity(stockUninventoryDto.getLockedQuantity());
-            newStockUnInventory.setBatchNo(stockUninventoryDto.getBatchNo());
-            newStockUnInventory.setVersion(1);
-            newStockUnInventory.setRemark(stockUninventoryDto.getRemark());
-            stockUninventoryMapper.insert(newStockUnInventory);
-        }else {
-            stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
+        if (UNQUALIFIED_TYPE.equals(inventoryType)) {
+            stockInRecordDto.setType("1");
+        } else {
+            stockInRecordDto.setType("2");
         }
+        stockInRecordDto.setManufacturerId(stockUninventoryDto.getManufacturerId());
+        stockInRecordDto.setSource(stockUninventoryDto.getSource());
+        stockInRecordService.add(stockInRecordDto);
+        //瀹℃壒鍐嶆坊鍔�
         return 1;
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Integer subtractStockUninventory(StockUninventoryDto stockUninventoryDto) {
+        String inventoryType = resolveInventoryType(stockUninventoryDto);
+        stockUninventoryDto.setType(inventoryType);
+        String batchNo = resolveOutboundBatchNo(stockUninventoryDto.getProductModelId(), inventoryType, stockUninventoryDto.getBatchNo());
+        stockUninventoryDto.setBatchNo(batchNo);
 
         //  鏂板鍑哄簱璁板綍
         StockOutRecordDto stockOutRecordDto = new StockOutRecordDto();
@@ -101,10 +135,18 @@
         stockOutRecordDto.setRecordType(stockUninventoryDto.getRecordType());
         stockOutRecordDto.setStockOutNum(stockUninventoryDto.getQualitity());
         stockOutRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
-        stockOutRecordDto.setBatchNo(stockUninventoryDto.getBatchNo());
+        stockOutRecordDto.setBatchNo(batchNo);
         stockOutRecordDto.setType("1");
         stockOutRecordService.add(stockOutRecordDto);
-        StockUninventory oldStockInventory = stockUninventoryMapper.selectOne(new QueryWrapper<StockUninventory>().lambda().eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId()));
+        LambdaQueryWrapper<StockUninventory> queryWrapper = new QueryWrapper<StockUninventory>().lambda()
+                .eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId())
+                .eq(StockUninventory::getType, inventoryType);
+        if (StringUtils.isEmpty(batchNo)) {
+            queryWrapper.isNull(StockUninventory::getBatchNo);
+        } else {
+            queryWrapper.eq(StockUninventory::getBatchNo, batchNo);
+        }
+        StockUninventory oldStockInventory = stockUninventoryMapper.selectOne(queryWrapper);
         if (ObjectUtils.isEmpty(oldStockInventory)) {
             throw new RuntimeException("浜у搧搴撳瓨涓嶅瓨鍦�");
         }else {
@@ -116,14 +158,22 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Integer addStockInRecordOnly(StockUninventoryDto stockUninventoryDto) {
+        String inventoryType = resolveInventoryType(stockUninventoryDto);
+        stockUninventoryDto.setType(inventoryType);
         StockInRecordDto stockInRecordDto = new StockInRecordDto();
         stockInRecordDto.setRecordId(stockUninventoryDto.getRecordId());
         stockInRecordDto.setRecordType(stockUninventoryDto.getRecordType());
         stockInRecordDto.setStockInNum(stockUninventoryDto.getQualitity());
-        stockInRecordDto.setBatchNo(stockUninventoryDto.getBatchNo());
+        String batchNo = resolveOrGenerateBatchNo(stockUninventoryDto.getBatchNo(), stockUninventoryDto.getProductModelId());
+        stockInRecordDto.setBatchNo(batchNo);
         stockInRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
-        stockInRecordDto.setType("1");
+        if (UNQUALIFIED_TYPE.equals(inventoryType)) {
+            stockInRecordDto.setType("1");
+        } else {
+            stockInRecordDto.setType("2");
+        }
         stockInRecordDto.setRemark(stockUninventoryDto.getRemark());
+        stockInRecordDto.setSource(stockUninventoryDto.getSource());
         stockInRecordService.add(stockInRecordDto);
         return 1;
     }
@@ -131,12 +181,17 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Integer addStockOutRecordOnly(StockUninventoryDto stockUninventoryDto) {
+        String inventoryType = resolveInventoryType(stockUninventoryDto);
+        stockUninventoryDto.setType(inventoryType);
+        String batchNo = resolveOutboundBatchNo(stockUninventoryDto.getProductModelId(), inventoryType, stockUninventoryDto.getBatchNo());
+        stockUninventoryDto.setBatchNo(batchNo);
         LambdaQueryWrapper<StockUninventory> eq = new LambdaQueryWrapper<>();
         eq.eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId());
-        if (StringUtils.isEmpty(stockUninventoryDto.getBatchNo())) {
+        eq.eq(StockUninventory::getType, inventoryType);
+        if (StringUtils.isEmpty(batchNo)) {
             eq.isNull(StockUninventory::getBatchNo);
         } else {
-            eq.eq(StockUninventory::getBatchNo, stockUninventoryDto.getBatchNo());
+            eq.eq(StockUninventory::getBatchNo, batchNo);
         }
         StockUninventory stockUninventory = stockUninventoryMapper.selectOne(eq);
         if (stockUninventory == null) {
@@ -162,7 +217,7 @@
         stockOutRecordDto.setRecordId(stockUninventoryDto.getRecordId());
         stockOutRecordDto.setRecordType(stockUninventoryDto.getRecordType());
         stockOutRecordDto.setStockOutNum(stockUninventoryDto.getQualitity());
-        stockOutRecordDto.setBatchNo(stockUninventoryDto.getBatchNo());
+        stockOutRecordDto.setBatchNo(batchNo);
         stockOutRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
         stockOutRecordDto.setType("1");
         stockOutRecordDto.setRemark(stockUninventoryDto.getRemark());
@@ -173,8 +228,31 @@
     @Override
     public void exportStockUninventory(HttpServletResponse response, StockUninventoryDto stockUninventoryDto) {
         List<StockUnInventoryExportData> list = stockUninventoryMapper.listStockInventoryExportData(stockUninventoryDto);
+        list.forEach(item -> {
+            if (StringUtils.isNotEmpty(item.getSource())) {
+                StockInventorySourceEnum sourceEnum = StockInventorySourceEnum.fromCode(item.getSource());
+                if (sourceEnum != null) {
+                    item.setSourceText(sourceEnum.getValue());
+                }
+            }
+        });
         ExcelUtil<StockUnInventoryExportData> util = new ExcelUtil<>(StockUnInventoryExportData.class);
         util.exportExcel(response,list, "涓嶅悎鏍煎簱瀛樹俊鎭�");
+    }
+
+    @Override
+    public void exportWasteQuery(HttpServletResponse response, StockUninventoryDto stockUninventoryDto) {
+        List<StockUnInventoryExportData> list = stockUninventoryMapper.listWasteQueryExportData(stockUninventoryDto);
+        list.forEach(item -> {
+            if (StringUtils.isNotEmpty(item.getSource())) {
+                StockInventorySourceEnum sourceEnum = StockInventorySourceEnum.fromCode(item.getSource());
+                if (sourceEnum != null) {
+                    item.setSourceText(sourceEnum.getValue());
+                }
+            }
+        });
+        ExcelUtil<StockUnInventoryExportData> util = new ExcelUtil<>(StockUnInventoryExportData.class);
+        util.exportExcel(response, list, "搴熷搧鏌ヨ淇℃伅");
     }
 
     @Override
@@ -200,4 +278,114 @@
         stockUninventory.setLockedQuantity(stockUninventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
         return this.updateById(stockUninventory);
     }
+
+    private String resolveInventoryType(StockUninventoryDto stockUninventoryDto) {
+        if (stockUninventoryDto != null && StringUtils.isNotEmpty(stockUninventoryDto.getType())) {
+            return stockUninventoryDto.getType().trim();
+        }
+        return UNQUALIFIED_TYPE;
+    }
+
+    //瑙勫垯鐢熸垚锛�20260424-浜у搧缂栧彿-001
+    private String generateAutoBatchNo(Long productModelId) {
+        if (productModelId == null) {
+            throw new ServiceException("浜у搧瑙勬牸ID涓嶈兘涓虹┖");
+        }
+        ProductModel productModel = productModelMapper.selectById(productModelId);
+        if (productModel == null) {
+            throw new ServiceException("浜у搧瑙勬牸涓嶅瓨鍦紝ID=" + productModelId);
+        }
+        String productCode = StringUtils.trim(productModel.getProductCode());
+        if (StringUtils.isEmpty(productCode)) {
+            throw new ServiceException("浜у搧瑙勬牸鏈淮鎶や骇鍝佺紪鐮侊紝ID=" + productModelId);
+        }
+
+        String dateText = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
+        String prefix = dateText + "-" + productCode + "-";
+        int maxSequence = resolveMaxSequence(prefix);
+        int sequence = maxSequence + 1;
+        while (sequence < 1_000_000) {
+            String batchNo = prefix + String.format("%03d", sequence);
+            if (!isBatchNoExists(batchNo)) {
+                return batchNo;
+            }
+            sequence++;
+        }
+        throw new ServiceException("鎵瑰彿搴忓彿瓒呭嚭鑼冨洿锛岃妫�鏌ユ壒鍙锋暟鎹�");
+    }
+
+    private int resolveMaxSequence(String prefix) {
+        int maxSequence = 0;
+        List<StockInventory> stockInventoryList = stockInventoryMapper.selectList(
+                Wrappers.<StockInventory>lambdaQuery()
+                        .select(StockInventory::getBatchNo)
+                        .likeRight(StockInventory::getBatchNo, prefix));
+        for (StockInventory stockInventory : stockInventoryList) {
+            maxSequence = Math.max(maxSequence, parseSequence(stockInventory.getBatchNo(), prefix));
+        }
+
+        List<StockInRecord> stockInRecordList = stockInRecordService.list(
+                Wrappers.<StockInRecord>lambdaQuery()
+                        .select(StockInRecord::getBatchNo)
+                        .likeRight(StockInRecord::getBatchNo, prefix));
+        for (StockInRecord stockInRecord : stockInRecordList) {
+            maxSequence = Math.max(maxSequence, parseSequence(stockInRecord.getBatchNo(), prefix));
+        }
+        return maxSequence;
+    }
+
+    private String resolveOrGenerateBatchNo(String batchNo, Long productModelId) {
+        String normalizedBatchNo = StringUtils.trim(batchNo);
+        if (StringUtils.isNotEmpty(normalizedBatchNo)) {
+            return normalizedBatchNo;
+        }
+        return generateAutoBatchNo(productModelId);
+    }
+
+    private String resolveOutboundBatchNo(Long productModelId, String inventoryType, String batchNo) {
+        String normalizedBatchNo = StringUtils.trim(batchNo);
+        if (StringUtils.isNotEmpty(normalizedBatchNo)) {
+            return normalizedBatchNo;
+        }
+        List<StockUninventory> stockUninventoryList = stockUninventoryMapper.selectList(
+                Wrappers.<StockUninventory>lambdaQuery()
+                        .eq(StockUninventory::getProductModelId, productModelId)
+                        .eq(StockUninventory::getType, inventoryType)
+                        .orderByDesc(StockUninventory::getId));
+        if (CollectionUtils.isEmpty(stockUninventoryList)) {
+            throw new BaseException("搴撳瓨璁板綍涓嶅瓨鍦�");
+        }
+        if (stockUninventoryList.size() == 1) {
+            return stockUninventoryList.get(0).getBatchNo();
+        }
+        throw new BaseException("褰撳墠浜у搧瑙勬牸瀛樺湪澶氫釜鎵规锛岃鍏堥�夋嫨鎵瑰彿鍚庡啀鍑哄簱");
+    }
+
+    private int parseSequence(String batchNo, String prefix) {
+        if (StringUtils.isEmpty(batchNo) || StringUtils.isEmpty(prefix) || !batchNo.startsWith(prefix)) {
+            return 0;
+        }
+        String sequenceText = batchNo.substring(prefix.length());
+        if (StringUtils.isEmpty(sequenceText) || !sequenceText.matches("\\d+")) {
+            return 0;
+        }
+        try {
+            return Integer.parseInt(sequenceText);
+        } catch (NumberFormatException ignored) {
+            return 0;
+        }
+    }
+
+    private boolean isBatchNoExists(String batchNo) {
+        if (StringUtils.isEmpty(batchNo)) {
+            return false;
+        }
+        Long inventoryCount = stockInventoryMapper.selectCount(
+                Wrappers.<StockInventory>lambdaQuery().eq(StockInventory::getBatchNo, batchNo));
+        if (inventoryCount != null && inventoryCount > 0) {
+            return true;
+        }
+        return stockInRecordService.count(
+                Wrappers.<StockInRecord>lambdaQuery().eq(StockInRecord::getBatchNo, batchNo)) > 0;
+    }
 }

--
Gitblit v1.9.3