From 30614bd223a4607fa13100215fe4b5fa82927011 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 21 Dec 2021 11:35:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(#I4NFRH):=20=E5=A2=9E=E5=8A=A0=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close https://gitee.com/LongbowEnterprise/BootstrapAdmin/issues/I4NFRH --- .../Extensions/ServicesExtensions.cs | 1 + .../Services/GroupService.cs | 53 ++++++++++++++ .../admin/BootstrapAdmin.Web.Core/IGroup.cs | 31 ++++++++ .../Extensions/SelectedItemExtensions.cs | 7 ++ .../Pages/Admin/Groups.razor.cs | 71 +++++++++---------- .../Pages/Admin/Users.razor.cs | 19 +++-- 6 files changed, 141 insertions(+), 41 deletions(-) create mode 100644 src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs create mode 100644 src/blazor/admin/BootstrapAdmin.Web.Core/IGroup.cs diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs index 08f03ba3..1e7be86f 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs @@ -39,6 +39,7 @@ namespace Microsoft.Extensions.DependencyInjection services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); return services; } } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs new file mode 100644 index 00000000..9e303072 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs @@ -0,0 +1,53 @@ +using BootstrapAdmin.DataAccess.Models; +using BootstrapAdmin.Web.Core; +using PetaPoco; + +namespace BootstrapAdmin.DataAccess.PetaPoco.Services +{ + class GroupService : BaseDatabase, IGroup + { + /// + /// + /// + /// + public GroupService(IDatabase db) => Database = db; + + /// + /// + /// + /// + public List GetAll() => Database.Fetch(); + + /// + /// + /// + /// + /// + public List GetGroupsByUserId(string? userId) => Database.Fetch("select GroupID from UserGroup where UserID = @0", userId); + + /// + /// + /// + /// + /// + /// + public bool SaveGroupsByUserId(string? userId, IEnumerable groupIds) + { + var ret = false; + try + { + Database.BeginTransaction(); + Database.Execute("delete from UserGroup where UserID = @0", userId); + Database.InsertBatch("UserGroup", groupIds.Select(g => new { GroupID = g, UserID = userId })); + Database.CompleteTransaction(); + ret = true; + } + catch (Exception) + { + Database.AbortTransaction(); + throw; + } + return ret; + } + } +} diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IGroup.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IGroup.cs new file mode 100644 index 00000000..fc060afd --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IGroup.cs @@ -0,0 +1,31 @@ +using BootstrapAdmin.DataAccess.Models; + +namespace BootstrapAdmin.Web.Core; + +/// +/// +/// +public interface IGroup +{ + + /// + /// 获得所有用户 + /// + /// + List GetAll(); + + /// + /// + /// + /// + /// + List GetGroupsByUserId(string? userId); + + /// + /// + /// + /// + /// + /// + bool SaveGroupsByUserId(string? userId, IEnumerable groupIds); +} diff --git a/src/blazor/admin/BootstrapAdmin.Web/Extensions/SelectedItemExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web/Extensions/SelectedItemExtensions.cs index 4adf8641..31a87337 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Extensions/SelectedItemExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Extensions/SelectedItemExtensions.cs @@ -20,4 +20,11 @@ public static class SelectedItemExtensions /// /// public static List ToSelectedItemList(this IEnumerable roles) => roles.Select(i => new SelectedItem { Value = i.Id!, Text = i.RoleName }).ToList(); + + /// + /// + /// + /// + /// + public static List ToSelectedItemList(this IEnumerable groups) => groups.Select(i => new SelectedItem { Value = i.Id!, Text = i.ToString() }).ToList(); } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Groups.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Groups.razor.cs index 69ee2e95..170848a3 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Groups.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Groups.razor.cs @@ -2,48 +2,47 @@ using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Extensions; -namespace BootstrapAdmin.Web.Pages.Admin +namespace BootstrapAdmin.Web.Pages.Admin; + +public partial class Groups { - public partial class Groups + [Inject] + [NotNull] + private DialogService? DialogService { get; set; } + + [Inject] + [NotNull] + private ToastService? ToastService { get; set; } + + [Inject] + [NotNull] + private IUser? UserService { get; set; } + + [Inject] + [NotNull] + private IRole? RoleService { get; set; } + + private async Task OnAssignmentUsers(Group group) { - [Inject] - [NotNull] - private DialogService? DialogService { get; set; } + var users = UserService.GetAll().ToSelectedItemList(); + var values = UserService.GetUsersByGroupId(group.Id); - [Inject] - [NotNull] - private ToastService? ToastService { get; set; } - - [Inject] - [NotNull] - private IUser? UserService { get; set; } - - [Inject] - [NotNull] - private IRole? RoleService { get; set; } - - private async Task OnAssignmentUsers(Group group) + await DialogService.ShowAssignmentDialog($"分配用户 - {group}", users, values, () => { - var users = UserService.GetAll().ToSelectedItemList(); - var values = UserService.GetUsersByGroupId(group.Id); + var ret = UserService.SaveUsersByGroupId(group.Id, values); + return Task.FromResult(ret); + }, ToastService); + } - await DialogService.ShowAssignmentDialog($"分配用户 - {group}", users, values, () => - { - var ret = UserService.SaveUsersByGroupId(group.Id, values); - return Task.FromResult(ret); - }, ToastService); - } + private async Task OnAssignmentRoles(Group group) + { + var users = RoleService.GetAll().ToSelectedItemList(); + var values = RoleService.GetRolesByGroupId(group.Id); - private async Task OnAssignmentRoles(Group group) + await DialogService.ShowAssignmentDialog($"分配角色 - {group}", users, values, () => { - var users = RoleService.GetAll().ToSelectedItemList(); - var values = RoleService.GetUsersByRoleId(group.Id); - - await DialogService.ShowAssignmentDialog($"分配角色 - {group}", users, values, () => - { - var ret = RoleService.SaveUsersByRoleId(group.Id, values); - return Task.FromResult(ret); - }, ToastService); - } + var ret = RoleService.SaveRolesByGroupId(group.Id, values); + return Task.FromResult(ret); + }, ToastService); } } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs index 97b62816..29e38dd7 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Users.razor.cs @@ -12,14 +12,23 @@ namespace BootstrapAdmin.Web.Pages.Admin [NotNull] private DialogService? DialogService { get; set; } + [Inject] + [NotNull] + private ToastService? ToastService { get; set; } + + [Inject] + [NotNull] + private IGroup? GroupService { get; set; } private async Task OnAssignmentGroups(User user) { - var option = new DialogOption() - { - Title = $"分配部门 - {user}", - }; + var groups = GroupService.GetAll().ToSelectedItemList(); + var values = GroupService.GetGroupsByUserId(user.Id); - await DialogService.Show(option); + await DialogService.ShowAssignmentDialog($"分配部门 - {user}", groups, values, () => + { + var ret = GroupService.SaveGroupsByUserId(user.Id, values); + return Task.FromResult(ret); + }, ToastService); } private async Task OnAssignmentRoles(User user)