Mega_New_Presence/presence_new/Domain/UseCase/UseCaseGeneratePresence.cs
2024-11-07 10:35:57 +03:00

170 lines
6.6 KiB
C#

using ClosedXML.Excel;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
using Demo.Data.Repository;
using Demo.domain.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo.Domain.UseCase
{
public class UseCaseGeneratePresence
{
public readonly IUserRepository _userRepository;
public readonly IPresenceRepository _presenceRepository;
private readonly IGroupRepository _groupRepository;
public UseCaseGeneratePresence(IUserRepository userRepository, IPresenceRepository presenceRepository, IGroupRepository groupRepository)
{
_userRepository = userRepository;
_presenceRepository = presenceRepository;
_groupRepository = groupRepository;
}
public List<PresenceLocalEntity> GetPresenceByGroupAndDate(int groupId, DateTime date)
{
return _presenceRepository.GetPresenceByGroupAndDate(groupId, date);
}
public void GeneratePresenceDaily(int firstLesson, int lastLesson, int groupId, DateTime currentDate)
{
var users = _userRepository.GetAllUsers.Where(u => u.GroupID == groupId).ToList();
List<PresenceLocalEntity> presences = new List<PresenceLocalEntity>();
for (int lessonNumber = firstLesson; lessonNumber <= lastLesson; lessonNumber++)
{
foreach (var user in users)
{
presences.Add(new PresenceLocalEntity
{
UserGuid = user.Guid,
Date = currentDate,
LessonNumber = lessonNumber,
IsAttedance = true
});
}
}
_presenceRepository.SavePresence(presences);
}
public void GenerateWeeklyPresence(int firstLesson, int lastLesson, int groupId, DateTime startTime)
{
for (int i = 0; i < 7; i++)
{
DateTime currentTime = startTime.AddDays(i);
GeneratePresenceDaily(firstLesson, lastLesson, groupId, currentTime);
}
}
// Отметить пользователя как отсутствующего на диапазоне занятий
public void MarkUserAsAbsent(Guid userGuid, int groupId, int firstLesson, int lastLesson, DateTime date)
{
var presences = _presenceRepository.GetPresenceByGroupAndDate(groupId, date);
foreach (var presence in presences.Where(p => p.UserGuid == userGuid && p.LessonNumber >= firstLesson && p.LessonNumber <= lastLesson))
{
presence.IsAttedance = false;
}
_presenceRepository.SavePresence(presences);
}
public List<PresenceLocalEntity> GetAllPresenceByGroup(int groupId)
{
return _presenceRepository.GetPresenceByGroup(groupId);
}
public GroupPresenceSummary GetGeneralPresenceForGroup(int groupId)
{
return _presenceRepository.GetGeneralPresenceForGroup(groupId);
}
public Dictionary<string, List<Excel>> GetAllAttendanceByGroups()
{
var attendanceByGroup = new Dictionary<string, List<Excel>>();
var allGroups = _groupRepository.GetAllGroup();
foreach (var group in allGroups)
{
// Получаем все записи посещаемости для группы
var groupAttendance = _presenceRepository.GetAttendanceByGroup(group.Id);
var attendanceRecords = new List<Excel>();
foreach (var record in groupAttendance)
{
// Подбираем имена пользователей и добавляем все записи для всех занятий
var user = _userRepository.GetUserNames().FirstOrDefault(u => u.Guid == record.UserGuid);
if (user != null)
{
attendanceRecords.Add(new Excel
{
UserName = user.FIO,
UserGuid = user.Guid,
Date = record.Date,
IsAttedance = record.IsAttedance,
LessonNumber = record.LessonNumber,
GroupName = group.Name
});
}
}
attendanceByGroup.Add(group.Name, attendanceRecords);
}
return attendanceByGroup;
}
public void ExportAttendanceToExcel()
{
var attendanceByGroup = GetAllAttendanceByGroups();
string projectDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
string reportsFolderPath = Path.Combine(projectDirectory, "Reports");
string filePath = Path.Combine(reportsFolderPath, "AttendanceReport.xlsx");
if (!Directory.Exists(reportsFolderPath))
{
Directory.CreateDirectory(reportsFolderPath);
}
using (var workbook = new XLWorkbook())
{
foreach (var group in attendanceByGroup)
{
var worksheet = workbook.Worksheets.Add($"{group.Key}");
worksheet.Cell(1, 1).Value = "ФИО";
worksheet.Cell(1, 2).Value = "Группа";
worksheet.Cell(1, 3).Value = "Дата";
worksheet.Cell(1, 4).Value = "Занятие";
worksheet.Cell(1, 5).Value = "Статус";
int row = 2;
foreach (var record in group.Value.OrderBy(r => r.Date).ThenBy(r => r.LessonNumber))
{
worksheet.Cell(row, 1).Value = record.UserName;
worksheet.Cell(row, 2).Value = record.GroupName;
worksheet.Cell(row, 3).Value = record.Date.ToString("dd.MM.yyyy");
worksheet.Cell(row, 4).Value = record.LessonNumber;
worksheet.Cell(row, 5).Value = record.IsAttedance ? "Присутствует" : "Отсутствует";
row++;
}
worksheet.Columns().AdjustToContents();
}
workbook.SaveAs(filePath);
}
}
}
}