Пример #1
0
        /// <summary>
        /// ChangeMobileAsync
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="newMobile"></param>
        /// <param name="mobileIsValid"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <bool> ChangeMobileAsync(int userId, string newMobile, bool mobileIsValid, ModelStateDictionary modelState)
        {
            var user = await _context.User.FirstOrDefaultAsync(m => m.UserId == userId);

            if (user == null)
            {
                modelState.AddModelError("UserId", "当前用户不存在");
                return(false);
            }
            if (!user.Mobile.IsNullOrWhiteSpace() &&
                user.Mobile.Equals(newMobile, StringComparison.InvariantCultureIgnoreCase))
            {
                modelState.AddModelError("UserId", "目标手机号和当前手机号相同");
                return(false);
            }
            if (await _context.User.AnyAsync(m => m.UserId != userId && m.Mobile == newMobile))
            {
                modelState.AddModelError("UserId", $"手机号 {newMobile} 已经被使用");
                return(false);
            }
            user.MobileIsValid = mobileIsValid;
            user.Mobile        = newMobile;
            await _context.SaveChangesAsync();

            return(true);
        }
Пример #2
0
        /// <summary>
        /// ChangeEmailAsync
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="newEmail"></param>
        /// <param name="emailIsValid"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <bool> ChangeEmailAsync(int userId, string newEmail, bool emailIsValid, ModelStateDictionary modelState)
        {
            var user = await _context.User.FirstOrDefaultAsync(m => m.UserId == userId);

            if (user == null)
            {
                modelState.AddModelError("UserId", "当前用户不存在");
                return(false);
            }
            if (!user.Email.IsNullOrWhiteSpace() &&
                user.Email.Equals(newEmail, StringComparison.InvariantCultureIgnoreCase))
            {
                modelState.AddModelError("UserId", "目标邮箱和当前邮箱相同");
                return(false);
            }
            if (await _context.User.AnyAsync(m => m.UserId != userId && m.Email == newEmail))
            {
                modelState.AddModelError("UserId", $"邮箱 {newEmail} 已经被使用");
                return(false);
            }
            user.EmailIsValid = emailIsValid;
            user.Email        = newEmail;
            await _context.SaveChangesAsync();

            return(true);
        }
Пример #3
0
        /// <summary>
        /// SaveAsync
        /// </summary>
        /// <param name="notificationInput"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <bool> SaveAsync(XM.NotificationInput notificationInput, ModelStateDictionary modelState)
        {
            User fromUser = null;
            User toUser   = null;

            if (notificationInput.FromUserId.HasValue)
            {
                fromUser = await _context.User.FirstOrDefaultAsync(m => m.UserId == notificationInput.FromUserId);

                if (fromUser == null)
                {
                    modelState.AddModelError("FromUserId", "无法获取通知发布者");
                    return(false);
                }
            }
            if (notificationInput.ToUserId.HasValue)
            {
                toUser = await _context.User.FirstOrDefaultAsync(m => m.UserId == notificationInput.ToUserId);

                if (toUser == null)
                {
                    modelState.AddModelError("FromUserId", "无法获取通知接收者");
                    return(false);
                }
            }
            Notification itemToSave;

            if (notificationInput.NotificationId.HasValue)
            {
                itemToSave = await _context.Notification.FirstOrDefaultAsync(m => m.NotificationId == notificationInput.NotificationId);

                if (itemToSave == null)
                {
                    modelState.AddModelError("FromUserId", "无法获取编辑的记录");
                    return(false);
                }
            }
            else
            {
                itemToSave = new Notification
                {
                    FromUser     = fromUser,
                    ToUser       = toUser,
                    CreationTime = DateTime.Now,
                    Url          = notificationInput.Url,
                };

                _context.Notification.Add(itemToSave);
            }

            itemToSave.Title   = notificationInput.Title;
            itemToSave.Message = notificationInput.Message;

            await _context.SaveChangesAsync();

            return(true);
        }
Пример #4
0
        /// <summary>
        /// SaveAsync
        /// </summary>
        /// <param name="userActionLogInput"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <bool> SaveAsync(UserActionLogInput userActionLogInput, ModelStateDictionary modelState)
        {
            var newUserActionLog = new UserActionLog();

            _mapper.Map(userActionLogInput, newUserActionLog);
            newUserActionLog.CreationTime = DateTime.Now;

            _context.UserActionLog.Add(newUserActionLog);
            await _context.SaveChangesAsync();

            return(true);
        }
