presence123/Demo/Domain/UseCase/UseCaseGeneratePresence.cs

114 lines
4.2 KiB
C#
Raw Normal View History

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;
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-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);
}
public void GeneratePresenceDaily(int firstLesson, int lastLesson, int groupId, DateTime currentDate)
{
2024-10-31 11:48:30 +00:00
try
2024-10-23 09:44:48 +00:00
{
2024-10-31 11:48:30 +00:00
var users = _userRepository.GetAllUsers.Where(u => u.GroupId == groupId).ToList();
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
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}");
2024-10-23 09:44:48 +00:00
}
}
}
2024-10-31 11:48:30 +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);
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);
}
2024-10-28 12:03:51 +00:00
public List<PresenceDao> GetAllPresenceByGroup(int groupId)
2024-10-24 08:39:55 +00:00
{
2024-10-28 12:03:51 +00:00
2024-10-24 08:39:55 +00:00
return _presenceRepository.GetPresenceByGroup(groupId);
}
2024-10-23 09:44:48 +00:00
}
}