using System.Linq;
using System.Transactions;
using Abp.Authorization.Users;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using YGNT.Exam.Authorization.Roles;
using YGNT.Exam.Authorization.Users;
using YGNT.Exam.Authorization.Users.Cache;
using YGNT.Infrastructure.Agent;

namespace YGNT.Exam.Agent
{
    public class UserAgent : IUserAgent
    {
        private readonly UserManager _userManager;

        public UserAgent(UserManager userManager)
        {
            _userManager = userManager;
        }

        /// <summary>
        /// 检查用户id是否为管理员
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [UnitOfWork(TransactionScopeOption.Suppress)]
        public bool CheckUserIsAdmin(long? userId)
        {
            if (userId == null)
                return false;
            var roles = _userManager.GetRolesAsync(new User() { Id = (long)userId }).Result;
            return roles.Contains(StaticRoleNames.Tenants.Admin);
        }
    }
}