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