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