导入和导出的功能

This commit is contained in:
2026-05-08 15:48:49 +08:00
parent a8c5cda332
commit 0be171ee32
18 changed files with 3039 additions and 4 deletions

View File

@@ -57,6 +57,12 @@ namespace CapMachine.Model
[Column(Name = "CapNo", IsNullable = true, StringLength = 150)] [Column(Name = "CapNo", IsNullable = true, StringLength = 150)]
public string? CapNo { get; set; } public string? CapNo { get; set; }
/// <summary>
/// 压缩机排量
/// </summary>
[Column(Name = "CapDisplacement", IsNullable = true, StringLength = 100)]
public string? CapDisplacement { get; set; }
/// <summary> /// <summary>
/// 制冷剂 /// 制冷剂
/// </summary> /// </summary>

View File

@@ -141,6 +141,8 @@ namespace CapMachine.Wpf
containerRegistry.RegisterSingleton<ComActionService>(); containerRegistry.RegisterSingleton<ComActionService>();
containerRegistry.RegisterSingleton<CanLinConfigImExportService>();
containerRegistry.RegisterSingleton<ProRuntimeService>(); containerRegistry.RegisterSingleton<ProRuntimeService>();
string strsqllite = System.AppDomain.CurrentDomain.BaseDirectory + @"Db\CapMachine.db"; string strsqllite = System.AppDomain.CurrentDomain.BaseDirectory + @"Db\CapMachine.db";
@@ -204,6 +206,7 @@ namespace CapMachine.Wpf
containerRegistry.RegisterDialog<DialogLimitConfigView, DialogLimitConfigViewModel>(); containerRegistry.RegisterDialog<DialogLimitConfigView, DialogLimitConfigViewModel>();
containerRegistry.RegisterDialog<DialogSuperHeatCoolConfigView, DialogSuperHeatCoolConfigViewModel>(); containerRegistry.RegisterDialog<DialogSuperHeatCoolConfigView, DialogSuperHeatCoolConfigViewModel>();
containerRegistry.RegisterDialog<DialogLogicRuleView, DialogLogicRuleViewModel>(); containerRegistry.RegisterDialog<DialogLogicRuleView, DialogLogicRuleViewModel>();
containerRegistry.RegisterDialog<DialogCanLinConfigImExportView, DialogCanLinConfigImExportViewModel>();
containerRegistry.RegisterDialog<DialogCANFdSchConfigView, DialogCANFdSchConfigViewModel>(); containerRegistry.RegisterDialog<DialogCANFdSchConfigView, DialogCANFdSchConfigViewModel>();
containerRegistry.RegisterDialog<DialogCANSchConfigView, DialogCANSchConfigViewModel>(); containerRegistry.RegisterDialog<DialogCANSchConfigView, DialogCANSchConfigViewModel>();

View File

@@ -92,6 +92,16 @@ namespace CapMachine.Wpf.Dtos
set { _CapNo = value; RaisePropertyChanged(); } set { _CapNo = value; RaisePropertyChanged(); }
} }
private string? _CapDisplacement;
/// <summary>
/// 压缩机排量
/// </summary>
public string? CapDisplacement
{
get { return _CapDisplacement; }
set { _CapDisplacement = value; RaisePropertyChanged(); }
}
private string? _RfNo; private string? _RfNo;
/// <summary> /// <summary>
/// 制冷剂 /// 制冷剂

View File

@@ -0,0 +1,14 @@
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CapMachine.Wpf.PrismEvent
{
public class CanLinConfigChangedEvent : PubSubEvent<string>
{
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -70,6 +70,9 @@ namespace CapMachine.Wpf.Services
} }
ShowLogicRule(msg.Par); ShowLogicRule(msg.Par);
break; break;
case "CAN/CANFD/LIN配置导入导出":
ShowCanLinConfigImExport(msg.Par);
break;
default: default:
break; break;
} }
@@ -136,10 +139,20 @@ namespace CapMachine.Wpf.Services
}); });
} }
/// <summary>
/// CAN/CANFD/LIN 配置导入导出弹窗。
/// </summary>
/// <param name="par">透传参数。</param>
private void ShowCanLinConfigImExport(object par)
{
DialogService.ShowDialog("DialogCanLinConfigImExportView", new DialogParameters() { { "Name", par } }, (dialogResult) =>
{
if (dialogResult.Result == ButtonResult.OK)
{
// Add your code here
}
});
}
#endregion #endregion

