fengling-platform/Fengling.Platform.Infrastructure/Repositories/TenantRepository.cs
movingsam 0bbc97e4a1 refactor: align TenantRepository with CleanDDD/NetCorePal规范
- remove duplicate ITenantRepository/TenantRepository from Console
- extend Platform ITenantRepository with GetByIdAsync, GetPagedAsync, CountAsync
- update Console services to use Platform.Infrastructure.Repositories
- fix nullable warnings (UserDto, OAuthClientService)
- fix YarpGateway Directory.Build.props duplicate import
- fix DynamicProxyConfigProvider CS8618 warning
2026-02-19 19:20:06 +08:00

79 lines
3.3 KiB
C#

using NetCorePal.Extensions.Repository.EntityFrameworkCore;
namespace Fengling.Platform.Infrastructure.Repositories;
using Fengling.Platform.Domain.AggregatesModel.TenantAggregate;
using NetCorePal.Extensions.Repository;
using Microsoft.EntityFrameworkCore;
public interface ITenantRepository : IRepository<Tenant, TenantId>
{
Task<Tenant?> GetByTenantIdAsync(string tenantId, CancellationToken cancellationToken = default);
Task<Tenant?> GetByIdIncludeH5Async(TenantId id, CancellationToken cancellationToken = default);
Task<Tenant?> GetByIdAsync(long id, CancellationToken cancellationToken = default);
Task<IEnumerable<Tenant>> GetPagedAsync(int page, int pageSize, string? name = null, string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default);
Task<int> CountAsync(string? name = null, string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default);
Task<int> GetUserCountAsync(TenantId tenantId, CancellationToken cancellationToken = default);
}
public class TenantRepository(PlatformDbContext context)
: RepositoryBase<Tenant, TenantId, PlatformDbContext>(context), ITenantRepository
{
public async Task<Tenant?> GetByTenantIdAsync(string tenantId, CancellationToken cancellationToken = default)
{
return await DbContext.Tenants.FirstOrDefaultAsync(t => t.TenantCode == tenantId, cancellationToken);
}
public async Task<Tenant?> GetByIdIncludeH5Async(TenantId id, CancellationToken cancellationToken = default)
{
return await DbContext.Tenants.FirstOrDefaultAsync(t => t.Id == id, cancellationToken);
}
public async Task<Tenant?> GetByIdAsync(long id, CancellationToken cancellationToken = default)
{
return await DbContext.Tenants.FindAsync(new object[] { id }, cancellationToken);
}
public async Task<IEnumerable<Tenant>> GetPagedAsync(int page, int pageSize, string? name = null,
string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default)
{
var query = DbContext.Tenants.AsQueryable();
if (!string.IsNullOrEmpty(name))
query = query.Where(t => t.Name.Contains(name));
if (!string.IsNullOrEmpty(tenantCode))
query = query.Where(t => t.TenantCode.Contains(tenantCode));
if (status.HasValue)
query = query.Where(t => t.Status == status);
return await query
.OrderByDescending(t => t.CreatedAt)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync(cancellationToken);
}
public async Task<int> CountAsync(string? name = null, string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default)
{
var query = DbContext.Tenants.AsQueryable();
if (!string.IsNullOrEmpty(name))
query = query.Where(t => t.Name.Contains(name));
if (!string.IsNullOrEmpty(tenantCode))
query = query.Where(t => t.TenantCode.Contains(tenantCode));
if (status.HasValue)
query = query.Where(t => t.Status == status);
return await query.CountAsync(cancellationToken);
}
public async Task<int> GetUserCountAsync(TenantId tenantId, CancellationToken cancellationToken = default)
{
return 0;
}
}