73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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
|
|
{
|
|
[ObservableProperty]
|
|
private string? _name;
|
|
|
|
[ObservableProperty]
|
|
private string? _description;
|
|
|
|
[ObservableProperty]
|
|
private ImageViewModel? _image;
|
|
|
|
public ObservableCollection<PropertyViewModel> Properties { get; set; }
|
|
|
|
public ObservableCollection<InspectionViewModel> Inspections { get; set; }
|
|
|
|
[ObservableProperty]
|
|
private string _nearestInspection;
|
|
|
|
public EquipmentViewModel()
|
|
{
|
|
Name = string.Empty;
|
|
Description = string.Empty;
|
|
Image = null;
|
|
Properties = [];
|
|
Inspections = [];
|
|
NearestInspection = "Нету инспекций";
|
|
}
|
|
|
|
public EquipmentViewModel(Equipment equipment)
|
|
{
|
|
Name = equipment.Name;
|
|
Description = equipment.Description;
|
|
Image = new ImageViewModel(equipment.Image ?? new Image());
|
|
Properties = new ObservableCollection<PropertyViewModel>(equipment.Properties.Select(e => new PropertyViewModel(e)));
|
|
|
|
List<DateTime[]> 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 = "Нету инспекций";
|
|
}
|
|
|
|
public Equipment GetEquipment()
|
|
{
|
|
return new Equipment()
|
|
{
|
|
Name = Name,
|
|
Description = Description,
|
|
Image = Image?.GetImage(),
|
|
Inspections = Inspections.Select(e => e.GetInspection()).ToList(),
|
|
};
|
|
}
|
|
} |