Files
DMS/DMS.Infrastructure/Repositories/MqttRepository.cs

180 lines
6.0 KiB
C#
Raw Normal View History

2025-07-03 13:53:29 +08:00
using System.Diagnostics;
using AutoMapper;
using DMS.Infrastructure.Entities;
using DMS.Core.Enums;
using DMS.Helper;
using DMS.Models;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// Mqtt仓储类用于操作DbMqtt实体
/// </summary>
public class MqttRepository
{
private readonly MenuRepository _menuRepository;
private readonly IMapper _mapper;
2025-07-03 13:53:29 +08:00
public MqttRepository(MenuRepository menuRepository, IMapper mapper)
{
_menuRepository = menuRepository;
_mapper = mapper;
}
/// <summary>
/// 根据ID获取Mqtt配置
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
public async Task<Mqtt> GetByIdAsync(int id)
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.Queryable<DbMqtt>()
.In(id)
.SingleAsync();
2025-07-03 13:53:29 +08:00
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"根据ID '{id}' 获取Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return _mapper.Map<Mqtt>(result);
}
}
/// <summary>
/// 获取所有Mqtt配置
/// </summary>
/// <returns></returns>
public async Task<List<Mqtt>> GetAllAsync()
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.Queryable<DbMqtt>()
.Includes(m => m.VariableMqtts, vm => vm.Variable)
.Includes(m => m.VariableMqtts, vm => vm.Mqtt)
.ToListAsync();
2025-07-03 13:53:29 +08:00
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"获取所有Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return result.Select(m => _mapper.Map<Mqtt>(m))
.ToList();
}
}
/// <summary>
/// 新增Mqtt配置
/// </summary>
/// <param name="mqtt">Mqtt实体</param>
/// <returns></returns>
public async Task<int> AddAsync(Mqtt mqtt)
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using var db = DbContext.GetInstance();
await db.BeginTranAsync();
try
{
var result = await db.Insertable(_mapper.Map<DbMqtt>(mqtt))
.ExecuteReturnIdentityAsync();
var mqttMenu = await _menuRepository.GetMainMenuByNameAsync("Mqtt服务器");
// AddAsync menu entry
var menu = new MenuBean()
{
Name = mqtt.Name,
Icon = SegoeFluentIcons.Wifi.Glyph,
Type = MenuType.MqttMenu,
DataId = result,
ParentId = mqttMenu.Id,
};
await _menuRepository.AddAsync(menu, db);
await db.CommitTranAsync();
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
catch (Exception ex)
{
await db.RollbackTranAsync();
NlogHelper.Error($"添加MQTT配置 {{mqtt.Name}} 失败", ex);
throw;
}
}
/// <summary>
/// 更新Mqtt配置
/// </summary>
/// <param name="mqtt">Mqtt实体</param>
/// <returns></returns>
public async Task<int> UpdateAsync(Mqtt mqtt)
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var db = DbContext.GetInstance())
{
await db.BeginTranAsync();
try
{
var result = await db.Updateable(_mapper.Map<DbMqtt>(mqtt))
.ExecuteCommandAsync();
// Update menu entry
var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
if (menu != null)
{
menu.Name = mqtt.Name;
await _menuRepository.UpdateAsync(menu, db);
}
await db.CommitTranAsync();
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
catch (Exception ex)
{
await db.RollbackTranAsync();
2025-07-06 19:51:53 +08:00
NlogHelper.Error($"更新MQTT配置 {{mqtt.Name}} 失败", ex);
throw;
}
}
}
/// <summary>
/// 根据ID删除Mqtt配置
/// </summary>
/// <param name="mqtt">Mqtt实体</param>
/// <returns></returns>
public async Task<int> DeleteAsync(Mqtt mqtt)
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var db = DbContext.GetInstance())
{
await db.BeginTranAsync();
try
{
var result = await db.Deleteable<DbMqtt>()
.In(mqtt.Id)
.ExecuteCommandAsync();
// DeleteAsync menu entry
var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
if (menu != null)
2025-07-13 18:05:31 +08:00
{
await _menuRepository.DeleteAsync(menu, db);
2025-07-13 18:05:31 +08:00
}
await db.CommitTranAsync();
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
catch (Exception ex)
{
await db.RollbackTranAsync();
NlogHelper.Error($"删除MQTT配置 {{mqtt.Name}} 失败", ex);
throw;
}
}
}
}