2024-11-05 18:36:49 +00:00
|
|
|
|
using ClosedXML.Excel;
|
|
|
|
|
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
|
|
|
|
using Demo.Data.Repository;
|
2024-11-01 07:40:53 +00:00
|
|
|
|
using Demo.domain.Models;
|
2024-11-05 18:36:49 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-11-01 07:40:53 +00:00
|
|
|
|
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;
|
2024-11-05 18:36:49 +00:00
|
|
|
|
private readonly IGroupRepository _groupRepository;
|
2024-11-01 07:40:53 +00:00
|
|
|
|
|
2024-11-05 18:36:49 +00:00
|
|
|
|
public UseCaseGeneratePresence(IUserRepository userRepository, IPresenceRepository presenceRepository, IGroupRepository groupRepository)
|
2024-11-01 07:40:53 +00:00
|
|
|
|
{
|
|
|
|
|
_userRepository = userRepository;
|
|
|
|
|
_presenceRepository = presenceRepository;
|
2024-11-05 18:36:49 +00:00
|
|
|
|
_groupRepository = groupRepository;
|
2024-11-01 07:40:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 18:36:49 +00:00
|
|
|
|
public List<PresenceLocalEntity> GetPresenceByGroupAndDate(int groupId, DateTime date)
|
2024-11-01 07:40:53 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-11-05 18:36:49 +00:00
|
|
|
|
|
2024-11-01 07:40:53 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Отметить пользователя как отсутствующего на диапазоне занятий
|
2024-11-05 18:36:49 +00:00
|
|
|
|
public void MarkUserAsAbsent(Guid userGuid, int groupId, int firstLesson, int lastLesson, DateTime date)
|
2024-11-01 07:40:53 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2024-11-05 18:36:49 +00:00
|
|
|
|
|
2024-11-01 07:40:53 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
_presenceRepository.SavePresence(presences);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<PresenceLocalEntity> GetAllPresenceByGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
return _presenceRepository.GetPresenceByGroup(groupId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 09:07:50 +00:00
|
|
|
|
public GroupPresenceSummary GetGeneralPresenceForGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
return _presenceRepository.GetGeneralPresenceForGroup(groupId);
|
|
|
|
|
}
|
2024-11-01 07:40:53 +00:00
|
|
|
|
|
2024-11-07 07:35:57 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-11-07 08:10:07 +00:00
|
|
|
|
var names = _userRepository.GetUserNames().Where(u => u.Guid == record.UserGuid);
|
|
|
|
|
foreach (var name in names)
|
2024-11-07 07:35:57 +00:00
|
|
|
|
{
|
|
|
|
|
attendanceRecords.Add(new Excel
|
|
|
|
|
{
|
2024-11-07 08:10:07 +00:00
|
|
|
|
UserName = name.FIO,
|
|
|
|
|
UserGuid = name.Guid,
|
2024-11-07 07:35:57 +00:00
|
|
|
|
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");
|
|
|
|
|
|
2024-11-07 08:10:07 +00:00
|
|
|
|
// Создаем папку, если она не существует
|
2024-11-07 07:35:57 +00:00
|
|
|
|
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;
|
2024-11-07 08:10:07 +00:00
|
|
|
|
int lesNum = 1;
|
|
|
|
|
foreach (var record in group.Value.OrderBy(r => r.Date).ThenBy(r => r.LessonNumber).ThenBy(r => r.UserGuid))
|
2024-11-07 07:35:57 +00:00
|
|
|
|
{
|
2024-11-07 08:10:07 +00:00
|
|
|
|
if (lesNum != record.LessonNumber)
|
|
|
|
|
{
|
|
|
|
|
row++;
|
|
|
|
|
}
|
2024-11-07 07:35:57 +00:00
|
|
|
|
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");
|
2024-11-07 11:46:50 +00:00
|
|
|
|
worksheet.Cell(row, 4).Value = record.LessonNumber.ToString();
|
2024-11-07 07:35:57 +00:00
|
|
|
|
worksheet.Cell(row, 5).Value = record.IsAttedance ? "Присутствует" : "Отсутствует";
|
|
|
|
|
row++;
|
2024-11-07 08:10:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lesNum = record.LessonNumber;
|
2024-11-07 07:35:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worksheet.Columns().AdjustToContents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workbook.SaveAs(filePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-01 07:40:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|