feat: 新增配额包/支付相关实体与迁移
App:新增 operation_logs/quota_packages/tenant_payments/tenant_quota_package_purchases 表 Identity:修正 Avatar 字段类型(varchar(256)->text),保持现有数据不变
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TakeoutSaaS.Domain.Tenants.Entities;
|
||||
using TakeoutSaaS.Domain.Tenants.Enums;
|
||||
using TakeoutSaaS.Domain.Tenants.Repositories;
|
||||
using TakeoutSaaS.Infrastructure.App.Persistence;
|
||||
|
||||
namespace TakeoutSaaS.Infrastructure.App.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// 订阅管理仓储实现。
|
||||
/// </summary>
|
||||
public sealed class EfSubscriptionRepository(TakeoutAppDbContext dbContext) : ISubscriptionRepository
|
||||
{
|
||||
#region 订阅查询
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<TenantSubscription?> FindByIdAsync(long subscriptionId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.TenantSubscriptions
|
||||
.FirstOrDefaultAsync(s => s.Id == subscriptionId, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<TenantSubscription>> FindByIdsAsync(
|
||||
IEnumerable<long> subscriptionIds,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var ids = subscriptionIds.ToList();
|
||||
return await dbContext.TenantSubscriptions
|
||||
.Where(s => ids.Contains(s.Id))
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<(IReadOnlyList<SubscriptionWithRelations> Items, int Total)> SearchPagedAsync(
|
||||
SubscriptionSearchFilter filter,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 构建基础查询
|
||||
var query = dbContext.TenantSubscriptions
|
||||
.AsNoTracking()
|
||||
.Join(
|
||||
dbContext.Tenants,
|
||||
sub => sub.TenantId,
|
||||
tenant => tenant.Id,
|
||||
(sub, tenant) => new { Subscription = sub, Tenant = tenant }
|
||||
)
|
||||
.Join(
|
||||
dbContext.TenantPackages,
|
||||
combined => combined.Subscription.TenantPackageId,
|
||||
package => package.Id,
|
||||
(combined, package) => new { combined.Subscription, combined.Tenant, Package = package }
|
||||
)
|
||||
.GroupJoin(
|
||||
dbContext.TenantPackages,
|
||||
combined => combined.Subscription.ScheduledPackageId,
|
||||
scheduledPackage => scheduledPackage.Id,
|
||||
(combined, scheduledPackages) => new { combined.Subscription, combined.Tenant, combined.Package, ScheduledPackage = scheduledPackages.FirstOrDefault() }
|
||||
);
|
||||
|
||||
// 2. 应用过滤条件
|
||||
if (filter.Status.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Subscription.Status == filter.Status.Value);
|
||||
}
|
||||
|
||||
if (filter.TenantPackageId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Subscription.TenantPackageId == filter.TenantPackageId.Value);
|
||||
}
|
||||
|
||||
if (filter.TenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Subscription.TenantId == filter.TenantId.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.TenantKeyword))
|
||||
{
|
||||
var keyword = filter.TenantKeyword.Trim().ToLower();
|
||||
query = query.Where(x => x.Tenant.Name.ToLower().Contains(keyword) || x.Tenant.Code.ToLower().Contains(keyword));
|
||||
}
|
||||
|
||||
if (filter.ExpiringWithinDays.HasValue)
|
||||
{
|
||||
var expiryDate = DateTime.UtcNow.AddDays(filter.ExpiringWithinDays.Value);
|
||||
query = query.Where(x => x.Subscription.EffectiveTo <= expiryDate && x.Subscription.EffectiveTo >= DateTime.UtcNow);
|
||||
}
|
||||
|
||||
if (filter.AutoRenew.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Subscription.AutoRenew == filter.AutoRenew.Value);
|
||||
}
|
||||
|
||||
// 3. 获取总数
|
||||
var total = await query.CountAsync(cancellationToken);
|
||||
|
||||
// 4. 排序和分页
|
||||
var items = await query
|
||||
.OrderByDescending(x => x.Subscription.CreatedAt)
|
||||
.Skip((filter.Page - 1) * filter.PageSize)
|
||||
.Take(filter.PageSize)
|
||||
.Select(x => new SubscriptionWithRelations
|
||||
{
|
||||
Subscription = x.Subscription,
|
||||
TenantName = x.Tenant.Name,
|
||||
TenantCode = x.Tenant.Code,
|
||||
PackageName = x.Package.Name,
|
||||
ScheduledPackageName = x.ScheduledPackage != null ? x.ScheduledPackage.Name : null
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return (items, total);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<SubscriptionDetailInfo?> GetDetailAsync(long subscriptionId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var result = await dbContext.TenantSubscriptions
|
||||
.AsNoTracking()
|
||||
.Where(s => s.Id == subscriptionId)
|
||||
.Select(s => new
|
||||
{
|
||||
Subscription = s,
|
||||
Tenant = dbContext.Tenants.FirstOrDefault(t => t.Id == s.TenantId),
|
||||
Package = dbContext.TenantPackages.FirstOrDefault(p => p.Id == s.TenantPackageId),
|
||||
ScheduledPackage = s.ScheduledPackageId.HasValue
|
||||
? dbContext.TenantPackages.FirstOrDefault(p => p.Id == s.ScheduledPackageId)
|
||||
: null
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new SubscriptionDetailInfo
|
||||
{
|
||||
Subscription = result.Subscription,
|
||||
TenantName = result.Tenant?.Name ?? "",
|
||||
TenantCode = result.Tenant?.Code ?? "",
|
||||
Package = result.Package,
|
||||
ScheduledPackage = result.ScheduledPackage
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<SubscriptionWithTenant>> FindByIdsWithTenantAsync(
|
||||
IEnumerable<long> subscriptionIds,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var ids = subscriptionIds.ToList();
|
||||
return await dbContext.TenantSubscriptions
|
||||
.Where(s => ids.Contains(s.Id))
|
||||
.Join(
|
||||
dbContext.Tenants,
|
||||
sub => sub.TenantId,
|
||||
tenant => tenant.Id,
|
||||
(sub, tenant) => new SubscriptionWithTenant
|
||||
{
|
||||
Subscription = sub,
|
||||
Tenant = tenant
|
||||
}
|
||||
)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 套餐查询
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<TenantPackage?> FindPackageByIdAsync(long packageId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.TenantPackages
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(p => p.Id == packageId, cancellationToken);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 订阅更新
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task UpdateAsync(TenantSubscription subscription, CancellationToken cancellationToken = default)
|
||||
{
|
||||
dbContext.TenantSubscriptions.Update(subscription);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 订阅历史
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task AddHistoryAsync(TenantSubscriptionHistory history, CancellationToken cancellationToken = default)
|
||||
{
|
||||
dbContext.TenantSubscriptionHistories.Add(history);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<SubscriptionHistoryWithPackageNames>> GetHistoryAsync(
|
||||
long subscriptionId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.TenantSubscriptionHistories
|
||||
.AsNoTracking()
|
||||
.Where(h => h.TenantSubscriptionId == subscriptionId)
|
||||
.OrderByDescending(h => h.CreatedAt)
|
||||
.Select(h => new SubscriptionHistoryWithPackageNames
|
||||
{
|
||||
History = h,
|
||||
FromPackageName = dbContext.TenantPackages
|
||||
.Where(p => p.Id == h.FromPackageId)
|
||||
.Select(p => p.Name)
|
||||
.FirstOrDefault() ?? "",
|
||||
ToPackageName = dbContext.TenantPackages
|
||||
.Where(p => p.Id == h.ToPackageId)
|
||||
.Select(p => p.Name)
|
||||
.FirstOrDefault() ?? ""
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 配额使用
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<TenantQuotaUsage>> GetQuotaUsagesAsync(
|
||||
long tenantId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.TenantQuotaUsages
|
||||
.AsNoTracking()
|
||||
.Where(q => q.TenantId == tenantId)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 通知
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task AddNotificationAsync(TenantNotification notification, CancellationToken cancellationToken = default)
|
||||
{
|
||||
dbContext.TenantNotifications.Add(notification);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 操作日志
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task AddOperationLogAsync(OperationLog log, CancellationToken cancellationToken = default)
|
||||
{
|
||||
dbContext.Set<OperationLog>().Add(log);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user