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.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.basic.dto.ProductDto; import com.ruoyi.basic.dto.ProductTreeDto; import com.ruoyi.basic.mapper.ProductMapper; import com.ruoyi.basic.mapper.ProductModelMapper; import com.ruoyi.basic.pojo.Product; import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.web.domain.R; import com.ruoyi.sales.mapper.SalesLedgerProductMapper; import com.ruoyi.sales.pojo.SalesLedgerProduct; import com.ruoyi.stock.dto.StockInRecordDto; import com.ruoyi.stock.dto.StockInventoryDto; import com.ruoyi.stock.dto.StockOutRecordDto; import com.ruoyi.stock.execl.StockInventoryExportData; import com.ruoyi.stock.mapper.StockInventoryMapper; import com.ruoyi.stock.pojo.StockInventory; import com.ruoyi.stock.service.StockInRecordService; import com.ruoyi.stock.service.StockInventoryService; import com.ruoyi.stock.service.StockOutRecordService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.util.*; /** *

* 库存表 服务实现类 *

* * @author 芯导软件(江苏)有限公司 * @since 2026-01-21 04:16:36 */ @Service public class StockInventoryServiceImpl extends ServiceImpl implements StockInventoryService { @Autowired private StockInventoryMapper stockInventoryMapper; @Autowired private StockInRecordService stockInRecordService; @Autowired private StockOutRecordService stockOutRecordService; @Autowired private SalesLedgerProductMapper salesLedgerProductMapper; @Autowired private ProductMapper productMapper; @Autowired private ProductModelMapper productModelMapper; @Override public IPage pagestockInventory(Page page, StockInventoryDto stockInventoryDto) { return stockInventoryMapper.pagestockInventory(page, stockInventoryDto); } //入库调用 @Override @Transactional(rollbackFor = Exception.class) public Boolean addstockInventory(StockInventoryDto stockInventoryDto) { //新增入库记录再添加库存 if (stockInventoryDto.getRecordType() != null) { StockInRecordDto stockInRecordDto = new StockInRecordDto(); stockInRecordDto.setRecordId(stockInventoryDto.getRecordId()); stockInRecordDto.setRecordType(stockInventoryDto.getRecordType()); stockInRecordDto.setStockInNum(stockInventoryDto.getQualitity()); stockInRecordDto.setProductModelId(stockInventoryDto.getProductModelId()); stockInRecordDto.setType("0"); stockInRecordDto.setBatchNo(stockInventoryDto.getBatchNo()); stockInRecordDto.setCustomer(stockInventoryDto.getCustomer()); stockInRecordService.add(stockInRecordDto); } //再进行新增库存数量库存 //先查询库存表中的产品是否存在,不存在新增,存在更新 StockInventory oldStockInventory = stockInventoryMapper.selectOne(new QueryWrapper().lambda() .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId()) .eq(StockInventory::getBatchNo, stockInventoryDto.getBatchNo()) .eq(StockInventory::getCustomer, stockInventoryDto.getCustomer()) ); if (ObjectUtils.isEmpty(oldStockInventory)) { StockInventory newStockInventory = new StockInventory(); newStockInventory.setProductModelId(stockInventoryDto.getProductModelId()); newStockInventory.setQualitity(stockInventoryDto.getQualitity()); newStockInventory.setVersion(1); newStockInventory.setRemark(stockInventoryDto.getRemark()); newStockInventory.setLockedQuantity(stockInventoryDto.getLockedQuantity()); newStockInventory.setWarnNum(stockInventoryDto.getWarnNum()); newStockInventory.setBatchNo(stockInventoryDto.getBatchNo()); newStockInventory.setCustomer(stockInventoryDto.getCustomer()); stockInventoryMapper.insert(newStockInventory); }else { stockInventoryMapper.updateAddStockInventory(stockInventoryDto); } return true; } //出库调用 @Override @Transactional(rollbackFor = Exception.class) public Boolean subtractStockInventory(StockInventoryDto stockInventoryDto) { // 新增出库记录 if (stockInventoryDto.getRecordType() != null) { StockOutRecordDto stockOutRecordDto = new StockOutRecordDto(); stockOutRecordDto.setRecordId(stockInventoryDto.getRecordId()); stockOutRecordDto.setRecordType(stockInventoryDto.getRecordType()); stockOutRecordDto.setStockOutNum(stockInventoryDto.getQualitity()); stockOutRecordDto.setProductModelId(stockInventoryDto.getProductModelId()); stockOutRecordDto.setBatchNo(stockInventoryDto.getBatchNo()); stockOutRecordDto.setCustomer(stockInventoryDto.getCustomer()); stockOutRecordDto.setType("0"); stockOutRecordService.add(stockOutRecordDto); } StockInventory oldStockInventory = stockInventoryMapper.selectOne(new QueryWrapper().lambda() .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId()) .eq(StockInventory::getBatchNo, stockInventoryDto.getBatchNo()) .eq(StockInventory::getCustomer, stockInventoryDto.getCustomer()) ); if (ObjectUtils.isEmpty(oldStockInventory)) { throw new RuntimeException("产品库存不存在"); } BigDecimal lockedQty = oldStockInventory.getLockedQuantity(); if (lockedQty == null) { lockedQty = BigDecimal.ZERO; } if (stockInventoryDto.getQualitity().compareTo(oldStockInventory.getQualitity().subtract(lockedQty)) > 0) { throw new RuntimeException("库存不足无法出库"); } stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto); return true; } @Override public R importStockInventory(MultipartFile file) { try { // 查询所有的产品 List salesLedgerProducts = salesLedgerProductMapper.selectProduct(); ExcelUtil util = new ExcelUtil(StockInventoryExportData.class); List list = util.importExcel(file.getInputStream()); // 记录未找到匹配项的数据 List unmatchedRecords = new ArrayList<>(); list.forEach(dto -> { boolean matched = false; for (SalesLedgerProduct item : salesLedgerProducts) { if (item.getProductCategory().equals(dto.getProductName()) && item.getSpecificationModel().equals(dto.getModel())) { StockInventoryDto stockInventoryDto = new StockInventoryDto(); stockInventoryDto.setRecordId(0L); stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode()); stockInventoryDto.setQualitity(dto.getQualitity()); stockInventoryDto.setRemark(dto.getRemark()); stockInventoryDto.setWarnNum(dto.getWarnNum()); if (ObjectUtils.isNotEmpty(dto.getLockedQuantity())&&dto.getLockedQuantity().compareTo(dto.getQualitity())>0) { throw new RuntimeException("冻结数量不能超过本次导入的库存数量"); } stockInventoryDto.setLockedQuantity(dto.getLockedQuantity()); stockInventoryDto.setProductModelId(item.getProductModelId()); this.addstockInventory(stockInventoryDto); matched = true; break; // 找到匹配项后跳出循环 } } if (!matched) { // 记录未匹配的数据 String unmatchedInfo = String.format("产品名称:%s,规格型号:%s", dto.getProductName(), dto.getModel()); unmatchedRecords.add(unmatchedInfo); } }); // 构建返回信息 StringBuilder message = new StringBuilder(); if (!unmatchedRecords.isEmpty()) { message.append("以下产品未找到匹配项:\n"); for (String record : unmatchedRecords) { message.append(record).append("\n"); } throw new RuntimeException(message.toString()); } } catch (Exception e) { e.printStackTrace(); return R.fail("导入失败:" + e.getMessage()); } return R.ok("导入成功"); } @Override public void exportStockInventory(HttpServletResponse response, StockInventoryDto stockInventoryDto) { List list = stockInventoryMapper.listStockInventoryExportData(stockInventoryDto); ExcelUtil util = new ExcelUtil<>(StockInventoryExportData.class); util.exportExcel(response,list, "库存信息"); } @Override public IPage stockInventoryPage(StockInventoryDto stockInventoryDto, Page page) { return stockInventoryMapper.stockInventoryPage(stockInventoryDto,page); } @Override public IPage stockInAndOutRecord(StockInventoryDto stockInventoryDto, Page page) { return stockInventoryMapper.stockInAndOutRecord(stockInventoryDto,page); } @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.getLockedQuantity())) { stockInventory.setLockedQuantity(stockInventoryDto.getLockedQuantity()); }else { stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().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("解冻数量不能超过冻结数量"); } stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity())); return this.updateById(stockInventory); } @Override public List getMaterials(StockInventoryDto stockInventoryDto) { return stockInventoryMapper.getMaterials(stockInventoryDto); } @Override public List getStockInventoryAll(ProductDto productDto) { // 查询库存列表 List stockList = stockInventoryMapper.getStockInventoryAll(productDto); if (CollectionUtils.isEmpty(stockList)) { return new ArrayList<>(); } // 构建产品树(基于产品表的父子关系) Map productNodeMap = buildProductTree(stockList); // 将库存数据(型号->批次->客户)挂载到对应的产品节点下 attachStockDataToProduct(productNodeMap, stockList); // 返回根节点 List tree = new ArrayList<>(); for (ProductTreeDto node : productNodeMap.values()) { if (node.getParentId() == null || node.getParentId() == 0) { tree.add(node); } } return tree; } /** * 构建产品树(自动递归查询所有父级,包含根节点) */ private Map buildProductTree(List stockList) { // 1. 收集所有需要加载的产品ID Set needQueryIds = new HashSet<>(); for (StockInventoryDto stock : stockList) { if (stock.getProductId() != null) { needQueryIds.add(stock.getProductId()); } } // 递归查询所有节点(直到根节点) Set allAncestors = getAllAncestorIds(needQueryIds); needQueryIds.addAll(allAncestors); // 批量查询所有产品(包含完整层级) LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.in(Product::getId, needQueryIds); List products = productMapper.selectList(wrapper); // 构建节点Map Map nodeMap = new HashMap<>(); for (Product product : products) { ProductTreeDto node = new ProductTreeDto(); node.setId(product.getId()); node.setParentId(product.getParentId()); node.setProductName(product.getProductName()); node.setLabel(product.getProductName()); node.setNodeType("product"); node.setChildren(new ArrayList<>()); nodeMap.put(product.getId(), node); } // 构建父子关系 for (ProductTreeDto node : nodeMap.values()) { Long parentId = node.getParentId(); if (parentId != null && parentId != 0 && nodeMap.containsKey(parentId)) { nodeMap.get(parentId).getChildren().add(node); } } return nodeMap; } /** * 递归查询所有产品的祖先ID(直到根节点) */ private Set getAllAncestorIds(Set productIds) { Set ancestorIds = new HashSet<>(); Queue queue = new LinkedList<>(productIds); while (!queue.isEmpty()) { Long currentId = queue.poll(); Product product = productMapper.selectById(currentId); if (product == null) continue; Long parentId = product.getParentId(); if (parentId != null && parentId != 0 && !ancestorIds.contains(parentId)) { ancestorIds.add(parentId); queue.add(parentId); } } return ancestorIds; } /** * 将库存数据(型号->批次->客户)挂载到产品节点下 */ private void attachStockDataToProduct(Map productNodeMap, List stockList) { for (StockInventoryDto stock : stockList) { Long productId = stock.getProductId(); if (productId == null || !productNodeMap.containsKey(productId)) { continue; } ProductTreeDto productNode = productNodeMap.get(productId); // 构建该产品的库存树(型号 -> 批次 -> 客户) ProductTreeDto stockTree = buildStockTree(stock); // 合并到产品节点的 children 中 mergeStockTree(productNode.getChildren(), stockTree); } } /** * 构建单个库存的树结构(型号 -> 批次 -> 客户) */ private ProductTreeDto buildStockTree(StockInventoryDto stock) { // 型号节点 ProductTreeDto modelNode = new ProductTreeDto(); modelNode.setModel(stock.getModel()); modelNode.setUidNo(stock.getUidNo()); modelNode.setUnit(stock.getUnit()); modelNode.setProductModelId(stock.getProductModelId()); modelNode.setLabel(stock.getModel()); modelNode.setNodeType("model"); modelNode.setChildren(new ArrayList<>()); // 批次节点 ProductTreeDto batchNode = new ProductTreeDto(); String batchNo = StringUtils.isBlank(stock.getBatchNo()) ? "无批次" : stock.getBatchNo(); batchNode.setBatchNo(batchNo); batchNode.setLabel("批次: " + batchNo); batchNode.setNodeType("batch"); batchNode.setChildren(new ArrayList<>()); // 客户节点 ProductTreeDto customerNode = new ProductTreeDto(); String customer = StringUtils.isBlank(stock.getCustomer()) ? "无客户" : stock.getCustomer(); customerNode.setCustomer(customer); customerNode.setLabel(customer); customerNode.setNodeType("customer"); customerNode.setChildren(new ArrayList<>()); batchNode.getChildren().add(customerNode); modelNode.getChildren().add(batchNode); return modelNode; } /** * 合并库存树到产品节点的 children 中 */ private void mergeStockTree(List children, ProductTreeDto newStockTree) { if (children == null) { return; } // 查找是否已有相同型号的节点 ProductTreeDto existingModelNode = null; for (ProductTreeDto child : children) { if (child.getNodeType().equals("model") && child.getModel() != null && child.getModel().equals(newStockTree.getModel())) { existingModelNode = child; break; } } if (existingModelNode == null) { // 没有相同型号,直接添加 children.add(newStockTree); } else { // 有相同型号,合并批次节点 ProductTreeDto newBatchNode = newStockTree.getChildren().get(0); // 查找是否已有相同批次 ProductTreeDto existingBatchNode = null; for (ProductTreeDto batchChild : existingModelNode.getChildren()) { if (batchChild.getNodeType().equals("batch") && batchChild.getBatchNo() != null && batchChild.getBatchNo().equals(newBatchNode.getBatchNo())) { existingBatchNode = batchChild; break; } } if (existingBatchNode == null) { existingModelNode.getChildren().add(newBatchNode); } else { // 合并客户节点 ProductTreeDto newCustomerNode = newBatchNode.getChildren().get(0); boolean customerExists = false; for (ProductTreeDto customerChild : existingBatchNode.getChildren()) { if (customerChild.getNodeType().equals("customer") && customerChild.getCustomer() != null && customerChild.getCustomer().equals(newCustomerNode.getCustomer())) { customerExists = true; break; } } if (!customerExists) { existingBatchNode.getChildren().add(newCustomerNode); } } } } }