Inventory/AvaloniaApplication14_Inven.../ViewModels/EquipmentEditingWindowViewM...

134 lines
5.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using AvaloniaApplication14_Inventory_300326.Models.DataBase;
using AvaloniaApplication14_Inventory_300326.Models.Factoryes;
using AvaloniaApplication14_Inventory_300326.Models.Models;
using AvaloniaApplication14_Inventory_300326.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
namespace AvaloniaApplication14_Inventory_300326.ViewModels;
public partial class EquipmentEditingWindowViewModel : ViewModelBase
{
private EquipmentEditingWindow _currentWindow;
private bool _isEditing;
private EquipmentVisual _equipment;
private IServiceProvider _serviceProvider;
private EquipmentFactory _equipmentFactory;
private EquipmentVisualFactory _equipmentVisualFactory;
[ObservableProperty] private string _name;
[ObservableProperty] private string _invNumber;
[ObservableProperty] private DateTimeOffset? _date;
[ObservableProperty] private decimal _cost;
[ObservableProperty] private bool _isWrittenOff;
[ObservableProperty] private string _isWrittenOffString;
[ObservableProperty] private Employee _currentEmployee;
[ObservableProperty] private ObservableCollection<Employee> _employees;
[RelayCommand]
private async Task Confrm()
{
using (var repo = _serviceProvider.GetService<EquipmentRepository>())
{
_equipment.InvNumber = InvNumber;
_equipment.Date = Date;
_equipment.Cost = Cost;
_equipment.IsWrittenOff = IsWrittenOff;
_equipment.CurrentEmployee = CurrentEmployee;
_equipment.Name = Name;
if (_isEditing)
{
repo.Update(_equipmentFactory.CreateFromVisual(_equipment));
}
else
{
if (repo.ValidateInvNumber(InvNumber))
{
if (Cost>0)
{
repo.Add(_equipmentFactory.CreateFromVisual(_equipment));
}
else
{
var ErrorWin = MessageBoxManager.GetMessageBoxStandard("Ошибка", "Стоимость должна быть больше 0", ButtonEnum.Ok, Icon.Error);
await ErrorWin.ShowWindowDialogAsync(_currentWindow);
}
}
else
{
var ErrorWin = MessageBoxManager.GetMessageBoxStandard("Ошибка", "Объект с таким инвентарным номером уже существует", ButtonEnum.Ok, Icon.Error);
await ErrorWin.ShowWindowDialogAsync(_currentWindow);
}
}
}
_currentWindow.Close();
}
[RelayCommand]
private void Close()
{
_currentWindow.Close();
}
public EquipmentEditingWindowViewModel(IServiceProvider serviceProvider, Equipment equipment)
{
_serviceProvider = serviceProvider;
_isEditing = !equipment.IsNew();
_equipmentFactory = _serviceProvider.GetService<EquipmentFactory>();
_equipmentVisualFactory = _serviceProvider.GetService<EquipmentVisualFactory>();
using (var repo = _serviceProvider.GetService<EmployeeRepository>())
{
_equipment = _equipmentVisualFactory.CreateFromEquipment(equipment);
_equipment.CurrentEmployee = repo.GetById(equipment.CurrentEmployeeId);
}
if (_isEditing)
{
Name = _equipment.Name;
InvNumber = _equipment.InvNumber;
Date = _equipment.Date;
Cost = _equipment.Cost;
IsWrittenOff = _equipment.IsWrittenOff;
IsWrittenOffString = _equipment.IsWrittenOff?"Да":"Нет";
CurrentEmployee = _equipment.CurrentEmployee;
Employees = new ObservableCollection<Employee>();
Employees.Add(CurrentEmployee);
}
else
{
using (var repo = _serviceProvider.GetService<EmployeeRepository>())
{
Employees = new ObservableCollection<Employee>(repo.GetAll());
}
CurrentEmployee = Employees[0];
Date = DateTimeOffset.Now;
}
}
public void SetWindow(EquipmentEditingWindow window)
{
_currentWindow = window;
_currentWindow.OkButton.Content = _isEditing ? "Изменить" : "Добавить";
if (_isEditing)
{
_currentWindow.CurrentEmployeeBox.IsVisible = false;
_currentWindow.IsWrittenOffCheckBox.IsVisible = false;
}
else
{
_currentWindow.CurrentEmployeeLabel.IsVisible = false;
_currentWindow.IsWrittenOffLabel.IsVisible = false;
}
}
}