presence/Demo/Domain/UseCase/UseCaseGeneratePresence.cs

87 lines
2.8 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-28 12:03:51 +00:00
var users = _userRepository.GetAllUsers.Where(u => u.GroupId == groupId).ToList();
List<PresenceDao> presences = new List<PresenceDao>();
2024-10-23 09:44:48 +00:00
for (int lessonNumber = firstLesson; lessonNumber <= lastLesson; lessonNumber++)
{
foreach (var user in users)
{
2024-10-28 12:03:51 +00:00
presences.Add(new PresenceDao
2024-10-23 09:44:48 +00:00
{
2024-10-28 12:03:51 +00:00
UserId = user.UserId,
GroupId = user.GroupId,
Date = DateOnly.FromDateTime(currentDate),
2024-10-23 09:44:48 +00:00
LessonNumber = lessonNumber,
IsAttedance = true
});
}
_presenceRepository.SavePresence(presences);
}
}
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
}
}