using MediatR; using TakeoutSaaS.Application.App.Billings.Queries; using TakeoutSaaS.Domain.Tenants.Repositories; using TakeoutSaaS.Domain.Tenants.Services; using TakeoutSaaS.Shared.Abstractions.Constants; using TakeoutSaaS.Shared.Abstractions.Exceptions; namespace TakeoutSaaS.Application.App.Billings.Handlers; /// /// 导出账单处理器。 /// public sealed class ExportBillingsQueryHandler( ITenantBillingRepository billingRepository, IBillingExportService exportService) : IRequestHandler { /// public async Task Handle(ExportBillingsQuery request, CancellationToken cancellationToken) { // 1. 参数验证 if (request.BillingIds.Length == 0) { throw new BusinessException(ErrorCodes.BadRequest, "账单 ID 列表不能为空"); } // 2. 查询账单数据 var billings = await billingRepository.GetByIdsAsync(request.BillingIds, cancellationToken); if (billings.Count == 0) { throw new BusinessException(ErrorCodes.NotFound, "未找到任何匹配的账单"); } // 3. 根据格式导出 var format = (request.Format ?? string.Empty).Trim().ToLowerInvariant(); return format switch { "excel" or "xlsx" => await exportService.ExportToExcelAsync(billings, cancellationToken), "pdf" => await exportService.ExportToPdfAsync(billings, cancellationToken), "csv" => await exportService.ExportToCsvAsync(billings, cancellationToken), _ => throw new BusinessException(ErrorCodes.BadRequest, $"不支持的导出格式: {request.Format}") }; } }