feat: 创建代发必绑,解绑回退直发并补事务
All checks were successful
Build and Deploy / deploy (push) Successful in 1m21s

绑定保存/清空/扣减与删单走事务;工作台与 status-counts 不再统计 pendingBind。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zzh 2026-11-18 11:23:23 +08:00
parent 2e415f79c0
commit 8376e03aa9
7 changed files with 75 additions and 29 deletions

View File

@ -5,7 +5,7 @@ namespace Oms.Application.Dtos.Orders;
/// </summary>
public class FulfillWorkplaceStatsOutput
{
/// <summary>待绑定包裹/库存。</summary>
/// <summary>历史字段:创建代发必绑后工作台恒为 0,保留兼容旧前端。</summary>
public int PendingBind { get; set; }
/// <summary>国内快递在途。</summary>

View File

@ -1,5 +1,7 @@
using System.Data;
using Fake.Application;
using Fake.Domain.Exceptions;
using Fake.UnitOfWork;
using Microsoft.AspNetCore.Mvc;
using Oms.Application.Dtos.Orders;
using Oms.Application.Dtos.Shared;
@ -189,20 +191,23 @@ public partial class OrderService
/// 保存绑定(全量覆盖 + 冻结)。
/// </summary>
[HttpPost("/order/stock-bind/save")]
[UnitOfWork(IsTransactional = true, IsolationLevel = IsolationLevel.ReadCommitted)]
public async Task<OrderStockBindDetailOutput> SaveStockBindAsync([FromBody] OrderStockBindSaveInput input)
{
var order = await GetOrderOrThrowAsync(input.OrderId);
EnsureOrderCanBindStock(order);
await EnsureNoDeductedBindsAsync(order.Id);
var customerId = await ResolveStockBindCustomerIdAsync(order);
var warehouseId = ResolveBindWarehouseId(order, null);
var items = input.Items ?? [];
if (items.Count > 0)
// 空明细 = 清空绑定:仓内代发不可处于「已建单未绑定」,清空后回退直发
if (items.Count == 0)
{
await EnsureProxyPackingConfiguredForOrderAsync(order);
return await ClearStockBindCoreAsync(order);
}
var customerId = await ResolveStockBindCustomerIdAsync(order);
var warehouseId = ResolveBindWarehouseId(order, null);
await EnsureProxyPackingConfiguredForOrderAsync(order);
await ReleaseBoundBindsAsync(order.Id);
foreach (var item in items)
@ -234,19 +239,16 @@ public partial class OrderService
needOrderUpdate = true;
}
if (items.Count > 0)
if (!FulfillmentTypeCodes.IsWarehouse(order.FulfillmentType))
{
if (!FulfillmentTypeCodes.IsWarehouse(order.FulfillmentType))
{
order.FulfillmentType = FulfillmentTypeCodes.Warehouse;
needOrderUpdate = true;
}
order.FulfillmentType = FulfillmentTypeCodes.Warehouse;
needOrderUpdate = true;
}
if (string.IsNullOrWhiteSpace(order.FulfillmentSource))
{
order.FulfillmentSource = FulfillmentSourceCodes.PostBind;
needOrderUpdate = true;
}
if (string.IsNullOrWhiteSpace(order.FulfillmentSource))
{
order.FulfillmentSource = FulfillmentSourceCodes.PostBind;
needOrderUpdate = true;
}
if (needOrderUpdate)
@ -261,16 +263,31 @@ public partial class OrderService
}
/// <summary>
/// 清空绑定并释放冻结。
/// 清空绑定并释放冻结;若原为仓内代发则回退为直发(创建代发必绑,禁止无绑定的 warehouse 单)。
/// </summary>
[HttpPost("/order/stock-bind/clear")]
[UnitOfWork(IsTransactional = true, IsolationLevel = IsolationLevel.ReadCommitted)]
public async Task<OrderStockBindDetailOutput> ClearStockBindAsync([FromBody] OrderStockBindOrderInput input)
{
var order = await GetOrderOrThrowAsync(input.OrderId);
EnsureOrderCanBindStock(order);
await EnsureNoDeductedBindsAsync(order.Id);
return await ClearStockBindCoreAsync(order);
}
private async Task<OrderStockBindDetailOutput> ClearStockBindCoreAsync(Order order)
{
await ReleaseBoundBindsAsync(order.Id);
order = await GetOrderOrThrowAsync(order.Id);
if (FulfillmentTypeCodes.IsWarehouse(order.FulfillmentType))
{
order.FulfillmentType = FulfillmentTypeCodes.Direct;
order.FulfillmentSource = null;
await UpdateOrderAsync(order);
order = await GetOrderOrThrowAsync(order.Id);
}
await SyncProxyPackingFeeByStockBindAsync(order);
return await BuildStockBindDetailAsync(order);
}
@ -279,6 +296,7 @@ public partial class OrderService
/// 确认扣减:将 bound 明细转为 deducted,扣减实物并释放冻结。
/// </summary>
[HttpPost("/order/stock-bind/confirm-deduct")]
[UnitOfWork(IsTransactional = true, IsolationLevel = IsolationLevel.ReadCommitted)]
public async Task<OrderStockBindDetailOutput> ConfirmStockBindDeductAsync([FromBody] OrderStockBindOrderInput input)
{
var order = await GetOrderOrThrowAsync(input.OrderId);

View File

@ -101,7 +101,8 @@ public partial class OrderService
StoreOrderPending = storeOrderPending,
Fulfill = new FulfillWorkplaceStatsOutput
{
PendingBind = FulfillCount(FulfillmentProgressCodes.PendingBind),
// 创建代发必绑:工作台不再统计「待绑定」
PendingBind = 0,
DomesticInTransit = FulfillCount(FulfillmentProgressCodes.DomesticInTransit),
ArrivedPendingPick = FulfillCount(FulfillmentProgressCodes.ArrivedPendingPick),
OutboundDone = FulfillCount(FulfillmentProgressCodes.OutboundDone),

View File

@ -478,6 +478,7 @@ public partial class OrderService : ApplicationService
/// <param name="input">入参:订单操作参数。</param>
/// <returns>无返回体,仅完成删除订单。</returns>
[HttpPost("/order/delete")]
[UnitOfWork(IsTransactional = true, IsolationLevel = IsolationLevel.ReadCommitted)]
public async Task DeleteAsync(OrderActionInput input)
{
var order = await GetOrderOrThrowAsync(input.Id);
@ -486,6 +487,10 @@ public partial class OrderService : ApplicationService
throw new BusinessException("仅草稿或已预报订单可删除");
}
// 删除前释放库存/包裹冻结;仓内代发回退直发,避免删单后冻结残留
await ClearStockBindCoreAsync(order);
order = await GetOrderOrThrowAsync(input.Id);
// 业务删除:只改 OrderStatus=deleted,不做 IsDeleted 软删,否则「已删除」Tab 因全局软删过滤永远查不到。
order.OrderStatus = OrderStatusCodes.Deleted;
if (order.IsPaid > 0 && order.CustomerId.HasValue)

View File

@ -623,19 +623,13 @@ public class PlatformStoreOrderService : ApplicationService
try
{
// 确保物流单带仓库,绑定校验依赖 WarehouseId
// 先只落仓库,FulfillmentType 在绑定成功后写入,避免出现「warehouse 但未绑定」
if (resolvedWarehouseId is > 0)
{
var logisticsOrder = await OrderRepository.FirstOrDefaultAsync(x => x.Id == parcelOutput.OrderId);
if (logisticsOrder != null)
if (logisticsOrder != null && logisticsOrder.WarehouseId is not > 0)
{
if (logisticsOrder.WarehouseId is not > 0)
{
logisticsOrder.WarehouseId = resolvedWarehouseId;
}
logisticsOrder.FulfillmentType = FulfillmentTypeCodes.Warehouse;
logisticsOrder.FulfillmentSource = FulfillmentSourceCodes.StoreFulfill;
logisticsOrder.WarehouseId = resolvedWarehouseId;
await OrderRepository.UpdateAsync(logisticsOrder);
}
}
@ -645,6 +639,32 @@ public class PlatformStoreOrderService : ApplicationService
OrderId = parcelOutput.OrderId,
Items = bindItems
});
var boundOrder = await OrderRepository.FirstOrDefaultAsync(x => x.Id == parcelOutput.OrderId);
if (boundOrder != null)
{
var needUpdate = false;
if (!string.Equals(
boundOrder.FulfillmentSource,
FulfillmentSourceCodes.StoreFulfill,
StringComparison.OrdinalIgnoreCase))
{
boundOrder.FulfillmentSource = FulfillmentSourceCodes.StoreFulfill;
needUpdate = true;
}
if (!FulfillmentTypeCodes.IsWarehouse(boundOrder.FulfillmentType))
{
boundOrder.FulfillmentType = FulfillmentTypeCodes.Warehouse;
needUpdate = true;
}
if (needUpdate)
{
await OrderRepository.UpdateAsync(boundOrder);
}
}
return parcelOutput;
}
catch (Exception)

