2025-08-22 20:46:23 +08:00
|
|
|
using System.Collections;
|
2025-07-04 13:41:50 +08:00
|
|
|
using System.Collections.ObjectModel;
|
2025-08-22 20:46:23 +08:00
|
|
|
using System.Linq;
|
2025-08-22 20:24:09 +08:00
|
|
|
using AutoMapper;
|
2025-07-04 13:41:50 +08:00
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-08-22 20:24:09 +08:00
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-08-25 20:16:57 +08:00
|
|
|
using DMS.Core.Interfaces.Services;
|
2025-08-22 20:24:09 +08:00
|
|
|
using DMS.Core.Models;
|
|
|
|
|
using DMS.Helper;
|
|
|
|
|
using DMS.WPF.ViewModels.Items;
|
2025-07-04 13:41:50 +08:00
|
|
|
|
2025-07-19 11:11:01 +08:00
|
|
|
namespace DMS.WPF.ViewModels.Dialogs;
|
2025-07-04 13:41:50 +08:00
|
|
|
|
2025-08-22 20:24:09 +08:00
|
|
|
public partial class ImportExcelDialogViewModel : DialogViewModelBase<List<Variable>>
|
2025-07-04 13:41:50 +08:00
|
|
|
{
|
2025-08-23 09:09:07 +08:00
|
|
|
private readonly IMapper _mapper;
|
2025-08-22 20:24:09 +08:00
|
|
|
private readonly IExcelService _excelService;
|
|
|
|
|
|
2025-07-04 13:41:50 +08:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private string? _filePath;
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-08-22 20:24:09 +08:00
|
|
|
private List<Variable> _variables = new();
|
2025-08-23 09:09:07 +08:00
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private ObservableCollection<VariableItemViewModel> _variableItemViewModels ;
|
2025-08-22 20:24:09 +08:00
|
|
|
|
2025-08-22 20:46:23 +08:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private IList _selectedVariables = new ArrayList();
|
2025-08-22 20:24:09 +08:00
|
|
|
|
2025-08-23 09:09:07 +08:00
|
|
|
public ImportExcelDialogViewModel(IMapper mapper,IExcelService excelService)
|
2025-08-22 20:24:09 +08:00
|
|
|
{
|
2025-08-23 09:09:07 +08:00
|
|
|
_mapper = mapper;
|
2025-08-22 20:24:09 +08:00
|
|
|
_excelService = excelService;
|
2025-08-23 09:09:07 +08:00
|
|
|
VariableItemViewModels = new();
|
2025-08-22 20:24:09 +08:00
|
|
|
}
|
2025-07-04 13:41:50 +08:00
|
|
|
|
|
|
|
|
partial void OnFilePathChanged(string? value)
|
|
|
|
|
{
|
2025-08-22 20:24:09 +08:00
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Variables = _excelService.ImprotFromTiaVariableTable(value);
|
2025-08-23 09:09:07 +08:00
|
|
|
VariableItemViewModels=new ObservableCollection<VariableItemViewModel>(_mapper.Map<List<VariableItemViewModel>>(Variables));
|
2025-08-22 20:24:09 +08:00
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
|
|
|
|
NotificationHelper.ShowError($"从Excel文件中读取变量时发生了错误:{ex.Message}",ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-22 20:46:23 +08:00
|
|
|
|
2025-08-22 20:24:09 +08:00
|
|
|
[RelayCommand]
|
2025-08-22 20:46:23 +08:00
|
|
|
private void ImportAll()
|
2025-08-22 20:24:09 +08:00
|
|
|
{
|
2025-08-22 20:46:23 +08:00
|
|
|
Close(Variables);
|
2025-08-22 20:24:09 +08:00
|
|
|
}
|
2025-08-22 20:46:23 +08:00
|
|
|
|
2025-08-22 20:24:09 +08:00
|
|
|
[RelayCommand]
|
2025-08-22 20:46:23 +08:00
|
|
|
private void ImportSelected()
|
2025-08-22 20:24:09 +08:00
|
|
|
{
|
2025-08-23 09:09:07 +08:00
|
|
|
var selected = SelectedVariables.Cast<VariableItemViewModel>().ToList();
|
|
|
|
|
Close(_mapper.Map<List<Variable>>(selected));
|
2025-08-22 20:24:09 +08:00
|
|
|
}
|
2025-08-22 20:46:23 +08:00
|
|
|
|
2025-08-22 20:24:09 +08:00
|
|
|
[RelayCommand]
|
|
|
|
|
private void CancleButton()
|
|
|
|
|
{
|
|
|
|
|
Close(null);
|
2025-07-04 13:41:50 +08:00
|
|
|
}
|
2025-08-22 20:46:23 +08:00
|
|
|
}
|