using BBM.ChatGLM.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;

namespace BBM.ChatGLM.User
{
    public class UserRepository : EfCoreRepository<IChatGLMDbContext, IdentityUser, Guid>, IUsersRepository
    {
        public UserRepository(IDbContextProvider<IChatGLMDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<List<IdentityUser>> PageAsync(string name, int pageSize, int pageIndex, Guid? tenantId, bool? enabled = null)
        {
            return await (await GetDbSetAsync())
                  .WhereIf(name.IsNotNullOrWhiteSpace(), e => name.Contains(e.UserName) || name.Contains(e.Name))
                  .WhereIf(enabled != null, e => e.LockoutEnabled == enabled)
                  .WhereIf(tenantId != null, e => e.TenantId == tenantId)
                  .PageBy(pageIndex * pageSize, pageSize)
                  .ToListAsync();
        }

        public async Task<long> CountAsync(string name, Guid? tenantId, bool? enabled = null)
        {
            return await (await GetDbSetAsync())
                 .WhereIf(name.IsNotNullOrWhiteSpace(), e => name.Contains(e.UserName) || name.Contains(e.Name))
                 .WhereIf(enabled != null, e => e.LockoutEnabled == enabled)
                 .WhereIf(tenantId != null, e => e.TenantId == tenantId)
                .CountAsync();
        }

    }
}
