From c94ab7a91bfb3015d929a94837f3a45289e8bbf1 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 03 四月 2026 11:51:49 +0800
Subject: [PATCH] feat:采购/库存入库添加批号

---
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java |  158 ++++++++++++++++++++++++++++++++++------------------
 1 files changed, 102 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
index 2d72acd..a0afd96 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -1,29 +1,23 @@
 package com.ruoyi.purchase.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.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.account.pojo.AccountExpense;
-import com.ruoyi.account.pojo.AccountIncome;
 import com.ruoyi.account.service.AccountExpenseService;
-import com.ruoyi.account.service.AccountIncomeService;
 import com.ruoyi.approve.pojo.ApproveProcess;
 import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl;
 import com.ruoyi.approve.vo.ApproveProcessVO;
 import com.ruoyi.basic.mapper.ProductMapper;
 import com.ruoyi.basic.mapper.ProductModelMapper;
 import com.ruoyi.basic.mapper.SupplierManageMapper;
-import com.ruoyi.basic.pojo.Customer;
 import com.ruoyi.basic.pojo.Product;
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.basic.pojo.SupplierManage;
 import com.ruoyi.common.enums.FileNameType;
 import com.ruoyi.common.exception.base.BaseException;
-import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -39,19 +33,27 @@
 import com.ruoyi.purchase.dto.PurchaseLedgerImportDto;
 import com.ruoyi.purchase.dto.PurchaseLedgerProductImportDto;
 import com.ruoyi.purchase.mapper.*;
-import com.ruoyi.purchase.pojo.*;
+import com.ruoyi.purchase.pojo.PaymentRegistration;
+import com.ruoyi.purchase.pojo.ProductRecord;
+import com.ruoyi.purchase.pojo.PurchaseLedger;
+import com.ruoyi.purchase.pojo.TicketRegistration;
 import com.ruoyi.purchase.service.IPurchaseLedgerService;
 import com.ruoyi.quality.mapper.*;
-import com.ruoyi.quality.pojo.*;
-import com.ruoyi.sales.dto.SalesLedgerImportDto;
-import com.ruoyi.sales.dto.SalesLedgerProductImportDto;
-import com.ruoyi.sales.mapper.*;
+import com.ruoyi.quality.pojo.QualityInspect;
+import com.ruoyi.quality.pojo.QualityInspectParam;
+import com.ruoyi.quality.pojo.QualityTestStandard;
+import com.ruoyi.quality.pojo.QualityTestStandardParam;
+import com.ruoyi.sales.mapper.CommonFileMapper;
+import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper;
+import com.ruoyi.sales.mapper.SalesLedgerMapper;
+import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.sales.pojo.CommonFile;
 import com.ruoyi.sales.pojo.InvoiceRegistrationProduct;
 import com.ruoyi.sales.pojo.SalesLedger;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