View File

@@ -44,6 +44,10 @@ namespace CapMachine.Wpf.Services
{ {
new NavigationItem("Rule","规则转换","DialogLogicRuleView"), new NavigationItem("Rule","规则转换","DialogLogicRuleView"),
}), }),
new NavigationItem("", "配置导出","",new ObservableCollection<NavigationItem>()
{
new NavigationItem("","CAN/CANFD/LIN配置导入导出","DialogCanLinConfigImExport"),
}),
//new NavigationItem("", "PID设置","",new ObservableCollection<NavigationItem>() //new NavigationItem("", "PID设置","",new ObservableCollection<NavigationItem>()
//{ //{
// new NavigationItem("Circle","转速PID",""), // new NavigationItem("Circle","转速PID",""),

View File

@@ -57,6 +57,7 @@ namespace CapMachine.Wpf.ViewModels
DialogService = dialogService; DialogService = dialogService;
EventAggregator.GetEvent<LogicRuleChangeEvent>().Subscribe(LogicRuleChangeEventCall); EventAggregator.GetEvent<LogicRuleChangeEvent>().Subscribe(LogicRuleChangeEventCall);
EventAggregator.GetEvent<CanLinConfigChangedEvent>().Subscribe(CanLinConfigChangedEventCall);
//数据波特率 //数据波特率
DataBaudRateCbxItems = new ObservableCollection<CbxItems>() DataBaudRateCbxItems = new ObservableCollection<CbxItems>()
@@ -150,6 +151,15 @@ namespace CapMachine.Wpf.ViewModels
//InitWriteRuleCbx(); //InitWriteRuleCbx();
} }
/// <summary>
/// CAN/LIN 配置更改事件(导入后刷新数据)。
/// </summary>
/// <param name="msg"></param>
private void CanLinConfigChangedEventCall(string msg)
{
InitLoadCanConfigPro();
}
/// <summary> /// <summary>
/// 初始化写规则下拉框 /// 初始化写规则下拉框
/// </summary> /// </summary>

View File

@@ -55,6 +55,7 @@ namespace CapMachine.Wpf.ViewModels
DialogService = dialogService; DialogService = dialogService;
EventAggregator.GetEvent<LogicRuleChangeEvent>().Subscribe(LogicRuleChangeEventCall); EventAggregator.GetEvent<LogicRuleChangeEvent>().Subscribe(LogicRuleChangeEventCall);
EventAggregator.GetEvent<CanLinConfigChangedEvent>().Subscribe(CanLinConfigChangedEventCall);
//仲裁波特率 //仲裁波特率
ArbBaudRateCbxItems = new ObservableCollection<CbxItems>() ArbBaudRateCbxItems = new ObservableCollection<CbxItems>()
@@ -170,6 +171,15 @@ namespace CapMachine.Wpf.ViewModels
//InitWriteRuleCbx(); //InitWriteRuleCbx();
} }
/// <summary>
/// CAN/LIN 配置更改事件(导入后刷新数据)。
/// </summary>
/// <param name="msg"></param>
private void CanLinConfigChangedEventCall(string msg)
{
InitLoadCanConfigPro();
}
/// <summary> /// <summary>
/// 初始化写规则下拉框 /// 初始化写规则下拉框
/// </summary> /// </summary>

View File

