using MediatR;
using Microsoft.AspNetCore.Http;
using TakeoutSaaS.Application.App.Subscriptions.Dto;
using TakeoutSaaS.Application.App.Subscriptions.Queries;
using TakeoutSaaS.Domain.Tenants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Results;
namespace TakeoutSaaS.Application.App.Subscriptions.Handlers;
///
/// 订阅分页查询处理器。
///
public sealed class GetSubscriptionListQueryHandler(
ISubscriptionRepository subscriptionRepository,
IHttpContextAccessor httpContextAccessor)
: IRequestHandler>
{
///
public async Task> Handle(GetSubscriptionListQuery request, CancellationToken cancellationToken)
{
var ignoreTenantFilter = SubscriptionTenantAccess.ShouldIgnoreTenantFilter(httpContextAccessor);
// 1. 构建查询过滤条件
var filter = new SubscriptionSearchFilter
{
Status = request.Status,
TenantPackageId = request.TenantPackageId,
TenantId = request.TenantId,
TenantKeyword = request.TenantKeyword,
ExpiringWithinDays = request.ExpiringWithinDays,
AutoRenew = request.AutoRenew,
Page = request.Page,
PageSize = request.PageSize
};
// 2. 执行分页查询
var (items, total) = await subscriptionRepository.SearchPagedAsync(
filter,
cancellationToken,
ignoreTenantFilter: ignoreTenantFilter);
// 3. 映射为 DTO
var dtos = items.Select(x => new SubscriptionListDto
{
Id = x.Subscription.Id,
TenantId = x.Subscription.TenantId,
TenantName = x.TenantName,
TenantCode = x.TenantCode,
TenantPackageId = x.Subscription.TenantPackageId,
PackageName = x.PackageName,
ScheduledPackageId = x.Subscription.ScheduledPackageId,
ScheduledPackageName = x.ScheduledPackageName,
Status = x.Subscription.Status,
EffectiveFrom = x.Subscription.EffectiveFrom,
EffectiveTo = x.Subscription.EffectiveTo,
NextBillingDate = x.Subscription.NextBillingDate,
AutoRenew = x.Subscription.AutoRenew,
Notes = x.Subscription.Notes,
CreatedAt = x.Subscription.CreatedAt,
UpdatedAt = x.Subscription.UpdatedAt
}).ToList();
// 4. 返回分页结果
return new PagedResult(dtos, request.Page, request.PageSize, total);
}
}