huminmin
2026-04-24 afdf7cc772baee20d5afc94d4ec3e3d691c3ed12
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;
@@ -18,6 +21,7 @@
import com.ruoyi.stock.execl.StockUnInventoryExportData;
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;
@@ -28,8 +32,11 @@
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>
@@ -50,6 +57,8 @@
    private StockInRecordService stockInRecordService;
    @Autowired
    private ApproveProcessServiceImpl approveProcessService;
    @Autowired
    private ProductModelMapper productModelMapper;
    @Override
    public IPage<StockUninventoryDto> pageStockUninventory(Page page, StockUninventoryDto stockUninventoryDto) {
@@ -59,6 +68,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());
@@ -68,6 +78,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();
@@ -80,6 +110,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;
        // 正则匹配:前缀 + 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 {
@@ -129,10 +186,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)) {
            // 不存在则新增
@@ -141,19 +205,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);
        }
    }
@@ -209,4 +286,4 @@
        stockUninventory.setLockedQuantity(stockUninventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
        return this.updateById(stockUninventory);
    }
}
}