2025-07-03 08:17:27 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-07-03 13:53:29 +08:00
|
|
|
|
using System.Diagnostics;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2025-07-03 13:53:29 +08:00
|
|
|
|
using NLog;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
using PMSWPF.Data.Entities;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Data.Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 用户仓储类,用于操作DbUser实体
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class UserRepository
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
2025-07-03 08:17:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID获取用户
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">主键ID</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<DbUser> GetByIdAsync(int id)
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 08:17:27 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Queryable<DbUser>().In(id).SingleAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"根据ID '{id}' 获取用户耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有用户
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<List<DbUser>> GetAllAsync()
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 08:17:27 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Queryable<DbUser>().ToListAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"获取所有用户耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 新增用户
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="user">用户实体</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> AddAsync(DbUser user)
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 08:17:27 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Insertable(user).ExecuteReturnIdentityAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"新增用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新用户
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="user">用户实体</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> UpdateAsync(DbUser user)
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 08:17:27 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Updateable(user).ExecuteCommandAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"更新用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID删除用户
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">主键ID</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> DeleteAsync(int id)
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 08:17:27 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Deleteable<DbUser>().In(id).ExecuteCommandAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"删除用户ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|