From 6ee5f10aed090ce2200390f545ccc70cfe55198d Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Thu, 12 Jun 2025 18:41:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=86=99=E4=BA=86Object=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=89=A9?= =?UTF-8?q?=E5=B1=95CopyTo=E6=96=B9=E6=B3=95=EF=BC=8C=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E4=BA=86=E6=95=B0=E6=8D=AE=E8=A1=A8=E7=9A=84=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 2 + Data/Entities/DbDevice.cs | 6 +++ Data/Repositories/DevicesRepositories.cs | 18 +++++-- Exceptions/DbExistException.cs | 10 ++++ Extensions/ObjectExtensions.cs | 28 +++++++++++ Models/Device.cs | 16 ++++++- Services/DeviceDialogService.cs | 2 +- ViewModels/DevicesViewModel.cs | 33 +++++++++++-- ViewModels/Dialogs/DeviceDialogViewModel.cs | 19 +++++++- Views/Dialogs/DeviceDialog.xaml | 52 ++++++++++++--------- Views/Dialogs/DeviceDialog.xaml.cs | 4 +- 11 files changed, 154 insertions(+), 36 deletions(-) create mode 100644 Exceptions/DbExistException.cs create mode 100644 Extensions/ObjectExtensions.cs diff --git a/App.xaml.cs b/App.xaml.cs index a61ba8a..fb36400 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -3,6 +3,7 @@ using System.Data; using System.Windows; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using PMSWPF.Data.Repositories; using PMSWPF.Services; using PMSWPF.ViewModels; using PMSWPF.Views; @@ -21,6 +22,7 @@ namespace PMSWPF { var container = new ServiceCollection(); container.AddSingleton(); + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); diff --git a/Data/Entities/DbDevice.cs b/Data/Entities/DbDevice.cs index 4e755f5..004fbcd 100644 --- a/Data/Entities/DbDevice.cs +++ b/Data/Entities/DbDevice.cs @@ -1,10 +1,16 @@ +using SqlSugar; + namespace PMSWPF.Data.Entities; +[SugarTable("Device")] public class DbDevice { + + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增 public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } + public string Ip { get; set; } public bool IsActive { get; set; } public bool IsRuning { get; set; } diff --git a/Data/Repositories/DevicesRepositories.cs b/Data/Repositories/DevicesRepositories.cs index 1c81b13..6685839 100644 --- a/Data/Repositories/DevicesRepositories.cs +++ b/Data/Repositories/DevicesRepositories.cs @@ -1,17 +1,29 @@ using PMSWPF.Data.Entities; +using PMSWPF.Excptions; namespace PMSWPF.Data.Repositories; public class DevicesRepositories:BaseRepositories { - public DevicesRepositories() + public DevicesRepositories():base() { - + var tableExist= _db.DbMaintenance.IsAnyTable(); + if (!tableExist) + { + _db.CodeFirst.InitTables(); + } } public async Task Add(DbDevice dbDevice) { - return await _db.Insertable(dbDevice).ExecuteCommandAsync(); + var exist=await _db.Queryable().Where(d=>d.Name==dbDevice.Name).FirstAsync(); + if (exist != null) + { + throw new DbExistException("设备名称已经存在。"); + } + var res= await _db.Insertable(dbDevice).ExecuteCommandAsync(); + + return res; } public async Task> GetAll() diff --git a/Exceptions/DbExistException.cs b/Exceptions/DbExistException.cs new file mode 100644 index 0000000..45ba436 --- /dev/null +++ b/Exceptions/DbExistException.cs @@ -0,0 +1,10 @@ +namespace PMSWPF.Excptions; + +public class DbExistException: Exception +{ + public DbExistException() : base() { } + + public DbExistException(string message) : base(message) { } + + public DbExistException(string message, System.Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/Extensions/ObjectExtensions.cs b/Extensions/ObjectExtensions.cs new file mode 100644 index 0000000..365026f --- /dev/null +++ b/Extensions/ObjectExtensions.cs @@ -0,0 +1,28 @@ +using System.Reflection; + +namespace PMSWPF.Extensions; + +public static class ObjectExtensions +{ + /// + /// 对象转换,将source对象上的所有属性的值,都转换到target对象上 + /// + /// + /// + /// + public static void CopyTo(this Object source ,T target) + { + var sourceType = source.GetType(); + var targetType = target.GetType(); + var sourceProperties = sourceType.GetProperties(); + foreach (PropertyInfo sourceProperty in sourceProperties) + { + PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name); + if (targetProperty!= null && targetProperty.CanWrite && sourceProperty.CanRead && targetProperty.PropertyType == sourceProperty.PropertyType) + { + object value = sourceProperty.GetValue(source, null); + targetProperty.SetValue(target, value, null); + } + } + } +} \ No newline at end of file diff --git a/Models/Device.cs b/Models/Device.cs index b35156d..afee96e 100644 --- a/Models/Device.cs +++ b/Models/Device.cs @@ -1,6 +1,20 @@ +using CommunityToolkit.Mvvm.ComponentModel; + namespace PMSWPF.Models; -public class Device +public partial class Device:ObservableObject { + [ObservableProperty] + private int id; + [ObservableProperty] + private string name; + [ObservableProperty] + private string description ; + [ObservableProperty] + private string ip ; + [ObservableProperty] + private bool isActive ; + [ObservableProperty] + private bool isRuning ; } \ No newline at end of file diff --git a/Services/DeviceDialogService.cs b/Services/DeviceDialogService.cs index 570ba4d..82d5282 100644 --- a/Services/DeviceDialogService.cs +++ b/Services/DeviceDialogService.cs @@ -8,7 +8,7 @@ public class DeviceDialogService : IDeviceDialogService { public async Task ShowAddDeviceDialog(Device device) { - DeviceDialogViewModel ddvm = new DeviceDialogViewModel() + DeviceDialogViewModel ddvm = new DeviceDialogViewModel(device) { Title = "添加设备" }; diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 4c3899c..78f6c23 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -1,4 +1,10 @@ -using CommunityToolkit.Mvvm.Input; +using System.Windows; +using CommunityToolkit.Mvvm.Input; +using PMSWPF.Data.Entities; +using PMSWPF.Data.Repositories; +using PMSWPF.Excptions; +using PMSWPF.Extensions; +using PMSWPF.Helper; using PMSWPF.Models; using PMSWPF.Services; using PMSWPF.ViewModels.Dialogs; @@ -9,17 +15,36 @@ namespace PMSWPF.ViewModels; public partial class DevicesViewModel : ViewModelBase { private readonly IDeviceDialogService _deviceDialogService; + private readonly DevicesRepositories _devicesRepositories; - public DevicesViewModel(IDeviceDialogService deviceDialogService) + public DevicesViewModel(IDeviceDialogService deviceDialogService,DevicesRepositories devicesRepositories) { _deviceDialogService = deviceDialogService; + _devicesRepositories = devicesRepositories; } [RelayCommand] public async void AddDevice() { - Device device = new Device(); - await _deviceDialogService.ShowAddDeviceDialog(device); + try + { + Device device = new Device(); + await _deviceDialogService.ShowAddDeviceDialog(device); + DbDevice dbDevice = new DbDevice(); + device.CopyTo(dbDevice); + await _devicesRepositories.Add(dbDevice); + } + catch (DbExistException e) + { + Console.WriteLine(e); + MessageBox.Show(e.Message); + + } + catch (Exception e) + { + Console.WriteLine(e); + MessageBox.Show(e.Message); + } } } \ No newline at end of file diff --git a/ViewModels/Dialogs/DeviceDialogViewModel.cs b/ViewModels/Dialogs/DeviceDialogViewModel.cs index c90f6c5..4cd6e9d 100644 --- a/ViewModels/Dialogs/DeviceDialogViewModel.cs +++ b/ViewModels/Dialogs/DeviceDialogViewModel.cs @@ -1,13 +1,28 @@ using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using PMSWPF.Extensions; +using PMSWPF.Models; namespace PMSWPF.ViewModels.Dialogs; public partial class DeviceDialogViewModel:ObservableObject { + private readonly Device _saveDevice; + [ObservableProperty] private string title="添加设备"; - public DeviceDialogViewModel() + [ObservableProperty] + private Device device; + public DeviceDialogViewModel(Device saveDevice) { - + _saveDevice = saveDevice; + this.device = new Device(); + } + + + [RelayCommand] + public void AddDevice() + { + this.device.CopyTo(_saveDevice); } } \ No newline at end of file diff --git a/Views/Dialogs/DeviceDialog.xaml b/Views/Dialogs/DeviceDialog.xaml index e98b658..19a058a 100644 --- a/Views/Dialogs/DeviceDialog.xaml +++ b/Views/Dialogs/DeviceDialog.xaml @@ -11,40 +11,46 @@ Title="{Binding Title}" d:DesignHeight="756" d:DesignWidth="700" - CloseButtonClick="OnCloseButtonClick" CloseButtonText="取消" Closed="OnClosed" DefaultButton="Primary" - PrimaryButtonClick="OnPrimaryButtonClick" + PrimaryButtonCommand="{Binding AddDeviceCommand}" PrimaryButtonText="添加" FullSizeDesired="False" IsShadowEnabled="True" d:DataContext="{d:DesignInstance vmd:DeviceDialogViewModel}" mc:Ignorable="d"> + + + - - - - - - - -