2024-12-22 12:37:22 +00:00
|
|
|
using Avalonia.Data.Converters;
|
|
|
|
using presence.data.RemoteData.RemoteDataBase.DAO;
|
|
|
|
using presence.domain.Models;
|
|
|
|
using presence.domain.UseCase;
|
|
|
|
using Presence.Desktop.Models;
|
|
|
|
using Presence.Desktop.ViewModels;
|
|
|
|
using ReactiveUI;
|
|
|
|
using System;
|
2024-12-26 10:10:54 +00:00
|
|
|
using System.Collections.Generic;
|
2024-12-22 12:37:22 +00:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Globalization;
|
2024-12-26 08:03:24 +00:00
|
|
|
using System.Linq;
|
2024-12-22 12:37:22 +00:00
|
|
|
using System.Reactive;
|
2024-12-13 07:21:14 +00:00
|
|
|
|
2024-12-22 12:37:22 +00:00
|
|
|
namespace Presence.Desktop.ViewModels
|
2024-12-13 07:21:14 +00:00
|
|
|
{
|
2024-12-22 12:37:22 +00:00
|
|
|
public class PresenceViewModel : ViewModelBase, IRoutableViewModel
|
|
|
|
{
|
|
|
|
public string? UrlPathSegment { get; } = "Presence";
|
|
|
|
public IScreen HostScreen { get; }
|
|
|
|
|
|
|
|
private readonly GroupUseCase _groupUseCase;
|
|
|
|
private readonly PresenceUseCase _presenceUseCase;
|
|
|
|
|
|
|
|
public ObservableCollection<PresencePresenter> AttendanceRecords { get; set; } = new();
|
|
|
|
public ObservableCollection<GroupPresenter> Groups { get; set; } = new();
|
2024-12-26 08:03:24 +00:00
|
|
|
private ObservableCollection<PresencePresenter> _selectedItems = new();
|
|
|
|
public ObservableCollection<PresencePresenter> SelectedItems
|
|
|
|
{
|
|
|
|
get => _selectedItems;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedItems, value);
|
|
|
|
}
|
2024-12-22 12:37:22 +00:00
|
|
|
|
|
|
|
private GroupPresenter? _selectedGroup;
|
|
|
|
public GroupPresenter? SelectedGroup
|
|
|
|
{
|
|
|
|
get => _selectedGroup;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
this.RaiseAndSetIfChanged(ref _selectedGroup, value);
|
|
|
|
FilterAttendanceRecords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-26 10:10:54 +00:00
|
|
|
private List<DateTime?> _selectedDate = new List<DateTime?>();
|
|
|
|
public List<DateTime?> SelectedDate
|
2024-12-22 12:37:22 +00:00
|
|
|
{
|
|
|
|
get => _selectedDate;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
this.RaiseAndSetIfChanged(ref _selectedDate, value);
|
|
|
|
FilterAttendanceRecords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ReactiveCommand<Unit, Unit> NavigateBackCommand { get; }
|
2024-12-26 08:03:24 +00:00
|
|
|
public ReactiveCommand<Unit, Unit> DeleteSelectedPresenceCommand { get; }
|
2024-12-22 12:37:22 +00:00
|
|
|
|
2024-12-23 11:56:27 +00:00
|
|
|
public PresenceViewModel(GroupUseCase groupUseCase, PresenceUseCase presenceUseCase)
|
2024-12-22 12:37:22 +00:00
|
|
|
{
|
|
|
|
_groupUseCase = groupUseCase;
|
|
|
|
|
2024-12-23 11:56:27 +00:00
|
|
|
_presenceUseCase = presenceUseCase;
|
|
|
|
|
2024-12-22 12:37:22 +00:00
|
|
|
|
|
|
|
NavigateBackCommand = ReactiveCommand.Create(() => { });
|
2024-12-26 08:03:24 +00:00
|
|
|
DeleteSelectedPresenceCommand = ReactiveCommand.Create(DeleteSelectedItems);
|
2024-12-22 12:37:22 +00:00
|
|
|
|
2024-12-26 08:03:24 +00:00
|
|
|
LoadGroups();
|
2024-12-22 12:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadGroups()
|
|
|
|
{
|
|
|
|
Groups.Clear();
|
|
|
|
var groups = _groupUseCase.GetAllGroups();
|
|
|
|
|
|
|
|
foreach (var group in groups)
|
|
|
|
{
|
|
|
|
Groups.Add(new GroupPresenter
|
|
|
|
{
|
|
|
|
Id = group.Id,
|
|
|
|
Name = group.Name
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-26 10:10:54 +00:00
|
|
|
public void FilterAttendanceRecords()
|
2024-12-22 12:37:22 +00:00
|
|
|
{
|
2024-12-26 08:03:24 +00:00
|
|
|
if (SelectedGroup == null || SelectedDate == null)
|
2024-12-22 12:37:22 +00:00
|
|
|
{
|
|
|
|
AttendanceRecords.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AttendanceRecords.Clear();
|
2024-12-26 10:10:54 +00:00
|
|
|
|
|
|
|
foreach (var selDate in SelectedDate)
|
2024-12-22 12:37:22 +00:00
|
|
|
{
|
2024-12-26 10:10:54 +00:00
|
|
|
var records = _presenceUseCase.GetPresenceByGroupAndDate(
|
|
|
|
SelectedGroup.Id,
|
|
|
|
DateOnly.FromDateTime(selDate.Value));
|
|
|
|
|
|
|
|
foreach (var record in records)
|
2024-12-22 12:37:22 +00:00
|
|
|
{
|
2024-12-26 10:10:54 +00:00
|
|
|
AttendanceRecords.Add(new PresencePresenter
|
|
|
|
{
|
|
|
|
Id = record.Id,
|
|
|
|
Date = record.Date,
|
|
|
|
ClassNumber = record.ClassNumber,
|
|
|
|
IsAttendence = record.IsAttendence,
|
|
|
|
User = new UserPresenter
|
|
|
|
{
|
|
|
|
Id = record.User.Id,
|
|
|
|
Name = record.User.FIO
|
|
|
|
}
|
|
|
|
});
|
2024-12-22 12:37:22 +00:00
|
|
|
}
|
2024-12-26 10:10:54 +00:00
|
|
|
|
2024-12-22 12:37:22 +00:00
|
|
|
}
|
2024-12-26 08:03:24 +00:00
|
|
|
|
|
|
|
// Логирование для отладки
|
|
|
|
Console.WriteLine($"Загружено записей посещаемости: {AttendanceRecords.Count}");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DeleteSelectedItems()
|
|
|
|
{
|
|
|
|
if (SelectedItems.Any())
|
|
|
|
{
|
|
|
|
foreach (var item in SelectedItems.ToList())
|
|
|
|
{
|
|
|
|
_presenceUseCase.DeletePresenceByClassNumberAndDateAndUserId(item.ClassNumber, item.Date, item.User.Id); // Удаляем из базы данных
|
|
|
|
AttendanceRecords.Remove(item); // Удаляем из коллекции
|
|
|
|
}
|
|
|
|
}
|
2024-12-22 12:37:22 +00:00
|
|
|
}
|
2024-12-13 07:21:14 +00:00
|
|
|
|
2024-12-22 12:37:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|