FinalPresenceLexa/UI/PresenceConsoleUI.cs
2025-04-28 11:51:01 +03:00

173 lines
6.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using domain.UseCase;
using System;
using System.Globalization;
namespace ui
{
public class PresenceConsoleUI
{
private readonly PresenceUseCase _presenceUseCase;
public PresenceConsoleUI(PresenceUseCase presenceUseCase)
{
_presenceUseCase = presenceUseCase;
}
public void GenerateDailyAttendance(int groupId, int firstLesson, int lastLesson, DateTime date)
{
try
{
_presenceUseCase.GenerateDailyPresence(
groupId,
DateOnly.FromDateTime(date),
firstLesson,
lastLesson
);
Console.WriteLine($"Посещаемость на {date:dd.MM.yyyy} создана!");
}
catch (Exception ex)
{
ShowError("Ошибка генерации", ex);
}
}
public void ShowAttendance(DateTime date, int groupId)
{
try
{
var records = _presenceUseCase.GetDailyAttendance(
groupId,
DateOnly.FromDateTime(date)
);
if (records.Count == 0)
{
Console.WriteLine("Данные отсутствуют");
return;
}
Console.WriteLine($"\nПосещаемость группы {groupId} на {date:dd.MM.yyyy}");
Console.WriteLine(new string('-', 40));
foreach (var lesson in records.GroupBy(r => r.LessonNumber))
{
Console.WriteLine($"Занятие {lesson.Key}:");
foreach (var student in lesson)
{
Console.WriteLine($" Студент {student.UserId}: " +
$"{(student.IsAttendance ? "Присутствовал" : "Отсутствовал")}");
}
Console.WriteLine(new string('-', 40));
}
}
catch (Exception ex)
{
ShowError("Ошибка получения данных", ex);
}
}
public void ExportToExcel(int groupId)
{
try
{
_presenceUseCase.ExportToExcel(groupId);
Console.WriteLine("Экспорт завершен успешно!");
}
catch (Exception ex)
{
ShowError("Ошибка экспорта", ex);
}
}
private void ShowError(string message, Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{message}: {ex.Message}");
Console.ResetColor();
}
public void GenerateWeeklyAttendance(int groupId, int firstLesson, int lastLesson, DateTime startDate)
{
try
{
_presenceUseCase.GenerateWeeklyPresence(
groupId,
DateOnly.FromDateTime(startDate),
firstLesson,
lastLesson
);
Console.WriteLine("Посещаемость на неделю успешно создана!");
}
catch (Exception ex)
{
ShowError("Ошибка генерации недельной посещаемости", ex);
}
}
public void MarkAbsence(int userId, int groupId, DateTime date, int firstLesson, int lastLesson)
{
try
{
_presenceUseCase.MarkAbsence(
userId,
groupId,
DateOnly.FromDateTime(date),
firstLesson,
lastLesson
);
Console.WriteLine("Отметка об отсутствии сохранена");
}
catch (Exception ex)
{
ShowError("Ошибка отметки отсутствия", ex);
}
}
public void ShowFullGroupAttendance(int groupId)
{
try
{
var records = _presenceUseCase.GetCompleteGroupAttendance(groupId);
Console.WriteLine($"\nПолная посещаемость группы {groupId}");
Console.WriteLine(new string('=', 50));
foreach (var dateGroup in records.GroupBy(r => r.Date))
{
Console.WriteLine($"Дата: {dateGroup.Key:dd.MM.yyyy}");
foreach (var lessonGroup in dateGroup.GroupBy(r => r.LessonNumber))
{
Console.WriteLine($" Занятие {lessonGroup.Key}:");
foreach (var record in lessonGroup)
{
Console.WriteLine($" Студент {record.UserId}: " +
$"{(record.IsAttendance ? "Присутствовал" : "Отсутствовал")}");
}
Console.WriteLine(new string('-', 30));
}
}
}
catch (Exception ex)
{
ShowError("Ошибка получения данных", ex);
}
}
public void DisplayGeneralPresence(int groupId)
{
try
{
var stats = _presenceUseCase.GetGeneralPresence(groupId);
Console.WriteLine("\nОбщая статистика посещаемости:");
Console.WriteLine($"Количество студентов: {stats.UserCount}");
Console.WriteLine($"Всего занятий: {stats.TotalLessons}");
Console.WriteLine($"Средняя посещаемость: {stats.AttendancePercentage:F1}%");
}
catch (Exception ex)
{
ShowError("Ошибка получения статистики", ex);
}
}
}
}