fix: 平台端查询租户订阅与套餐不再受租户过滤影响

This commit is contained in:
2025-12-15 10:00:31 +08:00
parent f6e7fa2f4a
commit f54d4cf405
3 changed files with 19273 additions and 9 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -170,8 +170,9 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context) : ITenantRep
public Task<TenantVerificationProfile?> GetVerificationProfileAsync(long tenantId, CancellationToken cancellationToken = default) public Task<TenantVerificationProfile?> GetVerificationProfileAsync(long tenantId, CancellationToken cancellationToken = default)
{ {
return context.TenantVerificationProfiles return context.TenantVerificationProfiles
.IgnoreQueryFilters()
.AsNoTracking() .AsNoTracking()
.FirstOrDefaultAsync(x => x.TenantId == tenantId, cancellationToken); .FirstOrDefaultAsync(x => x.DeletedAt == null && x.TenantId == tenantId, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -187,8 +188,9 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context) : ITenantRep
// 2. 批量查询实名资料 // 2. 批量查询实名资料
return await context.TenantVerificationProfiles return await context.TenantVerificationProfiles
.IgnoreQueryFilters()
.AsNoTracking() .AsNoTracking()
.Where(x => tenantIds.Contains(x.TenantId)) .Where(x => x.DeletedAt == null && tenantIds.Contains(x.TenantId))
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
@@ -197,7 +199,8 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context) : ITenantRep
{ {
// 1. 查询现有实名资料 // 1. 查询现有实名资料
var existing = await context.TenantVerificationProfiles var existing = await context.TenantVerificationProfiles
.FirstOrDefaultAsync(x => x.TenantId == profile.TenantId, cancellationToken); .IgnoreQueryFilters()
.FirstOrDefaultAsync(x => x.DeletedAt == null && x.TenantId == profile.TenantId, cancellationToken);
if (existing == null) if (existing == null)
{ {
@@ -215,8 +218,9 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context) : ITenantRep
public Task<TenantSubscription?> GetActiveSubscriptionAsync(long tenantId, CancellationToken cancellationToken = default) public Task<TenantSubscription?> GetActiveSubscriptionAsync(long tenantId, CancellationToken cancellationToken = default)
{ {
return context.TenantSubscriptions return context.TenantSubscriptions
.IgnoreQueryFilters()
.AsNoTracking() .AsNoTracking()
.Where(x => x.TenantId == tenantId) .Where(x => x.DeletedAt == null && x.TenantId == tenantId)
.OrderByDescending(x => x.EffectiveTo) .OrderByDescending(x => x.EffectiveTo)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
} }
@@ -234,8 +238,9 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context) : ITenantRep
// 2. 批量查询订阅数据 // 2. 批量查询订阅数据
return await context.TenantSubscriptions return await context.TenantSubscriptions
.IgnoreQueryFilters()
.AsNoTracking() .AsNoTracking()
.Where(x => tenantIds.Contains(x.TenantId)) .Where(x => x.DeletedAt == null && tenantIds.Contains(x.TenantId))
.OrderByDescending(x => x.EffectiveTo) .OrderByDescending(x => x.EffectiveTo)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
@@ -244,7 +249,10 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context) : ITenantRep
public Task<TenantSubscription?> FindSubscriptionByIdAsync(long tenantId, long subscriptionId, CancellationToken cancellationToken = default) public Task<TenantSubscription?> FindSubscriptionByIdAsync(long tenantId, long subscriptionId, CancellationToken cancellationToken = default)
{ {
return context.TenantSubscriptions return context.TenantSubscriptions
.FirstOrDefaultAsync(x => x.TenantId == tenantId && x.Id == subscriptionId, cancellationToken); .IgnoreQueryFilters()
.FirstOrDefaultAsync(
x => x.DeletedAt == null && x.TenantId == tenantId && x.Id == subscriptionId,
cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -270,8 +278,9 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context) : ITenantRep
public async Task<IReadOnlyList<TenantSubscriptionHistory>> GetSubscriptionHistoryAsync(long tenantId, CancellationToken cancellationToken = default) public async Task<IReadOnlyList<TenantSubscriptionHistory>> GetSubscriptionHistoryAsync(long tenantId, CancellationToken cancellationToken = default)
{ {
return await context.TenantSubscriptionHistories return await context.TenantSubscriptionHistories
.IgnoreQueryFilters()
.AsNoTracking() .AsNoTracking()
.Where(x => x.TenantId == tenantId) .Where(x => x.DeletedAt == null && x.TenantId == tenantId)
.OrderByDescending(x => x.EffectiveFrom) .OrderByDescending(x => x.EffectiveFrom)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
@@ -286,8 +295,9 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context) : ITenantRep
public async Task<IReadOnlyList<TenantAuditLog>> GetAuditLogsAsync(long tenantId, CancellationToken cancellationToken = default) public async Task<IReadOnlyList<TenantAuditLog>> GetAuditLogsAsync(long tenantId, CancellationToken cancellationToken = default)
{ {
return await context.TenantAuditLogs return await context.TenantAuditLogs
.IgnoreQueryFilters()
.AsNoTracking() .AsNoTracking()
.Where(x => x.TenantId == tenantId) .Where(x => x.DeletedAt == null && x.TenantId == tenantId)
.OrderByDescending(x => x.CreatedAt) .OrderByDescending(x => x.CreatedAt)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }

View File

@@ -61,7 +61,7 @@ public abstract class TenantAwareDbContext(
private void SetTenantFilter<TEntity>(ModelBuilder modelBuilder) private void SetTenantFilter<TEntity>(ModelBuilder modelBuilder)
where TEntity : class, IMultiTenantEntity where TEntity : class, IMultiTenantEntity
{ {
modelBuilder.Entity<TEntity>().HasQueryFilter(entity => CurrentTenantId == 0 || entity.TenantId == CurrentTenantId); modelBuilder.Entity<TEntity>().HasQueryFilter(entity => entity.TenantId == CurrentTenantId);
} }
/// <summary> /// <summary>