using Fake.Application; using Fake.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Oms.Application.Dtos.Orders; using Oms.Application.Messaging; using Oms.Domain.OrdersAggregate.Consts; namespace Oms.Application.Services.Orders.Batch; /// /// 订单状态/支付/异常/单据类批量操作执行器。 /// public sealed class OrderBatchOrderActionHandler(IServiceScopeFactory scopeFactory) : IScopedDependency { public async Task ExecuteAsync( OrderBatchJobMessage message, CancellationToken cancellationToken = default) { try { using var scope = scopeFactory.CreateScope(); var orderService = scope.ServiceProvider.GetRequiredService(); switch (message.Action) { case OrderBatchActionCodes.Pay: await orderService.PayForBatchAsync(message.OrderId, cancellationToken); return OrderBatchActionExecuteResult.Ok(); case OrderBatchActionCodes.PreDeclare: await orderService.PreDeclareForBatchAsync(message.OrderId, cancellationToken); return OrderBatchActionExecuteResult.Ok(); case OrderBatchActionCodes.Intercept: await orderService.InterceptForBatchAsync(message.OrderId, cancellationToken); return OrderBatchActionExecuteResult.Ok(); case OrderBatchActionCodes.CancelIntercept: await orderService.CancelInterceptForBatchAsync(message.OrderId, cancellationToken); return OrderBatchActionExecuteResult.Ok(); case OrderBatchActionCodes.Delete: await orderService.DeleteForBatchAsync(message.OrderId, cancellationToken); return OrderBatchActionExecuteResult.Ok(); case OrderBatchActionCodes.Print: { var print = await orderService.PrintForBatchAsync(message.OrderId, cancellationToken); return OrderBatchActionExecuteResult.Ok( resultJson: JsonConvert.SerializeObject(new { printUrl = print.PrintUrl })); } case OrderBatchActionCodes.GenerateLabelPdf: { var result = await orderService.GenerateLabelPdfForBatchAsync(message.OrderId, cancellationToken); if (!result.Success) { return OrderBatchActionExecuteResult.BusinessFail(result.Error ?? "面单生成失败"); } return OrderBatchActionExecuteResult.Ok( resultJson: JsonConvert.SerializeObject(new { labelUrl = result.LabelUrl })); } case OrderBatchActionCodes.MarkProblematic: await orderService.MarkProblematicForBatchAsync( message.OrderId, message.PayloadJson, message.OperatorUserId, cancellationToken); return OrderBatchActionExecuteResult.Ok(); case OrderBatchActionCodes.MarkClaimed: await orderService.MarkClaimedForBatchAsync( message.OrderId, message.PayloadJson, cancellationToken); return OrderBatchActionExecuteResult.Ok(); case OrderBatchActionCodes.HandleStatusUpdate: await orderService.UpdateHandleStatusForBatchAsync( message.OrderId, message.PayloadJson, cancellationToken); return OrderBatchActionExecuteResult.Ok(); default: return OrderBatchActionExecuteResult.BusinessFail($"未实现的批量操作: {message.Action}"); } } catch (BusinessException ex) { return OrderBatchActionExecuteResult.BusinessFail(ex.Message); } catch (Exception ex) { return OrderBatchActionExecuteResult.SystemFail(ex.Message); } } }