Merge branch 'develop' of Tiamo/UserPointManagement into master

master
yosheng 2023-07-30 08:21:16 +00:00
commit 6b885a0c3e
7 changed files with 82 additions and 26 deletions

View File

@ -11,6 +11,8 @@ public interface IUserPointService
Task<PageResultDto<UserPointDto>> GetUserPoints(GetUserPointDto req); Task<PageResultDto<UserPointDto>> GetUserPoints(GetUserPointDto req);
Task CreateUserPoint(CreateUserPointDto input); Task CreateUserPoint(CreateUserPointDto input);
Task DeletePoint(long userPointId);
} }
public class UserPointService : IUserPointService public class UserPointService : IUserPointService
@ -36,8 +38,10 @@ public class UserPointService : IUserPointService
CreateTime = userPoint.CreateTime CreateTime = userPoint.CreateTime
}; };
queryable = queryable.Where(x => x.UserId == req.UserId, req.UserId.HasValue);
var count = queryable.Count(); var count = queryable.Count();
var data = await queryable.Paging(req).ToListAsync().ConfigureAwait(false); var data = await queryable.OrderByDescending(x => x.CreateTime).Paging(req).ToListAsync().ConfigureAwait(false);
return new PageResultDto<UserPointDto>() return new PageResultDto<UserPointDto>()
{ {
@ -52,4 +56,14 @@ public class UserPointService : IUserPointService
_userPointManagementDbContext.UserPoints.Add(new UserPoint(input.UserId, input.Point)); _userPointManagementDbContext.UserPoints.Add(new UserPoint(input.UserId, input.Point));
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false); await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
} }
public async Task DeletePoint(long userPointId)
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
var userPoint = await _userPointManagementDbContext.UserPoints.FirstOrDefaultAsync(x => x.Id == userPointId)
.ConfigureAwait(false);
_userPointManagementDbContext.Remove(userPoint);
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
}
} }

View File

@ -35,8 +35,11 @@ public class UserService : IUserService
var queryable = from user in _userPointManagementDbContext.Users var queryable = from user in _userPointManagementDbContext.Users
select user; select user;
queryable = queryable.Where(x => x.Mobile.Contains(req.Keyword) || x.Name.Contains(req.Keyword),
!string.IsNullOrEmpty(req.Keyword));
var count = queryable.Count(); var count = queryable.Count();
var data = await queryable.Paging(req).ToListAsync().ConfigureAwait(false); var data = await queryable.OrderByDescending(x => x.Id).Paging(req).ToListAsync().ConfigureAwait(false);
return new PageResultDto<User>() return new PageResultDto<User>()
{ {
@ -60,6 +63,15 @@ public class UserService : IUserService
public async Task CreateUser(User input) public async Task CreateUser(User input)
{ {
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync(); await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
if (_userPointManagementDbContext.Users.Any(x => x.Mobile == input.Mobile))
{
throw new ArgumentException("手机号不可重复!");
}
if (_userPointManagementDbContext.Users.Any(x => x.Name == input.Name))
{
throw new ArgumentException("名称不可重复!");
}
_userPointManagementDbContext.Users.Add(input); _userPointManagementDbContext.Users.Add(input);
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false); await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
} }

View File

@ -32,7 +32,7 @@
Path = "/user-point", Path = "/user-point",
Name = "用户积分", Name = "用户积分",
Key = "user-point", Key = "user-point",
Icon = "smile", Icon = "unordered-list",
} }
}; };

View File

