pr1/Demo/Domain/UseCase/UseCaseGeneratePresence.cs

172 lines
6.9 KiB
C#
Raw Permalink Normal View History

2024-11-17 16:24:01 +00:00
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;
private readonly IGroupRepository _groupRepository;
public UseCaseGeneratePresence(IUserRepository userRepository, IPresenceRepository presenceRepository, IGroupRepository groupRepository)
{
_userRepository = userRepository;
_presenceRepository = presenceRepository;
_groupRepository = groupRepository;
}
public List<PresenceLocalEntity> GetPresenceByGroupAndDate(int groupId, DateTime date)
{
return _presenceRepository.GetPresenceByGroupAndDate(groupId, date);
}
public void GeneratePresenceForDay(int firstLesson, int lastLesson, int groupId, DateTime currentDate)
{
// Проверка существования группы
var groupExists = _groupRepository.GetAllGroup().Any(g => g.Id == groupId);
if (!groupExists)
{
throw new ArgumentException($"Группа с ID {groupId} не существует.");
}
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);
}
public void GeneratePresenceForWeek(int firstLesson, int lastLesson, int groupId, DateTime startTime)
{
// Проверка существования группы
var groupExists = _groupRepository.GetAllGroup().Any(g => g.Id == groupId);
if (!groupExists)
{
throw new ArgumentException($"Группа с ID {groupId} не существует.");
}
for (int i = 0; i < 7; i++)
{
DateTime currentTime = startTime.AddDays(i);
GeneratePresenceForDay(firstLesson, lastLesson, groupId, currentTime);
}
}
public void MarkUserAsAbsent(Guid userGuid, int groupId, int firstLesson, int lastLesson, DateTime date)
{
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;
}
_presenceRepository.SavePresence(presences);
}
public List<PresenceLocalEntity> GetAllPresenceByGroup(int groupId)
{
return _presenceRepository.GetPresenceByGroup(groupId);
}
public GroupPresenceSummary GetGeneralPresenceForGroup(int groupId)
{
return _presenceRepository.GetGeneralPresenceForGroup(groupId);
}
public class UseCaseGeneratePresence
{
public readonly IUserRepository _userRepository;
public readonly IPresenceRepository _presenceRepository;
private readonly IGroupRepository _groupRepository;
public UseCaseGeneratePresence(IUserRepository userRepository, IPresenceRepository presenceRepository, IGroupRepository groupRepository)
{
_userRepository = userRepository;
_presenceRepository = presenceRepository;
_groupRepository = groupRepository;
}
public List<PresenceLocalEntity> GetPresenceByGroupAndDate(int groupId, DateTime date)
{
return _presenceRepository.GetPresenceByGroupAndDate(groupId, date);
}
public void GeneratePresenceForDay(int firstLesson, int lastLesson, int groupId, DateTime currentDate)
{
// Проверка существования группы
var groupExists = _groupRepository.GetAllGroup().Any(g => g.Id == groupId);
if (!groupExists)
{
throw new ArgumentException($"Группа с ID {groupId} не существует.");
}
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);
}
public void GeneratePresenceForWeek(int firstLesson, int lastLesson, int groupId, DateTime startTime)
{
// Проверка существования группы
var groupExists = _groupRepository.GetAllGroup().Any(g => g.Id == groupId);
if (!groupExists)
{
throw new ArgumentException($"Группа с ID {groupId} не существует.");
}
for (int i = 0; i < 7; i++)
{
DateTime currentTime = startTime.AddDays(i);
GeneratePresenceForDay(firstLesson, lastLesson, groupId, currentTime);
}
}
public void MarkUserAsAbsent(Guid userGuid, int groupId, int firstLesson, int lastLesson, DateTime date)
{
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;
}
_presenceRepository.SavePresence(presences);
}
}
}
}