176 lines
7.6 KiB
C#
176 lines
7.6 KiB
C#
|
using ClosedXML.Excel;
|
|||
|
using presence.Domain.UseCase;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace presence.UI
|
|||
|
{
|
|||
|
public class PresenceConsoleUI
|
|||
|
{
|
|||
|
PresenceUseCase _presenceUseCase;
|
|||
|
UseCaseGeneratePresence _useCaseGeneratePresence;
|
|||
|
private PresenceUseCase presenceUseCase;
|
|||
|
|
|||
|
public PresenceConsoleUI(PresenceUseCase presenceUseCase, UseCaseGeneratePresence useCaseGeneratePresence)
|
|||
|
{
|
|||
|
_presenceUseCase = presenceUseCase;
|
|||
|
_useCaseGeneratePresence = useCaseGeneratePresence;
|
|||
|
}
|
|||
|
|
|||
|
public PresenceConsoleUI(PresenceUseCase presenceUseCase)
|
|||
|
{
|
|||
|
this.presenceUseCase = presenceUseCase;
|
|||
|
}
|
|||
|
|
|||
|
public void GetPresenceByGroup(int groupId)
|
|||
|
{
|
|||
|
StringBuilder presenceOutput = new StringBuilder();
|
|||
|
var presence = _presenceUseCase.GetPresenceByGroup(groupId);
|
|||
|
foreach (var p in presence)
|
|||
|
{
|
|||
|
presenceOutput.AppendLine($"{p.ClassNum}\t{p.User}\t{p.Date}");
|
|||
|
}
|
|||
|
Console.WriteLine(presenceOutput);
|
|||
|
}
|
|||
|
|
|||
|
public void GetPresenceByGroupAndDAte(int groupId, DateOnly date)
|
|||
|
{
|
|||
|
StringBuilder presenceOutput = new StringBuilder();
|
|||
|
var presence = _presenceUseCase.GetPresenceByGroupAndDate(groupId, date);
|
|||
|
foreach (var p in presence)
|
|||
|
{
|
|||
|
presenceOutput.AppendLine($"{p.ClassNum}\t{p.User}\t{p.Date}");
|
|||
|
}
|
|||
|
Console.WriteLine(presenceOutput);
|
|||
|
}
|
|||
|
|
|||
|
public void UnCheckAttendence(int firstClass, int lastClass, DateOnly date, int userId)
|
|||
|
{
|
|||
|
var result = _presenceUseCase.UnCheckAttendance(firstClass, lastClass, date, userId);
|
|||
|
Console.WriteLine(result ? "Посещаемость успешно обновлена" : "Ошибка при обновлении посещаемости");
|
|||
|
}
|
|||
|
|
|||
|
public void AddPresence(int firstClass, int lastClass, int groupId, DateOnly date)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_useCaseGeneratePresence.AddPrecence(firstClass, lastClass, groupId, date);
|
|||
|
Console.WriteLine("Посещаемость успешно добавлена");
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Console.WriteLine($"Ошибка при добавлении посещаемости: {ex.Message}");
|
|||
|
}
|
|||
|
}
|
|||
|
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.IsAttedance),
|
|||
|
Missed = stats["Количество занятий"] - g.Count(p => p.IsAttedance),
|
|||
|
Percentage = (g.Count(p => p.IsAttedance) * 100) / stats["Количество занятий"]
|
|||
|
});
|
|||
|
|
|||
|
foreach (var student in students)
|
|||
|
{
|
|||
|
output.AppendLine($"\nСтудент: {student.Student.UserFIO}");
|
|||
|
output.AppendLine($"Посещено занятий: {student.Attended}");
|
|||
|
output.AppendLine($"Пропущено занятий: {student.Missed}");
|
|||
|
output.AppendLine($"Процент посещаемости: {student.Percentage}%");
|
|||
|
}
|
|||
|
|
|||
|
Console.WriteLine(output.ToString());
|
|||
|
}
|
|||
|
|
|||
|
public void ExportPresenceToExcel(string groupId, string filePath)
|
|||
|
{
|
|||
|
int Id;
|
|||
|
bool isParsed = int.TryParse(groupId, out Id);
|
|||
|
if (!isParsed)
|
|||
|
{
|
|||
|
Console.WriteLine("Введено не число");
|
|||
|
}
|
|||
|
var presence = _presenceUseCase.GetPresenceByGroup(Id);
|
|||
|
var stats = _presenceUseCase.GetPresenceStatsByGroup(Id);
|
|||
|
|
|||
|
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.UserFIO;
|
|||
|
|
|||
|
// Заполняем посещаемость по датам
|
|||
|
col = 3;
|
|||
|
foreach (var date in dates)
|
|||
|
{
|
|||
|
var presenceOnDate = studentGroup.FirstOrDefault(p => p.Date == date);
|
|||
|
worksheet.Cell(row, col).Value = presenceOnDate?.IsAttedance == 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}");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|