using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using DMS.Infrastructure.Entities;
using DMS.Helper;
namespace DMS.Infrastructure.Repositories;
///
/// 用户仓储类,用于操作DbUser实体
///
public class UserRepository
{
///
/// 根据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();
NlogHelper.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();
NlogHelper.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();
NlogHelper.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();
NlogHelper.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();
NlogHelper.Info($"删除用户ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
}