Exemplo n.º 1
0
        public async Task <IResponseOutput> AssignAsync(PermissionAssignInput input)
        {
            //查询角色权限
            var permissionIds = await _rolePermissionRepo.Query.Where(d => d.RoleId == input.RoleId).Select(x => x.PermissionId).ToListAsync();

            //批量删除权限
            var deleteIds = permissionIds.Where(d => !input.PermissionIds.Contains(d));

            if (deleteIds.Count() > 0)
            {
                await _rolePermissionRepo.DeleteAsync(m => m.RoleId == input.RoleId && deleteIds.Contains(m.PermissionId));
            }

            //批量插入权限
            var insertRolePermissions = new List <RolePermissionEntity>();
            var insertPermissionIds   = input.PermissionIds.Where(d => !permissionIds.Contains(d));

            if (insertPermissionIds.Count() > 0)
            {
                foreach (var permissionId in insertPermissionIds)
                {
                    var id = SnowflakeId.CreateInstance().NextId();

                    insertRolePermissions.Add(new RolePermissionEntity()
                    {
                        Id           = id,
                        RoleId       = input.RoleId,
                        PermissionId = permissionId,
                    });
                }
                await _rolePermissionRepo.BulkInsertAsync(insertRolePermissions);
            }


            return(ResponseOutput.Ok());
        }