45 lines
1.7 KiB
C#
45 lines
1.7 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 导出账单处理器。
|
|
/// </summary>
|
|
public sealed class ExportBillingsQueryHandler(
|
|
ITenantBillingRepository billingRepository,
|
|
IBillingExportService exportService)
|
|
: IRequestHandler<ExportBillingsQuery, byte[]>
|
|
{
|
|
/// <inheritdoc />
|
|
public async Task<byte[]> 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}")
|
|
};
|
|
}
|
|
}
|