From 4d03835d9ab44054e489636ad71d8be8ca266ac9 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期四, 14 五月 2026 16:35:30 +0800
Subject: [PATCH] 修改销售台账后根据批次回滚库存,多个批次会有多个库存记录

---
 src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java |  137 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 121 insertions(+), 16 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 6fdef58..6dfdc89 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
@@ -1,5 +1,6 @@
 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.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -8,6 +9,8 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl;
 import com.ruoyi.approve.vo.ApproveProcessVO;
+import com.ruoyi.basic.mapper.ProductModelMapper;
+import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.framework.security.LoginUser;
@@ -22,14 +25,17 @@
 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 org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.servlet.http.HttpServletResponse;
 import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
 import java.util.Date;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * <p>
@@ -40,13 +46,18 @@
  * @since 2026-01-22 10:17:45
  */
 @Service
-@AllArgsConstructor
 public class StockUninventoryServiceImpl extends ServiceImpl<StockUninventoryMapper, StockUninventory> implements StockUninventoryService {
 
+    @Autowired
     private StockUninventoryMapper stockUninventoryMapper;
+    @Autowired
     private StockOutRecordService stockOutRecordService;
+    @Autowired
     private StockInRecordService stockInRecordService;
+    @Autowired
     private ApproveProcessServiceImpl approveProcessService;
+    @Autowired
+    private ProductModelMapper productModelMapper;
 
     @Override
     public IPage<StockUninventoryDto> pageStockUninventory(Page page, StockUninventoryDto stockUninventoryDto) {
@@ -56,6 +67,7 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Integer addStockUninventory(StockUninventoryDto stockUninventoryDto) {
+        List<StockUninventory> stockUninventoryList = stockUninventoryMapper.selectList(null);
         //鏂板鍏ュ簱璁板綍鍐嶆坊鍔犲簱瀛�
         StockInRecordDto stockInRecordDto = new StockInRecordDto();
         stockInRecordDto.setRecordId(stockUninventoryDto.getRecordId());
@@ -65,6 +77,26 @@
         stockInRecordDto.setRemark(stockUninventoryDto.getRemark());
         stockInRecordDto.setApproveStatus(0);
         stockInRecordDto.setType("1");
+        if (stockUninventoryDto.getBatchNo() == null || stockUninventoryDto.getBatchNo().isEmpty()) {
+            String batchNo;
+            // 鑾峰彇褰撳墠鏈堜唤锛堜袱浣嶏級
+            LocalDate now = LocalDate.now();
+            String monthFlag = now.format(DateTimeFormatter.ofPattern("MM"));
+
+            // 鑾峰彇褰撳墠鏈堜唤鐨勬渶澶ф祦姘村彿
+            int maxSeq = getCurrentMonthMaxSeq(stockUninventoryDto, monthFlag, stockUninventoryList);
+
+            // 鏂版祦姘村彿 = 鏈�澶ф祦姘村彿 + 1
+            int newSeq = maxSeq + 1;
+            String seqStr = String.format("%03d", newSeq);
+
+            // 缁勮batchNo
+            ProductModel productModel = productModelMapper.selectById(stockUninventoryDto.getProductModelId());
+            batchNo = stockUninventoryDto.getMaterialCode() + productModel.getModel() + "P" + monthFlag + seqStr;
+            stockInRecordDto.setBatchNo(batchNo);
+        } else {
+            stockInRecordDto.setBatchNo(stockUninventoryDto.getBatchNo());
+        }
         Long id = stockInRecordService.add(stockInRecordDto);
 
         LoginUser loginUser = SecurityUtils.getLoginUser();
@@ -77,6 +109,33 @@
         }
 
         return 1;
+    }
+
+    /**
+     * 鏌ヨ褰撳墠鏈堜唤宸插瓨鍦ㄧ殑鏈�澶ф祦姘村彿
+     */
+    private static int getCurrentMonthMaxSeq(StockUninventoryDto dto, String monthFlag, List<StockUninventory> existingList) {
+        int maxSeq = 0;
+
+        String prefix = dto.getMaterialCode() + dto.getModel() + "P" + monthFlag;
+
+        // 姝e垯鍖归厤锛氬墠缂� + 3浣嶆暟瀛�
+        Pattern pattern = Pattern.compile(Pattern.quote(prefix) + "(\\d{3})");
+
+        for (StockUninventory item : existingList) {
+            String batchNo = item.getBatchNo();
+            if (batchNo == null) continue;
+
+            Matcher matcher = pattern.matcher(batchNo);
+            if (matcher.find()) {
+                int seq = Integer.parseInt(matcher.group(1));
+                if (seq > maxSeq) {
+                    maxSeq = seq;
+                }
+            }
+        }
+
+        return maxSeq;
     }
 
     public void addApproveByPurchase(LoginUser loginUser, StockInRecordDto stockInRecordDto,Long id) throws Exception {
@@ -93,6 +152,32 @@
         approveProcessService.addApprove(approveProcessVO);
     }
 
+    @Override
+    public Integer addStockUninventoryNoReview(StockUninventoryDto stockUninventoryDto) {
+        //鏂板鍏ュ簱璁板綍鍐嶆坊鍔犲簱瀛�
+        StockInRecordDto stockInRecordDto = new StockInRecordDto();
+        stockInRecordDto.setRecordId(stockUninventoryDto.getRecordId());
+        stockInRecordDto.setRecordType(stockUninventoryDto.getRecordType());
+        stockInRecordDto.setStockInNum(stockUninventoryDto.getQualitity());
+        stockInRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
+        stockInRecordDto.setType("1");
+        stockInRecordService.add(stockInRecordDto);
+        //鍐嶈繘琛屾柊澧炲簱瀛樻暟閲忓簱瀛�
+        //鍏堟煡璇㈠簱瀛樿〃涓殑浜у搧鏄惁瀛樺湪锛屼笉瀛樺湪鏂板锛屽瓨鍦ㄦ洿鏂�
+        StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne(new QueryWrapper<StockUninventory>().lambda().eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId()));
+        if (ObjectUtils.isEmpty(oldStockUnInventory)) {
+            StockUninventory newStockUnInventory = new StockUninventory();
+            newStockUnInventory.setProductModelId(stockUninventoryDto.getProductModelId());
+            newStockUnInventory.setQualitity(stockUninventoryDto.getQualitity());
+            newStockUnInventory.setVersion(1);
+            newStockUnInventory.setRemark(stockUninventoryDto.getRemark());
+            stockUninventoryMapper.insert(newStockUnInventory);
+        }else {
+            stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
+        }
+        return 1;
+    }
+
     /**
      * 鏇存柊鎴栧垱寤洪潪鑹搧搴撳瓨淇℃伅
      *
@@ -100,10 +185,17 @@
      */
     public void updateOrCreateStockUninventory(StockInRecord stockInRecord) {
         // 鍏堟煡璇㈠簱瀛樿〃涓殑浜у搧鏄惁瀛樺湪
-        StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne(
-                new QueryWrapper<StockUninventory>().lambda()
-                        .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId())
-        );
+        LambdaQueryWrapper<StockUninventory> queryWrapper = new QueryWrapper<StockUninventory>().lambda()
+                .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId());
+
+        // 鏍规嵁 batchNo 鏄惁涓虹┖鏋勫缓涓嶅悓鐨勬煡璇㈡潯浠�
+        if (stockInRecord.getBatchNo() != null && !stockInRecord.getBatchNo().isEmpty()) {
+            queryWrapper.eq(StockUninventory::getBatchNo, stockInRecord.getBatchNo());
+        } else {
+            queryWrapper.isNull(StockUninventory::getBatchNo);
+        }
+
+        StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne(queryWrapper);
 
         if (ObjectUtils.isEmpty(oldStockUnInventory)) {
             // 涓嶅瓨鍦ㄥ垯鏂板
@@ -112,19 +204,32 @@
             newStockUnInventory.setQualitity(stockInRecord.getStockInNum());
             newStockUnInventory.setVersion(1);
             newStockUnInventory.setRemark(stockInRecord.getRemark());
+            newStockUnInventory.setLockedQuantity(stockInRecord.getLockedQuantity());
+            newStockUnInventory.setBatchNo(stockInRecord.getBatchNo());
             stockUninventoryMapper.insert(newStockUnInventory);
         } else {
+            // 瀛樺湪鍒欐洿鏂�
             LambdaUpdateWrapper<StockUninventory> updateWrapper = new LambdaUpdateWrapper<>();
-            if (stockInRecord.getStockInNum() != null) {
-                updateWrapper.setSql("qualitity = qualitity + " + stockInRecord.getStockInNum());
+            updateWrapper
+                    .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId());
+
+            // 鏍规嵁 batchNo 鏄惁涓虹┖鏋勫缓涓嶅悓鐨勬洿鏂版潯浠�
+            if (stockInRecord.getBatchNo() != null && !stockInRecord.getBatchNo().isEmpty()) {
+                updateWrapper.eq(StockUninventory::getBatchNo, stockInRecord.getBatchNo());
+            } else {
+                updateWrapper.isNull(StockUninventory::getBatchNo);
             }
-            updateWrapper.setSql("version = version + 1");
-            String remark = stockInRecord.getRemark();
-            if (remark != null && !remark.isEmpty()) {
-                updateWrapper.set(StockUninventory::getRemark, remark);
-            }
-            updateWrapper.set(StockUninventory::getUpdateTime, new Date());
-            updateWrapper.eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId());
+
+            updateWrapper
+                    .setSql(stockInRecord.getStockInNum() != null,
+                            "qualitity = qualitity + " + stockInRecord.getStockInNum())
+                    .setSql(true, "version = version + 1")
+                    .set(stockInRecord.getRemark() != null && !stockInRecord.getRemark().isEmpty(),
+                            StockUninventory::getRemark, stockInRecord.getRemark())
+                    .setSql(stockInRecord.getLockedQuantity() != null,
+                            "locked_quantity = locked_quantity + " + stockInRecord.getLockedQuantity())
+                    .set(StockUninventory::getUpdateTime, new Date());
+
             stockUninventoryMapper.update(null, updateWrapper);
         }
     }
@@ -180,4 +285,4 @@
         stockUninventory.setLockedQuantity(stockUninventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
         return this.updateById(stockUninventory);
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3