feat: 完善手动创建租户功能,添加账单明细和支付记录
1. 手动创建租户时自动生成账单明细(LineItemsJson) 2. 账单状态为已支付时自动创建支付记录 3. 租户列表接口返回联系人信息和认证状态 4. 账单详情接口返回支付记录和解析后的账单明细 5. 管理员账号自动复用租户联系人信息 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -238,4 +238,15 @@ public sealed class EfBillingRepository(TakeoutAdminDbContext context) : IBillin
|
||||
// 1. 添加支付记录
|
||||
await context.TenantPayments.AddAsync(payment, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<TenantPayment>> GetPaymentsByBillingIdAsync(long billingStatementId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 查询账单的支付记录(排除已删除,按创建时间排序)
|
||||
return await context.TenantPayments
|
||||
.AsNoTracking()
|
||||
.Where(p => p.BillingStatementId == billingStatementId && p.DeletedAt == null)
|
||||
.OrderBy(p => p.CreatedAt)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ using TakeoutSaaS.Infrastructure.App.Persistence;
|
||||
namespace TakeoutSaaS.Infrastructure.App.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// 租户只读仓储实现(AdminApi 使用)。
|
||||
/// 租户仓储实现(AdminApi 使用)。
|
||||
/// </summary>
|
||||
public sealed class EfTenantRepository(TakeoutAdminDbContext context) : ITenantRepository
|
||||
{
|
||||
@@ -36,6 +36,24 @@ public sealed class EfTenantRepository(TakeoutAdminDbContext context) : ITenantR
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> ExistsByCodeAsync(string code, long? excludeTenantId = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 构建查询
|
||||
var query = context.Tenants
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Code == code && x.DeletedAt == null);
|
||||
|
||||
// 2. 排除指定租户
|
||||
if (excludeTenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Id != excludeTenantId.Value);
|
||||
}
|
||||
|
||||
// 3. 返回是否存在
|
||||
return await query.AnyAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<Tenant>> GetAllAsync(string? keyword, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -123,4 +141,99 @@ public sealed class EfTenantRepository(TakeoutAdminDbContext context) : ITenantR
|
||||
// 4. 返回结果
|
||||
return (items, totalCount);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task CreateTenantWithRelatedDataAsync(
|
||||
Tenant tenant,
|
||||
TenantSubscription subscription,
|
||||
TenantVerificationProfile verification,
|
||||
IReadOnlyList<TenantQuotaUsage> quotaUsages,
|
||||
TenantBillingStatement? billing,
|
||||
TenantPayment? payment,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 使用执行策略保证可靠性
|
||||
var strategy = context.Database.CreateExecutionStrategy();
|
||||
await strategy.ExecuteAsync(async () =>
|
||||
{
|
||||
// 2. 开启事务
|
||||
await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
// 3. 批量添加实体
|
||||
await context.Tenants.AddAsync(tenant, cancellationToken);
|
||||
await context.TenantSubscriptions.AddAsync(subscription, cancellationToken);
|
||||
await context.TenantVerificationProfiles.AddAsync(verification, cancellationToken);
|
||||
|
||||
// 4. 添加配额使用记录
|
||||
if (quotaUsages.Count > 0)
|
||||
{
|
||||
await context.TenantQuotaUsages.AddRangeAsync(quotaUsages, cancellationToken);
|
||||
}
|
||||
|
||||
// 5. 添加账单记录
|
||||
if (billing is not null)
|
||||
{
|
||||
await context.TenantBillingStatements.AddAsync(billing, cancellationToken);
|
||||
}
|
||||
|
||||
// 6. 添加支付记录
|
||||
if (payment is not null)
|
||||
{
|
||||
await context.TenantPayments.AddAsync(payment, cancellationToken);
|
||||
}
|
||||
|
||||
// 7. 保存变更
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 8. 提交事务
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<Tenant?> GetByIdForUpdateAsync(long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 带跟踪查询租户(用于更新)
|
||||
return context.Tenants
|
||||
.Where(x => x.Id == tenantId && x.DeletedAt == null)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<TenantVerificationProfile?> GetVerificationForUpdateAsync(long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 带跟踪查询认证资料(取最新一条,用于更新)
|
||||
return context.TenantVerificationProfiles
|
||||
.Where(v => v.TenantId == tenantId && v.DeletedAt == null)
|
||||
.OrderByDescending(v => v.CreatedAt)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task AddAsync(Tenant tenant, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 新增租户
|
||||
await context.Tenants.AddAsync(tenant, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task AddSubscriptionAsync(TenantSubscription subscription, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 新增订阅
|
||||
await context.TenantSubscriptions.AddAsync(subscription, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task AddVerificationAsync(TenantVerificationProfile verification, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 新增认证资料
|
||||
await context.TenantVerificationProfiles.AddAsync(verification, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 保存变更
|
||||
return context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user