fix: 轨迹订单 trackNo 仅取跟踪号,变更后重新 Upsert
All checks were successful
Build and Deploy / deploy (push) Successful in 1m1s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zzh 2026-10-10 19:20:09 +08:00
parent fa9777bd08
commit 32156dcfe0
6 changed files with 60 additions and 12 deletions

View File

@ -67,16 +67,11 @@ internal sealed class SmallBagTrackUpsertRequestBuilder(
};
}
internal static string ResolveTrackNo(Order order)
{
var trackNo = order.TrackNo?.Trim();
if (!string.IsNullOrWhiteSpace(trackNo))
{
return trackNo;
}
return order.ServiceNo?.Trim() ?? string.Empty;
}
/// <summary>
/// 仅取运单跟踪号;无值则空串,不回落运单号/客户单号等。
/// </summary>
internal static string ResolveTrackNo(Order order) =>
order.TrackNo?.Trim() ?? string.Empty;
internal static string? PickSupplierCodeFromProviders(
IEnumerable<SalesProductAllocationProviderSnapshotDto> providers,

View File

@ -6,6 +6,7 @@ using Oms.Application.Dtos.Orders;
using Oms.Application.Integrations.Itfx;
using Oms.Application.Integrations.SmallBagTrack;
using Oms.Application.Messaging;
using Oms.Application.Messaging.ParcelCreate;
using Oms.Application.Shared;
using Oms.Application.Storage;
using Oms.Domain.ChannelsAggregate.Repositories;
@ -23,6 +24,7 @@ public sealed class OrderBatchActionExecutor(
IObjectStorage fileStorage,
ISmallBagTrackOrderClient smallBagTrackOrderClient,
ISmallBagTrackUpsertRequestBuilder smallBagTrackUpsertRequestBuilder,
IOrderParcelCreateEventPublisher parcelCreateEventPublisher,
OrderDownstreamCredentialResolver downstreamCredentialResolver,
IItfxForwardClient itfxForwardClient,
OrderFileManager orderFileManager,
@ -231,9 +233,31 @@ public sealed class OrderBatchActionExecutor(
if (ItfxCreateOrderOneDataParser.TryParse(forward.ResponseBody, out var data)
&& data != null)
{
var previousTrackNo = order.TrackNo;
if (ItfxCreateOrderOneDataParser.ApplyToOrder(order, data))
{
await orderRepository.UpdateAsync(order, cancellationToken);
var nextTrackNo = order.TrackNo?.Trim() ?? string.Empty;
var prevTrackNo = previousTrackNo?.Trim() ?? string.Empty;
if (!string.IsNullOrWhiteSpace(nextTrackNo)
&& !string.Equals(nextTrackNo, prevTrackNo, StringComparison.Ordinal))
{
var trackRequest = await smallBagTrackUpsertRequestBuilder.BuildAsync(
order,
cancellationToken: cancellationToken);
trackRequest.AppendInitialTrackOnSuccess = true;
if (!string.IsNullOrWhiteSpace(authorization))
{
trackRequest.Authorization = authorization.Trim();
}
await parcelCreateEventPublisher.PublishTrackUpsertAsync(
order,
trackRequest,
customerId: order.CustomerId,
cancellationToken);
}
}
if (!string.IsNullOrWhiteSpace(data.LabelUrl))

View File

@ -57,9 +57,11 @@ public partial class OrderService
&& ItfxCreateOrderOneDataParser.TryParse(forward.ResponseBody, out var data)
&& data != null)
{
var previousTrackNo = order.TrackNo;
if (ItfxCreateOrderOneDataParser.ApplyToOrder(order, data))
{
await OrderRepository.UpdateAsync(order);
await EnqueueSmallBagTrackUpsertWhenTrackNoChangedAsync(order, previousTrackNo);
}
if (!string.IsNullOrWhiteSpace(data.LabelUrl))
@ -118,9 +120,11 @@ public partial class OrderService
&& ItfxGetFileDataParser.TryParse(forward.ResponseBody, out var data)
&& data != null)
{
var previousTrackNo = order.TrackNo;
if (ItfxGetFileDataParser.ApplyToOrder(order, data))
{
await OrderRepository.UpdateAsync(order);
await EnqueueSmallBagTrackUpsertWhenTrackNoChangedAsync(order, previousTrackNo);
}
if (!string.IsNullOrWhiteSpace(data.Url))

View File

@ -48,6 +48,7 @@ public partial class OrderService
order.ClientNo = newClientNo;
}
var previousTrackNo = order.TrackNo;
if (input.TrackNo != null)
{
order.TrackNo = string.IsNullOrWhiteSpace(input.TrackNo) ? null : input.TrackNo.Trim();
@ -155,6 +156,11 @@ public partial class OrderService
afterSnap,
remark);
if (input.TrackNo != null)
{
await EnqueueSmallBagTrackUpsertWhenTrackNoChangedAsync(order, previousTrackNo);
}
return (await MapOrderOutputsAsync([order])).First();
}

View File

@ -3201,6 +3201,25 @@ public partial class OrderService
await ParcelCreateEventPublisher.PublishTrackUpsertAsync(order, request, customer?.Id);
}
/// <summary>
/// 跟踪号相对旧值有变更且新值非空时,再创建/更新一次轨迹订单。
/// </summary>
private async Task EnqueueSmallBagTrackUpsertWhenTrackNoChangedAsync(
Order order,
string? previousTrackNo,
bool appendInitialTrack = true)
{
var next = order.TrackNo?.Trim() ?? string.Empty;
var prev = previousTrackNo?.Trim() ?? string.Empty;
if (string.IsNullOrWhiteSpace(next)
|| string.Equals(next, prev, StringComparison.Ordinal))
{
return;
}
await EnqueueSmallBagTrackUpsertAsync(order, customer: null, receiver: null, appendInitialTrack);
}
private async Task EnqueueLifecycleTrackAppendAsync(Order order, string trackCode)
{
if (string.IsNullOrWhiteSpace(order.ServiceNo))

View File

@ -20,7 +20,7 @@ public class SmallBagTrackUpsertRequestBuilderTests
}
[Fact]
public void ResolveTrackNo_FallsBackToServiceNoWhenTrackNoEmpty()
public void ResolveTrackNo_EmptyWhenTrackNoMissing()
{
var order = new Order
{
@ -28,7 +28,7 @@ public class SmallBagTrackUpsertRequestBuilderTests
TrackNo = null
};
Assert.Equal("IDE2607221859123456", SmallBagTrackUpsertRequestBuilder.ResolveTrackNo(order));
Assert.Equal(string.Empty, SmallBagTrackUpsertRequestBuilder.ResolveTrackNo(order));
}
[Fact]