View File

@ -5,7 +5,9 @@ namespace Oms.Domain.OrdersAggregate.Consts;
/// </summary>
public static class FulfillmentProgressCodes
{
/// <summary>待绑定包裹/库存。</summary>
/// <summary>
/// 历史兼容:仓内代发但无绑定明细。现网「创建代发必绑」,新单不应再落入此进度。
/// </summary>
public const string PendingBind = "pendingBind";
/// <summary>国内快递在途,未到仓。</summary>

View File

@ -1616,10 +1616,10 @@ public class OrderRepository : OmsSqlSugarRepository<Order>, IOrderRepository, I
return await query.CountAsync(cancellationToken);
}
// 创建代发必绑:进度聚合不再返回 pendingBind(筛选接口仍兼容该值)
var result = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
["all"] = await CountAsync(null),
[FulfillmentProgressCodes.PendingBind] = await CountAsync(FulfillmentProgressCodes.PendingBind),
[FulfillmentProgressCodes.DomesticInTransit] = await CountAsync(FulfillmentProgressCodes.DomesticInTransit),
[FulfillmentProgressCodes.ArrivedPendingPick] = await CountAsync(FulfillmentProgressCodes.ArrivedPendingPick),
[FulfillmentProgressCodes.OutboundDone] = await CountAsync(FulfillmentProgressCodes.OutboundDone),