using Hotline.Repository.SqlSugar.DataPermissions;
using SqlSugar;
using XF.Domain.Dependency;
using Hotline.ThirdAccountDomainServices;
using Hotline.ThirdAccountDomainServices.Interfaces;

namespace Hotline.Repository.SqlSugar.Snapshot;
public class ThirdAccountRepository : BaseRepository<ThirdAccount>,  IThirdAccountRepository , IScopeDependency
{
    public ThirdAccountRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider) : base(uow, dataPermissionFilterBuilder, serviceProvider)
    {
    }

    public async Task<ThirdAccount> GetByExternalIdAsync(string externalId, CancellationToken token)
    {
        return await Queryable()
            .Where(m => m.ExternalId == externalId)
            .FirstAsync(token);
    }

    public async Task<ThirdAccount> GetByOpenIdAsync(string openId, CancellationToken token)
        => await Queryable()
        .Where(p => p.OpenId == openId)
        .FirstAsync(token);

    public async Task<List<ThirdAccount>> GetByPhoneNumberAsync(string phoneNumber, CancellationToken token)
        => await Queryable()
        .Where(m => m.PhoneNumber == phoneNumber)
        .ToListAsync(token);

    public async Task UpdateExternalIdAsync(string id, string externalId, CancellationToken token)
    {
        await Updateable()
            .SetColumns(m => m.ExternalId, externalId)
            .Where(m => m.Id == id)
            .ExecuteCommandAsync(token);
    }
}
