using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.Repository.SqlSugar.DataPermissions;
using SqlSugar;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;

namespace Hotline.Repository.SqlSugar.Identity
{
    public class AccountRepository : BaseRepository<Account>, IAccountRepository, IScopeDependency
    {
        public AccountRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider) : base(uow, dataPermissionFilterBuilder, serviceProvider)
        {
        }

        public async Task SetAccountRolesAsync(string userId, ICollection<string> roleIds, CancellationToken cancellationToken)
        {
            var account = await GetAsync(userId, cancellationToken);
            if (account == null)
                throw UserFriendlyException.SameMessage("无效账号编号");

            account.Roles = roleIds.Select(d => new Role { Id = d }).ToList();
            await Db.UpdateNav(account)
                .Include(d => d.Roles)
                .ExecuteCommandAsync();
        }
    }
}
