2024-10-28 08:32:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2024-11-06 04:10:09 +00:00
|
|
|
|
using System.Text;
|
2024-10-28 08:32:57 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Posechaemost.Domain.UseCase;
|
2024-11-11 20:02:21 +00:00
|
|
|
|
using ClosedXML.Excel;
|
|
|
|
|
using Posechaemost.Data.Repository;
|
2024-10-28 08:32:57 +00:00
|
|
|
|
|
|
|
|
|
namespace Posechaemost.UI
|
|
|
|
|
{
|
|
|
|
|
public class PresenceConsoleUI
|
|
|
|
|
{
|
2024-11-11 20:02:21 +00:00
|
|
|
|
private readonly PresenceUseCase _presenceUseCase;
|
|
|
|
|
|
|
|
|
|
private readonly IPresenceRepository _presenceRepository;
|
|
|
|
|
public PresenceConsoleUI(PresenceUseCase presenceUseCase, IPresenceRepository presenceRepository)
|
2024-11-06 04:10:09 +00:00
|
|
|
|
{
|
2024-10-28 08:32:57 +00:00
|
|
|
|
_presenceUseCase = presenceUseCase;
|
2024-11-11 20:02:21 +00:00
|
|
|
|
_presenceRepository = presenceRepository;
|
2024-10-28 08:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 04:10:09 +00:00
|
|
|
|
public void GetPresenceByGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder presenceOutput = new StringBuilder();
|
2024-10-28 08:32:57 +00:00
|
|
|
|
var presence = _presenceUseCase.GetPresenceByGroup(groupId);
|
2024-11-06 04:10:09 +00:00
|
|
|
|
foreach (var p in presence)
|
|
|
|
|
{
|
|
|
|
|
presenceOutput.AppendLine($"{p.User.UserId}\t{p.User.FIO}\t{p.ClassNumber}\t{p.Date}\t{p.IsAttendence}");
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine(presenceOutput);
|
2024-10-28 08:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 04:10:09 +00:00
|
|
|
|
public void GetPresenceByGroupAndDate(int groupId, DateOnly date)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder presenceOutput = new StringBuilder();
|
2024-10-28 08:32:57 +00:00
|
|
|
|
var presence = _presenceUseCase.GetPresenceByGroupAndDate(groupId, date);
|
2024-11-06 04:10:09 +00:00
|
|
|
|
foreach (var p in presence)
|
|
|
|
|
{
|
|
|
|
|
presenceOutput.AppendLine($"{p.User.UserId}\t{p.User.FIO}\t{p.ClassNumber}\t{p.Date}\t{p.IsAttendence}");
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine(presenceOutput);
|
2024-10-28 08:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 04:10:09 +00:00
|
|
|
|
public void UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId)
|
|
|
|
|
{
|
|
|
|
|
string output = _presenceUseCase.UncheckAttendence(firstClass, lastClass, date, userId) ?
|
|
|
|
|
"Посещаемость обновлена" : "Посещаемость не обновлена";
|
|
|
|
|
Console.WriteLine(output);
|
2024-10-28 08:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 04:10:09 +00:00
|
|
|
|
public void AddPresence(int firstClass, int lastClass, int groupId, DateOnly date)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder presenceOutput = new StringBuilder();
|
2024-10-28 08:32:57 +00:00
|
|
|
|
_presenceUseCase.AddPresence(firstClass, lastClass, groupId, date);
|
2024-11-06 04:10:09 +00:00
|
|
|
|
presenceOutput.AppendLine("Посещаемость добавлена");
|
|
|
|
|
Console.WriteLine(presenceOutput);
|
2024-10-28 08:32:57 +00:00
|
|
|
|
}
|
2024-11-11 20:02:21 +00:00
|
|
|
|
|
|
|
|
|
public void GetPresenceStatsByGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
var stats = _presenceUseCase.GetPresenceStatsByGroup(groupId);
|
|
|
|
|
StringBuilder output = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
output.AppendLine($"Информация о группе {groupId}:");
|
|
|
|
|
output.AppendLine($"Количество студентов: {stats["Количество студентов"]}");
|
|
|
|
|
output.AppendLine($"Количество занятий: {stats["Количество занятий"]}");
|
|
|
|
|
output.AppendLine($"Общий процент посещаемости: {stats["Процент посещаемости"]}%");
|
|
|
|
|
output.AppendLine("\nСтатистика по студентам:");
|
|
|
|
|
|
|
|
|
|
var presence = _presenceUseCase.GetPresenceByGroup(groupId);
|
|
|
|
|
var students = presence.GroupBy(p => p.User)
|
|
|
|
|
.Select(g => new {
|
|
|
|
|
Student = g.Key,
|
|
|
|
|
Total = stats["Количество занятий"],
|
|
|
|
|
Attended = g.Count(p => p.IsAttendence),
|
|
|
|
|
Missed = stats["Количество занятий"] - g.Count(p => p.IsAttendence),
|
|
|
|
|
Percentage = (g.Count(p => p.IsAttendence) * 100) / stats["Количество занятий"]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
foreach(var student in students) {
|
|
|
|
|
output.AppendLine($"\nСтудент: {student.Student.FIO}");
|
|
|
|
|
output.AppendLine($"Посещено занятий: {student.Attended}");
|
|
|
|
|
output.AppendLine($"Пропущено занятий: {student.Missed}");
|
|
|
|
|
output.AppendLine($"Процент посещаемости: {student.Percentage}%");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(output.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GenerateWeeklyPresence(int firstClass, int lastClass, int groupId, DateOnly startDate)
|
|
|
|
|
{
|
|
|
|
|
_presenceUseCase.GenerateWeeklyPresence(firstClass, lastClass, groupId, startDate);
|
|
|
|
|
Console.WriteLine("Посещаемость на неделю сгенерирована");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ExportPresenceToExcel(int groupId, string filePath)
|
|
|
|
|
{
|
|
|
|
|
var presence = _presenceUseCase.GetPresenceByGroup(groupId);
|
|
|
|
|
var stats = _presenceUseCase.GetPresenceStatsByGroup(groupId);
|
|
|
|
|
|
|
|
|
|
using (var workbook = new XLWorkbook())
|
|
|
|
|
{
|
|
|
|
|
var worksheet = workbook.Worksheets.Add("Посещаемость");
|
|
|
|
|
|
|
|
|
|
// Заголовок листа
|
|
|
|
|
worksheet.Cell(1, 1).Value = $"Группа {groupId}";
|
|
|
|
|
worksheet.Range(1, 1, 1, 3).Merge();
|
|
|
|
|
worksheet.Cell(1, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
|
|
|
|
|
|
|
|
|
// Заголовки столбцов
|
|
|
|
|
worksheet.Cell(3, 1).Value = "№";
|
|
|
|
|
worksheet.Cell(3, 2).Value = "ФИО";
|
|
|
|
|
|
|
|
|
|
// Получаем все уникальные даты
|
|
|
|
|
var dates = presence.Select(p => p.Date).Distinct().OrderBy(d => d).ToList();
|
|
|
|
|
int col = 3;
|
|
|
|
|
foreach (var date in dates)
|
|
|
|
|
{
|
|
|
|
|
worksheet.Cell(3, col).Value = date.ToString("dd.MM.yyyy");
|
|
|
|
|
col++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Группируем данные по студентам
|
|
|
|
|
var studentGroups = presence.GroupBy(p => p.User);
|
|
|
|
|
int row = 4;
|
|
|
|
|
int studentNumber = 1;
|
|
|
|
|
|
|
|
|
|
foreach (var studentGroup in studentGroups)
|
|
|
|
|
{
|
|
|
|
|
// Номер и ФИО студента
|
|
|
|
|
worksheet.Cell(row, 1).Value = studentNumber++;
|
|
|
|
|
worksheet.Cell(row, 2).Value = studentGroup.Key.FIO;
|
|
|
|
|
|
|
|
|
|
// Заполняем посещаемость по датам
|
|
|
|
|
col = 3;
|
|
|
|
|
foreach (var date in dates)
|
|
|
|
|
{
|
|
|
|
|
var presenceOnDate = studentGroup.FirstOrDefault(p => p.Date == date);
|
|
|
|
|
worksheet.Cell(row, col).Value = presenceOnDate?.IsAttendence == true ? "+" : "н";
|
|
|
|
|
worksheet.Cell(row, col).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
|
|
|
|
col++;
|
|
|
|
|
}
|
|
|
|
|
row++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Форматирование
|
|
|
|
|
var tableRange = worksheet.Range(3, 1, row - 1, dates.Count + 2);
|
|
|
|
|
tableRange.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
|
|
|
|
tableRange.Style.Border.InsideBorder = XLBorderStyleValues.Thin;
|
|
|
|
|
|
|
|
|
|
worksheet.Columns().AdjustToContents();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
workbook.SaveAs(filePath);
|
|
|
|
|
Console.WriteLine($"Данные успешно экспортированы в файл: {filePath}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Ошибка при сохранении файла: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-28 08:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|