2024-10-28 12:42:04 +00:00
|
|
|
|
// C:\Users\adm\Source\Repos\presence1\Demo\Domain\UseCase\UseCaseGeneratePresence.cs
|
|
|
|
|
using System;
|
2024-10-24 08:50:32 +00:00
|
|
|
|
using System.Linq;
|
2024-10-28 12:42:04 +00:00
|
|
|
|
using Demo.Data.LocalData;
|
|
|
|
|
using Demo.Data.LocalData.Entity;
|
|
|
|
|
using Demo.Data.Repository;
|
2024-10-24 08:50:32 +00:00
|
|
|
|
|
2024-10-28 12:42:04 +00:00
|
|
|
|
namespace Demo.Domain.UseCase
|
2024-10-24 08:50:32 +00:00
|
|
|
|
{
|
|
|
|
|
public class UseCaseGeneratePresence
|
|
|
|
|
{
|
|
|
|
|
private readonly IPresenceRepository _presenceRepository;
|
|
|
|
|
private readonly IUserRepository _userRepository;
|
|
|
|
|
|
2024-10-28 12:42:04 +00:00
|
|
|
|
public UseCaseGeneratePresence(IPresenceRepository presenceRepository, IUserRepository userRepository)
|
2024-10-24 08:50:32 +00:00
|
|
|
|
{
|
|
|
|
|
_presenceRepository = presenceRepository;
|
|
|
|
|
_userRepository = userRepository;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 12:42:04 +00:00
|
|
|
|
public void GenerateDailyPresence(int groupId, int firstLesson, int lastLesson, DateTime date)
|
2024-10-24 08:50:32 +00:00
|
|
|
|
{
|
2024-10-28 12:42:04 +00:00
|
|
|
|
var users = _userRepository.GetAllUsers().Where(u => u.GroupID == groupId).ToList();
|
2024-10-24 08:50:32 +00:00
|
|
|
|
|
|
|
|
|
foreach (var user in users)
|
|
|
|
|
{
|
2024-10-28 12:42:04 +00:00
|
|
|
|
for (int lesson = firstLesson; lesson <= lastLesson; lesson++)
|
2024-10-24 08:50:32 +00:00
|
|
|
|
{
|
2024-10-28 12:42:04 +00:00
|
|
|
|
var existing = LocalStaticData.Presences.FirstOrDefault(p =>
|
|
|
|
|
p.UserId == user.Id &&
|
|
|
|
|
p.ClassDate.Date == date.Date &&
|
|
|
|
|
p.LessonNumber == lesson);
|
|
|
|
|
|
|
|
|
|
if (existing == null)
|
2024-10-24 08:50:32 +00:00
|
|
|
|
{
|
2024-10-28 12:42:04 +00:00
|
|
|
|
var presence = new LocalPresence
|
|
|
|
|
{
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
ClassDate = date.Date,
|
|
|
|
|
LessonNumber = lesson,
|
|
|
|
|
WasPresent = true
|
|
|
|
|
};
|
|
|
|
|
_presenceRepository.AddPresence(presence);
|
|
|
|
|
}
|
2024-10-24 08:50:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 12:42:04 +00:00
|
|
|
|
public void GenerateWeeklyPresence(int groupId, int firstLesson, int lastLesson, DateTime startDate)
|
2024-10-24 08:50:32 +00:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < 7; i++)
|
|
|
|
|
{
|
2024-10-28 12:42:04 +00:00
|
|
|
|
var date = startDate.AddDays(i);
|
|
|
|
|
if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
|
|
|
|
|
continue; // Пропускаем выходные
|
|
|
|
|
GenerateDailyPresence(groupId, firstLesson, lastLesson, date);
|
2024-10-24 08:50:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|