导入和导出的功能
This commit is contained in:
319
CapMachine.Wpf/ViewModels/DialogCanLinConfigImExportViewModel.cs
Normal file
319
CapMachine.Wpf/ViewModels/DialogCanLinConfigImExportViewModel.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user