feat(#I4NFRH): 增加部门服务

close https://gitee.com/LongbowEnterprise/BootstrapAdmin/issues/I4NFRH
This commit is contained in:
Argo-Tianyi 2021-12-21 11:35:30 +08:00
parent 69e8428b2e
commit 30614bd223
6 changed files with 141 additions and 41 deletions

View File

@ -39,6 +39,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton<IUser, UserService>(); services.AddSingleton<IUser, UserService>();
services.AddSingleton<ILogin, LoginService>(); services.AddSingleton<ILogin, LoginService>();
services.AddSingleton<IRole, RoleService>(); services.AddSingleton<IRole, RoleService>();
services.AddSingleton<IGroup, GroupService>();
return services; return services;
} }
} }

View File

@ -0,0 +1,53 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services
{
class GroupService : BaseDatabase, IGroup
{
/// <summary>
///
/// </summary>
/// <param name="db"></param>
public GroupService(IDatabase db) => Database = db;
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<Group> GetAll() => Database.Fetch<Group>();
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public List<string> GetGroupsByUserId(string? userId) => Database.Fetch<string>("select GroupID from UserGroup where UserID = @0", userId);
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public bool SaveGroupsByUserId(string? userId, IEnumerable<string> 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;
}
}
}

View File

@ -0,0 +1,31 @@
using BootstrapAdmin.DataAccess.Models;
namespace BootstrapAdmin.Web.Core;
/// <summary>
///
/// </summary>
public interface IGroup
{
/// <summary>
/// 获得所有用户
/// </summary>
/// <returns></returns>
List<Group> GetAll();
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
List<string> GetGroupsByUserId(string? userId);
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="values"></param>
/// <returns></returns>
bool SaveGroupsByUserId(string? userId, IEnumerable<string> groupIds);
}

View File

@ -20,4 +20,11 @@ public static class SelectedItemExtensions
/// <param name="users"></param> /// <param name="users"></param>
/// <returns></returns> /// <returns></returns>
public static List<SelectedItem> ToSelectedItemList(this IEnumerable<Role> roles) => roles.Select(i => new SelectedItem { Value = i.Id!, Text = i.RoleName }).ToList(); public static List<SelectedItem> ToSelectedItemList(this IEnumerable<Role> roles) => roles.Select(i => new SelectedItem { Value = i.Id!, Text = i.RoleName }).ToList();
/// <summary>
///
/// </summary>
/// <param name="users"></param>
/// <returns></returns>
public static List<SelectedItem> ToSelectedItemList(this IEnumerable<Group> groups) => groups.Select(i => new SelectedItem { Value = i.Id!, Text = i.ToString() }).ToList();
} }

View File

@ -2,48 +2,47 @@
using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Extensions; 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] var users = UserService.GetAll().ToSelectedItemList();
[NotNull] var values = UserService.GetUsersByGroupId(group.Id);
private DialogService? DialogService { get; set; }
[Inject] await DialogService.ShowAssignmentDialog($"分配用户 - {group}", users, values, () =>
[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)
{ {
var users = UserService.GetAll().ToSelectedItemList(); var ret = UserService.SaveUsersByGroupId(group.Id, values);
var values = UserService.GetUsersByGroupId(group.Id); return Task.FromResult(ret);
}, ToastService);
}
await DialogService.ShowAssignmentDialog($"分配用户 - {group}", users, values, () => private async Task OnAssignmentRoles(Group group)
{ {
var ret = UserService.SaveUsersByGroupId(group.Id, values); var users = RoleService.GetAll().ToSelectedItemList();
return Task.FromResult(ret); var values = RoleService.GetRolesByGroupId(group.Id);
}, ToastService);
}
private async Task OnAssignmentRoles(Group group) await DialogService.ShowAssignmentDialog($"分配角色 - {group}", users, values, () =>
{ {
var users = RoleService.GetAll().ToSelectedItemList(); var ret = RoleService.SaveRolesByGroupId(group.Id, values);
var values = RoleService.GetUsersByRoleId(group.Id); return Task.FromResult(ret);
}, ToastService);
await DialogService.ShowAssignmentDialog($"分配角色 - {group}", users, values, () =>
{
var ret = RoleService.SaveUsersByRoleId(group.Id, values);
return Task.FromResult(ret);
}, ToastService);
}
} }
} }

View File

@ -12,14 +12,23 @@ namespace BootstrapAdmin.Web.Pages.Admin
[NotNull] [NotNull]
private DialogService? DialogService { get; set; } 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) private async Task OnAssignmentGroups(User user)
{ {
var option = new DialogOption() var groups = GroupService.GetAll().ToSelectedItemList();
{ var values = GroupService.GetGroupsByUserId(user.Id);
Title = $"分配部门 - {user}",
};
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) private async Task OnAssignmentRoles(User user)