Mega_New_Presence/presence_new/Domain/UseCase/UseCaseGeneratePresence.cs

90 lines
3.0 KiB
C#
Raw Normal View History

2024-11-05 18:36:49 +00:00
using ClosedXML.Excel;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
using Demo.Data.Repository;
2024-11-01 07:40:53 +00:00
using Demo.domain.Models;
2024-11-05 18:36:49 +00:00
using Microsoft.EntityFrameworkCore;
2024-11-01 07:40:53 +00:00
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;
2024-11-05 18:36:49 +00:00
private readonly IGroupRepository _groupRepository;
2024-11-01 07:40:53 +00:00
2024-11-05 18:36:49 +00:00
public UseCaseGeneratePresence(IUserRepository userRepository, IPresenceRepository presenceRepository, IGroupRepository groupRepository)
2024-11-01 07:40:53 +00:00
{
_userRepository = userRepository;
_presenceRepository = presenceRepository;
2024-11-05 18:36:49 +00:00
_groupRepository = groupRepository;
2024-11-01 07:40:53 +00:00
}
2024-11-05 18:36:49 +00:00
public List<PresenceLocalEntity> GetPresenceByGroupAndDate(int groupId, DateTime date)
2024-11-01 07:40:53 +00:00
{
return _presenceRepository.GetPresenceByGroupAndDate(groupId, date);
}
public void GeneratePresenceDaily(int firstLesson, int lastLesson, int groupId, DateTime currentDate)
{
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);
}
2024-11-05 18:36:49 +00:00
2024-11-01 07:40:53 +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);
}
}
// Отметить пользователя как отсутствующего на диапазоне занятий
2024-11-05 18:36:49 +00:00
public void MarkUserAsAbsent(Guid userGuid, int groupId, int firstLesson, int lastLesson, DateTime date)
2024-11-01 07:40:53 +00:00
{
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;
2024-11-05 18:36:49 +00:00
2024-11-01 07:40:53 +00:00
}
_presenceRepository.SavePresence(presences);
}
public List<PresenceLocalEntity> GetAllPresenceByGroup(int groupId)
{
return _presenceRepository.GetPresenceByGroup(groupId);
}
}
}