2024-11-01 07:40:53 +00:00
using Demo.Data.RemoteData.RemoteDataBase ;
using Demo.Data.RemoteData.RemoteDataBase.DAO ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using Demo.domain.Models ;
2024-11-06 09:07:50 +00:00
using Microsoft.EntityFrameworkCore ;
2024-11-01 07:40:53 +00:00
namespace Demo.Data.Repository
{
public class SQLPresenceRepositoryImpl : IPresenceRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext ;
public SQLPresenceRepositoryImpl ( RemoteDatabaseContext remoteDatabaseContext )
{
_remoteDatabaseContext = remoteDatabaseContext ;
}
public void SavePresence ( List < PresenceLocalEntity > presences )
{
_remoteDatabaseContext . PresenceDaos . AddRange ( presences . Select ( it = > new PresenceDao {
Date = DateOnly . FromDateTime ( it . Date ) ,
IsAttedance = it . IsAttedance ,
LessonNumber = it . LessonNumber ,
UserGuid = it . UserGuid } ) ) ;
_remoteDatabaseContext . SaveChanges ( ) ;
}
2024-11-05 18:36:49 +00:00
2024-11-01 07:40:53 +00:00
public void AddPresence ( PresenceLocalEntity presence )
{
if ( presence = = null ) throw new ArgumentNullException ( nameof ( presence ) ) ;
var newPresence = new PresenceDao
{
Date = DateOnly . FromDateTime ( presence . Date ) ,
UserGuid = presence . UserGuid ,
LessonNumber = presence . LessonNumber ,
IsAttedance = presence . IsAttedance
} ;
_remoteDatabaseContext . PresenceDaos . Add ( newPresence ) ;
}
public List < PresenceLocalEntity > GetPresenceByGroup ( int groupId )
{
2024-11-06 09:07:50 +00:00
return _remoteDatabaseContext . PresenceDaos . Include ( user = > user . UserDao )
2024-11-01 07:40:53 +00:00
. Where ( p = > p . UserDao ! = null & & p . UserDao . GroupID = = groupId ) // Проверяем на null перед использованием
. Select ( p = > new PresenceLocalEntity
{
Date = p . Date . ToDateTime ( TimeOnly . MinValue ) ,
UserGuid = p . UserGuid ,
LessonNumber = p . LessonNumber ,
IsAttedance = p . IsAttedance
} )
. ToList ( ) ;
}
public List < PresenceLocalEntity > GetPresenceByGroupAndDate ( int groupId , DateTime date )
{
return _remoteDatabaseContext . PresenceDaos
. Where ( p = > p . UserDao ! = null & & p . UserDao . GroupID = = groupId & & p . Date = = DateOnly . FromDateTime ( date ) )
. Select ( p = > new PresenceLocalEntity
{
Date = p . Date . ToDateTime ( TimeOnly . MinValue ) ,
UserGuid = p . UserGuid ,
LessonNumber = p . LessonNumber ,
IsAttedance = p . IsAttedance
} )
. ToList ( ) ;
}
2024-11-05 18:36:49 +00:00
public void MarkUserAsAbsent ( Guid userGuid , int firstLessonNumber , int lastLessonNumber )
2024-11-01 07:40:53 +00:00
{
foreach ( var lesson in Enumerable . Range ( firstLessonNumber , lastLessonNumber - firstLessonNumber + 1 ) )
{
var presence = _remoteDatabaseContext . PresenceDaos . FirstOrDefault ( p = >
p . UserGuid = = userGuid & &
p . LessonNumber = = lesson ) ;
if ( presence ! = null )
{
presence . IsAttedance = false ; // Помечаем как отсутствующего
}
}
}
2024-11-05 18:36:49 +00:00
public DateOnly ? GetLastDateByGroupId ( int groupId )
{
// Проверяем наличие записей о посещаемости в базе данных для данной группы.
var lastDate = _remoteDatabaseContext . PresenceDaos
2024-11-06 09:07:50 +00:00
. Where ( p = > p . UserDao . GroupID = = groupId )
2024-11-05 18:36:49 +00:00
. OrderByDescending ( p = > p . Date )
. Select ( p = > p . Date )
. FirstOrDefault ( ) ;
return lastDate = = default ? ( DateOnly ? ) null : lastDate ;
}
2024-11-06 09:07:50 +00:00
public GroupPresenceSummary GetGeneralPresenceForGroup ( int groupId )
2024-11-05 18:36:49 +00:00
{
var presences = _remoteDatabaseContext . PresenceDaos
2024-11-06 09:07:50 +00:00
. Where ( p = > p . UserDao . GroupID = = groupId )
2024-11-05 18:36:49 +00:00
. OrderBy ( p = > p . LessonNumber )
. ToList ( ) ;
var distinctDates = presences
. Select ( p = > p . Date )
. Distinct ( )
. ToList ( ) ;
int lessonCount = 0 ;
double totalAttendance = 0 ;
DateOnly previousDate = DateOnly . MinValue ;
HashSet < Guid > userGuids = new HashSet < Guid > ( ) ;
foreach ( var presence in presences )
{
userGuids . Add ( presence . UserGuid ) ;
if ( presence . Date ! = previousDate )
{
previousDate = presence . Date ;
lessonCount + + ;
}
if ( presence . IsAttedance )
{
totalAttendance + + ;
}
}
2024-11-06 09:07:50 +00:00
var userAttendances = userGuids . Select ( userGuid = >
2024-11-05 18:36:49 +00:00
{
var userPresences = presences . Where ( p = > p . UserGuid = = userGuid ) ;
double attended = userPresences . Count ( p = > p . IsAttedance ) ;
double missed = userPresences . Count ( p = > ! p . IsAttedance ) ;
2024-11-06 09:07:50 +00:00
return new UserAttendance
2024-11-05 18:36:49 +00:00
{
UserGuid = userGuid ,
Attended = attended ,
Missed = missed ,
AttendanceRate = attended / ( attended + missed ) * 100
2024-11-06 09:07:50 +00:00
} ;
} ) . ToList ( ) ;
2024-11-05 18:36:49 +00:00
2024-11-06 09:07:50 +00:00
return new GroupPresenceSummary
2024-11-05 18:36:49 +00:00
{
2024-11-06 09:07:50 +00:00
UserCount = userGuids . Count ,
LessonCount = lessonCount ,
TotalAttendancePercentage = ( totalAttendance / ( userGuids . Count * distinctDates . Count ) ) * 100 ,
UserAttendances = userAttendances
} ;
2024-11-05 18:36:49 +00:00
}
2024-11-06 09:07:50 +00:00
2024-11-05 18:36:49 +00:00
public void UpdateAtt ( Guid UserGuid , int groupId , int firstLesson , int lastLesson , DateOnly date , bool isAttendance )
{
// Находим все записи по UserId, GroupId, LessonNumber (в диапазоне) и дате
var presences = _remoteDatabaseContext . PresenceDaos
. Where ( p = > p . UserGuid = = UserGuid
2024-11-06 09:07:50 +00:00
& & p . UserDao . GroupID = = groupId
2024-11-05 18:36:49 +00:00
& & p . LessonNumber > = firstLesson
& & p . LessonNumber < = lastLesson
& & p . Date = = date )
. ToList ( ) ;
if ( presences . Any ( ) )
{
// Обновляем значение IsAttendance для всех найденных записей
foreach ( var presence in presences )
{
presence . IsAttedance = isAttendance ;
}
_remoteDatabaseContext . SaveChanges ( ) ; // Сохраняем изменения в базе данных
Console . WriteLine ( $"Статус посещаемости для пользователя {UserGuid} с {firstLesson} по {lastLesson} урок обновлён." ) ;
}
else
{
Console . WriteLine ( $"Посещаемость для пользователя ID: {UserGuid} на дату {date.ToShortDateString()} с {firstLesson} по {lastLesson} уроки не найдена." ) ;
}
}
2024-11-01 07:40:53 +00:00
}
}