FinalPresenceLexa/domain/UseCase/PresenceUseCase.cs
2025-04-28 11:51:01 +03:00

170 lines
5.9 KiB
C#

using ClosedXML.Excel;
using data.RemoteData.RemoteDataBase.DAO;
using data.Repository;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace domain.UseCase
{
public class PresenceUseCase
{
private readonly IPresenceRepository _presenceRepo;
private readonly IUserRepository _userRepo;
private readonly IGroupRepository _groupRepo;
public PresenceUseCase(
IPresenceRepository presenceRepo,
IUserRepository userRepo,
IGroupRepository groupRepo)
{
_presenceRepo = presenceRepo;
_userRepo = userRepo;
_groupRepo = groupRepo;
}
// Генерация посещаемости на день
public void GenerateDailyPresence(int groupId, DateOnly date, int firstLesson, int lastLesson)
{
var users = _userRepo.GetUsersByGroupId(groupId);
if (users.Count == 0)
throw new Exception("Группа не содержит студентов");
var presences = new List<PresenceDAO>();
for (int lesson = firstLesson; lesson <= lastLesson; lesson++)
{
presences.AddRange(users.Select(user => new PresenceDAO
{
UserId = user.UserId,
GroupId = groupId,
Date = date,
LessonNumber = lesson,
IsAttendance = true
}));
}
_presenceRepo.SavePresence(presences);
}
// Получение данных за день
public List<PresenceDAO> GetDailyAttendance(int groupId, DateOnly date)
{
return _presenceRepo.GetPresenceByDateAndGroup(date, date, groupId)
.OrderBy(p => p.LessonNumber)
.ThenBy(p => p.UserId)
.ToList();
}
// Экспорт в Excel
public void ExportToExcel(int groupId)
{
var data = GetGroupReport(groupId);
var path = PrepareReportFolder();
using var workbook = new XLWorkbook();
CreateWorksheets(workbook, data);
workbook.SaveAs(Path.Combine(path, "AttendanceReport.xlsx"));
}
private Dictionary<string, List<RecordAtt>> GetGroupReport(int groupId)
{
var report = new Dictionary<string, List<RecordAtt>>();
var group = _groupRepo.GetGroupById(groupId);
if (group == null)
throw new Exception("Группа не найдена");
var records = _presenceRepo.GetAttendanceByGroup(groupId)
.Select(r => new RecordAtt
{
UserName = _userRepo.GetUserById(r.UserId)?.FIO ?? "Неизвестный",
Date = r.Date,
LessonNumber = r.LessonNumber,
Status = r.IsAttendance ? "Присутствовал" : "Отсутствовал"
}).ToList();
report.Add(group.Name, records);
return report;
}
private string PrepareReportFolder()
{
var path = Path.Combine(
Directory.GetCurrentDirectory(),
"Reports",
DateTime.Now.ToString("yyyy-MM-dd"));
Directory.CreateDirectory(path);
return path;
}
private void CreateWorksheets(XLWorkbook workbook, Dictionary<string, List<RecordAtt>> data)
{
foreach (var group in data)
{
var ws = workbook.Worksheets.Add(group.Key);
ws.Cell(1, 1).Value = "ФИО";
ws.Cell(1, 2).Value = "Дата";
ws.Cell(1, 3).Value = "Занятие";
ws.Cell(1, 4).Value = "Статус";
int row = 2;
foreach (var record in group.Value)
{
ws.Cell(row, 1).Value = record.UserName;
ws.Cell(row, 2).Value = record.Date.ToString("dd.MM.yyyy");
ws.Cell(row, 3).Value = record.LessonNumber;
ws.Cell(row, 4).Value = record.Status;
row++;
}
ws.Columns().AdjustToContents();
}
}
public void GenerateWeeklyPresence(int groupId, DateOnly startDate, int firstLesson, int lastLesson)
{
for (int i = 0; i < 7; i++)
{
GenerateDailyPresence(groupId, startDate.AddDays(i), firstLesson, lastLesson);
}
}
public void MarkAbsence(int userId, int groupId, DateOnly date, int firstLesson, int lastLesson)
{
var presences = _presenceRepo.GetPresence(groupId, date, userId)
.Where(p => p.LessonNumber >= firstLesson && p.LessonNumber <= lastLesson)
.ToList();
foreach (var presence in presences)
{
presence.IsAttendance = false;
}
_presenceRepo.UpdatePresence(presences);
}
public List<PresenceDAO> GetCompleteGroupAttendance(int groupId)
{
return _presenceRepo.GetPresenceByGroup(groupId)
.OrderBy(p => p.Date)
.ThenBy(p => p.LessonNumber)
.ToList();
}
public GroupAttendanceStats GetGeneralPresence(int groupId)
{
var presences = _presenceRepo.GetPresenceByGroup(groupId);
var users = _userRepo.GetUsersByGroupId(groupId);
return new GroupAttendanceStats
{
UserCount = users.Count,
TotalLessons = presences.Select(p => p.LessonNumber).Distinct().Count(),
AttendancePercentage = (double)presences.Count(p => p.IsAttendance) / presences.Count * 100
};
}
}
}