Пример #5
0
        /// <summary>
        /// SaveAsync
        /// </summary>
        /// <param name="bulletinInput"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <bool> SaveAsync(BulletinInput bulletinInput, ModelStateDictionary modelState)
        {
            var dbBulletin = await _context.Bulletin.OrderByDescending(m => m.BulletinId).FirstOrDefaultAsync();

            if (dbBulletin == null)
            {
                return(false);
            }

            _mapper.Map(bulletinInput, dbBulletin);
            await _context.SaveChangesAsync();

            return(true);
        }
Пример #6
0
        public async Task <bool> SaveAsync(BulletinInput bulletin, ModelStateDictionary modelState)
        {
            var dbBulletin = await _tubumuContext.Bulletin.FirstOrDefaultAsync();

            if (dbBulletin == null)
            {
                return(false);
            }

            dbBulletin.UpdateFrom(bulletin);
            await _tubumuContext.SaveChangesAsync();

            return(true);
        }
Пример #7
0
        /// <summary>
        /// GetOrGenerateItemByWeixinMobileEndOpenIdAsync
        /// </summary>
        /// <param name="generateGroupId"></param>
        /// <param name="generateStatus"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task <XM.UserInfo> GetOrGenerateItemByWeixinMobileEndOpenIdAsync(Guid generateGroupId, XM.UserStatus generateStatus, string openId)
        {
            if (openId.IsNullOrWhiteSpace())
            {
                return(null);
            }
            var user = await GetItemByWeixinMobileEndOpenIdAsync(openId);

            if (user == null)
            {
                var newUser = new User
                {
                    Status                = generateStatus,
                    CreationTime          = DateTime.Now,
                    WeixinMobileEndOpenId = openId,
                    GroupId               = generateGroupId, // new Guid("11111111-1111-1111-1111-111111111111") 等待分配组
                    Username              = "******" + Guid.NewGuid().ToString("N").Substring(19),
                    Password              = openId,
                };

                _context.User.Add(newUser);
                await _context.SaveChangesAsync();

                user = await GetItemByWeixinMobileEndOpenIdAsync(openId);
            }

            return(user);
        }
