优化变量添加Mqtt服务器
This commit is contained in:
@@ -264,7 +264,7 @@ public class VarDataRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID删除VariableData
|
||||
/// 删除VariableData
|
||||
/// </summary>
|
||||
/// <param name="id">主键ID</param>
|
||||
/// <returns></returns>
|
||||
@@ -282,4 +282,43 @@ public class VarDataRepository
|
||||
NlogHelper.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为变量添加MQTT服务器关联。
|
||||
/// </summary>
|
||||
/// <param name="variableDatas">要添加MQTT服务器的变量数据列表。</param>
|
||||
/// <param name="mqtt">要关联的MQTT服务器。</param>
|
||||
/// <returns>成功添加关联的数量。</returns>
|
||||
public async Task<int> AddMqttToVariablesAsync(IEnumerable<VariableData> variableDatas, Mqtt mqtt)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
int addedCount = 0;
|
||||
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
foreach (var variableData in variableDatas)
|
||||
{
|
||||
// 检查是否已存在该关联
|
||||
var existingAssociation = await db.Queryable<DbVariableDataMqtt>()
|
||||
.Where(x => x.VariableDataId == variableData.Id && x.MqttId == mqtt.Id)
|
||||
.FirstAsync();
|
||||
|
||||
if (existingAssociation == null)
|
||||
{
|
||||
// 如果不存在,则添加新的关联
|
||||
await db.Insertable(new DbVariableDataMqtt
|
||||
{
|
||||
VariableDataId = variableData.Id,
|
||||
MqttId = mqtt.Id
|
||||
}).ExecuteCommandAsync();
|
||||
addedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"为 {variableDatas.Count()} 个变量添加MQTT服务器 '{mqtt.Name}' 关联,成功添加 {addedCount} 个,耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return addedCount;
|
||||
}
|
||||
}
|
||||
@@ -774,27 +774,53 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
return; // 用户取消选择
|
||||
}
|
||||
|
||||
// 遍历所有选定的变量,并为其添加选定的MQTT服务器
|
||||
foreach (VariableData variable in validVariables)
|
||||
// 调用新的仓库方法来添加MQTT服务器关联
|
||||
var addedCount = await _varDataRepository.AddMqttToVariablesAsync(validVariables, selectedMqtt);
|
||||
|
||||
if (addedCount > 0)
|
||||
{
|
||||
// 如果变量的Mqtts集合为空,则初始化它
|
||||
if (variable.Mqtts == null)
|
||||
//跟新已经加载的变量的Mqtt服务器
|
||||
foreach (var variable in validVariables)
|
||||
{
|
||||
variable.Mqtts = new List<Mqtt>();
|
||||
if (variable.Mqtts==null)
|
||||
{
|
||||
variable.Mqtts=new List<Mqtt>();
|
||||
}
|
||||
// 判断不存在再添加
|
||||
|
||||
var ids = variable.Mqtts.Select(m => m.Id)
|
||||
.ToList();
|
||||
if (!ids.Contains(selectedMqtt.Id))
|
||||
{
|
||||
variable.Mqtts.Add(selectedMqtt);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//将变量添加到Mqtt服务器的变量表中
|
||||
if (selectedMqtt.VariableDatas==null)
|
||||
{
|
||||
|
||||
selectedMqtt.VariableDatas=new List<VariableData>();
|
||||
}
|
||||
|
||||
// 避免重复添加相同的MQTT服务器
|
||||
if (!variable.Mqtts.Any(m => m.Id == selectedMqtt.Id))
|
||||
foreach (var variable in validVariables)
|
||||
{
|
||||
variable.Mqtts.Add(selectedMqtt);
|
||||
// variable.IsModified = true; // 标记为已修改 (如果需要UI立即反映,但此处批量更新数据库,所以可以省略)
|
||||
var ids = selectedMqtt.VariableDatas.Select(v => v.Id)
|
||||
.ToList();
|
||||
if (!ids.Contains(variable.Id))
|
||||
{
|
||||
variable.Mqtts.Add(selectedMqtt);
|
||||
}
|
||||
}
|
||||
// 刷新界面以反映更改
|
||||
await RefreshDataView();
|
||||
NotificationHelper.ShowSuccess($"已成功为 {addedCount} 个变量添加MQTT服务器: {selectedMqtt.Name}");
|
||||
}
|
||||
else
|
||||
{
|
||||
NotificationHelper.ShowInfo($"没有新的变量关联到MQTT服务器: {selectedMqtt.Name},可能已存在。");
|
||||
}
|
||||
|
||||
// 批量更新数据库中的变量数据
|
||||
await _varDataRepository.UpdateAsync(validVariables.ToList());
|
||||
// 显示成功通知
|
||||
NotificationHelper.ShowSuccess($"已成功为 {validVariables.Count} 个变量添加MQTT服务器: {selectedMqtt.Name}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user