using Fake.DependencyInjection; using Fake.EventBus; using Fake.UnitOfWork; using Oms.Application.Messaging; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Oms.Application.Services.Orders; namespace Oms.Application.Messaging.ParcelCreate; /// /// 处理创单后系统面单生成事件。 /// public sealed class OrderParcelLabelGenerateEventHandler( IServiceScopeFactory scopeFactory, IUnitOfWorkManager unitOfWorkManager, ILogger logger) : IEventHandler, ITransientDependency { public async Task HandleAsync( OrderParcelLabelGenerateIntegrationEvent @event, CancellationToken cancellationToken) { using var unitOfWork = unitOfWorkManager.Begin(OmsOutboxUnitOfWork.Transactional()); using var scope = scopeFactory.CreateScope(); var orderService = scope.ServiceProvider.GetRequiredService(); var result = await orderService.GenerateLabelPdfForBatchAsync(@event.OrderId, cancellationToken); if (!result.Success) { // 面单失败不阻断创单主链路;记录后正常 Ack,避免反复重试打满队列 logger.LogWarning( "Label generate failed OrderId={OrderId} Error={Error}", @event.OrderId, result.Error); } await unitOfWork.CompleteAsync(cancellationToken); } }