package com.ruoyi.stock.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.enums.ReviewStatusEnum; import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum; import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum; import com.ruoyi.common.exception.base.BaseException; import com.ruoyi.common.utils.EnumUtil; import com.ruoyi.common.utils.OrderUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.stock.dto.StockInRecordDto; import com.ruoyi.stock.dto.StockInventoryDto; import com.ruoyi.stock.dto.StockUninventoryDto; import com.ruoyi.stock.execl.StockInRecordExportData; import com.ruoyi.production.mapper.ProductionOrderPickMapper; import com.ruoyi.production.pojo.ProductionOrderPick; import com.ruoyi.stock.mapper.StockInRecordMapper; import com.ruoyi.stock.mapper.StockInventoryMapper; 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 jakarta.servlet.http.HttpServletResponse; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.List; @Service @AllArgsConstructor public class StockInRecordServiceImpl extends ServiceImpl implements StockInRecordService { private StockInRecordMapper stockInRecordMapper; private StockInventoryMapper stockInventoryMapper; private StockUninventoryMapper stockUninventoryMapper; private ProductionOrderPickMapper productionOrderPickMapper; @Override public IPage listPage(Page page, StockInRecordDto stockInRecordDto) { return stockInRecordMapper.listPage(page, stockInRecordDto); } // 新增入库 @Override @Transactional(rollbackFor = Exception.class) public int add(StockInRecordDto stockInRecordDto) { String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK","inbound_batches"); stockInRecordDto.setInboundBatches(no); StockInRecord stockInRecord = new StockInRecord(); BeanUtils.copyProperties(stockInRecordDto, stockInRecord); return stockInRecordMapper.insert(stockInRecord); } @Override @Transactional(rollbackFor = Exception.class) public int update(Long id, StockInRecordDto stockInRecordDto) { // 判断对象是否存在 StockInRecord stockInRecord = stockInRecordMapper.selectById(id); if (stockInRecord == null){ throw new BaseException("该入库记录不存在,无法更新!!!"); } String[] ignoreProperties = {"id", "inbound_batches"};//排除id属性 BeanUtils.copyProperties(stockInRecordDto, stockInRecord, ignoreProperties); return stockInRecordMapper.updateById(stockInRecord); } @Override @Transactional(rollbackFor = Exception.class) public int batchDelete(List ids) { for (Long id : ids) { StockInRecord stockInRecord = stockInRecordMapper.selectById(id); if (stockInRecord.getType().equals("0")) { LambdaQueryWrapper eq = new LambdaQueryWrapper() .eq(StockInventory::getProductModelId, stockInRecord.getProductModelId()); if (StringUtils.isEmpty(stockInRecord.getBatchNo())) { eq.isNull(StockInventory::getBatchNo); } else { eq.eq(StockInventory::getBatchNo, stockInRecord.getBatchNo()); } StockInventory stockInventory = stockInventoryMapper.selectOne(eq); if (stockInventory == null) { throw new BaseException("库存记录中没有对应的产品,无法删除!!!"); }else { StockInventoryDto stockInRecordDto = new StockInventoryDto(); stockInRecordDto.setProductModelId(stockInventory.getProductModelId()); stockInRecordDto.setBatchNo(stockInventory.getBatchNo()); stockInRecordDto.setQualitity(stockInRecord.getStockInNum()); stockInventoryMapper.updateSubtractStockInventory(stockInRecordDto); } }else if (stockInRecord.getType().equals("1")) { LambdaQueryWrapper eq = new LambdaQueryWrapper() .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()); if (StringUtils.isEmpty(stockInRecord.getBatchNo())) { eq.isNull(StockUninventory::getBatchNo); } else { eq.eq(StockUninventory::getBatchNo, stockInRecord.getBatchNo()); } StockUninventory stockUninventory = stockUninventoryMapper.selectOne(eq); if (stockUninventory == null) { throw new BaseException("库存记录中没有对应的产品,无法删除!!!"); }else { StockUninventoryDto stockUninventoryDto = new StockUninventoryDto(); stockUninventoryDto.setProductModelId(stockUninventory.getProductModelId()); stockUninventoryDto.setBatchNo(stockUninventory.getBatchNo()); stockUninventoryDto.setQualitity(stockInRecord.getStockInNum()); stockUninventoryMapper.updateSubtractStockUnInventory(stockUninventoryDto); } } } return stockInRecordMapper.deleteBatchIds(ids); } @Override public void exportStockInRecord(HttpServletResponse response, StockInRecordDto stockInRecordDto) { List list = stockInRecordMapper.listStockInRecordExportData(stockInRecordDto); for (StockInRecordExportData stockInRecordExportData : list) { if (!stockInRecordExportData.getType().equals("0")) { stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockOutQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue()); }else { stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockInQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue()); } } ExcelUtil util = new ExcelUtil<>(StockInRecordExportData.class); util.exportExcel(response,list, "入库记录信息"); } private StockInventory getStockInventory(Long productModelId, String batchNo) { LambdaQueryWrapper eq = new LambdaQueryWrapper<>(); eq.eq(StockInventory::getProductModelId, productModelId); if (StringUtils.isEmpty(batchNo)) { eq.isNull(StockInventory::getBatchNo); } else { eq.eq(StockInventory::getBatchNo, batchNo); } return stockInventoryMapper.selectOne(eq); } private StockUninventory getStockUninventory(Long productModelId, String batchNo) { LambdaQueryWrapper eq = new LambdaQueryWrapper<>(); eq.eq(StockUninventory::getProductModelId, productModelId); if (StringUtils.isEmpty(batchNo)) { eq.isNull(StockUninventory::getBatchNo); } else { eq.eq(StockUninventory::getBatchNo, batchNo); } return stockUninventoryMapper.selectOne(eq); } /** * 回滚生产退料入库的领料记录退料数量 * @param stockInRecord 入库记录 */ private void rollbackFeedReturnQty(StockInRecord stockInRecord) { ProductionOrderPick productionOrderPick = productionOrderPickMapper.selectById(stockInRecord.getRecordId()); if (productionOrderPick != null) { BigDecimal returnQty = productionOrderPick.getReturnQty(); if (returnQty == null) { returnQty = BigDecimal.ZERO; } BigDecimal newReturnQty = returnQty.subtract(stockInRecord.getStockInNum()); if (newReturnQty.compareTo(BigDecimal.ZERO) < 0) { newReturnQty = BigDecimal.ZERO; } productionOrderPick.setReturnQty(newReturnQty); // 重新计算实际用量 BigDecimal actualQty = productionOrderPick.getQuantity().add( productionOrderPick.getFeedingQty() != null ? productionOrderPick.getFeedingQty() : BigDecimal.ZERO) .subtract(newReturnQty); productionOrderPick.setActualQty(actualQty); productionOrderPick.setReturned(newReturnQty.compareTo(BigDecimal.ZERO) > 0); productionOrderPickMapper.updateById(productionOrderPick); } } @Override @Transactional(rollbackFor = Exception.class) public int batchDeletePending(List ids) { for (Long id : ids) { StockInRecord stockInRecord = stockInRecordMapper.selectById(id); if (stockInRecord == null) { throw new BaseException("入库记录不存在,无法删除!!!"); } if (stockInRecord.getApprovalStatus() != null && !ReviewStatusEnum.PENDING_REVIEW.getCode().equals(stockInRecord.getApprovalStatus())) { throw new BaseException("只有待审批状态的记录才能删除,入库批次:" + stockInRecord.getInboundBatches()); } // 如果是生产退料入库,删除时需要回滚领料记录的退料数量 if (StockInQualifiedRecordTypeEnum.FEED_RETURN_IN.getCode().equals(stockInRecord.getRecordType())) { rollbackFeedReturnQty(stockInRecord); } } return stockInRecordMapper.deleteBatchIds(ids); } @Override @Transactional(rollbackFor = Exception.class) public int batchApprove(List ids, Integer approvalStatus) { if (CollectionUtils.isEmpty(ids)) { throw new BaseException("请选择至少一条数据"); } if (approvalStatus == null || (!ReviewStatusEnum.APPROVED.getCode().equals(approvalStatus) && !ReviewStatusEnum.REJECTED.getCode().equals(approvalStatus))) { throw new BaseException("审批状态值无效"); } for (Long id : ids) { StockInRecord stockInRecord = stockInRecordMapper.selectById(id); if (stockInRecord == null) { throw new BaseException("入库记录不存在,无法审批!!!"); } if (stockInRecord.getApprovalStatus() != null && !ReviewStatusEnum.PENDING_REVIEW.getCode().equals(stockInRecord.getApprovalStatus())) { throw new BaseException("只有待审批状态的记录才能审批,入库批次:" + stockInRecord.getInboundBatches()); } stockInRecord.setApprovalStatus(approvalStatus); stockInRecordMapper.updateById(stockInRecord); // 审批驳回时,如果是生产退料入库,需要回滚领料记录的退料数量 if (ReviewStatusEnum.REJECTED.getCode().equals(approvalStatus) && StockInQualifiedRecordTypeEnum.FEED_RETURN_IN.getCode().equals(stockInRecord.getRecordType())) { rollbackFeedReturnQty(stockInRecord); } // 审批通过时,库存增加 if (ReviewStatusEnum.APPROVED.getCode().equals(approvalStatus)) { if ("0".equals(stockInRecord.getType())) { // 合格入库 -> 先查库存,存在则更新,不存在则新增 StockInventory stockInventory = getStockInventory(stockInRecord.getProductModelId(), stockInRecord.getBatchNo()); StockInventoryDto stockInventoryDto = new StockInventoryDto(); stockInventoryDto.setProductModelId(stockInRecord.getProductModelId()); stockInventoryDto.setBatchNo(stockInRecord.getBatchNo()); stockInventoryDto.setQualitity(stockInRecord.getStockInNum()); stockInventoryDto.setRemark(stockInRecord.getRemark()); if (stockInventory == null) { stockInventoryMapper.insert(new StockInventory() {{ setProductModelId(stockInRecord.getProductModelId()); setQualitity(stockInRecord.getStockInNum()); setBatchNo(stockInRecord.getBatchNo()); setRemark(stockInRecord.getRemark()); setVersion(1); }}); } else { stockInventoryMapper.updateAddStockInventory(stockInventoryDto); } } else if ("1".equals(stockInRecord.getType())) { // 不合格入库 -> 先查库存,存在则更新,不存在则新增 StockUninventory stockUninventory = getStockUninventory(stockInRecord.getProductModelId(), stockInRecord.getBatchNo()); StockUninventoryDto stockUninventoryDto = new StockUninventoryDto(); stockUninventoryDto.setProductModelId(stockInRecord.getProductModelId()); stockUninventoryDto.setBatchNo(stockInRecord.getBatchNo()); stockUninventoryDto.setQualitity(stockInRecord.getStockInNum()); stockUninventoryDto.setRemark(stockInRecord.getRemark()); if (stockUninventory == null) { stockUninventoryMapper.insert(new StockUninventory() {{ setProductModelId(stockInRecord.getProductModelId()); setQualitity(stockInRecord.getStockInNum()); setBatchNo(stockInRecord.getBatchNo()); setRemark(stockInRecord.getRemark()); setVersion(1); }}); } else { stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto); } } } } return ids.size(); } @Override @Transactional(rollbackFor = Exception.class) public int batchReAudit(List ids) { if (CollectionUtils.isEmpty(ids)) { throw new BaseException("请选择至少一条数据"); } for (Long id : ids) { StockInRecord stockInRecord = stockInRecordMapper.selectById(id); if (stockInRecord == null) { throw new BaseException("入库记录不存在,无法重新审核!!!"); } // 只有驳回状态才能重新审核 if (!ReviewStatusEnum.REJECTED.getCode().equals(stockInRecord.getApprovalStatus())) { throw new BaseException("只有驳回状态的记录才能重新审核,入库批次:" + stockInRecord.getInboundBatches()); } // 如果是生产退料入库,恢复退料数量(因为驳回时已扣减) if (StockInQualifiedRecordTypeEnum.FEED_RETURN_IN.getCode().equals(stockInRecord.getRecordType())) { ProductionOrderPick productionOrderPick = productionOrderPickMapper.selectById(stockInRecord.getRecordId()); if (productionOrderPick != null) { BigDecimal returnQty = productionOrderPick.getReturnQty(); if (returnQty == null) { returnQty = BigDecimal.ZERO; } // 重新审核时恢复退料数量 BigDecimal newReturnQty = returnQty.add(stockInRecord.getStockInNum()); productionOrderPick.setReturnQty(newReturnQty); // 重新计算实际用量 BigDecimal actualQty = productionOrderPick.getQuantity().add( productionOrderPick.getFeedingQty() != null ? productionOrderPick.getFeedingQty() : BigDecimal.ZERO) .subtract(newReturnQty); productionOrderPick.setActualQty(actualQty); productionOrderPick.setReturned(newReturnQty.compareTo(BigDecimal.ZERO) > 0); productionOrderPickMapper.updateById(productionOrderPick); } } // 将状态改为待审核 stockInRecord.setApprovalStatus(ReviewStatusEnum.PENDING_REVIEW.getCode()); stockInRecordMapper.updateById(stockInRecord); } return ids.size(); } }