@ -6,6 +6,7 @@
@using global::UserPointManagement.Model.Entities @using global::UserPointManagement.Model.Entities
@inject IJSRuntime JS @inject IJSRuntime JS
@inject IUserService UserService; @inject IUserService UserService;
@inject MessageService Message
<PageContainer Title="用户管理"> <PageContainer Title="用户管理">
<GridRow Style="margin: 10px 0"> <GridRow Style="margin: 10px 0">
@ -84,13 +85,14 @@
<Form Loading="loading" Model="@model" <Form Loading="loading" Model="@model"
LabelColSpan="8" LabelColSpan="8"
WrapperColSpan="16" WrapperColSpan="16"
ValidateMode=@FormValidateMode.Rules
OnFinish="OnFinish" OnFinish="OnFinish"
OnFinishFailed="OnFinishFailed" OnFinishFailed="OnFinishFailed"
@ref="@_form"> @ref="@_form">
<FormItem Label="姓名"> <FormItem Label="姓名" Rules=@(new FormValidationRule[] { new FormValidationRule { Required = true, Message = "名称不可为空!" }, new FormValidationRule() { Len = 50, Message = "名称上限50字" } })>
<Input @bind-Value="@context.Name"/> <Input @bind-Value="@context.Name"/>
</FormItem> </FormItem>
<FormItem Label="手机号"> <FormItem Label="手机号" Rules="@(new FormValidationRule[] { new FormValidationRule { Type = FormFieldType.Regexp, Pattern = @"^1\d{10}$", Message = "请输入正确手机号", Required = true } })">
<Input @bind-Value="@context.Mobile"/> <Input @bind-Value="@context.Mobile"/>
</FormItem> </FormItem>
</Form> </Form>
@ -102,7 +104,6 @@
public class Model public class Model
{ {
[Required]
public string Name { get; set; } public string Name { get; set; }
public string Mobile { get; set; } public string Mobile { get; set; }
@ -148,10 +149,9 @@
/// <summary> /// <summary>
/// when form is submited, close the modal /// when form is submited, close the modal
/// </summary> /// </summary>
/// <param name="args"></param> /// <param name="editContext"></param>
private void OnFinish(EditContext editContext) private void OnFinish(EditContext editContext)
{ {
Console.WriteLine("e");
_visible = false; _visible = false;
} }
@ -161,13 +161,26 @@
/// <param name="e"></param> /// <param name="e"></param>
private async Task HandleOk(MouseEventArgs e) private async Task HandleOk(MouseEventArgs e)
{ {
_form.Submit(); if (_form.Validate())
{
try
{
await UserService.CreateUser(new User() await UserService.CreateUser(new User()
{ {
Name = model.Name, Name = model.Name,
Mobile = model.Mobile Mobile = model.Mobile
}).ConfigureAwait(false); }).ConfigureAwait(false);
}
catch (Exception exception)
{
await Message.Error(exception.Message);
}
_visible = false;
model.Mobile = string.Empty;
model.Name = string.Empty;
await RefreshTable().ConfigureAwait(false); await RefreshTable().ConfigureAwait(false);
await Message.Success("保存成功!");
}
} }
} }

View File

