Files
GazpromApp/ViewModels/Basic/EquipmentViewModel.cs
T

72 lines
2.3 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using GazpromApp.Models;
using GazpromApp.Utils;
namespace GazpromApp.ViewModels;
public partial class EquipmentViewModel : ViewModelBase
{
private Equipment Equipment { get; }
[ObservableProperty]
private string? _name;
[ObservableProperty]
private string? _description;
public ObservableCollection<ImageViewModel> Images { get; init; } = [];
public ObservableCollection<PropertyViewModel> Properties { get; }
public ObservableCollection<InspectionViewModel> Inspections { get; }
[ObservableProperty]
private string _nearestInspection;
public EquipmentViewModel()
{
Name = string.Empty;
Description = string.Empty;
Properties = [];
Inspections = [];
NearestInspection = "Нету инспекций";
Equipment = new Equipment();
}
public EquipmentViewModel(Equipment equipment)
{
Name = equipment.Name;
Description = equipment.Description;
Images = new ObservableCollection<ImageViewModel>(equipment.Images.Select(i => new ImageViewModel(i)));
Properties = new ObservableCollection<PropertyViewModel>(equipment.Properties.Select(e => new PropertyViewModel(e)));
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;
}
public Equipment GetEquipment()
{
Equipment.Name = Name;
Equipment.Description = Description;
Equipment.Images = Images.Select(e => e.GetImage()).ToList();
Equipment.Inspections = Inspections.Select(e => e.GetInspection()).ToList();
return Equipment;
}
}