-import lombok.RequiredArgsConstructor;
+import com.ruoyi.stock.mapper.StockInventoryMapper;
+import com.ruoyi.stock.pojo.StockInventory;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.BeanUtils;
@@ -76,6 +78,8 @@
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -88,68 +92,70 @@
 @Slf4j
 public class PurchaseLedgerServiceImpl extends ServiceImpl<PurchaseLedgerMapper, PurchaseLedger> implements IPurchaseLedgerService {
     @Autowired
-    private  AccountExpenseService accountExpenseService;
+    private AccountExpenseService accountExpenseService;
     @Autowired
-    private  PurchaseLedgerMapper purchaseLedgerMapper;
+    private PurchaseLedgerMapper purchaseLedgerMapper;
 
     @Autowired
-    private  SalesLedgerMapper salesLedgerMapper;
+    private SalesLedgerMapper salesLedgerMapper;
     @Autowired
-    private  SalesLedgerProductMapper salesLedgerProductMapper;
+    private SalesLedgerProductMapper salesLedgerProductMapper;
 
     @Autowired
-    private  SysUserMapper userMapper;
+    private SysUserMapper userMapper;
 
     @Autowired
-    private  TempFileMapper tempFileMapper;
+    private TempFileMapper tempFileMapper;
 
     @Autowired
-    private  CommonFileMapper commonFileMapper;
+    private CommonFileMapper commonFileMapper;
 
     @Autowired
-    private  SupplierManageMapper supplierManageMapper;
+    private SupplierManageMapper supplierManageMapper;
 
     @Autowired
-    private  ProductMapper productMapper;
+    private ProductMapper productMapper;
 
     @Autowired
-    private  ProductModelMapper productModelMapper;
+    private ProductModelMapper productModelMapper;
 
     @Autowired
-    private  SysUserMapper sysUserMapper;
+    private SysUserMapper sysUserMapper;
 
     @Autowired
-    private  TicketRegistrationMapper ticketRegistrationMapper;
+    private TicketRegistrationMapper ticketRegistrationMapper;
 
     @Autowired
-    private  ProductRecordMapper productRecordMapper;
+    private ProductRecordMapper productRecordMapper;
 
     @Autowired
-    private  PaymentRegistrationMapper paymentRegistrationMapper;
+    private PaymentRegistrationMapper paymentRegistrationMapper;
     @Autowired
-    private  InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
+    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
     @Autowired
-    private  StringRedisTemplate redisTemplate;
+    private StringRedisTemplate redisTemplate;
     @Autowired
-    private  QualityInspectMapper qualityInspectMapper;
+    private QualityInspectMapper qualityInspectMapper;
     @Autowired
-    private  CommonFileServiceImpl commonFileService;
+    private CommonFileServiceImpl commonFileService;
     @Autowired
-    private  QualityTestStandardBindingMapper qualityTestStandardBindingMapper;
+    private QualityTestStandardBindingMapper qualityTestStandardBindingMapper;
     @Autowired
-    private  QualityTestStandardParamMapper qualityTestStandardParamMapper;
+    private QualityTestStandardParamMapper qualityTestStandardParamMapper;
     @Autowired
-    private  QualityTestStandardMapper qualityTestStandardMapper;
+    private QualityTestStandardMapper qualityTestStandardMapper;
     @Autowired
-    private  QualityInspectParamMapper qualityInspectParamMapper;
+    private QualityInspectParamMapper qualityInspectParamMapper;
     @Autowired
-    private  ApproveProcessServiceImpl approveProcessService;
+    private ApproveProcessServiceImpl approveProcessService;
     @Autowired
-    private  ProcurementRecordMapper procurementRecordStorageMapper;
+    private ProcurementRecordMapper procurementRecordStorageMapper;
     @Autowired
-    private  PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper;
+    private PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper;
     @Autowired
-    private  SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
+    private SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
+    @Autowired
+    private StockInventoryMapper stockInventoryMapper;
     @Value("${file.upload-dir}")
     private String uploadDir;
 
@@ -238,12 +244,12 @@
         qualityInspect.setUnit(saleProduct.getUnit());
         qualityInspect.setQuantity(saleProduct.getQuantity());
         qualityInspectMapper.insert(qualityInspect);
-        List<QualityTestStandard> qualityTestStandard = qualityTestStandardMapper.getQualityTestStandardByProductId(saleProduct.getProductId(), 0,null);
-        if (qualityTestStandard.size()>0){
+        List<QualityTestStandard> qualityTestStandard = qualityTestStandardMapper.getQualityTestStandardByProductId(saleProduct.getProductId(), 0, null);
+        if (qualityTestStandard.size() > 0) {
             qualityInspect.setTestStandardId(qualityTestStandard.get(0).getId());
             qualityInspectMapper.updateById(qualityInspect);
             qualityTestStandardParamMapper.selectList(Wrappers.<QualityTestStandardParam>lambdaQuery()
-                    .eq(QualityTestStandardParam::getTestStandardId,qualityTestStandard.get(0).getId()))
+                            .eq(QualityTestStandardParam::getTestStandardId, qualityTestStandard.get(0).getId()))
                     .forEach(qualityTestStandardParam -> {
                         QualityInspectParam param = new QualityInspectParam();
                         com.ruoyi.common.utils.bean.BeanUtils.copyProperties(qualityTestStandardParam, param);
@@ -284,9 +290,21 @@
         }
 
         // 璁剧疆瀛楁
+        List<StockInventory> stockInventoryList = stockInventoryMapper.selectList(null);
         for (SalesLedgerProduct product : products) {
-            product.setSalesLedgerId(salesLedgerId);
+            // 鑾峰彇褰撳墠鏈堜唤锛堜袱浣嶏級
+            LocalDate now = LocalDate.now();
+            String monthFlag = now.format(DateTimeFormatter.ofPattern("MM"));
+            // 鑾峰彇褰撳墠鏈堜唤鐨勬渶澶ф祦姘村彿
+            int maxSeq = getCurrentMonthMaxSeq(product.getMaterialCode(), product.getSpecificationModel(), monthFlag, stockInventoryList);
+            // 鏂版祦姘村彿 = 鏈�澶ф祦姘村彿 + 1
+            int newSeq = maxSeq + 1;
+            String seqStr = String.format("%03d", newSeq);
 
+            // 缁勮batchNo
+            String batchNo = product.getMaterialCode() + product.getSpecificationModel() + "P" + monthFlag + seqStr;
+            product.setSalesLedgerId(salesLedgerId);
+            product.setBatchNo(batchNo);
             Long productId = product.getProductId();
             if (productId != null && productMap.containsKey(productId)) {
                 product.setProductCategory(productMap.get(productId));
@@ -341,6 +359,33 @@
             // 鐩存帴鏇存柊鎸囧畾ID鐨勮褰曠殑contractAmount瀛楁涓簍otalTaxInclusiveAmount
             purchaseLedgerMapper.updateContractAmountById(salesLedgerId, totalTaxInclusiveAmount);
         }
+    }
+
+    /**
+     * 鏌ヨ褰撳墠鏈堜唤宸插瓨鍦ㄧ殑鏈�澶ф祦姘村彿
+     */
+    private static int getCurrentMonthMaxSeq(String materialCode, String model, String monthFlag, List<StockInventory> existingList) {
+        int maxSeq = 0;
+
+        String prefix = materialCode + model + "P" + monthFlag;
+
+        // 姝e垯鍖归厤锛氬墠缂� + 3浣嶆暟瀛�
+        Pattern pattern = Pattern.compile(Pattern.quote(prefix) + "(\\d{3})");
+
+        for (StockInventory 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;
     }
 
     /**
@@ -421,12 +466,12 @@
     @Transactional(rollbackFor = Exception.class)
     public int deletePurchaseLedgerByIds(Long[] ids) {
         if (ids == null || ids.length == 0) {
-           throw new BaseException("璇烽�変腑鑷冲皯涓�鏉℃暟鎹�");
+            throw new BaseException("璇烽�変腑鑷冲皯涓�鏉℃暟鎹�");
         }
         for (Long id : ids) {
             PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id);
             if (purchaseLedger.getApprovalStatus().equals(3)) {
-                throw new BaseException(purchaseLedger.getPurchaseContractNumber()+"宸茬粡瀹℃壒閫氳繃锛屼笉鍏佽鍒犻櫎");
+                throw new BaseException(purchaseLedger.getPurchaseContractNumber() + "宸茬粡瀹℃壒閫氳繃锛屼笉鍏佽鍒犻櫎");
             }
         }
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐叆搴撹褰�
@@ -449,11 +494,11 @@
         salesLedgerProductMapper.delete(queryWrapper);
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︾殑鏉ョエ鐧昏
         LambdaQueryWrapper<TicketRegistration> ticketRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
-        ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getPurchaseLedgerId,ids);
+        ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getPurchaseLedgerId, ids);
         ticketRegistrationMapper.delete(ticketRegistrationLambdaQueryWrapper);
         // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︾殑鏉ョエ鐧昏璁板綍
         LambdaQueryWrapper<ProductRecord> productRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
-        productRecordLambdaQueryWrapper.in(ProductRecord::getPurchaseLedgerId,ids);
+        productRecordLambdaQueryWrapper.in(ProductRecord::getPurchaseLedgerId, ids);
         productRecordMapper.delete(productRecordLambdaQueryWrapper);
         // 鎵归噺鍒犻櫎浠樻鐧昏
         LambdaQueryWrapper<PaymentRegistration> paymentRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
@@ -465,7 +510,7 @@
 
         List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(materialInspectLambdaQueryWrapper);
         qualityInspects.stream().forEach(qualityInspect -> {
-            if (ObjectUtils.isNotEmpty(qualityInspect.getInspectState())&&qualityInspect.getInspectState().equals(1)) {
+            if (ObjectUtils.isNotEmpty(qualityInspect.getInspectState()) && qualityInspect.getInspectState().equals(1)) {
                 throw new BaseException("宸叉彁浜ょ殑妫�楠屽崟涓嶈兘鍒犻櫎");
             }
         });
@@ -481,7 +526,7 @@
         // 鍒犻櫎閲囪喘瀹℃壒璁板綍
         for (Long id : ids) {
             PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id);
-            if(purchaseLedger != null){
+            if (purchaseLedger != null) {
                 ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
                         .eq(ApproveProcess::getApproveType, 5)
                         .eq(ApproveProcess::getApproveDelete, 0)
@@ -522,7 +567,7 @@
         // 3.鏌ヨ涓婁紶鏂囦欢
         LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
         salesLedgerFileWrapper.eq(CommonFile::getCommonId, purchaseLedger.getId())
-                .eq(CommonFile::getType,FileNameType.PURCHASE.getValue());
+                .eq(CommonFile::getType, FileNameType.PURCHASE.getValue());
         List<CommonFile> salesLedgerFiles = commonFileMapper.selectList(salesLedgerFileWrapper);
 
         // 4. 杞崲 DTO
@@ -673,7 +718,7 @@
             // 渚涘簲鍟嗘暟鎹�
             List<SupplierManage> customers = supplierManageMapper.selectList(new LambdaQueryWrapper<SupplierManage>().in(SupplierManage::getSupplierName,
                     salesLedgerImportDtoList.stream().map(PurchaseLedgerImportDto::getSupplierName).collect(Collectors.toList())));
-            List<Map<String,Object>> list = productModelMapper.getProductAndModelList();
+            List<Map<String, Object>> list = productModelMapper.getProductAndModelList();
             // 褰曞叆浜烘暟鎹�
             List<SysUser> sysUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getNickName,
                     salesLedgerImportDtoList.stream().map(PurchaseLedgerImportDto::getRecorderName).collect(Collectors.toList())));
@@ -681,7 +726,7 @@
                 PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectOne(new LambdaQueryWrapper<PurchaseLedger>()
                         .eq(PurchaseLedger::getPurchaseContractNumber, salesLedgerImportDto.getPurchaseContractNumber())
                         .last("limit 1"));
-                if(purchaseLedger != null){
+                if (purchaseLedger != null) {
                     continue;
                 }
                 PurchaseLedger salesLedger = new PurchaseLedger();
@@ -708,12 +753,12 @@
                     throw new RuntimeException("閲囪喘鍗曞彿:" + salesLedgerImportDto.getPurchaseContractNumber() + ",鏃犲搴斾骇鍝佹暟鎹紒");
                 salesLedger.setContractAmount(salesLedgerProductImportDtos.stream()
                         .map(PurchaseLedgerProductImportDto::getTaxInclusiveTotalPrice)
-                        .reduce(BigDecimal.ZERO,BigDecimal::add));
+                        .reduce(BigDecimal.ZERO, BigDecimal::add));
                 // 閫氳繃閿�鍞崟鍙风粦瀹氶攢鍞�
                 SalesLedger salesLedger1 = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>()
                         .eq(SalesLedger::getSalesContractNo, salesLedger.getSalesContractNo())
                         .last("LIMIT 1"));
-                if(salesLedger1 != null){
+                if (salesLedger1 != null) {
                     salesLedger.setSalesLedgerId(salesLedger1.getId());
                 }
                 // 閲囪喘瀹℃牳
@@ -754,13 +799,13 @@
                     salesLedgerProduct.setPendingTicketsTotal(salesLedgerProductImportDto.getTaxInclusiveTotalPrice());
                     // 鏄惁璐ㄦ鍒ゆ柇
                     salesLedgerProduct.setIsChecked(salesLedgerProductImportDto.getIsChecked() == 1);
-                    if(salesLedgerProductImportDto.getIsChecked() == 1){
+                    if (salesLedgerProductImportDto.getIsChecked() == 1) {
                         addQualityInspect(salesLedger, salesLedgerProduct);
                     }
                     salesLedgerProductMapper.insert(salesLedgerProduct);
                 }
                 // 閲囪喘瀹℃牳
-                addApproveByPurchase(loginUser,salesLedger);
+                addApproveByPurchase(loginUser, salesLedger);
             }
 
             return AjaxResult.success("瀵煎叆鎴愬姛");
@@ -800,7 +845,7 @@
         return resultDto;
     }
 
-    public void addApproveByPurchase(LoginUser loginUser,PurchaseLedger purchaseLedger) throws Exception {
+    public void addApproveByPurchase(LoginUser loginUser, PurchaseLedger purchaseLedger) throws Exception {
         ApproveProcessVO approveProcessVO = new ApproveProcessVO();
         approveProcessVO.setApproveType(5);
         approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
@@ -808,6 +853,7 @@
         approveProcessVO.setApproveUserIds(purchaseLedger.getApproveUserIds());
         approveProcessVO.setApproveUser(loginUser.getUserId());
         approveProcessVO.setApproveTime(LocalDate.now().toString());
+        approveProcessVO.setPurchaseLedgerId(purchaseLedger.getId());
         approveProcessService.addApprove(approveProcessVO);
     }
 

--
Gitblit v1.9.3