public async Task ExchangeWebMenuAuthorityParentIDAsync(Guid id, Guid?parentID, Guid?targetID, bool forUnder = true)
        {
            if (parentID.HasValue && !await _webMenuAuthorityRepository.ExistedAsync(parentID.Value))
            {
                throw new InvalidOperationException("父级唯一标识不存在");
            }
            WebMenuAuthority webMenuAuthorityFromDB = await _webMenuAuthorityRepository.FirstOrDefaultAsync(id);

            if (webMenuAuthorityFromDB == null)
            {
                throw new InvalidOperationException("该网页菜单权限不存在");
            }
            webMenuAuthorityFromDB.ParentID = parentID;
            if (targetID.HasValue)
            {
                WebMenuAuthority indexWebMenuAuthority = await _webMenuAuthorityRepository.FirstOrDefaultAsync(targetID.Value);

                List <WebMenuAuthority> webMenuAuthorities = await GetWebMenuAuthoritiesByIndex(webMenuAuthorityFromDB, indexWebMenuAuthority);

                ExchangeIndex(id, forUnder, webMenuAuthorities);
            }
            _authorityUnitOfWork.RegisterEdit(webMenuAuthorityFromDB);
            await _authorityUnitOfWork.CommitAsync();

            _webMenuAuthorityRepository.ClearCache();
        }
        public async Task EditWebMenuAuthorityAsync(EditWebMenuAuthorityModel model)
        {
            if (string.IsNullOrEmpty(model.Code))
            {
                throw new InvalidOperationException("代码为空");
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                throw new InvalidOperationException("名称为空");
            }
            if (await _webMenuAuthorityRepository.CountAsync(m => m.ID != model.ID && m.Code == model.Code) > 0)
            {
                throw new InvalidOperationException("代码重复");
            }
            WebMenuAuthority webMenuAuthorityFromDB = await _webMenuAuthorityRepository.FirstOrDefaultAsync(model.ID);

            if (webMenuAuthorityFromDB == null)
            {
                throw new InvalidOperationException("网页菜单权限不存在");
            }
            model.CopyProperties(webMenuAuthorityFromDB);
            _authorityUnitOfWork.RegisterEdit(webMenuAuthorityFromDB);
            await _authorityUnitOfWork.CommitAsync();

            _webMenuAuthorityRepository.ClearCache();
        }
        /// <summary>
        /// 调换位序
        /// </summary>
        /// <param name="webMenuAuthority1"></param>
        /// <param name="webMenuAuthority2"></param>
        private void ExchangeIndex(WebMenuAuthority webMenuAuthority1, WebMenuAuthority webMenuAuthority2)
        {
            int temp = webMenuAuthority1.Index;

            webMenuAuthority1.Index = webMenuAuthority2.Index;
            webMenuAuthority2.Index = temp;
        }
        public async Task ExchangeWebMenuAuthorityParentIDAsync(Guid id, Guid?parentID, Guid?indexID, bool forUnder = true)
        {
            if (parentID.HasValue && !await _webMenuAuthorityRepository.ExistedAsync(parentID.Value))
            {
                throw new InvalidOperationException("父级唯一标识不存在");
            }
            WebMenuAuthority webMenuAuthorityFromDB = await _webMenuAuthorityRepository.FirstOrDefaultAsync(id);

            if (webMenuAuthorityFromDB == null)
            {
                throw new InvalidOperationException("该网页菜单权限不存在");
            }
            webMenuAuthorityFromDB.ParentID = parentID;
            if (indexID.HasValue)
            {
                WebMenuAuthority indexWebMenuAuthority = await _webMenuAuthorityRepository.FirstOrDefaultAsync(indexID.Value);

                if (indexWebMenuAuthority == null)
                {
                    throw new InvalidOperationException("位序唯一标项不存在");
                }
                if (forUnder && webMenuAuthorityFromDB.Index < indexWebMenuAuthority.Index ||
                    !forUnder && webMenuAuthorityFromDB.Index > indexWebMenuAuthority.Index)
                {
                    ExchangeIndex(webMenuAuthorityFromDB, indexWebMenuAuthority);
                    _authorityUnitOfWork.RegisterEdit(indexWebMenuAuthority);
                }
            }
            _authorityUnitOfWork.RegisterEdit(webMenuAuthorityFromDB);
            await _authorityUnitOfWork.CommitAsync();
        }
        public async Task <WebMenuAuthorityDTO> GetWebMenuAuthorityInfoAsync(Guid id)
        {
            WebMenuAuthority webMenuAuthorityFromDB = await _webMenuAuthorityRepository.FirstOrDefaultAsync(id);

            if (webMenuAuthorityFromDB == null)
            {
                throw new InvalidOperationException("网页菜单权限不存在");
            }
            return(_mapper.Map <WebMenuAuthorityDTO>(webMenuAuthorityFromDB));
        }
        /// <summary>
        /// 根据位序获取同级的位序内信息
        /// </summary>
        /// <param name="webMenuAuthority1"></param>
        /// <param name="webMenuAuthority2"></param>
        /// <returns></returns>
        private async Task <List <WebMenuAuthority> > GetWebMenuAuthoritiesByIndex(WebMenuAuthority webMenuAuthority1, WebMenuAuthority webMenuAuthority2)
        {
            if (webMenuAuthority1.ParentID != webMenuAuthority2.ParentID)
            {
                throw new InvalidOperationException("两个网页菜单权限不属于同级");
            }
            var webMenuAuthorities = new List <WebMenuAuthority>
            {
                webMenuAuthority1,
                webMenuAuthority2
            };

            webMenuAuthorities = webMenuAuthorities.OrderBy(m => m.Index).ToList();
            WebMenuAuthority firstWebMenuAuthority = webMenuAuthorities[0];
            WebMenuAuthority lastWebMenuAuthority  = webMenuAuthorities[1];

            webMenuAuthorities.AddRange(await _webMenuAuthorityRepository.WhereAsync(m => m.ParentID == firstWebMenuAuthority.ParentID && m.Index > firstWebMenuAuthority.Index && m.Index < lastWebMenuAuthority.Index).ToList());
            webMenuAuthorities = webMenuAuthorities.OrderBy(m => m.Index).ToList();
            return(webMenuAuthorities);
        }
        public async Task DeleteWebMenuAuthorityAsync(Guid id)
        {
            List <WebMenuAuthority> allWebMenuAuthorities = await _webMenuAuthorityRepository.GetAllInfoFromCacheAsync();

            WebMenuAuthority webMenuAuthorityFromDB = allWebMenuAuthorities.FirstOrDefault(m => m.ID == id);

            if (webMenuAuthorityFromDB == null)
            {
                throw new InvalidOperationException("API权限不存在");
            }
            ICollection <WebMenuAuthority> allChild = GetAllChild(allWebMenuAuthorities, id);

            foreach (WebMenuAuthority menuAuthority in allChild)
            {
                _authorityUnitOfWork.RegisterDelete(menuAuthority);
            }
            _authorityUnitOfWork.RegisterDelete(webMenuAuthorityFromDB);
            await _authorityUnitOfWork.CommitAsync();

            _webMenuAuthorityRepository.ClearCache();
        }