using Fake.Application; using Fake.Security.Claims; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Oms.Application.Dtos.Orders; using Oms.Application.Dtos.Shared; using Oms.Application.Services.Orders.Batch; using Oms.Application.Messaging; using Oms.Application.Shared; using Oms.Domain.CustomersAggregate.Repositories; using Oms.Domain.OrdersAggregate.Consts; namespace Oms.Application.Services.Orders; public partial class OrderService { private OrderBatchJobCoordinator BatchJobCoordinator => LazyServiceProvider.GetRequiredService(); /// /// 创建订单批量异步任务并入队 RabbitMQ。 /// [HttpPost("/order/batch/jobs")] public Task CreateBatchJobAsync([FromBody] OrderBatchJobCreateInput input) { return BatchJobCoordinator.CreateAndEnqueueAsync(input, CurrentUser.GetId()); } /// /// 查询批量任务进度。 /// [HttpGet("/order/batch/jobs/{id:int}")] public async Task GetBatchJobAsync([FromRoute] int id) { var job = await BatchJobCoordinator.GetJobAsync(id); if (job == null) { throw new BusinessException("批量任务不存在"); } return job; } /// /// 分页查询批量任务列表。 /// [HttpGet("/order/batch/jobs")] public async Task> GetBatchJobListAsync([FromQuery] OrderBatchJobListInput input) { var customerId = await OmsSessionHelper.ResolveListCustomerIdAsync( LazyServiceProvider.GetRequiredService(), CurrentUser, null); return await BatchJobCoordinator.GetJobListAsync(input, customerId); } /// /// 查询批量任务子项明细。 /// [HttpGet("/order/batch/jobs/{id:int}/items")] public async Task> GetBatchJobItemsAsync([FromRoute] int id) { var job = await BatchJobCoordinator.GetJobEntityAsync(id); if (job == null) { throw new BusinessException("批量任务不存在"); } return await BatchJobCoordinator.GetJobItemsAsync(id); } /// /// 异步批量创建轨迹订单(替代前端逐条调用)。 /// [HttpPost("/order/batch/track-upsert")] public Task BatchTrackUpsertAsync([FromBody] OrderBatchJobCreateInput input) { input.Action = OrderBatchActionCodes.TrackUpsertSmallBag; var authorization = LazyServiceProvider .GetRequiredService() .HttpContext? .Request .Headers .Authorization .ToString(); return BatchJobCoordinator.CreateAndEnqueueAsync( input, CurrentUser.GetId(), string.IsNullOrWhiteSpace(authorization) ? null : authorization); } /// /// 异步批量预报下游订单。 /// [HttpPost("/order/batch/downstream-forward")] public Task BatchDownstreamForwardAsync([FromBody] OrderBatchJobCreateInput input) { input.Action = OrderBatchActionCodes.DownstreamForwardCreateOne; var authorization = LazyServiceProvider .GetRequiredService() .HttpContext? .Request .Headers .Authorization .ToString(); return BatchJobCoordinator.CreateAndEnqueueAsync( input, CurrentUser.GetId(), string.IsNullOrWhiteSpace(authorization) ? null : authorization); } private Task EnqueueOrderBatchAsync(string action, OrderBatchJobCreateInput input) { input.Action = action; return BatchJobCoordinator.CreateAndEnqueueAsync(input, CurrentUser.GetId()); } /// /// 异步批量支付订单。 /// [HttpPost("/order/batch/pay")] public Task BatchPayAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.Pay, input); /// /// 异步批量预报订单。 /// [HttpPost("/order/batch/pre-declare")] public Task BatchPreDeclareAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.PreDeclare, input); /// /// 异步批量拦截订单。 /// [HttpPost("/order/batch/intercept")] public Task BatchInterceptAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.Intercept, input); /// /// 异步批量取消拦截。 /// [HttpPost("/order/batch/cancel-intercept")] public Task BatchCancelInterceptAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.CancelIntercept, input); /// /// 异步批量删除订单。 /// [HttpPost("/order/batch/delete")] public Task BatchDeleteAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.Delete, input); /// /// 异步批量打印面单。 /// [HttpPost("/order/batch/print")] public Task BatchPrintAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.Print, input); /// /// 异步批量标记问题件。 /// [HttpPost("/order/batch/mark-problematic")] public Task BatchMarkProblematicAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.MarkProblematic, input); /// /// 异步批量重新排货。 /// [HttpPost("/order/batch/reallocate")] public Task BatchReallocateAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.Reallocate, input); /// /// 异步批量标记已认领。 /// [HttpPost("/order/batch/mark-claimed")] public Task BatchMarkClaimedAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.MarkClaimed, input); /// /// 异步批量更新处理状态。 /// [HttpPost("/order/batch/handle-status-update")] public Task BatchHandleStatusUpdateAsync([FromBody] OrderBatchJobCreateInput input) => EnqueueOrderBatchAsync(OrderBatchActionCodes.HandleStatusUpdate, input); /// /// 查询 RabbitMQ 队列与 DLX 堆积情况。 /// [HttpGet("/order/batch/dlx/stats")] public Task GetBatchDlxStatsAsync() { var inspector = LazyServiceProvider.GetRequiredService(); return inspector.GetStatsAsync(); } /// /// 重新投递失败子项。 /// [HttpPost("/order/batch/jobs/{id:int}/retry-failed")] public Task RetryFailedBatchJobAsync( [FromRoute] int id, [FromBody] OrderBatchJobRetryFailedInput? input) { return BatchJobCoordinator.RetryFailedAsync(id, input, CurrentUser.GetId()); } /// /// 取消尚未执行的 pending 子项。 /// [HttpPost("/order/batch/jobs/{id:int}/cancel")] public Task CancelBatchJobAsync([FromRoute] int id) { return BatchJobCoordinator.CancelPendingAsync(id, CurrentUser.GetId()); } }