/// <summary>
 /// 视图对象的属性转换为业务对象的属性
 /// </summary>
 /// <param name="bo"></param>
 private void _VMMapToBo(ApplicationUser bo, ApplicationUserVM boVM)
 {
     bo.ChineseFullName = boVM.Name;
     bo.UserName        = boVM.UserName;
     bo.Email           = boVM.Email;
     bo.MobileNumber    = boVM.MobileNumber;
 }
        /// <summary>
        /// 返回一个新的视图模型
        /// </summary>
        /// <returns></returns>
        public async Task <ApplicationUserVM> GetVM()
        {
            var boVM = new ApplicationUserVM();

            await SetTypeItems(boVM);

            return(boVM);
        }
 /// <summary>
 /// 业务对象的属性转换为视图对象的属性
 /// </summary>
 /// <param name="bo"></param>
 private void _BoMapToVM(ApplicationUser bo, ApplicationUserVM boVM)
 {
     boVM.Id             = bo.Id;
     boVM.UserName       = bo.UserName;
     boVM.MobileNumber   = bo.MobileNumber;
     boVM.Email          = bo.Email;
     boVM.Name           = bo.ChineseFullName;
     boVM.LockoutEnabled = bo.LockoutEnabled;
 }
        /// <summary>
        /// 根据学生 Id 返回系统用户视图模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <ApplicationUserVM> GetUserByStudentId(Guid id)
        {
            var boVM = new ApplicationUserVM();


            await SetTypeItems(boVM);

            return(boVM);
        }
        /// <summary>
        /// 根据系统用户对象返回一个系统用户视图模型对象
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        public ApplicationUserVM GetVMForList(ApplicationUser bo)
        {
            var boVM = new ApplicationUserVM();

            _BoMapToVM(bo, boVM);
            //await SetTypeItems(boVM);

            return(boVM);
        }
        /// <summary>
        /// 根据系统用户对象 Id 返回一个系统用户视图模型对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <ApplicationUserVM> GetVM(Guid id)
        {
            var boVM = new ApplicationUserVM();
            var bo   = await _userManager.FindByIdAsync(id.ToString());

            if (bo != null)
            {
                boVM.IsNew = false;
                _BoMapToVM(bo, boVM);
            }

            await SetTypeItems(boVM);

            return(boVM);
        }
        public async Task SetApplicationRoleItemCollection(ApplicationUserVM boVM)
        {
            var user = await _userManager.FindByIdAsync(boVM.Id.ToString());

            var resultItems = new List <PlainFacadeItem>();

            if (user != null)
            {
                var userInRoles = await _userManager.GetRolesAsync(user);

                foreach (var role in _roleManager.Roles)
                {
                    var item = new PlainFacadeItem()
                    {
                        ID = role.Id.ToString(), Name = role.Name, IsActive = false
                    };
                    if (userInRoles.Contains(role.Name))
                    {
                        item.IsActive = true;
                    }

                    resultItems.Add(item);
                }
            }
            else
            {
                foreach (var role in _roleManager.Roles)
                {
                    var item = new PlainFacadeItem()
                    {
                        ID = role.Id.ToString(), Name = role.Name, IsActive = false
                    };
                    resultItems.Add(item);
                }
            }

            boVM.ApplicationRoleItemCollection = resultItems;
            if (boVM.ApplicationRoleItemIdCollection != null)
            {
                foreach (var item in boVM.ApplicationRoleItemCollection)
                {
                    if (boVM.ApplicationRoleItemIdCollection.Contains(item.ID))
                    {
                        item.IsActive = true;
                    }
                }
            }
        }
        /// <summary>
        /// 设置与传入的视图模型相关的关联元素的集合值
        /// </summary>
        /// <param name="boVM"></param>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public async Task SetTypeItems(ApplicationUserVM boVM)
        {
            var user = await _userManager.FindByIdAsync(boVM.Id.ToString());

            #region 设置头像相关的数据
            Func <Guid, string> avatarPath = (itemId) =>
            {
                var path    = "";
                var imageBo = _businessImageRepository.GetSingleBy(x => x.RelevanceObjectID == itemId);
                if (imageBo != null)
                {
                    path = imageBo.UploadPath;
                }
                return(path);
            };
            if (user != null)
            {
                boVM.AvatarPath = avatarPath(user.Id);
            }
            else
            {
                boVM.AvatarPath = "";
            }
            #endregion

            #region 设置系统角色相关额数据
            boVM.ApplicationRoleItemIdCollection = new List <string>();
            boVM.ApplicationRoleItemCollection   = new List <PlainFacadeItem>();
            if (user != null)
            {
                var userInRoles = await _userManager.GetRolesAsync(user);

                foreach (var role in _roleManager.Roles)
                {
                    var item = new PlainFacadeItem()
                    {
                        ID = role.Id.ToString(), Name = role.Name, IsActive = false
                    };
                    if (userInRoles.Contains(role.Name))
                    {
                        item.IsActive = true;
                        boVM.ApplicationRoleItemIdCollection.Add(role.Id.ToString());
                    }

                    boVM.ApplicationRoleItemCollection.Add(item);
                }
            }
            else
            {
                foreach (var role in _roleManager.Roles)
                {
                    var item = new PlainFacadeItem()
                    {
                        ID = role.Id.ToString(), Name = role.Name, IsActive = false
                    };
                    boVM.ApplicationRoleItemCollection.Add(item);
                }
            }
            #endregion

            #region 设置人员相关的数据
            var employeeVM = await GetEmployeeVMByUserId(boVM.Id);

            if (employeeVM != null)
            {
                boVM.PersonId               = employeeVM.Id;
                boVM.PersonName             = employeeVM.Name;
                boVM.PersonCredentialsCode  = employeeVM.CredentialsCode;
                boVM.PersonAddress          = employeeVM.Address;
                boVM.PersonOrganizationName = employeeVM.ParentDepartmentName;
            }
            var studentVM = await GetStudentVMByUserId(boVM.Id);

            if (studentVM != null)
            {
                boVM.PersonId               = studentVM.Id;
                boVM.PersonName             = studentVM.Name;
                boVM.PersonCredentialsCode  = studentVM.CredentialsCode;
                boVM.PersonAddress          = studentVM.Address;
                boVM.PersonOrganizationName = studentVM.GradeAndClassName;
            }
            #endregion
        }
        /// <summary>
        /// 保存系统用户数据
        /// </summary>
        /// <param name="boVM"></param>
        /// <returns></returns>
        public async Task <bool> SaveBo(ApplicationUserVM boVM)
        {
            var bo = await _userManager.FindByIdAsync(boVM.Id.ToString());

            if (bo == null)
            {
                #region 新建用户
                bo = new ApplicationUser();
                _VMMapToBo(bo, boVM);


                var result = await _userManager.CreateAsync(bo, boVM.Password);

                if (result.Succeeded)
                {
                    if (boVM.ApplicationRoleItemIdCollection != null)
                    {
                        foreach (var item in boVM.ApplicationRoleItemIdCollection)
                        {
                            var role = await _roleManager.FindByIdAsync(item);

                            var addToRole = await _userManager.AddToRoleAsync(bo, role.Name);

                            // 检查用户关联角色组类型声明
                            var userClaims = await _userManager.GetClaimsAsync(bo);

                            var userRoleTypeClaim = userClaims.FirstOrDefault(x => x.Type == ClaimTypes.Name && x.Value == role.ApplicationRoleType.ToString());
                            // 添加角色组类型声明
                            if (userRoleTypeClaim == null)
                            {
                                // 为用户创建一个归属系统角色类型的身份声明
                                var claim = new Claim(ClaimTypes.Name, role.ApplicationRoleType.ToString());
                                await _userManager.AddClaimAsync(bo, claim);
                            }

                            //if (!addToRole.Succeeded)
                            //    return false;
                        }
                    }
                    else
                    {
                        // 添加到缺省组
                        var defaultRole = _roleManager.Roles.FirstOrDefault(x => x.ApplicationRoleType == ApplicationRoleTypeEnum.适用于普通注册用户);
                        if (defaultRole != null)
                        {
                            await _userManager.AddToRoleAsync(bo, defaultRole.Name);
                        }
                    }
                }
                else
                {
                    return(false);
                }
                #endregion
            }
            else
            {
                _VMMapToBo(bo, boVM);
                var result = await _userManager.UpdateAsync(bo);

                if (result.Succeeded)
                {
                    // 清空归属角色组
                    var userRoles = await _userManager.GetRolesAsync(bo);

                    foreach (var item in userRoles)
                    {
                        await _userManager.RemoveFromRoleAsync(bo, item);
                    }

                    // 重新根据选择项添加用户归属的角色
                    if (boVM.ApplicationRoleItemIdCollection != null)
                    {
                        foreach (var item in boVM.ApplicationRoleItemIdCollection)
                        {
                            var role = await _roleManager.FindByIdAsync(item);

                            var isInRole = await _userManager.IsInRoleAsync(bo, role.Name);

                            if (!isInRole)
                            {
                                await _userManager.AddToRoleAsync(bo, role.Name);
                            }

                            // 检查用户关联角色组类型声明
                            var userClaims = await _userManager.GetClaimsAsync(bo);

                            var userRoleTypeClaim = userClaims.FirstOrDefault(x => x.Type == ClaimTypes.Name && x.Value == role.ApplicationRoleType.ToString());
                            // 添加角色组类型声明
                            if (userRoleTypeClaim == null)
                            {
                                // 为用户创建一个归属系统角色类型的身份声明
                                var claim = new Claim(ClaimTypes.Name, role.ApplicationRoleType.ToString());
                                await _userManager.AddClaimAsync(bo, claim);
                            }
                        }
                    }
                    else
                    {
                        // 添加到缺省组
                        var defaultRole = _roleManager.Roles.FirstOrDefault(x => x.ApplicationRoleType == ApplicationRoleTypeEnum.适用于普通注册用户);
                        if (defaultRole != null)
                        {
                            await _userManager.AddToRoleAsync(bo, defaultRole.Name);
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// 根据员工 Id 返回系统用户视图模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApplicationUserVM GetUserByEmployeetId(Guid id)
        {
            var userVM = new ApplicationUserVM();

            return(userVM);
        }