From f821024756184f7f6854d388673e2df230b64194 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Fri, 22 Aug 2025 20:24:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interfaces/IVariableAppService.cs | 5 + .../Services/VariableAppService.cs | 14 + DMS.Core/Enums/SignalType.cs | 3 + DMS.Core/Interfaces/IExcelService.cs | 2 +- .../Repositories/IBaseRepository.cs | 6 + DMS.Infrastructure/DMS.Infrastructure.csproj | 4 - .../Repositories/BaseRepository.cs | 10 + .../Repositories/DeviceRepository.cs | 6 + .../Repositories/MenuRepository.cs | 6 + .../Repositories/MqttServerRepository.cs | 6 + .../Repositories/UserRepository.cs | 6 + .../Repositories/VariableHistoryRepository.cs | 6 + .../VariableMqttAliasRepository.cs | 6 + .../Repositories/VariableRepository.cs | 6 + .../Repositories/VariableTableRepository.cs | 6 + DMS.Infrastructure/Services/ExcelService.cs | 267 +++++++++++++++++- DMS.WPF/App.xaml.cs | 4 +- DMS.WPF/Profiles/MappingProfile.cs | 3 + DMS.WPF/Services/DialogService.cs | 1 + .../Dialogs/ImportExcelDialogViewModel.cs | 64 +++-- DMS.WPF/ViewModels/VariableTableViewModel.cs | 44 ++- DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml | 17 +- .../Views/Dialogs/ImportExcelDialog.xaml.cs | 3 +- 23 files changed, 454 insertions(+), 41 deletions(-) diff --git a/DMS.Application/Interfaces/IVariableAppService.cs b/DMS.Application/Interfaces/IVariableAppService.cs index 557b7dc..494e153 100644 --- a/DMS.Application/Interfaces/IVariableAppService.cs +++ b/DMS.Application/Interfaces/IVariableAppService.cs @@ -31,4 +31,9 @@ public interface IVariableAppService /// 异步删除一个变量。 /// Task DeleteVariableAsync(int id); + + /// + /// 异步批量导入变量。 + /// + Task BatchImportVariablesAsync(List variables); } \ No newline at end of file diff --git a/DMS.Application/Services/VariableAppService.cs b/DMS.Application/Services/VariableAppService.cs index 4443e08..9b0c3df 100644 --- a/DMS.Application/Services/VariableAppService.cs +++ b/DMS.Application/Services/VariableAppService.cs @@ -124,4 +124,18 @@ public class VariableAppService : IVariableAppService throw new ApplicationException($"删除变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex); } } + + public async Task BatchImportVariablesAsync(List variables) + { + try + { + var variableModels = _mapper.Map>(variables); + var result = await _repoManager.Variables.AddBatchAsync(variableModels); + return result; + } + catch (Exception ex) + { + throw new ApplicationException($"批量导入变量时发生错误,错误信息:{ex.Message}", ex); + } + } } \ No newline at end of file diff --git a/DMS.Core/Enums/SignalType.cs b/DMS.Core/Enums/SignalType.cs index 84178b7..f4c98c7 100644 --- a/DMS.Core/Enums/SignalType.cs +++ b/DMS.Core/Enums/SignalType.cs @@ -24,8 +24,11 @@ public enum CSharpDataType [Description("布尔型")] Bool, [Description("字节型")] Byte, [Description("短整型")] Short, + [Description("无符号短整型")] UShort, [Description("整型")] Int, + [Description("无符号整型")] UInt, [Description("长整型")] Long, + [Description("无符号长整型")] ULong, [Description("浮点型")] Float, [Description("双精度浮点型")] Double, [Description("字符串型")] String, diff --git a/DMS.Core/Interfaces/IExcelService.cs b/DMS.Core/Interfaces/IExcelService.cs index a4d185e..f653e48 100644 --- a/DMS.Core/Interfaces/IExcelService.cs +++ b/DMS.Core/Interfaces/IExcelService.cs @@ -1,6 +1,6 @@ using DMS.Core.Models; -namespace DMS.Infrastructure.Services; +namespace DMS.Core.Interfaces; public interface IExcelService { diff --git a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs index 655da79..8dd6c1b 100644 --- a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs +++ b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs @@ -48,4 +48,10 @@ public interface IBaseRepository where T : class /// /// 从数据库获取数据的条数 Task> TakeAsync(int number); + + /// + /// 异步批量添加实体。 + /// + /// 要添加的实体列表。 + Task AddBatchAsync(List entities); } \ No newline at end of file diff --git a/DMS.Infrastructure/DMS.Infrastructure.csproj b/DMS.Infrastructure/DMS.Infrastructure.csproj index 559f495..c727733 100644 --- a/DMS.Infrastructure/DMS.Infrastructure.csproj +++ b/DMS.Infrastructure/DMS.Infrastructure.csproj @@ -25,8 +25,4 @@ - - - - diff --git a/DMS.Infrastructure/Repositories/BaseRepository.cs b/DMS.Infrastructure/Repositories/BaseRepository.cs index f21fb34..dd45459 100644 --- a/DMS.Infrastructure/Repositories/BaseRepository.cs +++ b/DMS.Infrastructure/Repositories/BaseRepository.cs @@ -191,4 +191,14 @@ public abstract class BaseRepository NlogHelper.Info($"TakeAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return entity; } + + public async Task AddBatchAsync(List entities) + { + var stopwatch = new Stopwatch(); + stopwatch.Start(); + var result = await Db.Insertable(entities).ExecuteCommandAsync(); + stopwatch.Stop(); + NlogHelper.Info($"AddBatchAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result > 0; + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/DeviceRepository.cs b/DMS.Infrastructure/Repositories/DeviceRepository.cs index d433a6d..810669a 100644 --- a/DMS.Infrastructure/Repositories/DeviceRepository.cs +++ b/DMS.Infrastructure/Repositories/DeviceRepository.cs @@ -101,4 +101,10 @@ public class DeviceRepository : BaseRepository, IDeviceRepository return _mapper.Map>(dbList); } + + public Task AddBatchAsync(List entities) + { + var dbEntities = _mapper.Map>(entities); + return base.AddBatchAsync(dbEntities); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/MenuRepository.cs b/DMS.Infrastructure/Repositories/MenuRepository.cs index f94da4e..353edb3 100644 --- a/DMS.Infrastructure/Repositories/MenuRepository.cs +++ b/DMS.Infrastructure/Repositories/MenuRepository.cs @@ -173,4 +173,10 @@ public class MenuRepository : BaseRepository, IMenuRepository var dbList = await base.TakeAsync(number); return _mapper.Map>(dbList); } + + public Task AddBatchAsync(List entities) + { + var dbEntities = _mapper.Map>(entities); + return base.AddBatchAsync(dbEntities); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/MqttServerRepository.cs b/DMS.Infrastructure/Repositories/MqttServerRepository.cs index 0a9a95b..c73c085 100644 --- a/DMS.Infrastructure/Repositories/MqttServerRepository.cs +++ b/DMS.Infrastructure/Repositories/MqttServerRepository.cs @@ -100,4 +100,10 @@ public class MqttServerRepository : BaseRepository, IMqttServerRep return _mapper.Map>(dbList); } + + public Task AddBatchAsync(List entities) + { + var dbEntities = _mapper.Map>(entities); + return base.AddBatchAsync(dbEntities); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/UserRepository.cs b/DMS.Infrastructure/Repositories/UserRepository.cs index 494e645..dd85998 100644 --- a/DMS.Infrastructure/Repositories/UserRepository.cs +++ b/DMS.Infrastructure/Repositories/UserRepository.cs @@ -103,4 +103,10 @@ public class UserRepository : BaseRepository, IUserRepository return _mapper.Map>(dbList); } + + public Task AddBatchAsync(List entities) + { + var dbEntities = _mapper.Map>(entities); + return base.AddBatchAsync(dbEntities); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs index 24854e1..c113089 100644 --- a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs @@ -102,4 +102,10 @@ public class VariableHistoryRepository : BaseRepository, IVar return _mapper.Map>(dbList); } + + public Task AddBatchAsync(List entities) + { + var dbEntities = _mapper.Map>(entities); + return base.AddBatchAsync(dbEntities); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs index 7ce4310..30bf982 100644 --- a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs @@ -102,4 +102,10 @@ public class VariableMqttAliasRepository : BaseRepository, return _mapper.Map>(dbList); } + + public Task AddBatchAsync(List entities) + { + var dbEntities = _mapper.Map>(entities); + return base.AddBatchAsync(dbEntities); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableRepository.cs b/DMS.Infrastructure/Repositories/VariableRepository.cs index ded882c..694934a 100644 --- a/DMS.Infrastructure/Repositories/VariableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableRepository.cs @@ -184,4 +184,10 @@ public class VariableRepository : BaseRepository, IVariableRepositor return _mapper.Map>(dbList); } + + public Task AddBatchAsync(List entities) + { + var dbEntities = _mapper.Map>(entities); + return base.AddBatchAsync(dbEntities); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableTableRepository.cs b/DMS.Infrastructure/Repositories/VariableTableRepository.cs index a08d1b9..b2724e0 100644 --- a/DMS.Infrastructure/Repositories/VariableTableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableTableRepository.cs @@ -103,6 +103,12 @@ public class VariableTableRepository : BaseRepository, IVariabl } + public Task AddBatchAsync(List entities) + { + var dbEntities = _mapper.Map>(entities); + return base.AddBatchAsync(dbEntities); + } + /// /// 异步根据设备ID删除所有关联的变量表。 /// diff --git a/DMS.Infrastructure/Services/ExcelService.cs b/DMS.Infrastructure/Services/ExcelService.cs index 170da54..2280e7a 100644 --- a/DMS.Infrastructure/Services/ExcelService.cs +++ b/DMS.Infrastructure/Services/ExcelService.cs @@ -1,6 +1,269 @@ +using System.Data; +using System.Reflection; +using DMS.Core.Enums; +using DMS.Core.Interfaces; +using DMS.Core.Models; +using DMS.Infrastructure.Helper; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; + namespace DMS.Infrastructure.Services; -public class ExcelService +public class ExcelService : IExcelService { - + /// + /// 将数据列表导出到 Excel 文件。 + /// + /// 数据类型。 + /// 要导出的数据列表。 + /// Excel 文件的保存路径。 + /// 工作表的名称。 + public void ExportToExcel(IEnumerable data, string filePath, string sheetName = "Sheet1") where T : class + { + if (data == null) + { + throw new ArgumentNullException(nameof(data)); + } + + using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) + { + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet(sheetName); + + // 获取T类型的属性 + PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); + + // 创建表头 + IRow headerRow = sheet.CreateRow(0); + for (int i = 0; i < properties.Length; i++) + { + headerRow.CreateCell(i).SetCellValue(properties[i].Name); + } + + // 填充数据 + int rowIndex = 1; + foreach (var item in data) + { + IRow dataRow = sheet.CreateRow(rowIndex++); + for (int i = 0; i < properties.Length; i++) + { + object value = properties[i].GetValue(item, null); + dataRow.CreateCell(i).SetCellValue(value?.ToString() ?? string.Empty); + } + } + + workbook.Write(fs); + } + } + + /// + /// 将 DataTable 导出到 Excel 文件。 + /// + /// 要导出的 DataTable。 + /// Excel 文件的保存路径。 + /// 工作表的名称。 + public void ExportToExcel(DataTable dataTable, string filePath, string sheetName = "Sheet1") + { + if (dataTable == null) + { + throw new ArgumentNullException(nameof(dataTable)); + } + + using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) + { + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet(sheetName); + + // 创建表头 + IRow headerRow = sheet.CreateRow(0); + for (int i = 0; i < dataTable.Columns.Count; i++) + { + headerRow.CreateCell(i).SetCellValue(dataTable.Columns[i].ColumnName); + } + + // 填充数据 + for (int i = 0; i < dataTable.Rows.Count; i++) + { + IRow dataRow = sheet.CreateRow(i + 1); + for (int j = 0; j < dataTable.Columns.Count; j++) + { + dataRow.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString()); + } + } + + workbook.Write(fs); + } + } + + /// + /// 从 Excel 文件导入数据到 DataTable。 + /// + /// Excel 文件的路径。 + /// 工作表的名称。 + /// 是否包含表头行。 + /// 包含导入数据的 DataTable。 + public static DataTable ImportFromExcel(string filePath, string sheetName = "Sheet1", bool hasHeaderRow = true) + { + if (!File.Exists(filePath)) + { + throw new FileNotFoundException("The specified file does not exist.", filePath); + } + + var dt = new DataTable(); + using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + IWorkbook workbook = new XSSFWorkbook(fs); + ISheet sheet = workbook.GetSheet(sheetName) ?? workbook.GetSheetAt(0); + + if (sheet == null) + { + throw new Exception($"Sheet with name '{sheetName}' not found."); + } + + IRow headerRow = hasHeaderRow ? sheet.GetRow(0) : null; + int firstRow = hasHeaderRow ? 1 : 0; + int cellCount = headerRow?.LastCellNum ?? sheet.GetRow(sheet.FirstRowNum).LastCellNum; + + // 创建列 + for (int i = 0; i < cellCount; i++) + { + string columnName = hasHeaderRow ? headerRow.GetCell(i)?.ToString() ?? $"Column{i + 1}" : $"Column{i + 1}"; + dt.Columns.Add(columnName); + } + + // 填充数据 + for (int i = firstRow; i <= sheet.LastRowNum; i++) + { + IRow row = sheet.GetRow(i); + if (row == null) continue; + + DataRow dataRow = dt.NewRow(); + for (int j = 0; j < cellCount; j++) + { + ICell cell = row.GetCell(j); + dataRow[j] = cell?.ToString() ?? string.Empty; + } + dt.Rows.Add(dataRow); + } + } + return dt; + } + + /// + /// 从 Excel 文件导入数据到对象列表。 + /// + /// 要转换的目标类型。 + /// Excel 文件的路径。 + /// 工作表的名称。 + /// 包含导入数据的对象列表。 + public List ImportFromExcel(string filePath, string sheetName = "Sheet1") where T : class, new() + { + if (!File.Exists(filePath)) + { + throw new FileNotFoundException("The specified file does not exist.", filePath); + } + + var list = new List(); + PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); + + using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + IWorkbook workbook = new XSSFWorkbook(fs); + ISheet sheet = workbook.GetSheet(sheetName) ?? workbook.GetSheetAt(0); + + if (sheet == null) + { + throw new Exception($"Sheet with name '{sheetName}' not found."); + } + + IRow headerRow = sheet.GetRow(0); + if (headerRow == null) + { + throw new Exception("Header row not found."); + } + + // 创建列名到属性的映射 + var columnMap = new Dictionary(); + for (int i = 0; i < headerRow.LastCellNum; i++) + { + string columnName = headerRow.GetCell(i)?.ToString(); + if (!string.IsNullOrEmpty(columnName)) + { + var prop = properties.FirstOrDefault(p => p.Name.Equals(columnName, StringComparison.OrdinalIgnoreCase)); + if (prop != null) + { + columnMap[i] = prop; + } + } + } + + // 读取数据行 + for (int i = 1; i <= sheet.LastRowNum; i++) + { + IRow row = sheet.GetRow(i); + if (row == null) continue; + + var item = new T(); + foreach (var map in columnMap) + { + ICell cell = row.GetCell(map.Key); + if (cell != null) + { + try + { + // 尝试进行类型转换 + object value = Convert.ChangeType(cell.ToString(), map.Value.PropertyType); + map.Value.SetValue(item, value, null); + } + catch (Exception) + { + // 转换失败时可以记录日志或设置默认值 + } + } + } + list.Add(item); + } + } + return list; + } + + /// + /// 从博途的变量表中导如变量 + /// + /// + /// + /// + public List ImprotFromTiaVariableTable(string excelFilePath) + { + // Act + // _testFilePath = "C:\Users\Administrator\Desktop\浓度变量.xlsx"; + var dataTable = ImportFromExcel(excelFilePath); + // 判断表头的名字 + if (dataTable.Columns[0].ColumnName != "Name" || dataTable.Columns[2].ColumnName != "Data Type" && + dataTable.Columns[3].ColumnName != "Logical Address") + throw new AggregateException( + "Excel表格式不正确:第一列的名字是:Name,第三列的名字是:Data Type,Data Type,第四列的名字是:Logical Address,请检查"); + + + List variableDatas = new List(); + foreach (DataRow dataRow in dataTable.Rows) + { + DMS.Core.Models.Variable variable = new DMS.Core.Models.Variable(); + variable.Name = dataRow["Name"].ToString(); + variable.CSharpDataType = (DMS.Core.Enums.CSharpDataType)Enum.Parse(typeof(DMS.Core.Enums.CSharpDataType), SiemensHelper.S7ToCSharpTypeString(dataRow["Data Type"].ToString()), true); + variable.SignalType = SignalType.OtherASignal; + var exS7Addr = dataRow["Logical Address"].ToString(); + if (exS7Addr.StartsWith("%")) + { + variable.S7Address = exS7Addr.Substring(1); + } + + variable.OpcUaNodeId = ""; + variable.Protocol = ProtocolType.S7; + variable.PollLevel = PollLevelType.ThirtySeconds; + variableDatas.Add(variable); + } + + return variableDatas; + } } \ No newline at end of file diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index f4fbbba..5d78841 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -6,7 +6,6 @@ using DMS.Core.Enums; using DMS.Core.Interfaces; using DMS.Core.Interfaces.Repositories; using DMS.Helper; -using DMS.Services; using DMS.Services.Processors; using DMS.WPF.ViewModels; using DMS.WPF.Views; @@ -18,10 +17,12 @@ using DMS.Extensions; using DMS.Infrastructure.Configurations; using DMS.Infrastructure.Data; using DMS.Infrastructure.Repositories; +using DMS.Infrastructure.Services; using Microsoft.Extensions.Hosting; using DMS.WPF.Helper; using DMS.WPF.Services; using DMS.WPF.Services.Processors; +using DataProcessingService = DMS.Services.DataProcessingService; using IDataProcessingService = DMS.Services.IDataProcessingService; using LogLevel = Microsoft.Extensions.Logging.LogLevel; @@ -153,6 +154,7 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); // 注册App服务 diff --git a/DMS.WPF/Profiles/MappingProfile.cs b/DMS.WPF/Profiles/MappingProfile.cs index 2c12626..dd999c5 100644 --- a/DMS.WPF/Profiles/MappingProfile.cs +++ b/DMS.WPF/Profiles/MappingProfile.cs @@ -1,5 +1,6 @@ using AutoMapper; using DMS.Application.DTOs; +using DMS.Core.Models; using DMS.WPF.ViewModels.Items; namespace DMS.WPF.Profiles @@ -10,6 +11,8 @@ namespace DMS.WPF.Profiles { CreateMap() .ReverseMap(); + CreateMap() + .ReverseMap(); CreateMap() .ForMember(dest => dest.Children, opt => opt.Ignore()) diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs index 949863b..1bd8a68 100644 --- a/DMS.WPF/Services/DialogService.cs +++ b/DMS.WPF/Services/DialogService.cs @@ -16,6 +16,7 @@ namespace DMS.WPF.Services { typeof(DeviceDialogViewModel), typeof(DeviceDialog) }, { typeof(ConfrimDialogViewModel), typeof(ConfirmDialog) }, { typeof(VariableTableDialogViewModel), typeof(VariableTableDialog) }, + { typeof(ImportExcelDialogViewModel), typeof(ImportExcelDialog) }, // { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here // ... other dialogs }; diff --git a/DMS.WPF/ViewModels/Dialogs/ImportExcelDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/ImportExcelDialogViewModel.cs index 3f991f2..08cfbf0 100644 --- a/DMS.WPF/ViewModels/Dialogs/ImportExcelDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/ImportExcelDialogViewModel.cs @@ -1,31 +1,63 @@ using System.Collections.ObjectModel; +using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using DMS.Core.Interfaces; +using DMS.Core.Models; +using DMS.Helper; +using DMS.WPF.ViewModels.Items; namespace DMS.WPF.ViewModels.Dialogs; -public partial class ImportExcelDialogViewModel : ObservableObject +public partial class ImportExcelDialogViewModel : DialogViewModelBase> { + private readonly IMapper _mapper; + private readonly IExcelService _excelService; + [ObservableProperty] private string? _filePath; [ObservableProperty] - private ObservableCollection _variables = new(); + private List _variables = new(); + + + + public ImportExcelDialogViewModel(IMapper mapper,IExcelService excelService) + { + _mapper = mapper; + _excelService = excelService; + } partial void OnFilePathChanged(string? value) { - // if (string.IsNullOrEmpty(value)) - // { - // return; - // } - // - // try - // { - // var data = ExcelHelper.ImprotFromTiaVariableTable(value); - // Variables = new ObservableCollection(data); - // } - // catch (System.Exception ex) - // { - // // Handle exception - // } + if (string.IsNullOrEmpty(value)) + { + return; + } + + try + { + Variables = _excelService.ImprotFromTiaVariableTable(value); + } + catch (System.Exception ex) + { + NotificationHelper.ShowError($"从Excel文件中读取变量时发生了错误:{ex.Message}",ex); + } + } + [RelayCommand] + public void SecondaryButton() + { + + } + + [RelayCommand] + private void PrimaryButton() + { + Close(Variables); + } + [RelayCommand] + private void CancleButton() + { + Close(null); } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index e1dd4b0..e5d1207 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -1,25 +1,23 @@ -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Windows.Data; -using System.Windows.Input; using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using DMS.Core.Enums; +using DMS.Core.Interfaces; using DMS.Core.Models; -using DMS.Helper; -using DMS.Services; -using iNKORE.UI.WPF.Modern.Controls; -using Newtonsoft.Json; -using DMS.Extensions; using DMS.WPF.Services; +using DMS.WPF.ViewModels.Dialogs; using DMS.WPF.ViewModels.Items; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Windows.Data; namespace DMS.WPF.ViewModels; -partial class VariableTableViewModel : ViewModelBase,INavigatable +partial class VariableTableViewModel : ViewModelBase, INavigatable { private readonly IMapper _mapper; + private readonly IExcelService _excelService; /// /// 对话服务接口,用于显示各种对话框(如确认、编辑、导入等)。 @@ -93,9 +91,10 @@ partial class VariableTableViewModel : ViewModelBase,INavigatable /// 对话服务接口的实例。 private readonly DataServices _dataServices; - public VariableTableViewModel(IMapper mapper, IDialogService dialogService, DataServices dataServices) + public VariableTableViewModel(IMapper mapper, IExcelService excelService, IDialogService dialogService, DataServices dataServices) { _mapper = mapper; + _excelService = excelService; _dialogService = dialogService; _dataServices = dataServices; IsLoadCompletion = false; // 初始设置为 false,表示未完成加载 @@ -296,6 +295,13 @@ partial class VariableTableViewModel : ViewModelBase,INavigatable [RelayCommand] private async void ImprotFromTiaVarTable() { + ImportExcelDialogViewModel viewModel = new ImportExcelDialogViewModel(_mapper, _excelService); + + List improtVariable = await _dialogService.ShowDialogAsync(viewModel); + if (improtVariable == null) return; + + + // ContentDialog processingDialog = null; // 用于显示处理中的对话框 // try // { @@ -887,10 +893,20 @@ partial class VariableTableViewModel : ViewModelBase,INavigatable public async Task OnNavigatedToAsync(MenuItemViewModel menu) { var varTable = _dataServices.VariableTables.FirstOrDefault(v => v.Id == menu.TargetId); - if (varTable!=null) + if (varTable != null) { - CurrentVariableTable=varTable; + CurrentVariableTable = varTable; + + if (CurrentVariableTable.Protocol == ProtocolType.S7) + { + IsS7ProtocolSelected = true; + } + else if (CurrentVariableTable.Protocol == ProtocolType.OpcUa) + { + IsOpcUaProtocolSelected = true; + } + } - + } } \ No newline at end of file diff --git a/DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml b/DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml index 35a30fa..aa7c5a0 100644 --- a/DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml +++ b/DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml @@ -4,17 +4,32 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" + xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs" Title="从Excel导入" CloseButtonText="取消" DefaultButton="Primary" - PrimaryButtonText="导入" + PrimaryButtonText="导入全部" + SecondaryButtonText="导入选择" + d:DataContext="{d:DesignInstance vmd:ImportExcelDialogViewModel}" mc:Ignorable="d" AllowDrop="True" DragEnter="Dialog_DragEnter" Drop="Dialog_Drop"> + + + + + + + + + + + + diff --git a/DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml.cs b/DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml.cs index 624afa1..d8ee4e2 100644 --- a/DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml.cs +++ b/DMS.WPF/Views/Dialogs/ImportExcelDialog.xaml.cs @@ -8,10 +8,9 @@ namespace DMS.WPF.Views.Dialogs; public partial class ImportExcelDialog : ContentDialog { - public ImportExcelDialog(ImportExcelDialogViewModel viewModel) + public ImportExcelDialog() { InitializeComponent(); - DataContext = viewModel; } private void Dialog_DragEnter(object sender, DragEventArgs e)