2024-12-02 10:04:59 +00:00
|
|
|
|
using DocumentFormat.OpenXml.Bibliography;
|
2024-12-04 18:00:36 +00:00
|
|
|
|
using domain.Models;
|
2024-12-02 10:04:59 +00:00
|
|
|
|
using domain.UseCase;
|
|
|
|
|
using ReactiveUI;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reactive.Linq;
|
2024-12-04 18:00:36 +00:00
|
|
|
|
using System.Windows.Input;
|
2024-11-24 18:05:17 +00:00
|
|
|
|
|
|
|
|
|
namespace Presence.Desktop.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class MainWindowViewModel : ViewModelBase
|
|
|
|
|
{
|
2024-12-02 10:04:59 +00:00
|
|
|
|
private readonly GroupUseCase _groupUseCase;
|
|
|
|
|
private List<GroupPresenter> groupPresentersDataSource = new List<GroupPresenter>();
|
|
|
|
|
private ObservableCollection<GroupPresenter> _groups;
|
|
|
|
|
public ObservableCollection<GroupPresenter> Groups => _groups;
|
|
|
|
|
|
|
|
|
|
public GroupPresenter? SelectedGroupItem
|
|
|
|
|
{
|
|
|
|
|
get => _selectedGroupItem;
|
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GroupPresenter? _selectedGroupItem;
|
|
|
|
|
|
2024-12-04 18:00:36 +00:00
|
|
|
|
public ObservableCollection<UserPresenter> Users { get => _users; }
|
2024-12-02 10:04:59 +00:00
|
|
|
|
private ObservableCollection<UserPresenter> _users;
|
|
|
|
|
|
2024-12-04 18:00:36 +00:00
|
|
|
|
// Список опций сортировки
|
|
|
|
|
public List<string> SortOptions { get; } = new List<string> { "По фамилии", "По убыванию" };
|
|
|
|
|
|
|
|
|
|
private string _selectedSortOption;
|
|
|
|
|
public string SelectedSortOption
|
|
|
|
|
{
|
|
|
|
|
get => _selectedSortOption;
|
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedSortOption, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ICommand RemoveAllStudentsCommand { get; }
|
|
|
|
|
public ICommand AddStudentCommand { get; }
|
|
|
|
|
|
2024-11-24 18:05:17 +00:00
|
|
|
|
public MainWindowViewModel(GroupUseCase groupUseCase)
|
|
|
|
|
{
|
2024-12-02 10:04:59 +00:00
|
|
|
|
_groupUseCase = groupUseCase;
|
|
|
|
|
|
|
|
|
|
foreach (var item in _groupUseCase.GetAllGroupWithStident())
|
|
|
|
|
{
|
|
|
|
|
GroupPresenter groupPresenter = new GroupPresenter
|
|
|
|
|
{
|
|
|
|
|
Id = item.Id,
|
|
|
|
|
Name = item.Name,
|
|
|
|
|
users = item.Users?.Select(user => new UserPresenter
|
|
|
|
|
{
|
|
|
|
|
Name = user.FIO,
|
|
|
|
|
Guid = user.Guid,
|
|
|
|
|
Group = new GroupPresenter { Id = item.Id, Name = item.Name }
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
groupPresentersDataSource.Add(groupPresenter);
|
|
|
|
|
}
|
|
|
|
|
_groups = new ObservableCollection<GroupPresenter>(groupPresentersDataSource);
|
|
|
|
|
_users = new ObservableCollection<UserPresenter>();
|
|
|
|
|
|
|
|
|
|
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
|
|
|
|
.Subscribe(vm => SetUsers());
|
2024-12-04 18:00:36 +00:00
|
|
|
|
|
|
|
|
|
// Обработчик изменения сортировки
|
|
|
|
|
this.WhenAnyValue(vm => vm.SelectedSortOption)
|
|
|
|
|
.Subscribe(_ => SortUsers());
|
|
|
|
|
|
|
|
|
|
RemoveAllStudentsCommand = ReactiveCommand.Create(RemoveAllStudents);
|
|
|
|
|
AddStudentCommand = ReactiveCommand.Create(AddStudent);
|
2024-12-02 10:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetUsers()
|
|
|
|
|
{
|
|
|
|
|
if (SelectedGroupItem?.users == null) return;
|
|
|
|
|
|
|
|
|
|
Users.Clear();
|
|
|
|
|
foreach (var item in SelectedGroupItem.users)
|
|
|
|
|
{
|
|
|
|
|
Users.Add(item);
|
|
|
|
|
}
|
2024-12-04 18:00:36 +00:00
|
|
|
|
|
|
|
|
|
// Сортировка при изменении группы
|
|
|
|
|
SortUsers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SortUsers()
|
|
|
|
|
{
|
|
|
|
|
if (SelectedGroupItem?.users == null) return;
|
|
|
|
|
|
|
|
|
|
var sortedUsers = SelectedGroupItem.users.ToList();
|
|
|
|
|
|
|
|
|
|
switch (SelectedSortOption)
|
|
|
|
|
{
|
|
|
|
|
case "По фамилии":
|
|
|
|
|
sortedUsers = sortedUsers.OrderBy(u => u.Name).ToList();
|
|
|
|
|
break;
|
|
|
|
|
case "По убыванию":
|
|
|
|
|
sortedUsers = sortedUsers.OrderByDescending(u => u.Name).ToList();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Users.Clear();
|
|
|
|
|
foreach (var item in sortedUsers)
|
|
|
|
|
{
|
|
|
|
|
Users.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RemoveAllStudents()
|
|
|
|
|
{
|
|
|
|
|
if (SelectedGroupItem == null) return;
|
|
|
|
|
|
|
|
|
|
// Удаляем всех студентов через UseCase
|
|
|
|
|
_groupUseCase.RemoveAllStudentsFromGroup(SelectedGroupItem.Id);
|
|
|
|
|
|
|
|
|
|
// Создаем новый список пользователей (пустой)
|
|
|
|
|
SelectedGroupItem.users = new List<UserPresenter>(); // Перезаписываем users новым пустым списком
|
|
|
|
|
SetUsers(); // Обновляем отображение
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddStudent()
|
|
|
|
|
{
|
|
|
|
|
if (SelectedGroupItem == null) return;
|
|
|
|
|
|
|
|
|
|
var newStudent = new UserPresenter
|
|
|
|
|
{
|
|
|
|
|
Name = "Новый студент",
|
|
|
|
|
Guid = Guid.NewGuid(),
|
|
|
|
|
Group = SelectedGroupItem
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Добавляем студента в базу данных через UseCase
|
|
|
|
|
_groupUseCase.AddStudentToGroup(SelectedGroupItem.Id, new User
|
|
|
|
|
{
|
|
|
|
|
Guid = newStudent.Guid,
|
|
|
|
|
FIO = newStudent.Name
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Создаем новый список пользователей, добавляем нового студента и присваиваем его
|
|
|
|
|
var updatedUsers = SelectedGroupItem.users?.ToList() ?? new List<UserPresenter>(); // Создаем новый список из существующих пользователей или пустой
|
|
|
|
|
updatedUsers.Add(newStudent); // Добавляем нового студента
|
|
|
|
|
|
|
|
|
|
SelectedGroupItem.users = updatedUsers; // Перезаписываем users новым списком с добавленным студентом
|
|
|
|
|
SetUsers(); // Обновляем отображение
|
2024-11-24 18:05:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|