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>
|
|
|
|
|
|
/// Mqtt仓储类,用于操作DbMqtt实体
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MqttRepository
|
|
|
|
|
|
{
|
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获取Mqtt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">主键ID</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<DbMqtt> 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<DbMqtt>().In(id).SingleAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"根据ID '{id}' 获取Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有Mqtt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<List<DbMqtt>> 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<DbMqtt>().ToListAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 新增Mqtt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mqtt">Mqtt实体</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> AddAsync(DbMqtt mqtt)
|
|
|
|
|
|
{
|
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(mqtt).ExecuteReturnIdentityAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新Mqtt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mqtt">Mqtt实体</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> UpdateAsync(DbMqtt mqtt)
|
|
|
|
|
|
{
|
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(mqtt).ExecuteCommandAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID删除Mqtt配置
|
|
|
|
|
|
/// </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<DbMqtt>().In(id).ExecuteCommandAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"删除Mqtt配置ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|