2024-10-28 12:03:51 +00:00
|
|
|
|
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
|
|
|
|
using Demo.Data.Repository;
|
2024-10-23 09:44:48 +00:00
|
|
|
|
using Demo.domain.Models;
|
2024-11-01 09:42:31 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-23 09:44:48 +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
|
|
|
|
|
{
|
2024-10-25 09:41:35 +00:00
|
|
|
|
public readonly IUserRepository _userRepository;
|
2024-10-23 09:44:48 +00:00
|
|
|
|
public readonly IPresenceRepository _presenceRepository;
|
2024-11-01 09:42:31 +00:00
|
|
|
|
|
2024-10-23 09:44:48 +00:00
|
|
|
|
|
2024-10-25 09:41:35 +00:00
|
|
|
|
public UseCaseGeneratePresence(IUserRepository userRepository, IPresenceRepository presenceRepository)
|
2024-10-23 09:44:48 +00:00
|
|
|
|
{
|
|
|
|
|
_userRepository = userRepository;
|
|
|
|
|
_presenceRepository = presenceRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-28 12:03:51 +00:00
|
|
|
|
public List<PresenceDao> GetPresenceByDateAndGroup(DateTime date, int groupId)
|
2024-10-23 09:44:48 +00:00
|
|
|
|
{
|
|
|
|
|
return _presenceRepository.GetPresenceByDateAndGroup(date, groupId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 09:42:31 +00:00
|
|
|
|
public void GeneratePresenceDaily(int firstLesson, int lastLesson, int groupId)
|
2024-10-23 09:44:48 +00:00
|
|
|
|
{
|
2024-10-31 11:48:30 +00:00
|
|
|
|
try
|
2024-10-23 09:44:48 +00:00
|
|
|
|
{
|
2024-11-01 09:42:31 +00:00
|
|
|
|
var users = _userRepository.GetAllUsers().Where(u => u.GroupId == groupId).ToList();
|
|
|
|
|
|
|
|
|
|
// Находим последнюю дату посещаемости для данной группы
|
|
|
|
|
DateOnly startDate = _presenceRepository.GetLastDateByGroupId(groupId)?.AddDays(1)
|
|
|
|
|
?? DateOnly.FromDateTime(DateTime.Today);
|
2024-10-31 11:48:30 +00:00
|
|
|
|
|
|
|
|
|
List<PresenceDao> presences = new List<PresenceDao>();
|
|
|
|
|
for (int lessonNumber = firstLesson; lessonNumber <= lastLesson; lessonNumber++)
|
2024-10-23 09:44:48 +00:00
|
|
|
|
{
|
2024-10-31 11:48:30 +00:00
|
|
|
|
foreach (var user in users)
|
2024-10-23 09:44:48 +00:00
|
|
|
|
{
|
2024-10-31 11:48:30 +00:00
|
|
|
|
var presence = new PresenceDao
|
|
|
|
|
{
|
|
|
|
|
UserId = user.UserId,
|
|
|
|
|
GroupId = user.GroupId,
|
2024-11-01 09:42:31 +00:00
|
|
|
|
Date = startDate,
|
2024-10-31 11:48:30 +00:00
|
|
|
|
LessonNumber = lessonNumber,
|
|
|
|
|
IsAttedance = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_presenceRepository.SavePresence(new List<PresenceDao> { presence });
|
2024-11-01 09:42:31 +00:00
|
|
|
|
Console.WriteLine($"Посещаемость добавлена для UserId = {user.UserId}, LessonNumber = {lessonNumber} на дату {startDate}");
|
2024-10-31 11:48:30 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Ошибка при добавлении посещаемости для UserId = {user.UserId}: {ex.Message}");
|
|
|
|
|
if (ex.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Ошибка при генерации посещаемости: {ex.Message}");
|
|
|
|
|
if (ex.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
|
2024-10-23 09:44:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-31 11:48:30 +00:00
|
|
|
|
|
2024-11-01 09:42:31 +00:00
|
|
|
|
|
|
|
|
|
|
2024-10-23 09:44:48 +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);
|
2024-11-01 09:42:31 +00:00
|
|
|
|
GeneratePresenceDaily(firstLesson, lastLesson, groupId);
|
2024-10-23 09:44:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Отметить пользователя как отсутствующего на диапазоне занятий
|
|
|
|
|
public void MarkUserAbsentForLessons(int userId, int groupId, int firstLesson, int lastLesson, DateTime date)
|
|
|
|
|
{
|
2024-11-01 09:42:31 +00:00
|
|
|
|
List<PresenceDao> presences = _presenceRepository.GetPresenceForAbsent(date, groupId);
|
|
|
|
|
|
|
|
|
|
// Обновляем состояние присутствия для указанных занятий
|
2024-10-23 09:44:48 +00:00
|
|
|
|
foreach (var presence in presences.Where(p => p.UserId == userId && p.LessonNumber >= firstLesson && p.LessonNumber <= lastLesson))
|
|
|
|
|
{
|
2024-11-01 09:42:31 +00:00
|
|
|
|
presence.IsAttedance = false; // Устанавливаем отсутствие
|
|
|
|
|
Console.WriteLine($"PresenceId: {presence.PresenceId}, UserId: {presence.UserId}, Lesson Num: {presence.LessonNumber}, Att: {presence.IsAttedance}");
|
2024-10-23 09:44:48 +00:00
|
|
|
|
}
|
2024-11-01 09:42:31 +00:00
|
|
|
|
// Сохраняем изменения в репозитории
|
2024-10-23 09:44:48 +00:00
|
|
|
|
_presenceRepository.SavePresence(presences);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 12:03:51 +00:00
|
|
|
|
public List<PresenceDao> GetAllPresenceByGroup(int groupId)
|
2024-10-24 08:39:55 +00:00
|
|
|
|
{
|
|
|
|
|
return _presenceRepository.GetPresenceByGroup(groupId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-01 09:42:31 +00:00
|
|
|
|
|
2024-10-23 09:44:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|