add test and outbound test

This commit is contained in:
zhougf committed 2026-06-01 23:30:36 +08:00
1 parent 38ecbf82e5
commit 7f621dd553
23 files changed
+1301 -26

No files matched your search

@@ -0,0 +1,8 @@
using WmsLite.Api.Dtos.Inventory;
namespace WmsLite.Api.Services.Interfaces;
public interface IInventoryAllocationService
{
Task<List<InventoryAllocationItem>> AllocateAsync(InventoryAllocationRequest request);
}
@@ -22,6 +22,8 @@ public interface IOutboundOrderService
Task<List<InventoryOrderLineViewDto>?> GetLinesAsync(long orderId, string tenantId, string ownerCode);
Task<List<OutboundPickTaskViewDto>?> GetPickTasksAsync(long orderId, string tenantId, string ownerCode);
Task<OrderViewDto> CreateAsync(OutboundOrderSaveRequest request);
Task<OrderViewDto?> PendingAsync(OrderActionRequest request);
@@ -0,0 +1,90 @@
using WmsLite.Api.Common;
using WmsLite.Api.Dtos.Inventory;
using WmsLite.Api.Enums;
using WmsLite.Api.Repositories.Interfaces;
using WmsLite.Api.Services.Interfaces;
namespace WmsLite.Api.Services;
public class InventoryAllocationService : IInventoryAllocationService
{
private readonly IInventoryRepository _inventoryRepository;
public InventoryAllocationService(IInventoryRepository inventoryRepository)
{
_inventoryRepository = inventoryRepository;
}
public async Task<List<InventoryAllocationItem>> AllocateAsync(InventoryAllocationRequest request)
{
if (request.Qty <= 0)
{
throw new BusinessException("Allocation quantity must be greater than 0.");
}
ValidateStockIdentity(request);
var candidates = await _inventoryRepository.GetAllocatableAsync(
request.TenantId,
request.OwnerCode,
request.WarehouseCode,
request.AreaCode,
request.InventoryKind,
request.MaterialCode,
request.ContainerCode,
request.LocationCode);
var remain = request.Qty;
var result = new List<InventoryAllocationItem>();
foreach (var inventory in candidates)
{
var available = inventory.Qty - inventory.PendingOutboundQty - inventory.FrozenQty;
if (available <= 0)
{
continue;
}
var allocated = Math.Min(available, remain);
result.Add(new InventoryAllocationItem
{
Inventory = inventory,
Qty = allocated
});
remain -= allocated;
if (remain <= 0)
{
break;
}
}
if (remain > 0)
{
throw new BusinessException($"Available quantity is not enough. Required: {request.Qty}, short: {remain}.");
}
return result;
}
private static void ValidateStockIdentity(InventoryAllocationRequest request)
{
switch (request.InventoryKind)
{
case InventoryKind.Material:
if (string.IsNullOrWhiteSpace(request.MaterialCode))
{
throw new BusinessException("MaterialCode is required for material allocation.");
}
break;
case InventoryKind.EmptyContainer:
if (string.IsNullOrWhiteSpace(request.ContainerCode))
{
throw new BusinessException("ContainerCode is required for empty container allocation.");
}
break;
default:
throw new BusinessException("Unsupported inventory kind.");
}
}
}
+132 -17
View File
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Globalization;
using WmsLite.Api.Dtos.Inventory;
using WmsLite.Api.Common;
using WmsLite.Api.Dtos.Orders;
using WmsLite.Api.Enums;
@@ -570,14 +571,18 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
private readonly ITransactionRunner _tx;
private readonly IOutboundOrderRepository _orderRepository;
private readonly IOutboundOrderLineRepository _lineRepository;
private readonly IOutboundPickTaskRepository _pickTaskRepository;
private readonly ILocationRepository _locationRepository;
private readonly IInventoryAllocationService _allocationService;
private readonly ICurrentUserContext _userContext;
public OutboundOrderService(
ITransactionRunner tx,
IOutboundOrderRepository orderRepository,
IOutboundOrderLineRepository lineRepository,
IOutboundPickTaskRepository pickTaskRepository,
ILocationRepository locationRepository,
IInventoryAllocationService allocationService,
IInventoryRepository inventoryRepository,
IInventoryMovementService inventoryMovementService,
ICurrentUserContext userContext) : base(inventoryRepository, inventoryMovementService)
@@ -585,7 +590,9 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
_tx = tx;
_orderRepository = orderRepository;
_lineRepository = lineRepository;
_pickTaskRepository = pickTaskRepository;
_locationRepository = locationRepository;
_allocationService = allocationService;
_userContext = userContext;
}
@@ -614,6 +621,19 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
return lines.ConvertAll(x => MapInventoryLine(x));
}
public async Task<List<OutboundPickTaskViewDto>?> GetPickTasksAsync(long orderId, string tenantId, string ownerCode)
{
var t = _userContext.TenantId.Trim();
var o = _userContext.OwnerCode.Trim();
if (await _orderRepository.GetByIdAsync(orderId, t, o) is null)
{
return null;
}
var tasks = await _pickTaskRepository.GetByOrderIdAsync(orderId);
return tasks.ConvertAll(MapPickTask);
}
public async Task<OrderViewDto> CreateAsync(OutboundOrderSaveRequest request)
{
return await _tx.ExecuteAsync(async () =>
@@ -649,10 +669,10 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
ContainerCode = line.InventoryKind == InventoryKind.EmptyContainer ? line.ContainerCode?.Trim() : null,
WarehouseCode = line.WarehouseCode.Trim(),
AreaCode = line.AreaCode.Trim(),
LocationCode = line.LocationCode.Trim(),
LocationCode = line.LocationCode?.Trim(),
Qty = line.Qty,
ReceivedQty = 0,
LineStatus = OrderLineStatus.Completed,
LineStatus = OrderLineStatus.Open,
Unit = line.Unit.Trim(),
Attrs = line.Attrs
});
@@ -676,14 +696,70 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
var lines = await _lineRepository.GetByOrderIdAsync(order.Id);
foreach (var line in lines)
{
var resolved = await _locationRepository.GetByWarehouseAreaAndLocationCodeAsync(
order.TenantId, order.OwnerCode, line.WarehouseCode, line.AreaCode, line.LocationCode!.Trim())
?? throw new BusinessException(
var existingTasks = await _pickTaskRepository.GetByOrderLineIdAsync(line.Id);
if (existingTasks.Count > 0)
{
throw new BusinessException("Outbound pick tasks already exist.");
}
if (!string.IsNullOrWhiteSpace(line.LocationCode)
&& await _locationRepository.GetByWarehouseAreaAndLocationCodeAsync(
order.TenantId, order.OwnerCode, line.WarehouseCode, line.AreaCode, line.LocationCode.Trim()) is null)
{
throw new BusinessException(
$"Location '{line.LocationCode}' not found for warehouse '{line.WarehouseCode}' and area '{line.AreaCode}'.");
var inventory = await RequireInventoryAsync(
order.TenantId, order.OwnerCode, line.InventoryKind, line.MaterialCode, line.ContainerCode,
line.WarehouseCode, line.AreaCode, resolved.ZoneCode, line.LocationCode!);
await InventoryMovementService.PendingOutboundAsync(inventory, line.Qty, order.OrderNo);
}
var allocations = await _allocationService.AllocateAsync(new InventoryAllocationRequest
{
TenantId = order.TenantId,
OwnerCode = order.OwnerCode,
InventoryKind = line.InventoryKind,
MaterialCode = line.MaterialCode,
ContainerCode = line.ContainerCode,
WarehouseCode = line.WarehouseCode,
AreaCode = line.AreaCode,
LocationCode = line.LocationCode,
Qty = line.Qty
});
var taskSeq = 1;
foreach (var allocation in allocations)
{
var inventory = allocation.Inventory;
if (string.Compare(inventory.Unit.Trim(), line.Unit?.Trim(), StringComparison.Ordinal) != 0)
{
throw new BusinessException("Unit does not match allocated inventory.");
}
await InventoryMovementService.PendingOutboundAsync(inventory, allocation.Qty, order.OrderNo);
await _pickTaskRepository.InsertAsync(new OutboundPickTask
{
TenantId = order.TenantId,
OwnerCode = order.OwnerCode,
OrderId = order.Id,
OrderLineId = line.Id,
TaskNo = $"{order.OrderNo}-P{line.LineNo:D3}-{taskSeq:D3}",
InventoryId = inventory.Id,
InventoryKind = inventory.InventoryKind,
MaterialCode = inventory.MaterialCode,
ContainerCode = inventory.ContainerCode,
WarehouseCode = inventory.WarehouseCode,
AreaCode = inventory.AreaCode,
ZoneCode = inventory.ZoneCode,
LocationCode = inventory.LocationCode,
Qty = allocation.Qty,
PickedQty = 0,
Unit = inventory.Unit,
TaskStatus = PickTaskStatus.Open,
Attrs = line.Attrs
});
taskSeq++;
}
line.ReceivedQty = allocations.Sum(x => x.Qty);
line.LineStatus = OrderLineStatus.Open;
await _lineRepository.UpdateAsync(line);
}
order.OrderStatus = OrderStatus.Pending;
@@ -703,17 +779,32 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
return await _tx.ExecuteAsync(async () =>
{
EnsurePending(order.OrderStatus);
var tasks = await _pickTaskRepository.GetByOrderIdAsync(order.Id);
if (tasks.Count == 0)
{
throw new BusinessException("Outbound pick tasks not found.");
}
foreach (var task in tasks)
{
if (task.TaskStatus != PickTaskStatus.Open)
{
throw new BusinessException("Only open pick tasks can be confirmed.");
}
var inventory = await InventoryRepository.GetByIdAsync(task.InventoryId, order.TenantId, order.OwnerCode)
?? throw new BusinessException("Allocated inventory not found.");
await InventoryMovementService.ConfirmOutboundAsync(inventory, task.Qty, order.OrderNo);
task.PickedQty = task.Qty;
task.TaskStatus = PickTaskStatus.Picked;
await _pickTaskRepository.UpdateAsync(task);
}
var lines = await _lineRepository.GetByOrderIdAsync(order.Id);
foreach (var line in lines)
{
var resolved = await _locationRepository.GetByWarehouseAreaAndLocationCodeAsync(
order.TenantId, order.OwnerCode, line.WarehouseCode, line.AreaCode, line.LocationCode!.Trim())
?? throw new BusinessException(
$"Location '{line.LocationCode}' not found for warehouse '{line.WarehouseCode}' and area '{line.AreaCode}'.");
var inventory = await RequireInventoryAsync(
order.TenantId, order.OwnerCode, line.InventoryKind, line.MaterialCode, line.ContainerCode,
line.WarehouseCode, line.AreaCode, resolved.ZoneCode, line.LocationCode!);
await InventoryMovementService.ConfirmOutboundAsync(inventory, line.Qty, order.OrderNo);
line.LineStatus = OrderLineStatus.Completed;
await _lineRepository.UpdateAsync(line);
}
order.OrderStatus = OrderStatus.Completed;
@@ -721,6 +812,30 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
return MapOrder(order, order.OrderNo, order.BizType, order.OrderStatus, order.Remark, order.Attrs);
});
}
private static OutboundPickTaskViewDto MapPickTask(OutboundPickTask task)
{
return new OutboundPickTaskViewDto
{
Id = FormatSnowflakeId(task.Id),
OrderId = FormatSnowflakeId(task.OrderId),
OrderLineId = FormatSnowflakeId(task.OrderLineId),
TaskNo = task.TaskNo,
InventoryId = FormatSnowflakeId(task.InventoryId),
InventoryKind = task.InventoryKind,
MaterialCode = task.MaterialCode,
ContainerCode = task.ContainerCode,
WarehouseCode = task.WarehouseCode,
AreaCode = task.AreaCode,
ZoneCode = task.ZoneCode,
LocationCode = task.LocationCode,
Qty = task.Qty,
PickedQty = task.PickedQty,
Unit = task.Unit,
TaskStatus = task.TaskStatus,
Attrs = task.Attrs
};
}
}
public class TransferOrderService : OrderServiceBase, ITransferOrderService