From a04bfc2010b613251f6c3eff690bf457f00cf5d2 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Thu, 26 Jun 2025 22:40:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=97=B6=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=92=8C=E6=9B=B4=E6=96=B0=E8=8F=9C=E5=8D=95=E6=A0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Data/Repositories/DeviceRepository.cs | 3 +- Data/Repositories/MenuRepository.cs | 54 ++++++++++++++++++++++++--- Helper/CovertHelper.cs | 36 ------------------ Helper/SqlSugarHelper.cs | 17 --------- Message/DialogMessage.cs | 9 ----- Message/OpenDialogMessage.cs | 21 ----------- Services/DialogService.cs | 29 -------------- ViewModels/DevicesViewModel.cs | 49 +++++++----------------- 8 files changed, 63 insertions(+), 155 deletions(-) delete mode 100644 Helper/CovertHelper.cs delete mode 100644 Helper/SqlSugarHelper.cs delete mode 100644 Message/DialogMessage.cs delete mode 100644 Message/OpenDialogMessage.cs diff --git a/Data/Repositories/DeviceRepository.cs b/Data/Repositories/DeviceRepository.cs index 25e1c97..51fbdc6 100644 --- a/Data/Repositories/DeviceRepository.cs +++ b/Data/Repositories/DeviceRepository.cs @@ -17,7 +17,8 @@ public class DeviceRepository public async Task Add(Device device) { var exist = await _db.Queryable().Where(d => d.Name == device.Name).FirstAsync(); - if (exist != null) throw new DbExistException("设备名称已经存在。"); + if (exist != null) + throw new InvalidOperationException("设备名称已经存在。"); var dbDevice = new DbDevice(); device.CopyTo(dbDevice); dbDevice.VariableTables = new List(); diff --git a/Data/Repositories/MenuRepository.cs b/Data/Repositories/MenuRepository.cs index e4072bd..5c593ac 100644 --- a/Data/Repositories/MenuRepository.cs +++ b/Data/Repositories/MenuRepository.cs @@ -1,6 +1,11 @@ using System.Windows.Controls; +using CommunityToolkit.Mvvm.Messaging; +using iNKORE.UI.WPF.Modern.Common.IconKeys; using PMSWPF.Data.Entities; +using PMSWPF.Enums; using PMSWPF.Extensions; +using PMSWPF.Helper; +using PMSWPF.Message; using PMSWPF.Models; using SqlSugar; @@ -34,11 +39,48 @@ public class MenuRepository return await _db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); } - public async Task AddDeviceMenu(MenuBean menu) + + public async Task AddDeviceMenu(MenuBean menu) { - var deviceMenu = await _db.Queryable().FirstAsync(m => m.Name == "设备"); - if (deviceMenu == null) return 0; - menu.ParentId = deviceMenu.Id; - return await _db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); + bool result = false; + var deviceMainMenu = await _db.Queryable().FirstAsync(m => m.Name == "设备"); + if (deviceMainMenu == null) + throw new InvalidOperationException("没有找到设备菜单!!"); + + menu.ParentId=deviceMainMenu.Id; + var addDeviceMenuRes = await _db.Insertable(menu.CopyTo()) + .ExecuteCommandAsync(); + if (addDeviceMenuRes == 0) + throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!"); + + var addDM = await _db.Queryable().OrderBy(m => m.Id, OrderByType.Desc) + .FirstAsync(m => m.Name == menu.Name); + if (addDM == null) + throw new InvalidOperationException($"添加默认变量表菜单时,没有找到名字为:{menu.Name}的菜单项!"); + + + var defVarTable=new MenuBean() + { + Name = "默认变量表", + Icon = SegoeFluentIcons.Tablet.Glyph, + ParentId = addDM.Id, + }; + var addVarTable=new MenuBean() + { + Name = "添加变量表", + Icon = SegoeFluentIcons.Add.Glyph, + ParentId = addDM.Id, + }; + var defTableRes = await _db.Insertable(defVarTable).ExecuteCommandAsync(); + var addTableRes = await _db.Insertable(addVarTable).ExecuteCommandAsync(); + if ((addTableRes+defTableRes) != 2) + { + // 如果出错删除原来添加的设备菜单 + await _db.Deleteable().Where(m=>m.Id==addDM.Id).ExecuteCommandAsync(); + throw new InvalidOperationException("添加默认变量表时发生了错误!!"); + } + + + return true; } -} \ No newline at end of file +} diff --git a/Helper/CovertHelper.cs b/Helper/CovertHelper.cs deleted file mode 100644 index 1119739..0000000 --- a/Helper/CovertHelper.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; - -namespace PMSWPF.Helper; - -public class CovertHelper -{ - public static List ConvertList(List sourceList) - { - var targetList = new List(); - var sourceType = typeof(TSource); - var targetType = typeof(TTarget); - - // 获取源类型和目标类型的公共属性 - var sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance); - var targetProperties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance); - - foreach (var sourceObject in sourceList) - { - var targetObject = Activator.CreateInstance(); - foreach (var targetProperty in targetProperties) - { - var sourceProperty = sourceProperties.FirstOrDefault(p => - p.Name == targetProperty.Name && p.PropertyType == targetProperty.PropertyType); - if (sourceProperty != null) - { - var value = sourceProperty.GetValue(sourceObject); - targetProperty.SetValue(targetObject, value); - } - } - - targetList.Add(targetObject); - } - - return targetList; - } -} \ No newline at end of file diff --git a/Helper/SqlSugarHelper.cs b/Helper/SqlSugarHelper.cs deleted file mode 100644 index 442c8ff..0000000 --- a/Helper/SqlSugarHelper.cs +++ /dev/null @@ -1,17 +0,0 @@ -using PMSWPF.Data; - -namespace PMSWPF.Helper; - -public class SqlSugarHelper -{ - private DbContext _db; - - public SqlSugarHelper() - { - _db = new DbContext(); - } - - public void InitTables() - { - } -} \ No newline at end of file diff --git a/Message/DialogMessage.cs b/Message/DialogMessage.cs deleted file mode 100644 index 28c509e..0000000 --- a/Message/DialogMessage.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace PMSWPF.Message; - -public class DialogMessage -{ - public bool IsCancel { get; set; } - public bool IsConfirm { get; set; } - public Object? Request { get; set; } - public Object? Response { get; set; } -} \ No newline at end of file diff --git a/Message/OpenDialogMessage.cs b/Message/OpenDialogMessage.cs deleted file mode 100644 index bc16d2a..0000000 --- a/Message/OpenDialogMessage.cs +++ /dev/null @@ -1,21 +0,0 @@ -using CommunityToolkit.Mvvm.Messaging.Messages; - -namespace PMSWPF.Message; - -public class OpenDialogMessage:RequestMessage -{ - // public bool IsCancel { get; set; } - // public bool IsConfirm { get; set; } - // public Object? Request { get; set; } - // public Object? Response { get; set; } - public DialogMessage Message { get; set; } - - public OpenDialogMessage() - { - } - - public OpenDialogMessage(DialogMessage message) - { - Message = message; - } -} \ No newline at end of file diff --git a/Services/DialogService.cs b/Services/DialogService.cs index aceb66c..48b11f4 100644 --- a/Services/DialogService.cs +++ b/Services/DialogService.cs @@ -33,33 +33,4 @@ public class DialogService :IDialogService MessageBox.Show(message); } - - public void Receive(OpenDialogMessage message) - { - // DialogMessage response = new DialogMessage(); - // Device device = new Device(); - // if (message.Message! != null && message.Message.Request != null && message.Message.Request is Device) - // { - // device = message.Message.Request as Device; - // } - // else - // { - // var ddvm = new DeviceDialogViewModel(device) - // { - // Title = "添加设备" - // }; - // var dialog = new DeviceDialog(ddvm); - // var res = dialog.ShowAsync().GetAwaiter().GetResult(); - // if (res == ContentDialogResult.Primary) - // { - // response.IsConfirm = true; - // response.Response = device; - // } - // else - // { - // response.IsCancel = true; - // } - // } - // message.Reply(response); - } } \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index f780de9..c8464bd 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -58,54 +58,31 @@ public partial class DevicesViewModel : ViewModelBase Name = device.Name, Icon = SegoeFluentIcons.Devices4.Glyph, }; - var rows = await _menuRepository.AddDeviceMenu(deviceMenu); - if (rows > 0) + bool addMenuRes = await _menuRepository.AddDeviceMenu(deviceMenu); + if (addMenuRes) { - WeakReferenceMessenger.Default.Send(new UpdateMenuMessage(2)); + // 通知更新菜单 + WeakReferenceMessenger.Default.Send(new UpdateMenuMessage(0)); + NotificationHelper.ShowMessage(msg, NotificationType.Success); + } + else + { + var msgerr = $"给设备添加菜单失败:{device.Name}"; + _logger.LogInformation(msgerr); + NotificationHelper.ShowMessage(msgerr, NotificationType.Error); } } else { var msg = $"添加设备失败:{device.Name}"; _logger.LogInformation(msg); + NotificationHelper.ShowMessage(msg, NotificationType.Error); } } - - // OpenDialogMessage dialog = new OpenDialogMessage(); - // - // var res=WeakReferenceMessenger.Default.Send(dialog); - - Console.WriteLine(""); - - // device = await _deviceDialogService.ShowAddDeviceDialog(); - // if (device != null) - // { - // var isOk = await _deviceRepository.Add(device); - // if (isOk) - // { - // // 添加菜单项 - // MenuBean deviceMenu = new MenuBean() - // { Name = device.Name, Icon = SegoeFluentIcons.Devices4.Glyph, ParentId = 2 }; - // MenuRepository mre = new MenuRepository(); - // mre.AddMenu(deviceMenu); - // - // // MessageBox.Show("Device added successfully"); - // await OnLoadedAsync(); - // var msg = $"设备添加成功:{device.Name}"; - // _logger.LogInformation(msg); - // NotificationHelper.ShowMessage(msg, NotificationType.Success); - // } - // } - } - catch (DbExistException e) - { - var msg = $"设备添加失败:名称为{device?.Name}的设备已经存在。请更换是被名称"; - _logger.LogError(msg); - NotificationHelper.ShowMessage(msg, NotificationType.Error); } catch (Exception e) { - var msg = $"添加设备的过程中发生错误:{e.Message}"; + var msg = $"添加设备失败:{e.Message}"; _logger.LogError(msg); NotificationHelper.ShowMessage(msg, NotificationType.Success); }