refactor:删除不必要的方法

This commit is contained in:
2025-10-21 13:01:27 +08:00
parent 6872631933
commit 3116e4ce92
27 changed files with 105 additions and 608 deletions

View File

@@ -109,7 +109,12 @@ public class MenuAppService : IMenuAppService
try
{
await _repoManager.BeginTranAsync();
var delRes = await _repoManager.Menus.DeleteByIdAsync(id);
var menu = await _repoManager.Menus.GetByIdAsync(id);
if (menu == null)
{
throw new InvalidOperationException($"删除菜单失败菜单ID:{id}请检查菜单Id是否存在");
}
var delRes = await _repoManager.Menus.DeleteAsync(menu);
if (delRes == 0)
{
throw new InvalidOperationException($"删除菜单失败菜单ID:{id}请检查菜单Id是否存在");

View File

@@ -46,7 +46,12 @@ public class MqttAliasAppService : IMqttAliasAppService
/// </summary>
public async Task<int> RemoveAliasAsync(int aliasId)
{
return await _repoManager.MqttAliases.DeleteByIdAsync(aliasId);
var alias = await _repoManager.MqttAliases.GetByIdAsync(aliasId);
if (alias == null)
{
return 0;
}
return await _repoManager.MqttAliases.DeleteAsync(alias);
}
public async Task<List<MqttAlias>> GetAllAsync()

View File

@@ -116,7 +116,12 @@ public class MqttAppService : IMqttAppService
{
try
{
return await _repoManager.MqttServers.DeleteByIdAsync(id);
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(id);
if (mqttServer == null)
{
return 0;
}
return await _repoManager.MqttServers.DeleteAsync(mqttServer);
}
catch (Exception ex)
{
@@ -171,27 +176,27 @@ public class MqttAppService : IMqttAppService
}
}
/// <summary>
/// 异步批量删除MQTT服务器事务性操作
/// </summary>
/// <param name="ids">要删除的MQTT服务器ID列表。</param>
/// <returns>如果删除成功则为 true否则为 false。</returns>
/// <exception cref="ApplicationException">如果批量删除MQTT服务器时发生错误。</exception>
public async Task<bool> DeleteMqttServersAsync(List<int> ids)
{
try
{
if (ids == null || !ids.Any()) return true;
await _repoManager.BeginTranAsync();
var result = await _repoManager.MqttServers.DeleteByIdsAsync(ids);
await _repoManager.CommitAsync();
return result > 0;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException($"批量删除MQTT服务器时发生错误操作已回滚,错误信息:{ex.Message}", ex);
}
}
// /// <summary>
// /// 异步批量删除MQTT服务器事务性操作
// /// </summary>
// /// <param name="ids">要删除的MQTT服务器ID列表。</param>
// /// <returns>如果删除成功则为 true否则为 false。</returns>
// /// <exception cref="ApplicationException">如果批量删除MQTT服务器时发生错误。</exception>
// public async Task<bool> DeleteMqttServersAsync(List<int> ids)
// {
// try
// {
// if (ids == null || !ids.Any()) return true;
//
// await _repoManager.BeginTranAsync();
// var result = await _repoManager.MqttServers.DeleteByIdsAsync(ids);
// await _repoManager.CommitAsync();
// return result > 0;
// }
// catch (Exception ex)
// {
// await _repoManager.RollbackAsync();
// throw new ApplicationException($"批量删除MQTT服务器时发生错误操作已回滚,错误信息:{ex.Message}", ex);
// }
// }
}

View File

@@ -174,7 +174,12 @@ namespace DMS.Application.Services.Database
await _repositoryManager.BeginTranAsync();
// 删除触发器本身
var rowsAffected = await _repositoryManager.Triggers.DeleteByIdAsync(id);
var trigger = await _repositoryManager.Triggers.GetByIdAsync(id);
if (trigger == null)
{
return false;
}
var rowsAffected = await _repositoryManager.Triggers.DeleteAsync(trigger);
await _repositoryManager.CommitAsync();
return rowsAffected > 0;

View File

@@ -44,7 +44,12 @@ public class TriggerVariableAppService : ITriggerVariableAppService
/// </summary>
public async Task<int> RemoveTriggerVariableAsync(int triggerVariableId)
{
return await _repositoryManager.TriggerVariables.DeleteByIdAsync(triggerVariableId);
var triggerVariable = await _repositoryManager.TriggerVariables.GetByIdAsync(triggerVariableId);
if (triggerVariable == null)
{
return 0;
}
return await _repositoryManager.TriggerVariables.DeleteAsync(triggerVariable);
}
public async Task<List<TriggerVariable>> GetAllAsync()

View File

@@ -162,7 +162,12 @@ public class VariableAppService : IVariableAppService
try
{
await _repoManager.BeginTranAsync();
var delRes = await _repoManager.Variables.DeleteByIdAsync(id);
var variable = await _repoManager.Variables.GetByIdAsync(id);
if (variable == null)
{
throw new InvalidOperationException($"删除变量失败变量ID:{id}请检查变量Id是否存在");
}
var delRes = await _repoManager.Variables.DeleteAsync(variable);
if (delRes == 0)
{
throw new InvalidOperationException($"删除变量失败变量ID:{id}请检查变量Id是否存在");
@@ -196,13 +201,13 @@ public class VariableAppService : IVariableAppService
await _repoManager.BeginTranAsync();
// 批量删除变量
var deletedCount = await _repoManager.Variables.DeleteByIdsAsync(ids);
// 检查是否所有变量都被成功删除
if (deletedCount != ids.Count)
{
throw new InvalidOperationException($"删除变量失败:请求删除 {ids.Count} 个变量,实际删除 {deletedCount} 个变量");
}
// var deletedCount = await _repoManager.Variables.DeleteByIdsAsync(ids);
//
// // 检查是否所有变量都被成功删除
// if (deletedCount != ids.Count)
// {
// throw new InvalidOperationException($"删除变量失败:请求删除 {ids.Count} 个变量,实际删除 {deletedCount} 个变量");
// }
await _repoManager.CommitAsync();
return true;

View File

@@ -146,7 +146,12 @@ namespace DMS.Application.Services.Database
try
{
await _repositoryManager.BeginTranAsync();
var delRes = await _repositoryManager.VariableTables.DeleteByIdAsync(id);
var variableTable = await _repositoryManager.VariableTables.GetByIdAsync(id);
if (variableTable == null)
{
throw new InvalidOperationException($"删除变量表失败变量表ID:{id}请检查变量表Id是否存在");
}
var delRes = await _repositoryManager.VariableTables.DeleteAsync(variableTable);
if (delRes == 0)
{
throw new InvalidOperationException($"删除变量表失败变量表ID:{id}请检查变量表Id是否存在");

View File

@@ -135,22 +135,23 @@ public class MqttManagementService : IMqttManagementService
/// </summary>
public async Task<bool> DeleteMqttServersAsync(List<int> ids)
{
var result = await _mqttAppService.DeleteMqttServersAsync(ids);
// 批量删除成功后从内存中移除MQTT服务器
if (result && ids != null)
{
foreach (var id in ids)
{
if (_appStorageService.MqttServers.TryRemove(id, out var mqttServer))
{
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServer));
}
}
}
return result;
// var result = await _mqttAppService.DeleteMqttServersAsync(ids);
//
// // 批量删除成功后从内存中移除MQTT服务器
// if (result && ids != null)
// {
// foreach (var id in ids)
// {
// if (_appStorageService.MqttServers.TryRemove(id, out var mqttServer))
// {
// _eventService.RaiseMqttServerChanged(
// this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServer));
// }
// }
// }
//
// return result;
return false;
}
/// <summary>