gongchunyi
15 小时以前 6b4cfc6f9d660b92be99ba4e3411a3267bc57155
src/main/java/com/ruoyi/procurementrecord/utils/StockUtils.java
@@ -1,16 +1,24 @@
package com.ruoyi.procurementrecord.utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordOutMapper;
import com.ruoyi.stock.dto.StockInRecordDto;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.dto.StockUninventoryDto;
import com.ruoyi.stock.pojo.StockInRecord;
import com.ruoyi.stock.pojo.StockInventory;
import com.ruoyi.stock.pojo.StockOutRecord;
import com.ruoyi.stock.pojo.StockUninventory;
import com.ruoyi.stock.service.StockInRecordService;
import com.ruoyi.stock.service.StockInventoryService;
import com.ruoyi.stock.service.StockOutRecordService;
import com.ruoyi.stock.service.StockUninventoryService;
import com.ruoyi.stock.service.impl.StockInRecordServiceImpl;
import com.ruoyi.stock.service.impl.StockOutRecordServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@@ -27,78 +35,142 @@
    private final StockUninventoryService stockUninventoryService;
    private final StockInventoryService stockInventoryService;
    private final StockInRecordService stockInRecordService;
    private final StockOutRecordService stockOutRecordService;
    // 获取商品入库数量,出库数量,剩余库存
    public Map<String, BigDecimal> getStockQuantity(Long productModelId) {
        // 入库数量
        BigDecimal sumQuantity = procurementRecordMapper.getSumQuantity(productModelId);
        // 出库数量
        BigDecimal outQuantity = procurementRecordOutMapper.getSumQuantity(productModelId);
        // 剩余库存
        BigDecimal stockQuantity = outQuantity.compareTo(sumQuantity) > 0 ? BigDecimal.ZERO : sumQuantity.subtract(outQuantity);
        Map<String, BigDecimal> stockMap = new HashMap<>();
        stockMap.put("inboundNum", sumQuantity);
        stockMap.put("outboundNum", outQuantity);
        stockMap.put("stockQuantity", stockQuantity);
        return stockMap;
    /**
     * 合格库存出库前校验:按 stock_inventory 可用数量(数量 − 冻结)
     */
    public void assertQualifiedAvailable(Long productModelId, BigDecimal outboundQty) {
        if (productModelId == null) {
            throw new ServiceException("出库失败,产品规格未维护");
        }
        if (outboundQty == null || outboundQty.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        StockInventory inv = stockInventoryService.getOne(
                Wrappers.<StockInventory>lambdaQuery().eq(StockInventory::getProductModelId, productModelId));
        if (inv == null) {
            throw new ServiceException("出库失败,合格库中无该产品库存");
        }
        BigDecimal locked = inv.getLockedQuantity() == null ? BigDecimal.ZERO : inv.getLockedQuantity();
        BigDecimal qty = inv.getQualitity() == null ? BigDecimal.ZERO : inv.getQualitity();
        BigDecimal available = qty.subtract(locked);
        if (outboundQty.compareTo(available) > 0) {
            throw new ServiceException("出库失败,出库数量不能大于当前合格库存可用数量");
        }
    }
    /**
     * 不合格库存出库前校验:按 stock_uninventory 可用数量(数量 − 冻结)
     */
    public void assertUnqualifiedAvailable(Long productModelId, BigDecimal outboundQty) {
        if (productModelId == null) {
            throw new ServiceException("出库失败,产品规格未维护");
        }
        if (outboundQty == null || outboundQty.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        StockUninventory inv = stockUninventoryService.getOne(
                Wrappers.<StockUninventory>lambdaQuery().eq(StockUninventory::getProductModelId, productModelId));
        if (inv == null) {
            throw new ServiceException("出库失败,不合格库中无该产品库存");
        }
        BigDecimal locked = inv.getLockedQuantity() == null ? BigDecimal.ZERO : inv.getLockedQuantity();
        BigDecimal qty = inv.getQualitity() == null ? BigDecimal.ZERO : inv.getQualitity();
        BigDecimal available = qty.subtract(locked);
        if (outboundQty.compareTo(available) > 0) {
            throw new ServiceException("出库失败,出库数量不能大于当前不合格库存可用数量");
        }
    }
    /**
     * 不合格入库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void addUnStock(Long productModelId, BigDecimal quantity, String recordType,Long recordId) {
    public void addUnStock(Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        addUnStock(null, null, productModelId, quantity, recordType, recordId);
    }
    /**
     * 不合格入库(带销售订单关联,写入入库记录)
     */
    public void addUnStock(Long salesLedgerId, Long salesLedgerProductId, Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
        stockUninventoryDto.setRecordId(recordId);
        stockUninventoryDto.setRecordType(String.valueOf(recordType));
        stockUninventoryDto.setQualitity(quantity);
        stockUninventoryDto.setProductModelId(productModelId);
        stockUninventoryDto.setSalesLedgerId(salesLedgerId);
        stockUninventoryDto.setSalesLedgerProductId(salesLedgerProductId);
        stockUninventoryService.addStockUninventory(stockUninventoryDto);
    }
    /**
     * 不合格出库
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void subtractUnStock(Long productModelId, BigDecimal quantity, Integer recordType,Long recordId) {
    public void subtractUnStock(Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        subtractUnStock(null, null, productModelId, quantity, recordType, recordId);
    }
    /**
     * 不合格出库(可带销售订单关联写入出库记录)
     */
    public void subtractUnStock(Long salesLedgerId, Long salesLedgerProductId, Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
        stockUninventoryDto.setRecordId(recordId);
        stockUninventoryDto.setRecordType(String.valueOf(recordType));
        stockUninventoryDto.setRecordType(recordType);
        stockUninventoryDto.setQualitity(quantity);
        stockUninventoryDto.setProductModelId(productModelId);
        stockUninventoryDto.setSalesLedgerId(salesLedgerId);
        stockUninventoryDto.setSalesLedgerProductId(salesLedgerProductId);
        stockUninventoryService.subtractStockUninventory(stockUninventoryDto);
    }
    /**
     * 合格入库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void addStock(Long productModelId, BigDecimal quantity, String recordType,Long recordId) {
    public void addStock(Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        addStock(null, null, productModelId, quantity, recordType, recordId);
    }
    /**
     * 合格入库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void addStock(Long salesLedgerId, Long salesLedgerProductId, Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockInventoryDto stockInventoryDto = new StockInventoryDto();
        stockInventoryDto.setRecordId(recordId);
        stockInventoryDto.setRecordType(String.valueOf(recordType));
        stockInventoryDto.setQualitity(quantity);
        stockInventoryDto.setProductModelId(productModelId);
        stockInventoryDto.setSalesLedgerId(salesLedgerId);
        stockInventoryDto.setSalesLedgerProductId(salesLedgerProductId);
        stockInventoryService.addstockInventory(stockInventoryDto);
    }
    /**
     * 合格出库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void substractStock(Long productModelId, BigDecimal quantity, String recordType,Long recordId) {
    public void substractStock(Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockInventoryDto stockInventoryDto = new StockInventoryDto();
        stockInventoryDto.setRecordId(recordId);
        stockInventoryDto.setRecordType(String.valueOf(recordType));
@@ -107,12 +179,41 @@
        stockInventoryService.subtractStockInventory(stockInventoryDto);
    }
    /**
     * 合格出库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void substractStock(Long salesId, Long salseProductId, Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockInventoryDto stockInventoryDto = new StockInventoryDto();
        stockInventoryDto.setRecordId(recordId);
        stockInventoryDto.setRecordType(String.valueOf(recordType));
        stockInventoryDto.setQualitity(quantity);
        stockInventoryDto.setProductModelId(productModelId);
        stockInventoryDto.setSalesLedgerId(salesId);
        stockInventoryDto.setSalesLedgerProductId(salseProductId);
        stockInventoryService.subtractStockInventory(stockInventoryDto);
    }
    //不合格库存删除
    public void deleteStockRecord(Long recordId, String recordType) {
    public void deleteStockInRecord(Long recordId, String recordType) {
        StockInRecord one = stockInRecordService.getOne(new QueryWrapper<StockInRecord>()
                .lambda().eq(StockInRecord::getRecordId, recordId)
                .eq(StockInRecord::getRecordType, recordType));
        if (ObjectUtils.isNotEmpty(one)) {
            stockInRecordService.batchDelete(Collections.singletonList(one.getId()));
        }
    }
        stockInRecordService.batchDelete(Collections.singletonList(one.getId()));
    public void deleteStockOutRecord(Long recordId, String recordType) {
        StockOutRecord one = stockOutRecordService.getOne(new QueryWrapper<StockOutRecord>()
                .lambda().eq(StockOutRecord::getRecordId, recordId)
                .eq(StockOutRecord::getRecordType, recordType));
        if (ObjectUtils.isNotEmpty(one)) {
            stockOutRecordService.batchDelete(Collections.singletonList(one.getId()));
        }
    }
}