presence/Demo/Domain/UseCase/UseCaseGeneratePresence.cs
2024-10-31 14:48:30 +03:00

114 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Demo.Data.RemoteData.RemoteDataBase.DAO;
using Demo.Data.Repository;
using Demo.domain.Models;
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, DateTime currentDate)
{
try
{
var users = _userRepository.GetAllUsers.Where(u => u.GroupId == groupId).ToList();
List<PresenceDao> presences = new List<PresenceDao>();
for (int lessonNumber = firstLesson; lessonNumber <= lastLesson; lessonNumber++)
{
foreach (var user in users)
{
Console.WriteLine($"UserId: {user.UserId}, GroupId: {user.GroupId}"); // Выводим информацию о пользователе
var presence = new PresenceDao
{
UserId = user.UserId,
GroupId = user.GroupId,
Date = DateOnly.FromDateTime(currentDate),
LessonNumber = lessonNumber,
IsAttedance = true
};
try
{
_presenceRepository.SavePresence(new List<PresenceDao> { presence });
Console.WriteLine($"Посещаемость добавлена для UserId = {user.UserId}, LessonNumber = {lessonNumber}");
}
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, currentTime);
}
}
// Отметить пользователя как отсутствующего на диапазоне занятий
public void MarkUserAbsentForLessons(int userId, int groupId, int firstLesson, int lastLesson, DateTime date)
{
var presences = _presenceRepository.GetPresenceByDateAndGroup(date, groupId);
foreach (var presence in presences.Where(p => p.UserId == userId && p.LessonNumber >= firstLesson && p.LessonNumber <= lastLesson))
{
presence.IsAttedance = false;
}
_presenceRepository.SavePresence(presences);
}
public List<PresenceDao> GetAllPresenceByGroup(int groupId)
{
return _presenceRepository.GetPresenceByGroup(groupId);
}
}
}