gongchunyi
7 小时以前 02cf72a4443db4f1a2406c3919718c38aa6367db
src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
@@ -152,8 +152,8 @@
            List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectProduct();
            Map<String, SalesLedgerProduct> productMap = new HashMap<>();
            for (SalesLedgerProduct product : salesLedgerProducts) {
                // 使用产品类别和规格型号作为键
                String key = product.getProductCategory() + "|" + product.getSpecificationModel();
                // 使用产品名称 + 规格型号 + 厚度作为键
                String key = buildProductKey(product.getProductCategory(), product.getSpecificationModel(), product.getThickness());
                productMap.put(key, product);
            }
@@ -166,7 +166,7 @@
            for (StockInventoryExportData dto : list) {
                // 构建查找键
                String key = dto.getProductName() + "|" + dto.getModel();
                String key = buildProductKey(dto.getProductName(), dto.getModel(), dto.getThickness());
                SalesLedgerProduct matchedProduct = productMap.get(key);
                
                if (matchedProduct != null) {
@@ -217,7 +217,7 @@
                    }
                } else {
                    // 记录未匹配的产品
                    String unmatchedRecord = "产品名称:" + dto.getProductName() + ",型号:" + dto.getModel();
                    String unmatchedRecord = "产品名称:" + dto.getProductName() + ",型号:" + dto.getModel() + ",厚度:" + normalizeThickness(dto.getThickness());
                    unmatchedRecords.add(unmatchedRecord);
                }
            }
@@ -261,24 +261,49 @@
    @Override
    public Boolean frozenStock(StockInventoryDto stockInventoryDto) {
        StockInventory stockInventory = stockInventoryMapper.selectById(stockInventoryDto.getId());
        if (stockInventory.getQualitity().compareTo(stockInventoryDto.getLockedQuantity()) < 0) {
            throw new RuntimeException("冻结数量不能超过库存数量");
        if (ObjectUtils.isEmpty(stockInventory)) {
            throw new ServiceException("库存记录不存在");
        }
        if (ObjectUtils.isEmpty(stockInventory.getLockedQuantity())) {
            stockInventory.setLockedQuantity(stockInventoryDto.getLockedQuantity());
        } else {
            stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().add(stockInventoryDto.getLockedQuantity()));
        if (ObjectUtils.isEmpty(stockInventoryDto.getLockedQuantity()) || stockInventoryDto.getLockedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
            throw new ServiceException("冻结数量必须大于0");
        }
        BigDecimal totalQty = ObjectUtils.isEmpty(stockInventory.getQualitity()) ? BigDecimal.ZERO : stockInventory.getQualitity();
        BigDecimal currentLockedQty = ObjectUtils.isEmpty(stockInventory.getLockedQuantity()) ? BigDecimal.ZERO : stockInventory.getLockedQuantity();
        BigDecimal availableQty = totalQty.subtract(currentLockedQty);
        if (stockInventoryDto.getLockedQuantity().compareTo(availableQty) > 0) {
            throw new ServiceException("冻结数量不能超过可冻结库存数量");
        }
        stockInventory.setLockedQuantity(currentLockedQty.add(stockInventoryDto.getLockedQuantity()));
        return this.updateById(stockInventory);
    }
    @Override
    public Boolean thawStock(StockInventoryDto stockInventoryDto) {
        StockInventory stockInventory = stockInventoryMapper.selectById(stockInventoryDto.getId());
        if (stockInventory.getLockedQuantity().compareTo(stockInventoryDto.getLockedQuantity()) < 0) {
            throw new RuntimeException("解冻数量不能超过冻结数量");
        if (ObjectUtils.isEmpty(stockInventory)) {
            throw new ServiceException("库存记录不存在");
        }
        stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
        if (ObjectUtils.isEmpty(stockInventoryDto.getLockedQuantity()) || stockInventoryDto.getLockedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
            throw new ServiceException("解冻数量必须大于0");
        }
        BigDecimal currentLockedQty = ObjectUtils.isEmpty(stockInventory.getLockedQuantity()) ? BigDecimal.ZERO : stockInventory.getLockedQuantity();
        if (currentLockedQty.compareTo(stockInventoryDto.getLockedQuantity()) < 0) {
            throw new ServiceException("解冻数量不能超过冻结数量");
        }
        stockInventory.setLockedQuantity(currentLockedQty.subtract(stockInventoryDto.getLockedQuantity()));
        return this.updateById(stockInventory);
    }
    private String buildProductKey(String productName, String model, BigDecimal thickness) {
        String safeProductName = productName == null ? "" : productName.trim();
        String safeModel = model == null ? "" : model.trim();
        return safeProductName + "|" + safeModel + "|" + normalizeThickness(thickness);
    }
    private String normalizeThickness(BigDecimal thickness) {
        if (thickness == null) {
            return "";
        }
        return thickness.stripTrailingZeros().toPlainString();
    }
}