Пример #8
0
        /// <summary>
        /// SaveAsync
        /// </summary>
        /// <param name="userInput"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <XM.UserInfo> SaveAsync(UserInput userInput, ModelStateDictionary modelState)
        {
            User userToSave;

            if (userInput is UserInputEdit userInputEdit)
            {
                userToSave = await _context.User.
                             Include(m => m.UserGroup).
                             Include(m => m.UserRole).
                             Include(m => m.UserPermission).
                             FirstOrDefaultAsync(m => m.UserId == userInputEdit.UserId);

                if (userToSave == null)
                {
                    modelState.AddModelError("UserId", "尝试编辑不存在的记录");
                    return(null);
                }
                if (!userInput.Password.IsNullOrWhiteSpace())
                {
                    userToSave.Password = userInput.Password;
                }

                userToSave.Status = userInput.Status;
            }
            else
            {
                userToSave = new User();
                _context.User.Add(userToSave);
                userToSave.Status       = XM.UserStatus.Normal; // Fix
                userToSave.Password     = userInput.Password;
                userToSave.CreationTime = DateTime.Now;
            }

            var group = await _context.Group.Include(m => m.GroupAvailableRole).FirstOrDefaultAsync(m => m.GroupId == userInput.GroupId);

            if (group == null)
            {
                modelState.AddModelError("GroupId", "分组不存在");
                return(null);
            }
            if (userInput.RoleId.HasValue && group.GroupAvailableRole.All(m => m.RoleId != userInput.RoleId.Value))
            {
                modelState.AddModelError("GroupId", $"分组【{group.Name}】不允许使用该角色");
                return(null);
            }
            if (!group.IsContainsUser)
            {
                modelState.AddModelError("GroupId", $"分组【{group.Name}】不允许包含用户");
                return(null);
            }

            userToSave.GroupId         = userInput.GroupId;
            userToSave.RoleId          = userInput.RoleId;
            userToSave.Username        = userInput.Username;
            userToSave.DisplayName     = userInput.DisplayName;
            userToSave.HeadUrl         = userInput.HeadUrl;
            userToSave.LogoUrl         = userInput.LogoUrl;
            userToSave.RealName        = userInput.RealName;
            userToSave.RealNameIsValid = userInput.RealNameIsValid;
            userToSave.Email           = userInput.Email;
            userToSave.EmailIsValid    = userInput.EmailIsValid;
            userToSave.Mobile          = userInput.Mobile;
            userToSave.MobileIsValid   = userInput.MobileIsValid;
            userToSave.Description     = userInput.Description;
            userToSave.IsDeveloper     = userInput.IsDeveloper;
            userToSave.IsTester        = userInput.IsTester;

            #region 分组

            //移除项
            if (!userToSave.UserGroup.IsNullOrEmpty())
            {
                if (!userInput.GroupIds.IsNullOrEmpty())
                {
                    List <UserGroup> groupToRemove = (from p in userToSave.UserGroup
                                                      where !userInput.GroupIds.Contains(p.GroupId)
                                                      select p).ToList();
                    for (int i = 0; i < groupToRemove.Count; i++)
                    {
                        userToSave.UserGroup.Remove(groupToRemove[i]);
                    }
                }
                else
                {
                    userToSave.UserGroup.Clear();
                }
            }
            //添加项
            if (!userInput.GroupIds.IsNullOrEmpty())
            {
                //要添加的Id集
                List <Guid> groupIdToAdd = (from p in userInput.GroupIds
                                            where userToSave.UserGroup.All(m => m.GroupId != p)
                                            select p).ToList();

                //要添加的项
                List <UserGroup> groupToAdd = await(from p in _context.Group
                                                    where groupIdToAdd.Contains(p.GroupId)
                                                    select new UserGroup
                {
                    Group = p
                }).ToListAsync();
                foreach (var item in groupToAdd)
                {
                    userToSave.UserGroup.Add(item);
                }
            }

            #endregion

            #region 用户角色

            //移除项
            if (!userToSave.UserRole.IsNullOrEmpty())
            {
                if (!userInput.RoleIds.IsNullOrEmpty())
                {
                    List <UserRole> roleToRemove = (from p in userToSave.UserRole
                                                    where !userInput.RoleIds.Contains(p.RoleId)
                                                    select p).ToList();
                    for (int i = 0; i < roleToRemove.Count; i++)
                    {
                        userToSave.UserRole.Remove(roleToRemove[i]);
                    }
                }
                else
                {
                    userToSave.UserRole.Clear();
                }
            }
            //添加项
            if (!userInput.RoleIds.IsNullOrEmpty())
            {
                //要添加的Id集
                List <Guid> roleIdToAdd = (from p in userInput.RoleIds
                                           where userToSave.UserRole.All(m => m.RoleId != p)
                                           select p).ToList();

                //要添加的项
                List <UserRole> roleToAdd = await(from p in _context.Role
                                                  where roleIdToAdd.Contains(p.RoleId)
                                                  select new UserRole
                {
                    Role = p
                }).ToListAsync();
                foreach (var item in roleToAdd)
                {
                    userToSave.UserRole.Add(item);
                }
            }

            #endregion

            #region 用户权限

            //移除项
            if (!userToSave.UserPermission.IsNullOrEmpty())
            {
                if (!userInput.PermissionIds.IsNullOrEmpty())
                {
                    List <UserPermission> permissionToRemove = (from p in userToSave.UserPermission
                                                                where !userInput.PermissionIds.Contains(p.PermissionId)
                                                                select p).ToList();
                    for (int i = 0; i < permissionToRemove.Count; i++)
                    {
                        userToSave.UserPermission.Remove(permissionToRemove[i]);
                    }
                }
                else
                {
                    userToSave.UserPermission.Clear();
                }
            }
            //添加项
            if (!userInput.PermissionIds.IsNullOrEmpty())
            {
                //要添加的Id集
                List <Guid> permissionIdToAdd = (from p in userInput.PermissionIds
                                                 where userToSave.UserPermission.All(m => m.PermissionId != p)
                                                 select p).ToList();

                //要添加的项
                List <UserPermission> permissionToAdd = await(from p in _context.Permission
                                                              where permissionIdToAdd.Contains(p.PermissionId)
                                                              select new UserPermission
                {
                    Permission = p
                }).ToListAsync();
                foreach (var item in permissionToAdd)
                {
                    userToSave.UserPermission.Add(item);
                }
            }

            #endregion

            await _context.SaveChangesAsync();

            //return new[] { userToSave }.Select(_selector.Compile()).First();
            return(await GetItemByUserIdAsync(userToSave.UserId));
        }
