using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using NLog;
using PMSWPF.Data.Entities;
namespace PMSWPF.Data.Repositories;
///
/// Mqtt仓储类,用于操作DbMqtt实体
///
public class MqttRepository
{
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
///
/// 根据ID获取Mqtt配置
///
/// 主键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}' 获取Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
///
/// 获取所有Mqtt配置
///
///
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($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
///
/// 新增Mqtt配置
///
/// Mqtt实体
///
public async Task AddAsync(DbMqtt mqtt)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.Insertable(mqtt).ExecuteReturnIdentityAsync();
stopwatch.Stop();
Logger.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
///
/// 更新Mqtt配置
///
/// Mqtt实体
///
public async Task UpdateAsync(DbMqtt mqtt)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.Updateable(mqtt).ExecuteCommandAsync();
stopwatch.Stop();
Logger.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
///
/// 根据ID删除Mqtt配置
///
/// 主键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($"删除Mqtt配置ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
}