198 lines
6.4 KiB
C#
198 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Data;
|
|
using Avalonia.Input;
|
|
using Avalonia.LogicalTree;
|
|
using AvaloniaApplication14_Inventory_300326.Models.DataBase;
|
|
using AvaloniaApplication14_Inventory_300326.Models.Models;
|
|
using AvaloniaApplication14_Inventory_300326.Views;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
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;
|
|
|
|
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 void DoubleTappedTechDataGrid()
|
|
{
|
|
Console.WriteLine("DoubleTappedTechDataGrid");
|
|
}
|
|
|
|
private void DoubleTappedEmployeeDataGrid()
|
|
{
|
|
|
|
}
|
|
|
|
private void DoubleTappedPositionDataGrid()
|
|
{
|
|
|
|
}
|
|
|
|
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.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.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();
|
|
}
|
|
} |