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