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;
|
|
|
|
|
|
using PMSWPF.Data.Entities;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
using PMSWPF.Models;
|
|
|
|
|
|
using PMSWPF.Extensions;
|
2025-07-05 18:35:40 +08:00
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
using PMSWPF.Helper;
|
|
|
|
|
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
|
|
|
|
|
using PMSWPF.Enums;
|
2025-07-03 08:17:27 +08:00
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Data.Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Mqtt仓储类,用于操作DbMqtt实体
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MqttRepository
|
|
|
|
|
|
{
|
2025-07-05 18:35:40 +08:00
|
|
|
|
private readonly MenuRepository _menuRepository;
|
2025-07-03 13:53:29 +08:00
|
|
|
|
|
2025-07-05 18:35:40 +08:00
|
|
|
|
public MqttRepository()
|
|
|
|
|
|
{
|
|
|
|
|
|
_menuRepository = new MenuRepository();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-03 08:17:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID获取Mqtt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">主键ID</param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-07-04 22:39:44 +08:00
|
|
|
|
public async Task<Mqtt> GetById(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-04 22:39:44 +08:00
|
|
|
|
return result.CopyTo<Mqtt>();
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有Mqtt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2025-07-04 22:39:44 +08:00
|
|
|
|
public async Task<List<Mqtt>> GetAll()
|
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 22:57:54 +08:00
|
|
|
|
var result = await _db.Queryable<DbMqtt>().Includes(m=>m.VariableDatas)
|
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-05 21:49:41 +08:00
|
|
|
|
return result.Select(m => m.CopyTo<Mqtt>())
|
|
|
|
|
|
.ToList();
|
2025-07-03 08:17:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 新增Mqtt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mqtt">Mqtt实体</param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-07-04 22:39:44 +08:00
|
|
|
|
public async Task<int> Add(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-05 21:49:41 +08:00
|
|
|
|
var result = await db.Insertable(mqtt.CopyTo<DbMqtt>())
|
|
|
|
|
|
.ExecuteReturnIdentityAsync();
|
|
|
|
|
|
var mqttMenu = await _menuRepository.GetMainMenuByName("Mqtt服务器");
|
|
|
|
|
|
// Add menu entry
|
|
|
|
|
|
var menu = new MenuBean()
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = mqtt.Name,
|
|
|
|
|
|
Icon = SegoeFluentIcons.Wifi.Glyph,
|
|
|
|
|
|
Type = MenuType.MqttMenu,
|
|
|
|
|
|
DataId = result,
|
|
|
|
|
|
ParentId = mqttMenu.Id,
|
|
|
|
|
|
};
|
|
|
|
|
|
await _menuRepository.Add(menu, db);
|
|
|
|
|
|
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-06 19:51:53 +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-04 22:39:44 +08:00
|
|
|
|
public async Task<int> Edit(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.Updateable(mqtt.CopyTo<DbMqtt>())
|
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-07-05 18:35:40 +08:00
|
|
|
|
// Update menu entry
|
|
|
|
|
|
var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu);
|
|
|
|
|
|
if (menu != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
menu.Name = mqtt.Name;
|
|
|
|
|
|
await _menuRepository.Edit(menu, db);
|
|
|
|
|
|
}
|
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-04 22:39:44 +08:00
|
|
|
|
public async Task<int> Delete(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-05 18:35:40 +08:00
|
|
|
|
// Delete menu entry
|
|
|
|
|
|
var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu);
|
2025-07-05 21:49:41 +08:00
|
|
|
|
await _menuRepository.DeleteMenu(menu, db);
|
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-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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|