@@ -0,0 +1,319 @@
using CapMachine.Core;
using CapMachine.Wpf.PrismEvent;
using CapMachine.Wpf.Services;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
namespace CapMachine.Wpf.ViewModels
{
/// <summary>
/// CAN/CANFD/LIN 配置导入导出弹窗 ViewModel。
/// </summary>
public class DialogCanLinConfigImExportViewModel : DialogViewModel
{
/// <summary>
/// 初始化导入导出弹窗。
/// </summary>
/// <param name="canLinConfigImExportService">导入导出服务。</param>
/// <param name="eventAggregator">事件聚合器。</param>
public DialogCanLinConfigImExportViewModel(
CanLinConfigImExportService canLinConfigImExportService,
IEventAggregator eventAggregator)
{
CanLinConfigImExportService = canLinConfigImExportService;
EventAggregator = eventAggregator;
Title = "CAN/CANFD/LIN配置导入导出";
ImportModes = new ObservableCollection<ImportModeOption>
{
new ImportModeOption { Text = "覆盖导入(同名同类型先删除后导入)", Mode = CanLinImportMode.Overwrite },
new ImportModeOption { Text = "增量导入(同名同类型自动重命名)", Mode = CanLinImportMode.IncrementalRename },
};
SelectedImportMode = ImportModes.FirstOrDefault();
}
/// <summary>
/// 导入导出服务。
/// </summary>
public CanLinConfigImExportService CanLinConfigImExportService { get; }
/// <summary>
/// 事件聚合器。
/// </summary>
public IEventAggregator EventAggregator { get; }
private string _importFilePath = string.Empty;
/// <summary>
/// 导入文件路径。
/// </summary>
public string ImportFilePath
{
get { return _importFilePath; }
set { _importFilePath = value; RaisePropertyChanged(); }
}
private ObservableCollection<ImportModeOption> _importModes = new ObservableCollection<ImportModeOption>();
/// <summary>
/// 导入模式集合。
/// </summary>
public ObservableCollection<ImportModeOption> ImportModes
{
get { return _importModes; }
set { _importModes = value; RaisePropertyChanged(); }
}
private ImportModeOption? _selectedImportMode;
/// <summary>
/// 当前选中的导入模式。
/// </summary>
public ImportModeOption? SelectedImportMode
{
get { return _selectedImportMode; }
set { _selectedImportMode = value; RaisePropertyChanged(); }
}
private string _resultMessage = string.Empty;
/// <summary>
/// 操作结果消息。
/// </summary>
public string ResultMessage
{
get { return _resultMessage; }
set { _resultMessage = value; RaisePropertyChanged(); }
}
private DelegateCommand? _browseImportFileCmd;
/// <summary>
/// 选择导入文件命令。
/// </summary>
public DelegateCommand BrowseImportFileCmd
{
get
{
if (_browseImportFileCmd == null)
{
_browseImportFileCmd = new DelegateCommand(BrowseImportFile);
}
return _browseImportFileCmd;
}
}
private DelegateCommand? _importCmd;
/// <summary>
/// 导入命令。
/// </summary>
public DelegateCommand ImportCmd
{
get
{
if (_importCmd == null)
{
_importCmd = new DelegateCommand(ImportConfig);
}
return _importCmd;
}
}
private DelegateCommand? _exportCmd;
/// <summary>
/// 导出命令。
/// </summary>
public DelegateCommand ExportCmd
{
get
{
if (_exportCmd == null)
{
_exportCmd = new DelegateCommand(ExportConfig);
}
return _exportCmd;
}
}
private DelegateCommand? _closeCmd;
/// <summary>
/// 关闭命令。
/// </summary>
public DelegateCommand CloseCmd
{
get
{
if (_closeCmd == null)
{
_closeCmd = new DelegateCommand(CloseDialog);
}
return _closeCmd;
}
}
/// <summary>
/// 打开文件选择框并选择导入文件。
/// </summary>
private void BrowseImportFile()
{
try
{
var openFileDialog = new OpenFileDialog
{
Filter = "JSON文件 (*.json)|*.json|所有文件 (*.*)|*.*",
CheckFileExists = true,
CheckPathExists = true,
};
var ok = openFileDialog.ShowDialog();
if (ok == true)
{
ImportFilePath = openFileDialog.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show($"选择导入文件失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 执行配置导入。
/// </summary>
private void ImportConfig()
{
try
{
if (string.IsNullOrWhiteSpace(ImportFilePath))
{
MessageBox.Show("请先选择导入文件", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (SelectedImportMode == null)
{
MessageBox.Show("请选择导入模式", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var result = CanLinConfigImExportService.ImportFromFile(ImportFilePath, SelectedImportMode.Mode);
ResultMessage = BuildResultText(result);
if (!result.IsSuccess)
{
MessageBox.Show(ResultMessage, "导入失败", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
EventAggregator.GetEvent<CanLinConfigChangedEvent>().Publish("import");
EventAggregator.GetEvent<LogicRuleChangeEvent>().Publish("import");
MessageBox.Show(ResultMessage, "导入成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
ResultMessage = $"导入失败:{ex.Message}";
MessageBox.Show(ResultMessage, "导入失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 执行配置导出。
/// </summary>
private void ExportConfig()
{
try
{
var saveFileDialog = new SaveFileDialog
{
Filter = "JSON文件 (*.json)|*.json|所有文件 (*.*)|*.*",
DefaultExt = "json",
FileName = $"CanLinConfigExport_{DateTime.Now:yyyyMMddHHmmss}.json",
};
var ok = saveFileDialog.ShowDialog();
if (ok != true)
{
return;
}
var result = CanLinConfigImExportService.ExportToFile(saveFileDialog.FileName);
ResultMessage = BuildResultText(result);
if (!result.IsSuccess)
{
MessageBox.Show(ResultMessage, "导出失败", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
MessageBox.Show(ResultMessage, "导出成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
ResultMessage = $"导出失败:{ex.Message}";
MessageBox.Show(ResultMessage, "导出失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 关闭弹窗。
/// </summary>
private void CloseDialog()
{
RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
}
/// <summary>
/// 构建结果文本。
/// </summary>
/// <param name="result">执行结果。</param>
/// <returns>显示文本。</returns>
private static string BuildResultText(CanLinConfigImExportResult result)
{
var builder = new StringBuilder();
builder.AppendLine(result.Message);
if (result.RenamedConfigs != null && result.RenamedConfigs.Count > 0)
{
builder.AppendLine("重命名明细:");
foreach (var line in result.RenamedConfigs)
{
builder.AppendLine($"- {line}");
}
}
return builder.ToString();
}
/// <summary>
/// 弹窗打开回调。
/// </summary>
/// <param name="parameters">弹窗参数。</param>
public override void OnDialogOpened(IDialogParameters parameters)
{
ResultMessage = string.Empty;
}
/// <summary>
/// 导入模式选项。
/// </summary>
public class ImportModeOption
{
/// <summary>
/// 文本。
/// </summary>
public string Text { get; set; } = string.Empty;
/// <summary>
/// 模式值。
/// </summary>
public CanLinImportMode Mode { get; set; }
}
}
}

View File

@@ -4,6 +4,7 @@ using CapMachine.Model.CANLIN;
using CapMachine.Wpf.CanDrive; using CapMachine.Wpf.CanDrive;
using CapMachine.Wpf.Dtos; using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.LinDrive; using CapMachine.Wpf.LinDrive;
using CapMachine.Wpf.PrismEvent;
using CapMachine.Wpf.Services; using CapMachine.Wpf.Services;
using Prism.Commands; using Prism.Commands;
using Prism.Events; using Prism.Events;
@@ -56,6 +57,8 @@ namespace CapMachine.Wpf.ViewModels
//MachineDataService = machineDataService; //MachineDataService = machineDataService;
DialogService = dialogService; DialogService = dialogService;
EventAggregator.GetEvent<CanLinConfigChangedEvent>().Subscribe(CanLinConfigChangedEventCall);
//数据波特率 //数据波特率
DataBaudRateCbxItems = new ObservableCollection<CbxItems>() DataBaudRateCbxItems = new ObservableCollection<CbxItems>()
{ {
@@ -144,6 +147,15 @@ namespace CapMachine.Wpf.ViewModels
//InitWriteRuleCbx(); //InitWriteRuleCbx();
} }
/// <summary>
/// CAN/LIN 配置更改事件(导入后刷新数据)。
/// </summary>
/// <param name="msg"></param>
private void CanLinConfigChangedEventCall(string msg)
{
InitLoadLinConfigPro();
}
/// <summary> /// <summary>
/// 初始化写规则下拉框 /// 初始化写规则下拉框
/// </summary> /// </summary>

View File

@@ -3,6 +3,7 @@ using CapMachine.Core;
using CapMachine.Model.CANLIN; using CapMachine.Model.CANLIN;
using CapMachine.Wpf.CanDrive; using CapMachine.Wpf.CanDrive;
using CapMachine.Wpf.Dtos; using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.PrismEvent;
using CapMachine.Wpf.Services; using CapMachine.Wpf.Services;
using CapMachine.Wpf.Views; using CapMachine.Wpf.Views;
using ImTools; using ImTools;
@@ -80,6 +81,8 @@ namespace CapMachine.Wpf.ViewModels
ZlgLinDriveService = zlgLinDriveService; ZlgLinDriveService = zlgLinDriveService;
Mapper = mapper; Mapper = mapper;
EventAggregator.GetEvent<CanLinConfigChangedEvent>().Subscribe(CanLinConfigChangedEventCall);
SelectedMode = ZlgCanMode.Can; SelectedMode = ZlgCanMode.Can;
ArbBaudRateCbxItems = new ObservableCollection<CbxItems>() ArbBaudRateCbxItems = new ObservableCollection<CbxItems>()
@@ -2302,6 +2305,15 @@ namespace CapMachine.Wpf.ViewModels
MatchSeletedAndCanDbcModel(); MatchSeletedAndCanDbcModel();
} }
/// <summary>
/// CAN/LIN 配置更改事件(导入后刷新数据)。
/// </summary>
/// <param name="msg"></param>
private void CanLinConfigChangedEventCall(string msg)
{
InitLoadCanConfigPro();
}
/// <summary> /// <summary>
/// CAN 操作命令的 backing 字段。 /// CAN 操作命令的 backing 字段。
/// </summary> /// </summary>

View File

@@ -3,6 +3,7 @@ using CapMachine.Core;
using CapMachine.Model.CANLIN; using CapMachine.Model.CANLIN;
using CapMachine.Wpf.Dtos; using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.LinDrive; using CapMachine.Wpf.LinDrive;
using CapMachine.Wpf.PrismEvent;
using CapMachine.Wpf.Services; using CapMachine.Wpf.Services;
using CapMachine.Wpf.Views; using CapMachine.Wpf.Views;
using ImTools; using ImTools;
@@ -47,6 +48,8 @@ namespace CapMachine.Wpf.ViewModels
LogicRuleService = logicRuleService; LogicRuleService = logicRuleService;
Mapper = mapper; Mapper = mapper;
EventAggregator.GetEvent<CanLinConfigChangedEvent>().Subscribe(CanLinConfigChangedEventCall);
InitLoadLinConfigPro(); InitLoadLinConfigPro();
} }
@@ -984,6 +987,15 @@ namespace CapMachine.Wpf.ViewModels
} }
} }
/// <summary>
/// CAN/LIN 配置更改事件(导入后刷新数据)。
/// </summary>
/// <param name="msg"></param>
private void CanLinConfigChangedEventCall(string msg)
{
InitLoadLinConfigPro();
}
/// <summary> /// <summary>
/// 克隆读写配置 DTO。 /// 克隆读写配置 DTO。
/// 说明:打开弹窗前先克隆一份,避免用户在弹窗中编辑时直接影响主界面集合(取消时也不会污染当前 UI 状态)。 /// 说明:打开弹窗前先克隆一份,避免用户在弹窗中编辑时直接影响主界面集合(取消时也不会污染当前 UI 状态)。

View File

@@ -0,0 +1,83 @@
<UserControl
x:Class="CapMachine.Wpf.Views.DialogCanLinConfigImExportView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CapMachine.Wpf.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Width="800"
Height="450">
<Grid Margin="15">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock
FontSize="18"
FontWeight="Bold"
Text="CAN/CANFD/LIN 配置导入导出" />
<Grid Grid.Row="1" Margin="0,12,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox
Margin="0,0,8,0"
IsReadOnly="True"
Text="{Binding ImportFilePath, UpdateSourceTrigger=PropertyChanged}" />
<Button
Grid.Column="1"
Padding="16,4"
Command="{Binding BrowseImportFileCmd}"
Content="选择导入文件" />
</Grid>
<StackPanel
Grid.Row="2"
Margin="0,12,0,0"
Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="导入模式:" />
<ComboBox
Width="320"
Margin="8,0,0,0"
DisplayMemberPath="Text"
ItemsSource="{Binding ImportModes}"
SelectedItem="{Binding SelectedImportMode}" />
</StackPanel>
<TextBox
Grid.Row="3"
Margin="0,12,0,0"
HorizontalScrollBarVisibility="Auto"
IsReadOnly="True"
Text="{Binding ResultMessage}"
VerticalScrollBarVisibility="Auto" />
<StackPanel
Grid.Row="4"
Margin="0,12,0,0"
HorizontalAlignment="Right"
Orientation="Horizontal">
<Button
Margin="0,0,8,0"
Padding="16,4"
Command="{Binding ExportCmd}"
Content="导出" />
<Button
Margin="0,0,8,0"
Padding="16,4"
Command="{Binding ImportCmd}"
Content="导入" />
<Button
Padding="16,4"
Command="{Binding CloseCmd}"
Content="关闭" />
</StackPanel>
</Grid>
</UserControl>

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CapMachine.Wpf.Views
{
/// <summary>
/// DialogCanLinConfigImExport.xaml 的交互逻辑
/// </summary>
public partial class DialogCanLinConfigImExportView : UserControl
{
public DialogCanLinConfigImExportView()
{
InitializeComponent();
}
}
}

View File

@@ -125,6 +125,10 @@
Binding="{Binding CapNo}" Binding="{Binding CapNo}"
Header="压缩机编号" Header="压缩机编号"
IsReadOnly="{Binding Source={StaticResource Proxy}, Path=Data.IsComplete}" /> IsReadOnly="{Binding Source={StaticResource Proxy}, Path=Data.IsComplete}" />
<DataGridTextColumn
Binding="{Binding CapDisplacement}"
Header="压缩机排量"
IsReadOnly="{Binding Source={StaticResource Proxy}, Path=Data.IsComplete}" />
<DataGridTextColumn <DataGridTextColumn
Binding="{Binding RfNo}" Binding="{Binding RfNo}"
Header="制冷剂" Header="制冷剂"

1088
temp_canlin_service.txt Normal file

File diff suppressed because it is too large Load Diff

319
temp_canlin_viewmodel.txt Normal file
View File

@@ -0,0 +1,319 @@
using CapMachine.Core;
using CapMachine.Wpf.PrismEvent;
using CapMachine.Wpf.Services;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
namespace CapMachine.Wpf.ViewModels
{
/// <summary>
/// CAN/CANFD/LIN 配置导入导出弹窗 ViewModel。
/// </summary>
public class DialogCanLinConfigImExportViewModel : DialogViewModel
{
/// <summary>
/// 初始化导入导出弹窗。
/// </summary>
/// <param name="canLinConfigImExportService">导入导出服务。</param>
/// <param name="eventAggregator">事件聚合器。</param>
public DialogCanLinConfigImExportViewModel(
CanLinConfigImExportService canLinConfigImExportService,
IEventAggregator eventAggregator)
{
CanLinConfigImExportService = canLinConfigImExportService;
EventAggregator = eventAggregator;
Title = "CAN/CANFD/LIN配置导入导出";
ImportModes = new ObservableCollection<ImportModeOption>
{
new ImportModeOption { Text = "覆盖导入(同名同类型先删除后导入)", Mode = CanLinImportMode.Overwrite },
new ImportModeOption { Text = "增量导入(同名同类型自动重命名)", Mode = CanLinImportMode.IncrementalRename },
};
SelectedImportMode = ImportModes.FirstOrDefault();
}
/// <summary>
/// 导入导出服务。
/// </summary>
public CanLinConfigImExportService CanLinConfigImExportService { get; }
/// <summary>
/// 事件聚合器。
/// </summary>
public IEventAggregator EventAggregator { get; }
private string _importFilePath = string.Empty;
/// <summary>
/// 导入文件路径。
/// </summary>
public string ImportFilePath
{
get { return _importFilePath; }
set { _importFilePath = value; RaisePropertyChanged(); }
}
private ObservableCollection<ImportModeOption> _importModes = new ObservableCollection<ImportModeOption>();
/// <summary>
/// 导入模式集合。
/// </summary>
public ObservableCollection<ImportModeOption> ImportModes
{
get { return _importModes; }
set { _importModes = value; RaisePropertyChanged(); }
}
private ImportModeOption? _selectedImportMode;
/// <summary>
/// 当前选中的导入模式。
/// </summary>
public ImportModeOption? SelectedImportMode
{
get { return _selectedImportMode; }
set { _selectedImportMode = value; RaisePropertyChanged(); }
}
private string _resultMessage = string.Empty;
/// <summary>
/// 操作结果消息。
/// </summary>
public string ResultMessage
{
get { return _resultMessage; }
set { _resultMessage = value; RaisePropertyChanged(); }
}
private DelegateCommand? _browseImportFileCmd;
/// <summary>
/// 选择导入文件命令。
/// </summary>
public DelegateCommand BrowseImportFileCmd
{
get
{
if (_browseImportFileCmd == null)
{
_browseImportFileCmd = new DelegateCommand(BrowseImportFile);
}
return _browseImportFileCmd;
}
}
private DelegateCommand? _importCmd;
/// <summary>
/// 导入命令。
/// </summary>
public DelegateCommand ImportCmd
{
get
{
if (_importCmd == null)
{
_importCmd = new DelegateCommand(ImportConfig);
}
return _importCmd;
}
}
private DelegateCommand? _exportCmd;
/// <summary>
/// 导出命令。
/// </summary>
public DelegateCommand ExportCmd
{
get
{
if (_exportCmd == null)
{
_exportCmd = new DelegateCommand(ExportConfig);
}
return _exportCmd;
}
}
private DelegateCommand? _closeCmd;
/// <summary>
/// 关闭命令。
/// </summary>
public DelegateCommand CloseCmd
{
get
{
if (_closeCmd == null)
{
_closeCmd = new DelegateCommand(CloseDialog);
}
return _closeCmd;
}
}
/// <summary>
/// 打开文件选择框并选择导入文件。
/// </summary>
private void BrowseImportFile()
{
try
{
var openFileDialog = new OpenFileDialog
{
Filter = "JSON文件 (*.json)|*.json|所有文件 (*.*)|*.*",
CheckFileExists = true,
CheckPathExists = true,
};
var ok = openFileDialog.ShowDialog();
if (ok == true)
{
ImportFilePath = openFileDialog.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show($"选择导入文件失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 执行配置导入。
/// </summary>
private void ImportConfig()
{
try
{
if (string.IsNullOrWhiteSpace(ImportFilePath))
{
MessageBox.Show("请先选择导入文件", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (SelectedImportMode == null)
{
MessageBox.Show("请选择导入模式", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var result = CanLinConfigImExportService.ImportFromFile(ImportFilePath, SelectedImportMode.Mode);
ResultMessage = BuildResultText(result);
if (!result.IsSuccess)
{
MessageBox.Show(ResultMessage, "导入失败", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
EventAggregator.GetEvent<CanLinConfigChangedEvent>().Publish("import");
EventAggregator.GetEvent<LogicRuleChangeEvent>().Publish("import");
MessageBox.Show(ResultMessage, "导入成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
ResultMessage = $"导入失败:{ex.Message}";
MessageBox.Show(ResultMessage, "导入失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 执行配置导出。
/// </summary>
private void ExportConfig()
{
try
{
var saveFileDialog = new SaveFileDialog
{
Filter = "JSON文件 (*.json)|*.json|所有文件 (*.*)|*.*",
DefaultExt = "json",
FileName = $"CanLinConfigExport_{DateTime.Now:yyyyMMddHHmmss}.json",
};
var ok = saveFileDialog.ShowDialog();
if (ok != true)
{
return;
}
var result = CanLinConfigImExportService.ExportToFile(saveFileDialog.FileName);
ResultMessage = BuildResultText(result);
if (!result.IsSuccess)
{
MessageBox.Show(ResultMessage, "导出失败", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
MessageBox.Show(ResultMessage, "导出成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
ResultMessage = $"导出失败:{ex.Message}";
MessageBox.Show(ResultMessage, "导出失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 关闭弹窗。
/// </summary>
private void CloseDialog()
{
RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
}
/// <summary>
/// 构建结果文本。
/// </summary>
/// <param name="result">执行结果。</param>
/// <returns>显示文本。</returns>
private static string BuildResultText(CanLinConfigImExportResult result)
{
var builder = new StringBuilder();
builder.AppendLine(result.Message);
if (result.RenamedConfigs != null && result.RenamedConfigs.Count > 0)
{
builder.AppendLine("重命名明细:");
foreach (var line in result.RenamedConfigs)
{
builder.AppendLine($"- {line}");
}
}
return builder.ToString();
}
/// <summary>
/// 弹窗打开回调。
/// </summary>
/// <param name="parameters">弹窗参数。</param>
public override void OnDialogOpened(IDialogParameters parameters)
{
ResultMessage = string.Empty;
}
/// <summary>
/// 导入模式选项。
/// </summary>
public class ImportModeOption
{
/// <summary>
/// 文本。
/// </summary>
public string Text { get; set; } = string.Empty;
/// <summary>
/// 模式值。
/// </summary>
public CanLinImportMode Mode { get; set; }
}
}
}