@ -11,7 +11,8 @@ namespace UserPointManagement.Web.Pages.UserManagement;
public class UserManagementBase : ComponentBase public class UserManagementBase : ComponentBase
{ {
[Inject] private IUserService _userService { get; set; } [Inject] private IUserService UserService { get; set; }
[Inject] private MessageService MessageService { get; set; }
protected IDictionary<int, (bool edit, User data)> editCache = protected IDictionary<int, (bool edit, User data)> editCache =
new Dictionary<int, (bool edit, User data)>(); new Dictionary<int, (bool edit, User data)>();
@ -38,7 +39,7 @@ public class UserManagementBase : ComponentBase
protected async Task RefreshTable() protected async Task RefreshTable()
{ {
_loading = true; _loading = true;
var res = await _userService.GetUsers(new GetUserDto() var res = await UserService.GetUsers(new GetUserDto()
{ {
Keyword = _searchValue, Keyword = _searchValue,
PageIndex = _pageIndex, PageIndex = _pageIndex,
@ -72,7 +73,8 @@ public class UserManagementBase : ComponentBase
protected async Task Delete(int userId) protected async Task Delete(int userId)
{ {
await _userService.DeleteUser(userId).ConfigureAwait(false); await UserService.DeleteUser(userId).ConfigureAwait(false);
await MessageService.Success("删除成功!");
await RefreshTable(); await RefreshTable();
} }
@ -92,7 +94,7 @@ public class UserManagementBase : ComponentBase
{ {
var index = _users.FindIndex(item => item.Id == id); var index = _users.FindIndex(item => item.Id == id);
_users[index] = editCache[id].data; // apply the copy to data source _users[index] = editCache[id].data; // apply the copy to data source
await _userService.ModifyUser(editCache[id].data.Id, new ModifyUserDto() await UserService.ModifyUser(editCache[id].data.Id, new ModifyUserDto()
{ {
Mobile = editCache[id].data.Mobile, Mobile = editCache[id].data.Mobile,
Name = editCache[id].data.Name, Name = editCache[id].data.Name,

View File

@ -1,4 +1,4 @@
@inherits UserPointManageBase @inherits UserPointManagement.Web.Pages.UserPointPage.UserPointDetailBase
@page "/user-point" @page "/user-point"
@using System.ComponentModel.DataAnnotations @using System.ComponentModel.DataAnnotations
@using System.Text.Json @using System.Text.Json
@ -7,7 +7,7 @@
@using UserPointManagement.Application.Services @using UserPointManagement.Application.Services
@inject IUserPointService UserPointService; @inject IUserPointService UserPointService;
<PageContainer Title="用户积分管理"> <PageContainer Title="用户积分明细">
<GridRow Style="margin: 10px 0"> <GridRow Style="margin: 10px 0">
<GridCol Span="12"> <GridCol Span="12">
<Space Direction="@DirectionVHType.Horizontal"> <Space Direction="@DirectionVHType.Horizontal">
@ -45,7 +45,14 @@
OnPageSizeChange="OnPageSizeChange"> OnPageSizeChange="OnPageSizeChange">
<PropertyColumn Property="c => c.Name" title="用户姓名"/> <PropertyColumn Property="c => c.Name" title="用户姓名"/>
<PropertyColumn Property="c => c.Point" title="积分"/> <PropertyColumn Property="c => c.Point" title="积分"/>
<PropertyColumn Property="c => c.CreateTime" title="新增时间" Format="yyyy-MM-dd hh:mm"/> <PropertyColumn Property="c => c.CreateTime" title="新增时间" Format="yyyy-MM-dd HH:mm"/>
<ActionColumn Title="操作" Width="220">
<Space Size=@("middle")>
<SpaceItem>
<Button Danger OnClick="()=>Delete(context.UserPointId)">删除</Button>
</SpaceItem>
</Space>
</ActionColumn>
</Table> </Table>
</PageContainer> </PageContainer>

View File

@ -9,10 +9,11 @@ using UserPointManagement.Model.Dtos.UserPoint;
namespace UserPointManagement.Web.Pages.UserPointPage; namespace UserPointManagement.Web.Pages.UserPointPage;
public class UserPointManageBase : ComponentBase public class UserPointDetailBase : ComponentBase
{ {
[Inject] private IUserPointService _UserPointService { get; set; } [Inject] private IUserPointService UserPointService { get; set; }
[Inject] private IUserService _UserService { get; set; } [Inject] private IUserService UserService { get; set; }
[Inject] private MessageService MessageService { get; set; }
protected List<UserPointDto> _userPoints; protected List<UserPointDto> _userPoints;
protected List<UserDto> _users; protected List<UserDto> _users;
@ -24,7 +25,7 @@ public class UserPointManageBase : ComponentBase
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
_users = await _UserService.GetAllUsers().ConfigureAwait(false); _users = await UserService.GetAllUsers().ConfigureAwait(false);
await RefreshTable(); await RefreshTable();
} }
@ -38,7 +39,7 @@ public class UserPointManageBase : ComponentBase
protected async Task RefreshTable() protected async Task RefreshTable()
{ {
_loading = true; _loading = true;
var res = await _UserPointService.GetUserPoints(new GetUserPointDto() var res = await UserPointService.GetUserPoints(new GetUserPointDto()
{ {
UserId = _selectedUserId, UserId = _selectedUserId,
PageIndex = _pageIndex, PageIndex = _pageIndex,
@ -64,4 +65,11 @@ public class UserPointManageBase : ComponentBase
_pageSize = args.PageSize; _pageSize = args.PageSize;
await RefreshTable(); await RefreshTable();
} }
protected async Task Delete(long userPointId)
{
await UserPointService.DeletePoint(userPointId).ConfigureAwait(false);
await MessageService.Success("删除成功!");
await RefreshTable();
}
} }