Пример #9
0
        /// <summary>
        /// SaveAsync
        /// </summary>
        /// <param name="roleInput"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <bool> SaveAsync(RoleInput roleInput, ModelStateDictionary modelState)
        {
            Role roleToSave = null;

            if (roleInput.RoleId.HasValue)
            {
                roleToSave = await _context.Role.
                             Include(m => m.RolePermission).
                             FirstOrDefaultAsync(m => m.RoleId == roleInput.RoleId.Value);
            }
            if (roleToSave == null)
            {
                roleToSave = new Role
                {
                    RoleId   = Guid.NewGuid(),
                    IsSystem = false
                };
                _context.Role.Add(roleToSave);
                int maxDisplayOrder = await _context.Role.MaxAsync(m => (int?)m.DisplayOrder) ?? 0;

                roleToSave.DisplayOrder = maxDisplayOrder + 1;
            }
            roleToSave.Name = roleInput.Name;

            #region 角色权限
            if (!roleToSave.RolePermission.IsNullOrEmpty())
            {
                // 移除项
                if (!roleInput.PermissionIds.IsNullOrEmpty())
                {
                    List <RolePermission> permissionToRemove = (from p in roleToSave.RolePermission
                                                                where !roleInput.PermissionIds.Contains(p.PermissionId)
                                                                select p).ToList();
                    for (int i = 0; i < permissionToRemove.Count; i++)
                    {
                        roleToSave.RolePermission.Remove(permissionToRemove[i]);
                    }
                }
                else
                {
                    roleToSave.RolePermission.Clear();
                }
            }
            if (!roleInput.PermissionIds.IsNullOrEmpty())
            {
                // 添加项
                // 要添加的Id集
                List <Guid> permissionIdToAdd = (from p in roleInput.PermissionIds
                                                 where roleToSave.RolePermission.All(m => m.PermissionId != p)
                                                 select p).ToList();

                // 要添加的项
                List <RolePermission> permissionToAdd = await(from p in _context.Permission
                                                              where permissionIdToAdd.Contains(p.PermissionId)
                                                              select new RolePermission
                {
                    Permission = p
                }).ToListAsync();
                foreach (var item in permissionToAdd)
                {
                    roleToSave.RolePermission.Add(item);
                }
            }
            #endregion

            await _context.SaveChangesAsync();

            return(true);
        }
