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;
|
2025-09-04 19:59:35 +08:00
|
|
|
using DMS.WPF.Interfaces;
|
2025-09-04 17:29:24 +08:00
|
|
|
using DMS.WPF.Services;
|
2025-10-06 18:17:56 +08:00
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-10-22 14:06:16 +08:00
|
|
|
using System.Threading.Tasks;
|
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-10-22 14:06:16 +08:00
|
|
|
public partial class ImportExcelDialogViewModel : DialogViewModelBase<List<VariableItem>>
|
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-09-04 19:59:35 +08:00
|
|
|
private readonly INotificationService _notificationService;
|
2025-08-22 20:24:09 +08:00
|
|
|
|
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]
|
2025-10-06 18:17:56 +08:00
|
|
|
private ObservableCollection<VariableItem> _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-09-04 19:59:35 +08:00
|
|
|
public ImportExcelDialogViewModel(IMapper mapper,IExcelService excelService, INotificationService notificationService)
|
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-09-04 17:29:24 +08:00
|
|
|
_notificationService = notificationService;
|
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-10-06 18:17:56 +08:00
|
|
|
VariableItemViewModels=new ObservableCollection<VariableItem>(_mapper.Map<List<VariableItem>>(Variables));
|
2025-08-22 20:24:09 +08:00
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
_notificationService.ShowError($"从Excel文件中读取变量时发生了错误:{ex.Message}",ex);
|
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-10-22 14:06:16 +08:00
|
|
|
private async Task ImportAll()
|
2025-08-22 20:24:09 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
await Close(VariableItemViewModels.ToList());
|
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-10-22 14:06:16 +08:00
|
|
|
private async Task ImportSelected()
|
2025-08-22 20:24:09 +08:00
|
|
|
{
|
2025-10-06 18:17:56 +08:00
|
|
|
var selected = SelectedVariables.Cast<VariableItem>().ToList();
|
2025-10-22 14:06:16 +08:00
|
|
|
await Close(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]
|
2025-10-22 14:06:16 +08:00
|
|
|
private async Task CancleButton()
|
2025-08-22 20:24:09 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
await Close(null);
|
2025-07-04 13:41:50 +08:00
|
|
|
}
|
2025-08-22 20:46:23 +08:00
|
|
|
}
|