重写了增删改查

This commit is contained in:
2025-07-04 18:33:48 +08:00
parent 2ea526d0e3
commit 8325717b95
9 changed files with 589 additions and 260 deletions

View File

@@ -62,16 +62,24 @@ public partial class DevicesViewModel : ViewModelBase
[RelayCommand]
public async void AddDevice()
{
// 1. 显示添加设备对话框
var device = await _dialogService.ShowAddDeviceDialog();
// 如果用户取消或对话框未返回设备,则直接返回
if (device == null)
try
{
_logger.LogInformation("用户取消了添加设备操作。");
return;
}
// 1. 显示添加设备对话框
var device = await _dialogService.ShowAddDeviceDialog();
// 如果用户取消或对话框未返回设备,则直接返回
if (device == null)
{
_logger.LogInformation("用户取消了添加设备操作。");
return;
}
await _deviceRepository.AddDeviceAndMenu(device);
await _deviceRepository.Add(device);
}
catch (Exception e)
{
NotificationHelper.ShowMessage($"添加设备的过程中发生错误:{e.Message}", NotificationType.Error);
_logger.LogError($"添加设备的过程中发生错误:{e}");
}
}
/// <summary>
@@ -80,6 +88,7 @@ public partial class DevicesViewModel : ViewModelBase
[RelayCommand]
public async void DeleteDevice()
{
try
{
if (SelectedDevice == null)
@@ -92,12 +101,9 @@ public partial class DevicesViewModel : ViewModelBase
var isDel = await _dialogService.ShowConfrimeDialog("删除设备", msg, "删除设备");
if (isDel)
{
// 删除设备
await _deviceRepository.DeleteById(SelectedDevice.Id);
// 删除菜单
var menu = DataServicesHelper.FindMenusForDevice(SelectedDevice, _dataServices.MenuTrees);
if (menu != null)
await _menuRepository.DeleteMenu(menu);
await _deviceRepository.Delete(SelectedDevice ,_dataServices.MenuTrees);
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);

View File

@@ -4,6 +4,7 @@ using iNKORE.UI.WPF.Modern.Common.IconKeys;
using iNKORE.UI.WPF.Modern.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PMSWPF.Data;
using PMSWPF.Data.Repositories;
using PMSWPF.Enums;
using PMSWPF.Helper;
@@ -69,6 +70,7 @@ public partial class MainViewModel : ViewModelBase
/// <param name="menu">当前菜单项,用于获取父级设备信息。</param>
private async Task AddVariableTable(MenuBean menu)
{
var db = DbContext.GetInstance();
try
{
// 1. 检查父级设备信息
@@ -94,29 +96,25 @@ public partial class MainViewModel : ViewModelBase
varTable.ProtocolType = device.ProtocolType;
// 4. 添加变量表到数据库
// 假设 _varTableRepository.Add 返回一个布尔值表示成功,或者一个表示 ID 的整数
// 假设 _varTableRepository.AddAsync 返回一个布尔值表示成功,或者一个表示 ID 的整数
// 这里为了演示我们假设它返回新添加的ID如果失败则返回0
var addVarTableId = await _varTableRepository.Add(varTable);
if (addVarTableId <= 0)
{
NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加失败", NotificationType.Error);
_logger.LogError($"变量表:{varTable.Name},添加失败");
return; // 添加变量表失败,提前返回
}
await db.BeginTranAsync();
var addVarTable = await _varTableRepository.Add(varTable,db);
// 5. 添加变量表菜单
MenuBean newMenu = new MenuBean
{
Icon = SegoeFluentIcons.Tablet.Glyph,
Name = varTable.Name,
DataId = addVarTableId, // 使用实际添加的ID
DataId = addVarTable.Id, // 使用实际添加的ID
Type = MenuType.VariableTableMenu,
ParentId = menu.Parent.Id
};
var addMenuRes = await _menuRepository.Add(newMenu);
var addMenuRes = await _menuRepository.Add(newMenu,db);
if (addMenuRes > 0)
{
await db.CommitTranAsync();
// 变量表和菜单都添加成功
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
@@ -125,6 +123,7 @@ public partial class MainViewModel : ViewModelBase
}
else
{
await db.RollbackTranAsync();
// 变量表菜单添加失败 (此时变量表可能已添加成功,需要根据业务决定是否回滚)
NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加菜单失败", NotificationType.Error);
_logger.LogError($"变量表:{varTable.Name},添加菜单失败");
@@ -134,8 +133,9 @@ public partial class MainViewModel : ViewModelBase
}
catch (Exception e)
{
await db.RollbackTranAsync();
// 捕获并记录所有未预料的异常
_logger.LogError(e, "添加变量表时出现了未预期的错误");
_logger.LogError($"添加变量表时出现了未预期的错误:{e}");
NotificationHelper.ShowMessage($"添加变量表时出现了错误:{e.Message}", NotificationType.Error);
}
}

View File

@@ -162,16 +162,14 @@ partial class VariableTableViewModel : ViewModelBase
variableData.VariableTableId = VariableTable.Id;
}
// 插入数据库
var resVarDataList= await _varDataRepository.AddAsync(importVarDataList);
var resVarDataCount= await _varDataRepository.AddAsync(importVarDataList);
//更新界面
// variableTable.DataVariables.AddRange(resVarDataList);
foreach (var variableData in resVarDataList)
{
variableTable.DataVariables.Add(variableData);
}
DataVariables=new ObservableCollection<VariableData>(resVarDataList);
variableTable.DataVariables= await _varDataRepository.GetAllAsync();
DataVariables=new ObservableCollection<VariableData>(variableTable.DataVariables);
processingDialog?.Hide();
string msgSuccess = $"成功导入变量:{resVarDataList.Count}个。";
string msgSuccess = $"成功导入变量:{resVarDataCount}个。";
Logger.Info(msgSuccess);
NotificationHelper.ShowMessage(msgSuccess, NotificationType.Success);