presence_new/Presence.Desktop/ViewModels/PresenceViewModel.cs

139 lines
4.5 KiB
C#
Raw Permalink Normal View History

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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
2024-12-26 08:03:24 +00:00
using System.Linq;
using System.Reactive;
2024-12-13 07:21:14 +00:00
namespace Presence.Desktop.ViewModels
2024-12-13 07:21:14 +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);
}
private GroupPresenter? _selectedGroup;
public GroupPresenter? SelectedGroup
{
get => _selectedGroup;
set
{
this.RaiseAndSetIfChanged(ref _selectedGroup, value);
FilterAttendanceRecords();
}
}
private List<DateTime?> _selectedDate = new List<DateTime?>();
public List<DateTime?> SelectedDate
{
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-23 11:56:27 +00:00
public PresenceViewModel(GroupUseCase groupUseCase, PresenceUseCase presenceUseCase)
{
_groupUseCase = groupUseCase;
2024-12-23 11:56:27 +00:00
_presenceUseCase = presenceUseCase;
NavigateBackCommand = ReactiveCommand.Create(() => { });
2024-12-26 08:03:24 +00:00
DeleteSelectedPresenceCommand = ReactiveCommand.Create(DeleteSelectedItems);
2024-12-26 08:03:24 +00:00
LoadGroups();
}
private void LoadGroups()
{
Groups.Clear();
var groups = _groupUseCase.GetAllGroups();
foreach (var group in groups)
{
Groups.Add(new GroupPresenter
{
Id = group.Id,
Name = group.Name
});
}
}
public void FilterAttendanceRecords()
{
2024-12-26 08:03:24 +00:00
if (SelectedGroup == null || SelectedDate == null)
{
AttendanceRecords.Clear();
return;
}
AttendanceRecords.Clear();
foreach (var selDate in SelectedDate)
{
var records = _presenceUseCase.GetPresenceByGroupAndDate(
SelectedGroup.Id,
DateOnly.FromDateTime(selDate.Value));
foreach (var record in records)
{
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-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-13 07:21:14 +00:00
}
}