| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | for (Long id : ids) { |
| | | StockOutRecord stockOutRecord = stockOutRecordMapper.selectById(id); |
| | | if (stockOutRecord.getType().equals("0")) { |
| | | StockInventory stockInventory = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>().eq(StockInventory::getProductModelId, stockOutRecord.getProductModelId())); |
| | | if (stockInventory == null) { |
| | | // 查询该产品规格的所有库存记录,按ID升序(与出库扣减顺序一致) |
| | | List<StockInventory> stockInventoryList = stockInventoryMapper.selectList( |
| | | new LambdaQueryWrapper<StockInventory>() |
| | | .eq(StockInventory::getProductModelId, stockOutRecord.getProductModelId()) |
| | | .orderByAsc(StockInventory::getId) |
| | | ); |
| | | if (stockInventoryList.isEmpty()) { |
| | | throw new BaseException("库存记录中没有对应的产品,无法删除!!!"); |
| | | }else { |
| | | StockInventoryDto stockInRecordDto = new StockInventoryDto(); |
| | | stockInRecordDto.setProductModelId(stockInventory.getProductModelId()); |
| | | stockInRecordDto.setQualitity(stockOutRecord.getStockOutNum()); |
| | | stockInventoryMapper.updateAddStockInventory(stockInRecordDto); |
| | | } |
| | | |
| | | // 按照出库扣减逻辑反向回滚 |
| | | BigDecimal remainingQty = stockOutRecord.getStockOutNum(); |
| | | for (StockInventory stockInventory : stockInventoryList) { |
| | | if (remainingQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | break; |
| | | } |
| | | |
| | | BigDecimal lockedQty = stockInventory.getLockedQuantity() == null ? BigDecimal.ZERO : stockInventory.getLockedQuantity(); |
| | | BigDecimal availableQty = stockInventory.getQualitity().subtract(lockedQty); |
| | | if (availableQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | continue; |
| | | } |
| | | |
| | | // 增加数量 = min(剩余需返还数量, 当前批次可用库存) |
| | | BigDecimal addQty = remainingQty.min(availableQty); |
| | | stockInventory.setQualitity(stockInventory.getQualitity().add(addQty)); |
| | | stockInventory.setVersion(stockInventory.getVersion() == null ? 1 : stockInventory.getVersion() + 1); |
| | | stockInventory.setUpdateTime(LocalDateTime.now()); |
| | | stockInventoryMapper.updateById(stockInventory); |
| | | |
| | | remainingQty = remainingQty.subtract(addQty); |
| | | } |
| | | }else if (stockOutRecord.getType().equals("1")) { |
| | | StockUninventory stockUninventory = stockUninventoryMapper.selectOne(new LambdaQueryWrapper<StockUninventory>().eq(StockUninventory::getProductModelId, stockOutRecord.getProductModelId())); |
| | |
| | | ExcelUtil<StockOutRecordExportData> util = new ExcelUtil<>(StockOutRecordExportData.class); |
| | | util.exportExcel(response,list, "出库记录信息"); |
| | | } |
| | | } |
| | | } |