123 lines
4.7 KiB
C#
123 lines
4.7 KiB
C#
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;
|
|
|
|
|
|
public UseCaseGeneratePresence(IUserRepository userRepository, IPresenceRepository presenceRepository)
|
|
{
|
|
_userRepository = userRepository;
|
|
_presenceRepository = presenceRepository;
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PresenceDao> GetPresenceByDateAndGroup(DateTime date, int groupId)
|
|
{
|
|
return _presenceRepository.GetPresenceByDateAndGroup(date, groupId);
|
|
}
|
|
|
|
public void GeneratePresenceDaily(int firstLesson, int lastLesson, int groupId)
|
|
{
|
|
try
|
|
{
|
|
var users = _userRepository.GetAllUsers().Where(u => u.GroupId == groupId).ToList();
|
|
|
|
// Находим последнюю дату посещаемости для данной группы
|
|
DateOnly startDate = _presenceRepository.GetLastDateByGroupId(groupId)?.AddDays(1)
|
|
?? DateOnly.FromDateTime(DateTime.Today);
|
|
|
|
List<PresenceDao> presences = new List<PresenceDao>();
|
|
for (int lessonNumber = firstLesson; lessonNumber <= lastLesson; lessonNumber++)
|
|
{
|
|
foreach (var user in users)
|
|
{
|
|
var presence = new PresenceDao
|
|
{
|
|
UserId = user.UserId,
|
|
GroupId = user.GroupId,
|
|
Date = startDate,
|
|
LessonNumber = lessonNumber,
|
|
IsAttedance = true
|
|
};
|
|
|
|
try
|
|
{
|
|
_presenceRepository.SavePresence(new List<PresenceDao> { presence });
|
|
Console.WriteLine($"Посещаемость добавлена для UserId = {user.UserId}, LessonNumber = {lessonNumber} на дату {startDate}");
|
|
}
|
|
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}");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Отметить пользователя как отсутствующего на диапазоне занятий
|
|
public void MarkUserAbsentForLessons(int userId, int groupId, int firstLesson, int lastLesson, DateTime date)
|
|
{
|
|
List<PresenceDao> presences = _presenceRepository.GetPresenceForAbsent(date, groupId);
|
|
|
|
// Обновляем состояние присутствия для указанных занятий
|
|
foreach (var presence in presences.Where(p => p.UserId == userId && p.LessonNumber >= firstLesson && p.LessonNumber <= lastLesson))
|
|
{
|
|
presence.IsAttedance = false; // Устанавливаем отсутствие
|
|
Console.WriteLine($"PresenceId: {presence.PresenceId}, UserId: {presence.UserId}, Lesson Num: {presence.LessonNumber}, Att: {presence.IsAttedance}");
|
|
}
|
|
// Сохраняем изменения в репозитории
|
|
_presenceRepository.SavePresence(presences);
|
|
}
|
|
|
|
public List<PresenceDao> GetAllPresenceByGroup(int groupId)
|
|
{
|
|
return _presenceRepository.GetPresenceByGroup(groupId);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|