240 lines
9.1 KiB
C#
240 lines
9.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Text;
|
||
using System.Windows;
|
||
using System.Windows.Input;
|
||
using XLIMS.PSV.Windows;
|
||
using XLIMS.DATA.Models;
|
||
using XLIMS.MVVM.Base;
|
||
using XLIMS.CONTRACT;
|
||
|
||
namespace XLIMS.PSV.ViewModels
|
||
{
|
||
public class PsvViewModel : ViewModelBase
|
||
{
|
||
#region Constructor
|
||
public PsvViewModel(ILimsService limsService, IDialogService dialogService, DocumentSet document=null)
|
||
{
|
||
_limsService = limsService;
|
||
_dialogService = dialogService;
|
||
|
||
if (document != null)
|
||
{
|
||
_document = document;
|
||
}
|
||
else
|
||
{
|
||
_document = new DocumentSet() {Name="ПСВ"};
|
||
}
|
||
LoadDataAsync();
|
||
}
|
||
|
||
|
||
#endregion //Constructor
|
||
|
||
#region Events
|
||
#endregion //Events
|
||
|
||
#region Fields
|
||
private DocumentSet _document;
|
||
private readonly ILimsService _limsService;
|
||
private readonly IDialogService _dialogService;
|
||
private int _devicesCount;
|
||
private DivisionSet _currentDivision;
|
||
private Tprz _currentTprz;
|
||
private DeviceSet _currentDevice;
|
||
private IGrouping<string, DataSet> _currentGroupedPov;
|
||
private DataSet _currentPov;
|
||
private PersonalSet _currentPersonal;
|
||
#endregion //Fields
|
||
|
||
#region Properties
|
||
public ObservableCollection<IGrouping<string, DataSet>> GroupedPovs { get; set; } = new();
|
||
public int ID => _document.Id;
|
||
public ObservableCollection<DivisionSet> AllDivisions { get; set; } = new();
|
||
public ObservableCollection<PersonalSet> AllPersonals { get; set; } = new();
|
||
public ObservableCollection<DataSet> AllPovs { get; set; } = new();
|
||
public DivisionSet CurrentDivision
|
||
{
|
||
get => _document.Division;
|
||
set
|
||
{
|
||
_document.Division = value;
|
||
OnPropertyChanged();
|
||
}
|
||
}
|
||
public PersonalSet CurrentPersonal
|
||
{
|
||
get
|
||
{
|
||
return _currentPersonal;
|
||
}
|
||
set
|
||
{
|
||
_currentPersonal = value;
|
||
if(_currentPersonal!=null) { _document.InPerson = _currentPersonal.Person; }
|
||
OnPropertyChanged();
|
||
}
|
||
}
|
||
public IGrouping<string, DataSet> CurrentGroupedPov
|
||
{
|
||
get => _currentGroupedPov;
|
||
set
|
||
{
|
||
_currentGroupedPov = value; OnPropertyChanged();
|
||
}
|
||
}
|
||
public DataSet CurrentPov
|
||
{
|
||
get => _currentPov;
|
||
set
|
||
{
|
||
_currentPov = value;
|
||
OnPropertyChanged();
|
||
}
|
||
}
|
||
public string? Number
|
||
{
|
||
get=>_document.Number;
|
||
set { _document.Number = value; OnPropertyChanged(); }
|
||
}
|
||
public DateTime? Date
|
||
{
|
||
get => _document.DocDate;
|
||
set { _document.DocDate = value; OnPropertyChanged(); }
|
||
}
|
||
#endregion //Properties
|
||
|
||
#region Methods
|
||
private async Task BrowseTprz()
|
||
{
|
||
var vm = new BrowseTprzViewModel(_limsService);
|
||
if (_dialogService.ShowDialog(vm, out _) != true || vm.CurrentTPRZ == null)
|
||
return;
|
||
|
||
var tprz = vm.CurrentTPRZ;
|
||
var serial = vm.Serial;
|
||
|
||
bool ExistsInList() => AllPovs.Any(p =>
|
||
p.Device.Serial == serial &&
|
||
p.Device.Dpzn == tprz.Dpzn &&
|
||
p.Device.Gsrs == tprz.Gsrs &&
|
||
p.Device.Hrtc == tprz.Hrtc);
|
||
|
||
if (ExistsInList())
|
||
{
|
||
MessageBox.Show("Документ уже содержит выбранный экземпляр!",
|
||
"Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
return;
|
||
}
|
||
|
||
var dataSets = await _limsService.Datas.GetAllAsync();
|
||
if (dataSets.Any(d =>
|
||
d.Device.Serial == serial &&
|
||
d.Device.Dpzn == tprz.Dpzn &&
|
||
d.Device.Gsrs == tprz.Gsrs &&
|
||
d.Device.Hrtc == tprz.Hrtc &&
|
||
d.Device.Division == CurrentDivision &&
|
||
d.Gdn == null))
|
||
{
|
||
MessageBox.Show($"Прибор с номером №{serial} уже находится в поверке!",
|
||
"Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
return;
|
||
}
|
||
|
||
var devices = await _limsService.Devices.GetAllAsync();
|
||
var dev = devices.FirstOrDefault(d =>
|
||
d.Serial == serial &&
|
||
d.Dpzn == tprz.Dpzn &&
|
||
d.Gsrs == tprz.Gsrs &&
|
||
d.Hrtc == tprz.Hrtc &&
|
||
d.Division == CurrentDivision)
|
||
?? new DeviceSet
|
||
{
|
||
Oi = tprz.IdtipNavigation.IdspoiNavigation.Nmoi,
|
||
Name = tprz.IdtipNavigation.IdspnmtpNavigation.Nmtp,
|
||
Tip = tprz.IdtipNavigation.Tp,
|
||
Serial = serial,
|
||
Dpzn = tprz.Dpzn,
|
||
Hrtc = tprz.Hrtc,
|
||
Division = CurrentDivision
|
||
};
|
||
|
||
AllPovs.Add(new DataSet() {Device=dev,Document=_document });
|
||
GroupedPovs = new ObservableCollection<IGrouping<string, DataSet>>(AllPovs.GroupBy(e => e.Device.Dpzn + e.Device.Hrtc + e.Device.Gsrs));
|
||
CurrentGroupedPov = GroupedPovs.FirstOrDefault();
|
||
OnPropertyChanged(nameof(GroupedPovs));
|
||
OnPropertyChanged(nameof(CurrentGroupedPov));
|
||
|
||
}
|
||
private async Task BrowseSerial()
|
||
{
|
||
var vm = new BrowseSerialViewModel(_limsService,CurrentDivision.Id);
|
||
if (_dialogService.ShowDialog(vm, out _) != true || vm.CurrentSerial == null)
|
||
return;
|
||
|
||
// Проверка на дублирование в текущем документе
|
||
bool alreadyExists = AllPovs.Any(e => e.Device == vm.CurrentSerial);
|
||
if (alreadyExists)
|
||
{
|
||
MessageBox.Show("Документ уже содержит выбранный экземпляр!",
|
||
"Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
return;
|
||
}
|
||
|
||
var dataSets = await _limsService.Datas.GetAllAsync();
|
||
if (vm.CurrentSerial.DataSets.Any(d =>d.Gdn == null))
|
||
{
|
||
MessageBox.Show($"Прибор с номером №{vm.CurrentSerial.Serial} уже находится в поверке!",
|
||
"Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
return;
|
||
}
|
||
|
||
AllPovs.Add(new DataSet() { Device = vm.CurrentSerial, Document = _document });
|
||
GroupedPovs = new ObservableCollection<IGrouping<string, DataSet>>(AllPovs.GroupBy(e => e.Device.Dpzn + e.Device.Hrtc + e.Device.Gsrs));
|
||
CurrentGroupedPov = GroupedPovs.FirstOrDefault();
|
||
OnPropertyChanged(nameof(GroupedPovs));
|
||
OnPropertyChanged(nameof(CurrentGroupedPov));
|
||
}
|
||
private async Task LoadDataAsync()
|
||
{
|
||
var povsTask = await Task.Run<IEnumerable<DataSet>>(() => _document.DataSetDocuments.ToList());
|
||
AllPersonals = new ObservableCollection<PersonalSet>(await _limsService.Personals.GetAllAsync());
|
||
AllDivisions = new ObservableCollection<DivisionSet>(await _limsService.Divisions.GetAllAsync());
|
||
AllPovs=new ObservableCollection<DataSet>(povsTask);
|
||
GroupedPovs = new ObservableCollection<IGrouping<string, DataSet>>(AllPovs.GroupBy(e => e.Device.Dpzn + e.Device.Hrtc + e.Device.Gsrs));
|
||
CurrentPersonal = AllPersonals.FirstOrDefault(f => f.Person == _document.InPerson);
|
||
OnPropertyChanged(nameof(AllDivisions));
|
||
OnPropertyChanged(nameof(AllPersonals));
|
||
OnPropertyChanged(nameof(GroupedPovs));
|
||
OnPropertyChanged(nameof(CurrentPersonal));
|
||
}
|
||
private async Task Save()
|
||
{
|
||
foreach (var pov in AllPovs)
|
||
{
|
||
if(pov.Id==0) await _limsService.Datas.AddAsync(pov);
|
||
else await _limsService.Datas.UpdateAsync(pov);
|
||
}
|
||
if(_document.Id== 0) {await _limsService.Documents.AddAsync(_document); }
|
||
else await _limsService.Documents.UpdateAsync(_document);
|
||
}
|
||
private async Task DelPov()
|
||
{
|
||
if(CurrentPov.Id!=0)
|
||
{
|
||
await _limsService.Datas.RemoveAsync(CurrentPov);
|
||
AllPovs.Remove(CurrentPov);
|
||
}
|
||
}
|
||
#endregion //Methods
|
||
|
||
#region Commands
|
||
public ICommand BrowseTprzCommand => new AsyncRelayCommand(BrowseTprz, ()=>CurrentDivision != null);
|
||
public ICommand BrowseSerialCommand => new AsyncRelayCommand(BrowseSerial,()=>CurrentDivision!=null);
|
||
public ICommand SaveCommand => new AsyncRelayCommand(Save);
|
||
#endregion //Commands
|
||
}
|
||
}
|