Inventory/AvaloniaApplication14_Inven.../ViewModels/MainWindowViewModel.cs

265 lines
9.5 KiB
C#
Raw 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.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using AvaloniaApplication14_Inventory_300326.Models.DataBase;
using AvaloniaApplication14_Inventory_300326.Models.Factories;
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.Dto;
using MsBox.Avalonia.Enums;
namespace AvaloniaApplication14_Inventory_300326.ViewModels;
public partial class MainWindowViewModel : ViewModelBase
{
private MainWindow _currentWindow;
private IServiceProvider _serviceProvider;
[ObservableProperty] private ObservableCollection<Entity> _entities;
[ObservableProperty] private Entity _selectedEntity;
[ObservableProperty] private ObservableCollection<EquipmentVisual> _techs;
[ObservableProperty] private ObservableCollection<EmployeeVisual> _employees;
[ObservableProperty] private ObservableCollection<Position> _positions;
[ObservableProperty] private EquipmentVisual _selectedEquipmentVisual;
[ObservableProperty] private EmployeeVisual _selectedEmployeeVisual;
[ObservableProperty] private Position _selectedPosition;
[RelayCommand]
private async Task Fire()
{
List<Equipment> temp = null;
using (var repo = _serviceProvider.GetService<EquipmentRepository>())
{
temp = repo.DoesEmployeeHaveEquipments(SelectedEmployeeVisual.Id);
}
if (temp != null)
{
if (temp.Count > 0)
{
string errorMessage = "За выбранным сотрудником числится следующие вещи: \n";
foreach (var item in temp)
{
errorMessage += $"{item.Name} \n";
}
var win = MessageBoxManager.GetMessageBoxStandard("Ошибка", errorMessage, ButtonEnum.Ok, Icon.Error);
await win.ShowWindowDialogAsync(_currentWindow);
}
}
}
[RelayCommand]
private async Task AddEntity()
{
switch (SelectedEntity.Id)
{
case 0:
var equipment = new Equipment();
var EquipmentVm = ActivatorUtilities.CreateInstance<EquipmentEditingWindowViewModel>(_serviceProvider, equipment);
var EquipmentWin = ActivatorUtilities.CreateInstance<EquipmentEditingWindow>(_serviceProvider, EquipmentVm);
await EquipmentWin.ShowDialog(_currentWindow);
ShowTeches();
GetTeches();
break;
case 1:
var employee = new Employee();
var EmployeeVm = ActivatorUtilities.CreateInstance<EmployeeEditingWindowViewModel>(_serviceProvider, employee);
var EmployeeWin = ActivatorUtilities.CreateInstance<EmployeeEditingWindow>(_serviceProvider, EmployeeVm);
await EmployeeWin.ShowDialog(_currentWindow);
ShowEmployees();
GetEmployees();
break;
case 2:
break;
}
}
partial void OnSelectedEntityChanged(Entity value)
{
if (_currentWindow != null)
{
switch (value.Id)
{
case 0:
GetTeches();
ShowTeches();
break;
case 1:
GetEmployees();
ShowEmployees();
break;
case 2:
GetPositions();
ShowPositions();
break;
}
}
}
//###################################################################################################################
public MainWindowViewModel(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
Entities = new();
Entities.Add(new Entity(){Id = 0, Name = "Техника"});
Entities.Add(new Entity(){Id = 1, Name = "Сотрудники"});
Entities.Add(new Entity(){Id = 2, Name = "Должности"});
SelectedEntity = Entities[0];
using (var equipmentRepository = _serviceProvider.GetService<EquipmentRepository>())
{
List<Equipment> equipments = equipmentRepository.GetAll();
Techs = new ObservableCollection<EquipmentVisual>(ConvertListEqToEqVis(equipments));
}
}
//###################################################################################################################
private void GetTeches()
{
using (var equipmentRepository = _serviceProvider.GetService<EquipmentRepository>())
{
List<Equipment> teches = equipmentRepository.GetAll();
Techs = new ObservableCollection<EquipmentVisual>(ConvertListEqToEqVis(teches));
}
}
public void GetEmployees()
{
using (var employeeRepository = _serviceProvider.GetService<EmployeeRepository>())
{
var employees = employeeRepository.GetAll();
Employees = new ObservableCollection<EmployeeVisual>(ConvertListEmplToEmplVis(employees));
}
}
public void GetPositions()
{
using (var positionRepository = _serviceProvider.GetService<PositionRepository>())
{
var positions = positionRepository.GetAll();
Positions = new ObservableCollection<Position>(positions);
}
}
public void ShowTeches()
{
_currentWindow.ScrollViewerDataGridTech.IsVisible = true;
_currentWindow.ScrollViewerDataGridEmpl.IsVisible = false;
_currentWindow.ScrollViewerDataGridPos.IsVisible = false;
}
public void ShowEmployees()
{
_currentWindow.ScrollViewerDataGridTech.IsVisible = false;
_currentWindow.ScrollViewerDataGridEmpl.IsVisible = true;
_currentWindow.ScrollViewerDataGridPos.IsVisible = false;
}
public void ShowPositions()
{
_currentWindow.ScrollViewerDataGridTech.IsVisible = false;
_currentWindow.ScrollViewerDataGridEmpl.IsVisible = false;
_currentWindow.ScrollViewerDataGridPos.IsVisible = true;
}
private async Task DoubleTappedTechDataGrid()
{
var vm = ActivatorUtilities.CreateInstance<EquipmentEditingWindowViewModel>(_serviceProvider, EquipmentFactory.CreateFromVisual(SelectedEquipmentVisual));
var win = ActivatorUtilities.CreateInstance<EquipmentEditingWindow>(_serviceProvider, vm);
await win.ShowDialog(_currentWindow);
ShowTeches();
GetTeches();
}
private async Task DoubleTappedEmployeeDataGrid()
{
var EmployeeVm = ActivatorUtilities.CreateInstance<EmployeeEditingWindowViewModel>(_serviceProvider, EmployeesFactory.CreateFromEmployeeVisual(SelectedEmployeeVisual));
var EmployeeWin = ActivatorUtilities.CreateInstance<EmployeeEditingWindow>(_serviceProvider, EmployeeVm);
await EmployeeWin.ShowDialog(_currentWindow);
ShowEmployees();
GetEmployees();
}
private async Task DoubleTappedPositionDataGrid()
{
var PositionVm = ActivatorUtilities.CreateInstance<PositionEditingWindowViewModel>(_serviceProvider, SelectedPosition);
var win = ActivatorUtilities.CreateInstance<PositionEditingWindow>(_serviceProvider, PositionVm);
await win.ShowDialog(_currentWindow);
ShowPositions();
GetPositions();
}
public List<EquipmentVisual> ConvertListEqToEqVis(List<Equipment> equipments)
{
List<EquipmentVisual> result = new List<EquipmentVisual>();
foreach (var item in equipments)
{
EquipmentVisual eq = new EquipmentVisual();
using (var empRepo = _serviceProvider.GetRequiredService<EmployeeRepository>())
{
eq = EquipmentVisualFactory.CreateFromEquipment(item);
eq.CurrentEmployee = empRepo.GetById(item.CurrentEmployeeId);
}
result.Add(eq);
}
return result;
}
public List<EmployeeVisual> ConvertListEmplToEmplVis(List<Employee> employees)
{
List<EmployeeVisual> result = new List<EmployeeVisual>();
foreach (var item in employees)
{
EmployeeVisual eve = new EmployeeVisual();
using (var posRepo = _serviceProvider.GetRequiredService<PositionRepository>())
{
eve = EmployeesVisualFactory.CreateFromEmployee(item);
eve.Position = posRepo.GetById(item.PositionId);
}
result.Add(eve);
}
return result;
}
public void SetScreen(MainWindow window)
{
_currentWindow = window;
Console.WriteLine(window);
ShowTeches();
INIT();
}
private void INIT()
{
_currentWindow.DataGridTech.DoubleTapped += (sender, args) => DoubleTappedTechDataGrid();
_currentWindow.DataGridEmployees.DoubleTapped += (sender, args) => DoubleTappedEmployeeDataGrid();
_currentWindow.DataGridPositions.DoubleTapped += (sender, args) => DoubleTappedPositionDataGrid();
}
}