Esempio n. 1
0
        /// <summary>
        /// 同步按钮
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="buttons"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        private async Task <bool> SyncButton(MenuEntity menu, List <MenuButtonAddModel> buttons, IDbTransaction transaction)
        {
            if (menu.Type != MenuType.Route)
            {
                return(true);
            }

            var oldButtons = await _buttonRepository.QueryByMenu(menu.RouteName, transaction);

            #region ==更新以及新增按钮==

            foreach (var newBtn in buttons)
            {
                var  oldBtn = oldButtons.FirstOrDefault(m => m.Code.EqualsIgnoreCase(newBtn.Code));
                bool result;
                if (oldBtn != null)
                {
                    oldBtn.Name = newBtn.Text;
                    oldBtn.Icon = newBtn.Icon;
                    result      = await _buttonRepository.UpdateAsync(oldBtn, transaction);
                }
                else
                {
                    oldBtn = new ButtonEntity
                    {
                        MenuCode = menu.RouteName,
                        Code     = newBtn.Code.ToLower(),
                        Icon     = newBtn.Icon,
                        Name     = newBtn.Text
                    };
                    result = await _buttonRepository.UpdateAsync(oldBtn, transaction);
                }

                if (!result)
                {
                    return(false);
                }

                if (!await SyncButtonPermission(oldBtn, newBtn.Permissions, transaction))
                {
                    return(false);
                }
            }

            #endregion

            foreach (var oldBtn in oldButtons)
            {
                var isDel = !buttons.Any(m => m.Code.EqualsIgnoreCase(oldBtn.Code));
                if (isDel)
                {
                    if (!await _buttonRepository.DeleteAsync(oldBtn.Id, transaction) || !await _buttonPermissionRepository.DeleteByButton(oldBtn.Code, transaction))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// 同步按钮
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="buttons"></param>
        /// <param name="uow"></param>
        /// <returns></returns>
        private async Task <bool> SyncButton(MenuEntity menu, List <MenuButtonAddModel> buttons, IUnitOfWork uow)
        {
            if (menu.Type != MenuType.Route)
            {
                return(true);
            }

            if (buttons == null)
            {
                buttons = new List <MenuButtonAddModel>();
            }

            var oldButtons = await _buttonRepository.QueryByMenu(menu.RouteName, uow);

            #region ==更新以及新增按钮==

            foreach (var newBtn in buttons)
            {
                var  oldBtn = oldButtons.FirstOrDefault(m => m.Code.EqualsIgnoreCase(newBtn.Code));
                bool result;
                if (oldBtn != null)
                {
                    oldBtn.Name = newBtn.Text;
                    oldBtn.Icon = newBtn.Icon;
                    result      = await _buttonRepository.UpdateAsync(oldBtn, uow);
                }
                else
                {
                    if (await _buttonRepository.ExistsByCode(newBtn.Code.ToLower(), uow))
                    {
                        _logger.LogError($"按钮编码{newBtn.Code}不能重复");
                        result = false;
                    }
                    else
                    {
                        oldBtn = new ButtonEntity
                        {
                            MenuCode = menu.RouteName,
                            Code     = newBtn.Code.ToLower(),
                            Icon     = newBtn.Icon,
                            Name     = newBtn.Text
                        };
                        result = await _buttonRepository.AddAsync(oldBtn, uow);
                    }
                }

                if (!result)
                {
                    return(false);
                }

                if (!await SyncButtonPermission(oldBtn, newBtn.Permissions, uow))
                {
                    return(false);
                }
            }

            #endregion

            foreach (var oldBtn in oldButtons)
            {
                var isDel = !buttons.Any(m => m.Code.EqualsIgnoreCase(oldBtn.Code));
                if (isDel)
                {
                    if (!await _buttonRepository.DeleteAsync(oldBtn.Id, uow) || !await _buttonPermissionRepository.DeleteByButton(oldBtn.Code, uow))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }