using NPin.Framework.SqlSugarCore.Abstractions;
using NPin.Framework.SqlSugarCore.Repositories;
using NPin.Framework.TenantManagement.Domain;
using Volo.Abp.DependencyInjection;

namespace NPin.Framework.TenantManagement.SqlSugarCore;

public class SqlSugarTenantRepository : SqlSugarRepository<TenantAggregateRoot, Guid>, ISqlSugarTenantRepository,
    ITransientDependency
{
    public SqlSugarTenantRepository(ISugarDbContextProvider<ISqlSugarDbContext> sugarDbContextProvider) : base(
        sugarDbContextProvider)
    {
    }

    public async Task<TenantAggregateRoot> FindByNameAsync(string name, bool includeDetails = true)
    {
        return await DbQueryable.FirstAsync(x => x.Name == name);
    }

    public async Task<List<TenantAggregateRoot>> GetListAsync(string sorting = null,
        int maxResultCount = Int32.MaxValue, int skipCount = 0, string filter = null,
        bool includeDetails = false)
    {
        return await DbQueryable
            .WhereIF(!string.IsNullOrEmpty(filter), x => x.Name.Contains(filter))
            .OrderByIF(!string.IsNullOrEmpty(sorting), sorting)
            .ToPageListAsync(skipCount, maxResultCount);
    }

    public async Task<long> GetCountAsync(string filter = null)
    {
        return await DbQueryable.WhereIF(!string.IsNullOrEmpty(filter), x => x.Name.Contains(filter)).CountAsync();
    }
}