From c7307c1942cb31910c96502e215f7ef7ceff4e38 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 29 Jan 2026 12:15:19 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E5=AE=A1=E6=A0=B8=E9=A2=86=E5=8F=96=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Tenants/Entities/TenantReviewClaim.cs | 34 ------------ .../Tenants/Repositories/ITenantRepository.cs | 32 ----------- .../App/Persistence/TakeoutAppDbContext.cs | 22 +------- .../App/Repositories/EfTenantRepository.cs | 54 ------------------- 4 files changed, 2 insertions(+), 140 deletions(-) delete mode 100644 src/Domain/TakeoutSaaS.Domain/Tenants/Entities/TenantReviewClaim.cs diff --git a/src/Domain/TakeoutSaaS.Domain/Tenants/Entities/TenantReviewClaim.cs b/src/Domain/TakeoutSaaS.Domain/Tenants/Entities/TenantReviewClaim.cs deleted file mode 100644 index 33e2948..0000000 --- a/src/Domain/TakeoutSaaS.Domain/Tenants/Entities/TenantReviewClaim.cs +++ /dev/null @@ -1,34 +0,0 @@ -using TakeoutSaaS.Shared.Abstractions.Entities; - -namespace TakeoutSaaS.Domain.Tenants.Entities; - -/// -/// 租户入驻审核领取记录(防止多管理员并发审核)。 -/// -public sealed class TenantReviewClaim : AuditableEntityBase -{ - /// - /// 被领取的租户 ID。 - /// - public long TenantId { get; set; } - - /// - /// 领取人用户 ID。 - /// - public long ClaimedBy { get; set; } - - /// - /// 领取人名称(展示用快照)。 - /// - public string ClaimedByName { get; set; } = string.Empty; - - /// - /// 领取时间(UTC)。 - /// - public DateTime ClaimedAt { get; set; } - - /// - /// 释放时间(UTC),未释放时为 null。 - /// - public DateTime? ReleasedAt { get; set; } -} diff --git a/src/Domain/TakeoutSaaS.Domain/Tenants/Repositories/ITenantRepository.cs b/src/Domain/TakeoutSaaS.Domain/Tenants/Repositories/ITenantRepository.cs index 74754f1..3046546 100644 --- a/src/Domain/TakeoutSaaS.Domain/Tenants/Repositories/ITenantRepository.cs +++ b/src/Domain/TakeoutSaaS.Domain/Tenants/Repositories/ITenantRepository.cs @@ -135,38 +135,6 @@ public interface ITenantRepository /// 异步任务。 Task UpsertVerificationProfileAsync(TenantVerificationProfile profile, CancellationToken cancellationToken = default); - /// - /// 获取当前审核领取信息(仅返回未释放的记录)。 - /// - /// 租户 ID(雪花算法)。 - /// 取消标记。 - /// 领取记录,未领取返回 null。 - Task GetActiveReviewClaimAsync(long tenantId, CancellationToken cancellationToken = default); - - /// - /// 查询当前审核领取信息(用于更新,返回可跟踪实体)。 - /// - /// 租户 ID(雪花算法)。 - /// 取消标记。 - /// 领取记录,未领取返回 null。 - Task FindActiveReviewClaimAsync(long tenantId, CancellationToken cancellationToken = default); - - /// - /// 新增审核领取记录。 - /// - /// 领取记录。 - /// 领取动作审计日志。 - /// 取消标记。 - /// 新增成功返回 true;若已被其他人领取导致冲突则返回 false。 - Task TryAddReviewClaimAsync(TenantReviewClaim claim, TenantAuditLog auditLog, CancellationToken cancellationToken = default); - - /// - /// 更新审核领取记录。 - /// - /// 领取记录。 - /// 取消标记。 - Task UpdateReviewClaimAsync(TenantReviewClaim claim, CancellationToken cancellationToken = default); - /// /// 获取当前订阅。 /// diff --git a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Persistence/TakeoutAppDbContext.cs b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Persistence/TakeoutAppDbContext.cs index d402a15..c0f05da 100644 --- a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Persistence/TakeoutAppDbContext.cs +++ b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Persistence/TakeoutAppDbContext.cs @@ -88,10 +88,6 @@ public sealed class TakeoutAppDbContext( /// public DbSet TenantVerificationProfiles => Set(); /// - /// 租户审核领取记录。 - /// - public DbSet TenantReviewClaims => Set(); - /// /// 配额包定义。 /// public DbSet QuotaPackages => Set(); @@ -398,7 +394,6 @@ public sealed class TakeoutAppDbContext( ConfigureTenantAnnouncement(modelBuilder.Entity()); ConfigureTenantAnnouncementRead(modelBuilder.Entity()); ConfigureTenantVerificationProfile(modelBuilder.Entity()); - ConfigureTenantReviewClaim(modelBuilder.Entity()); ConfigureQuotaPackage(modelBuilder.Entity()); ConfigureTenantQuotaPackagePurchase(modelBuilder.Entity()); ConfigureMerchantDocument(modelBuilder.Entity()); @@ -469,6 +464,7 @@ public sealed class TakeoutAppDbContext( ConfigureMetricSnapshot(modelBuilder.Entity()); ConfigureMetricAlertRule(modelBuilder.Entity()); + // 3. 应用多租户全局查询过滤器 ApplyTenantQueryFilters(modelBuilder); } @@ -509,21 +505,7 @@ public sealed class TakeoutAppDbContext( builder.HasIndex(x => x.TenantId).IsUnique(); } - private static void ConfigureTenantReviewClaim(EntityTypeBuilder builder) - { - builder.ToTable("tenant_review_claims"); - builder.HasKey(x => x.Id); - builder.Property(x => x.TenantId).IsRequired(); - builder.Property(x => x.ClaimedBy).IsRequired(); - builder.Property(x => x.ClaimedByName).HasMaxLength(64).IsRequired(); - builder.Property(x => x.ClaimedAt).IsRequired(); - builder.Property(x => x.ReleasedAt); - builder.HasIndex(x => x.TenantId); - builder.HasIndex(x => x.ClaimedBy); - builder.HasIndex(x => x.TenantId).IsUnique().HasFilter("\"ReleasedAt\" IS NULL AND \"DeletedAt\" IS NULL"); - } - - + // 租户订阅历史映射 private static void ConfigureTenantSubscriptionHistory(EntityTypeBuilder builder) { builder.ToTable("tenant_subscription_histories"); diff --git a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfTenantRepository.cs b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfTenantRepository.cs index 8e5c4f9..a25bae8 100644 --- a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfTenantRepository.cs +++ b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfTenantRepository.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using Npgsql; using TakeoutSaaS.Domain.Tenants.Entities; using TakeoutSaaS.Domain.Tenants.Enums; using TakeoutSaaS.Domain.Tenants.Repositories; @@ -251,59 +250,6 @@ public sealed class EfTenantRepository(TakeoutAppDbContext context, TakeoutLogsD context.Entry(existing).CurrentValues.SetValues(profile); } - /// - public Task GetActiveReviewClaimAsync(long tenantId, CancellationToken cancellationToken = default) - { - return context.TenantReviewClaims - .AsNoTracking() - .Where(x => x.TenantId == tenantId && x.ReleasedAt == null) - .OrderByDescending(x => x.ClaimedAt) - .FirstOrDefaultAsync(cancellationToken); - } - - /// - public Task FindActiveReviewClaimAsync(long tenantId, CancellationToken cancellationToken = default) - { - return context.TenantReviewClaims - .Where(x => x.TenantId == tenantId && x.ReleasedAt == null) - .OrderByDescending(x => x.ClaimedAt) - .FirstOrDefaultAsync(cancellationToken); - } - - /// - public async Task TryAddReviewClaimAsync( - TenantReviewClaim claim, - TenantAuditLog auditLog, - CancellationToken cancellationToken = default) - { - try - { - // 1. 写入领取记录 - await context.TenantReviewClaims.AddAsync(claim, cancellationToken); - await context.SaveChangesAsync(cancellationToken); - - // 2. 写入审计日志 - await logsContext.TenantAuditLogs.AddAsync(auditLog, cancellationToken); - await logsContext.SaveChangesAsync(cancellationToken); - return true; - } - catch (DbUpdateException ex) when (ex.InnerException is PostgresException pg && pg.SqlState == PostgresErrorCodes.UniqueViolation) - { - // 1. 释放实体跟踪避免重复写入 - context.Entry(claim).State = EntityState.Detached; - - // 2. 返回抢占失败 - return false; - } - } - - /// - public Task UpdateReviewClaimAsync(TenantReviewClaim claim, CancellationToken cancellationToken = default) - { - context.TenantReviewClaims.Update(claim); - return Task.CompletedTask; - } - /// public Task GetActiveSubscriptionAsync(long tenantId, CancellationToken cancellationToken = default) {