124 lines
4.3 KiB
C#
124 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using GazpromApp.Messages;
|
|
using GazpromApp.Models;
|
|
using GazpromApp.Utils;
|
|
|
|
namespace GazpromApp.ViewModels;
|
|
|
|
public partial class EquipmentViewModel : ViewModelBase
|
|
{
|
|
public ViewModelBase? Parent { get; set; }
|
|
private Equipment Equipment { get; }
|
|
|
|
[ObservableProperty]
|
|
private string? _name;
|
|
|
|
[ObservableProperty]
|
|
private string? _description;
|
|
|
|
[ObservableProperty]
|
|
private DateTime _creationDate;
|
|
|
|
public ObservableCollection<ImageViewModel> Images { get; set; } = [];
|
|
|
|
public ObservableCollection<PropertyViewModel> Properties { get; set; }
|
|
|
|
public ObservableCollection<InspectionViewModel> Inspections { get; set; }
|
|
|
|
public ObservableCollection<EquipmentViewModel> Equipments { get; set; }
|
|
|
|
[ObservableProperty]
|
|
private string _nearestInspection;
|
|
|
|
[RelayCommand]
|
|
private void AddEquipment()
|
|
{
|
|
Equipments.Add(new EquipmentViewModel{ Name = "Новое оборудование", Description = "Описание оборудования", Parent = this});
|
|
CallSavePoint(this);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void RemoveEquipment()
|
|
{
|
|
WeakReferenceMessenger.Default.Send(new RemoveEquipmentMessage(this));
|
|
}
|
|
|
|
private void RemoveEquipment(object sender, RemoveEquipmentMessage msg)
|
|
{
|
|
if (Equipments.Contains(msg.Equipment))
|
|
Equipments.Remove(msg.Equipment);
|
|
CallSavePoint(this);
|
|
}
|
|
|
|
private void CallSavePoint(EquipmentViewModel equipment)
|
|
{
|
|
var current = equipment;
|
|
while (current.Parent is EquipmentViewModel model)
|
|
current = model ?? throw new InvalidOperationException("Parent equipment not found in hierarchy");
|
|
|
|
if (current.Parent is PointViewModel pointViewModel)
|
|
WeakReferenceMessenger.Default.Send(new EditPointMessage(pointViewModel));
|
|
}
|
|
|
|
public EquipmentViewModel()
|
|
{
|
|
Name = string.Empty;
|
|
Description = string.Empty;
|
|
CreationDate = DateTime.Now;
|
|
Properties = [];
|
|
Inspections = [];
|
|
Equipments = [];
|
|
NearestInspection = "Нету инспекций";
|
|
Equipment = new Equipment();
|
|
|
|
WeakReferenceMessenger.Default.Register<RemoveEquipmentMessage>(this, RemoveEquipment);
|
|
}
|
|
|
|
public EquipmentViewModel(Equipment equipment)
|
|
{
|
|
Name = equipment.Name;
|
|
Description = equipment.Description;
|
|
CreationDate = equipment.CreationDate;
|
|
Images = new ObservableCollection<ImageViewModel>(equipment.Images.Select(i => new ImageViewModel(i)));
|
|
Properties = new ObservableCollection<PropertyViewModel>(equipment.Properties.Select(e => new PropertyViewModel(e)));
|
|
Equipments = new ObservableCollection<EquipmentViewModel>(equipment.Equipments.Select(e => new EquipmentViewModel(e){Parent = this}));
|
|
|
|
var times = equipment.Inspections.Select(e => new InspectionViewModel(e))
|
|
.Select(e => e.GetNearestDates())
|
|
.ToList();
|
|
|
|
Inspections = new ObservableCollection<InspectionViewModel>(equipment.Inspections
|
|
.Select((e, i) => new InspectionViewModel(e) { InspectionDate = times[i][0] }));
|
|
|
|
NearestInspection = times
|
|
.Where(e => e[1] > DateTime.Now)
|
|
.Select(e => e[1])
|
|
.OrderBy(e => e)
|
|
.FirstOrDefault()
|
|
.ToLongDateString();
|
|
|
|
if (Inspections.Count == 0) NearestInspection = "Нету инспекций";
|
|
Equipment = equipment;
|
|
|
|
WeakReferenceMessenger.Default.Register<RemoveEquipmentMessage>(this, RemoveEquipment);
|
|
}
|
|
|
|
public Equipment GetEquipment()
|
|
{
|
|
Equipment.Name = Name;
|
|
Equipment.Description = Description;
|
|
Equipment.CreationDate = CreationDate;
|
|
Equipment.Images = Images.Select(e => e.GetImage()).ToList();
|
|
Equipment.Inspections = Inspections.Select(e => e.GetInspection()).ToList();
|
|
Equipment.Properties = Properties.Select(e => e.GetProperty()).ToList();
|
|
Equipment.Equipments = Equipments.Select(e => e.GetEquipment()).ToList();
|
|
|
|
return Equipment;
|
|
}
|
|
} |