Пример #10
0
        /// <summary>
        /// SaveAsync
        /// </summary>
        /// <param name="permissionInput"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <bool> SaveAsync(PermissionInput permissionInput, ModelStateDictionary modelState)
        {
            string sql;

            Permission permissionToSave = null;

            if (!permissionInput.PermissionId.IsNullOrEmpty())
            {
                permissionToSave = await _context.Permission.FirstOrDefaultAsync(m => m.PermissionId == permissionInput.PermissionId.Value);

                if (permissionInput.PermissionId == permissionInput.ParentId)
                {
                    modelState.AddModelError("PermissionId", "尝试将自身作为父节点");
                    return(false);
                }
            }
            //添加操作
            if (permissionToSave == null)
            {
                #region 添加操作
                //创建要保存的对象
                permissionToSave = new Permission
                {
                    //提取权限时,permissionToSave的PermissionId为null,这时不用创建新Id
                    PermissionId = permissionInput.PermissionId.IsNullOrEmpty() ? Guid.NewGuid() : permissionInput.PermissionId.Value,
                    ParentId     = permissionInput.ParentId,
                    ModuleName   = permissionInput.ModuleName,
                    Name         = permissionInput.Name,
                };
                _context.Permission.Add(permissionToSave);
                //如果添加的是新的顶级节点,直接添加到末尾,不会影响其他节点
                if (permissionInput.ParentId.IsNullOrEmpty())
                {
                    permissionToSave.DisplayOrder = await GetMaxDisplayOrderAsync() + 1;

                    permissionToSave.Level = 1;
                }
                else//如果添加的是子节点,会影响其他节点的DisplayOrder
                {
                    //父节点树的最大DisplayerOrder
                    int maxDisplayOrderInParentTree = await GetMaxDisplayOrderInParentTreeAsync(permissionInput.ParentId.Value);

                    //父节点树的最大DisplayerOrder基础上加1作为保存对象的DisplayOrder
                    permissionToSave.DisplayOrder = maxDisplayOrderInParentTree + 1;
                    //父节点的Level基础上加1作为保存对象的Level
                    permissionToSave.Level = await GetLevelAsync(permissionInput.ParentId.Value) + 1;

                    //父节点树之后的所有节点的DisplayOrder加1
                    sql = "Update Permission Set DisplayOrder=DisplayOrder+1 Where DisplayOrder > @DisplayOrder";
                    await _context.Database.ExecuteSqlCommandAsync(sql, new SqlParameter("DisplayOrder", maxDisplayOrderInParentTree));
                }
                #endregion
            }
            else//编辑操作
            {
                permissionToSave.ModuleName = permissionInput.ModuleName;
                permissionToSave.Name       = permissionInput.Name;

                //如果父节点不改变,则仅仅保存数据就行了。下面处理的是父节点改变了的情况
                //如果父节点改变(从无父节点到有父节点,从有父节点到无父节点,从一个父节点到另一个父节点)
                if (permissionInput.ParentId != permissionToSave.ParentId)
                {
                    permissionToSave.ParentId = permissionInput.ParentId;

                    //获取当前节点的下一个兄弟节点或更高层下一个父节点(不是自己的父节点)的DisplayOrder
                    int displayOrderOfNextParentOrNextBrother = await GetDisplayOrderOfNextParentOrNextBrotherAsync(permissionToSave);

                    #region 当前节点树Id集合

                    List <Guid> currTreeIds;
                    if (displayOrderOfNextParentOrNextBrother == 0)
                    {
                        //说明当前节点是最后一个节点,直接获取
                        currTreeIds = await _context.Permission.Where(m => m.DisplayOrder >= permissionToSave.DisplayOrder).Select(m => m.PermissionId).ToListAsync();
                    }
                    else
                    {
                        currTreeIds = await _context.Permission
                                      .Where(m => m.DisplayOrder >= permissionToSave.DisplayOrder && m.DisplayOrder < displayOrderOfNextParentOrNextBrother)
                                      .Select(m => m.PermissionId).ToListAsync();
                    }
                    int currentTreeItemCount = currTreeIds.Count;

                    #endregion

                    //当前节点将由子节点升为顶级节点,直接将该节点数移到最后
                    if (!permissionToSave.ParentId.HasValue)
                    {
                        #region 将由子节点升为顶级节点

                        //需要提升的层级数
                        int xLevel = permissionToSave.Level - 1;

                        //当前节点树之后已无任何节点
                        if (displayOrderOfNextParentOrNextBrother == 0)
                        {
                            //将当前节点树的所有节点的Level都进行提升
                            sql = "Update Permission Set Level = Level - @Level Where DisplayOrder>=@DisplayOrder";
                            await _context.Database.ExecuteSqlCommandAsync(sql
                                                                           , new SqlParameter("Level", xLevel)
                                                                           , new SqlParameter("DisplayOrder", permissionToSave.DisplayOrder)
                                                                           );
                        }
                        else//当前节点树之后还有节点,应该将这些节点的向前面排,并且将当前节点树的所有节点往后排
                        {
                            //当前节点树之后的节点数量
                            int nextItemCount = await _context.Permission.CountAsync(m => m.DisplayOrder >= displayOrderOfNextParentOrNextBrother);

                            sql = "Update Permission Set DisplayOrder = DisplayOrder - @CTIC Where DisplayOrder>=@DOONPONB";

                            await _context.Database.ExecuteSqlCommandAsync(sql
                                                                           , new SqlParameter("CTIC", currentTreeItemCount)
                                                                           , new SqlParameter("DOONPONB", displayOrderOfNextParentOrNextBrother)
                                                                           );

                            sql = "Update Permission Set Level = Level - @Level,DisplayOrder = DisplayOrder + @NextItemCount Where 1<>1 ";
                            foreach (var id in currTreeIds)
                            {
                                sql += " Or PermissionId = '{0}'".FormatWith(id.ToString());
                            }

                            await _context.Database.ExecuteSqlCommandAsync(sql
                                                                           , new SqlParameter("Level", xLevel)
                                                                           , new SqlParameter("NextItemCount", nextItemCount)
                                                                           );
                        }

                        #endregion
                    }
                    else//当前节点将改变父节点,包括从顶级节点移至另一节点下,或从当前父节点下移至另一节点下
                    {
                        #region 从顶级节点移至另一节点下,或从当前父节点下移至另一节点下

                        //目标父节点
                        var tarParent = await _context.Permission.AsNoTracking().FirstOrDefaultAsync(m => m.PermissionId == permissionInput.ParentId.Value);

                        int xDisplayOrder = permissionToSave.DisplayOrder - tarParent.DisplayOrder;
                        int xLevel        = permissionToSave.Level - tarParent.Level;

                        if (xDisplayOrder > 0)//从下往上移
                        {
                            #region 从下往上移
                            //特例处理,当前节点要移至的父节点就是上一个节点,只需要改变当前树Level
                            if (xDisplayOrder == 1)
                            {
                                sql = "Update Permission Set Level = Level - @Level Where 1<>1 ";
                                foreach (var id in currTreeIds)
                                {
                                    sql += " Or PermissionId = '{0}'".FormatWith(id.ToString());
                                }

                                await _context.Database.ExecuteSqlCommandAsync(sql
                                                                               , new SqlParameter("Level", xLevel - 1)
                                                                               );
                            }
                            else
                            {
                                //新的父节点和本节点之间的节点往下移动,DisplayOrder增加
                                sql = "Update Permission Set DisplayOrder=DisplayOrder+@CurTreeCount Where DisplayOrder>@TDisplayOrder And DisplayOrder<@CDisplayOrder";
                                await _context.Database.ExecuteSqlCommandAsync(sql
                                                                               , new SqlParameter("CurTreeCount", currentTreeItemCount)
                                                                               , new SqlParameter("TDisplayOrder", tarParent.DisplayOrder)
                                                                               , new SqlParameter("CDisplayOrder", permissionToSave.DisplayOrder)
                                                                               );

                                sql = "Update Permission Set DisplayOrder = DisplayOrder-@XCount,Level = Level - @Level Where 1<>1 ";
                                foreach (var id in currTreeIds)
                                {
                                    sql += " Or PermissionId = '{0}'".FormatWith(id.ToString());
                                }
                                await _context.Database.ExecuteSqlCommandAsync(sql
                                                                               , new SqlParameter("XCount", xDisplayOrder - 1)//也就是新节点和本节点之间的节点的数量
                                                                               , new SqlParameter("Level", xLevel - 1)
                                                                               );
                            }
                            #endregion
                        }
                        else//从上往下移
                        {
                            #region 从上往下移
                            // 本节点树下已经不存在任何节点了,当然无法向下移
                            if (displayOrderOfNextParentOrNextBrother == 0)
                            {
                                modelState.AddModelError("PermissionId", "节点已处于最后位置,无法下移");
                                return(false);
                            }

                            // 更新本节点树至新的父节点(包括新的父节点)之间的节点的DisplayOrder
                            sql = "Update Permission Set DisplayOrder=DisplayOrder-@CurTreeCount Where DisplayOrder>=@DOONPONB And DisplayOrder<=@TDisplayOrder";
                            await _context.Database.ExecuteSqlCommandAsync(sql
                                                                           , new SqlParameter("CurTreeCount", currentTreeItemCount)
                                                                           , new SqlParameter("DOONPONB", displayOrderOfNextParentOrNextBrother)
                                                                           , new SqlParameter("TDisplayOrder", tarParent.DisplayOrder)
                                                                           );

                            // 本节点至新的节点之间的节点数
                            int nextItemCount = tarParent.DisplayOrder - displayOrderOfNextParentOrNextBrother + 1;
                            sql = "Update Permission Set DisplayOrder = DisplayOrder+ @XCount,Level = Level - @Level Where 1<>1 ";
                            foreach (var id in currTreeIds)
                            {
                                sql += " Or PermissionId = '{0}'".FormatWith(id.ToString());
                            }
                            await _context.Database.ExecuteSqlCommandAsync(sql
                                                                           , new SqlParameter("XCount", nextItemCount)
                                                                           , new SqlParameter("Level", xLevel - 1)
                                                                           );

                            #endregion
                        }

                        #endregion
                    }
                }
            }
            await _context.SaveChangesAsync();

            return(true);
        }