2025-07-07 18:41:36 +08:00
|
|
|
|
using Arch.EntityFrameworkCore.UnitOfWork;
|
|
|
|
|
using WorkStation.Server.Context.Entity;
|
|
|
|
|
using WorkStation.Share;
|
|
|
|
|
|
|
|
|
|
namespace WorkStation.Server.Services
|
|
|
|
|
{
|
|
|
|
|
public class UserService(IUnitOfWork unitOfWork) : IUserService
|
|
|
|
|
{
|
2025-07-08 17:32:28 +08:00
|
|
|
|
private readonly IUnitOfWork _unitOfWork = unitOfWork;
|
|
|
|
|
private readonly int _pageList = 100;
|
|
|
|
|
public async Task<ApiResponse> AddAsync(User entity)
|
2025-07-07 18:41:36 +08:00
|
|
|
|
{
|
2025-07-08 17:32:28 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(entity.Account))
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(false, "添加用户失败,账户无效", entity);
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(entity.Password))
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(false, "添加用户失败,密码无效", entity);
|
|
|
|
|
}
|
|
|
|
|
entity.CreateTime = DateTime.Now;
|
|
|
|
|
entity.UpdateTime = DateTime.Now;
|
|
|
|
|
entity.Id = 0;
|
|
|
|
|
var repository = _unitOfWork.GetRepository<User>();
|
|
|
|
|
await repository.InsertAsync(entity);
|
|
|
|
|
var count = await _unitOfWork.SaveChangesAsync();
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(true, "添加用户成功", entity);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(false, "添加用户失败,unitOfWork保存失败", entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(false, $"添加用户异常:{ex.Message}", null);
|
|
|
|
|
}
|
2025-07-07 18:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 17:32:28 +08:00
|
|
|
|
public async Task<ApiResponse> DelateAsync(int id)
|
2025-07-07 18:41:36 +08:00
|
|
|
|
{
|
2025-07-08 17:32:28 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (id <= 0)
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(false, "id = 无效", $"id = {id}");
|
|
|
|
|
}
|
|
|
|
|
var repository = _unitOfWork.GetRepository<User>();
|
|
|
|
|
var user = await repository.GetFirstOrDefaultAsync(predicate: item => item.Id == id);
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(false, "删除用户失败,没有查询到该用户", id);
|
|
|
|
|
}
|
|
|
|
|
repository.Delete(user);
|
|
|
|
|
var count = await _unitOfWork.SaveChangesAsync();
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(true, "删除用户成功", $"id = {id}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(false, "删除用户失败,unitOfWork保存失败", $"id = {id}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return new ApiResponse(false, $"删除用户异常:{ex.Message}", $"id = {id}");
|
|
|
|
|
}
|
2025-07-07 18:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 17:32:28 +08:00
|
|
|
|
public async Task<ApiRespon<List<User>>> GetAllAsync()
|
2025-07-07 18:41:36 +08:00
|
|
|
|
{
|
2025-07-08 17:32:28 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var repository = _unitOfWork.GetRepository<User>();
|
|
|
|
|
var pageIndex = 0;
|
|
|
|
|
var data = new List<User>();
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
var users = await repository.GetPagedListAsync(pageIndex: pageIndex,
|
|
|
|
|
pageSize: _pageList,
|
|
|
|
|
orderBy: source => source.OrderByDescending(item => item.Id));
|
|
|
|
|
if (users == null)
|
|
|
|
|
{
|
|
|
|
|
return new ApiRespon<List<User>>(false, "获取失败,GetPagedListAsync 结果为 null", null);
|
|
|
|
|
}
|
|
|
|
|
foreach(var item in users.Items)
|
|
|
|
|
{
|
|
|
|
|
data.Add(item);
|
|
|
|
|
}
|
|
|
|
|
if (!users.HasNextPage)
|
|
|
|
|
{
|
|
|
|
|
return new ApiRespon<List<User>>(true, "获取用户成功", data);
|
|
|
|
|
}
|
|
|
|
|
pageIndex++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return new ApiRespon<List<User>>(false, $"获取用户异常:{ex.Message}", null);
|
|
|
|
|
}
|
2025-07-07 18:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<ApiResponse> GetByIdAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 17:32:28 +08:00
|
|
|
|
public Task<ApiResponse> UpdateAsync(User entity)
|
2025-07-07 18:41:36 +08:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2025-07-08 17:32:28 +08:00
|
|
|
|
|
2025-07-07 18:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|