UserPointManagement/src/UserPointManagement.Web/Pages/UserManagement/UserManagement.razor.cs

104 lines
2.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AntDesign;
using Microsoft.AspNetCore.Components;
using UserPointManagement.Application.Services;
using UserPointManagement.Model.Dtos.User;
using UserPointManagement.Model.Entities;
namespace UserPointManagement.Web.Pages.UserManagement;
public class UserManagementBase : ComponentBase
{
[Inject] private IUserService UserService { get; set; }
[Inject] private MessageService MessageService { get; set; }
protected IDictionary<int, (bool edit, User data)> editCache =
new Dictionary<int, (bool edit, User data)>();
protected List<User> _users;
protected int _pageIndex = 1;
protected int _pageSize = 20;
protected int _total = 0;
protected bool _loading;
private string _searchValue;
protected override async Task OnInitializedAsync()
{
await RefreshTable();
}
protected async Task OnSearch(string arg)
{
_searchValue = arg;
_pageIndex = 1;
await RefreshTable();
}
protected async Task RefreshTable()
{
_loading = true;
var res = await UserService.GetUsers(new GetUserDto()
{
Keyword = _searchValue,
PageIndex = _pageIndex,
PageSize = _pageSize
});
_users = res.Items;
_total = res.TotalCount;
_users.ForEach(item =>
{
editCache[item.Id] = (false, item);
});
_loading = false;
}
protected async Task OnPageIndexChanged(PaginationEventArgs args)
{
_pageIndex = args.Page;
_pageSize = args.PageSize;
await RefreshTable();
}
protected async Task OnPageSizeChange(PaginationEventArgs args)
{
_pageIndex = args.Page;
_pageSize = args.PageSize;
await RefreshTable();
}
protected async Task Delete(int userId)
{
await UserService.DeleteUser(userId).ConfigureAwait(false);
await MessageService.Success("删除成功!");
await RefreshTable();
}
protected void startEdit(int id)
{
var data = editCache[id];
editCache[id] = (true, data.data ); // add a copy in cache
}
protected void cancelEdit(int id)
{
var data = _users.FirstOrDefault(item => item.Id == id);
editCache[id] = (false, data); // recovery
}
protected async Task saveEdit(int id)
{
var index = _users.FindIndex(item => item.Id == id);
_users[index] = editCache[id].data; // apply the copy to data source
await UserService.ModifyUser(editCache[id].data.Id, new ModifyUserDto()
{
Mobile = editCache[id].data.Mobile,
Name = editCache[id].data.Name,
}).ConfigureAwait(false);
editCache[id] = (false, _users[index]); // don't affect rows in editing
}
}