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 { Task GetByTenantIdAsync(string tenantId, CancellationToken cancellationToken = default); Task GetByIdIncludeH5Async(TenantId id, CancellationToken cancellationToken = default); Task GetByIdAsync(long id, CancellationToken cancellationToken = default); Task> GetPagedAsync(int page, int pageSize, string? name = null, string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default); Task CountAsync(string? name = null, string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default); Task GetUserCountAsync(TenantId tenantId, CancellationToken cancellationToken = default); } public class TenantRepository(PlatformDbContext context) : RepositoryBase(context), ITenantRepository { public async Task GetByTenantIdAsync(string tenantId, CancellationToken cancellationToken = default) { return await DbContext.Tenants.FirstOrDefaultAsync(t => t.TenantCode == tenantId, cancellationToken); } public async Task GetByIdIncludeH5Async(TenantId id, CancellationToken cancellationToken = default) { return await DbContext.Tenants.FirstOrDefaultAsync(t => t.Id == id, cancellationToken); } public async Task GetByIdAsync(long id, CancellationToken cancellationToken = default) { return await DbContext.Tenants.FindAsync(new object[] { id }, cancellationToken); } public async Task> 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 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 GetUserCountAsync(TenantId tenantId, CancellationToken cancellationToken = default) { return 0; } }