2024-12-23 09:12:26 +00:00
using System ;
2024-12-19 17:36:57 +00:00
using System.Collections.Generic ;
using System.Linq ;
2024-12-23 09:12:26 +00:00
using System.Runtime.CompilerServices ;
using System.Security.Cryptography.X509Certificates ;
2024-12-19 17:36:57 +00:00
using System.Threading.Tasks ;
2024-12-23 09:12:26 +00:00
using domain.Models.ResponseModels ;
using presence.data.RemoteData.RemoteDataBase.DAO ;
using presence.data.Repository ;
using presence.domain.Models ;
2024-12-19 17:36:57 +00:00
2024-12-23 09:12:26 +00:00
namespace presence.domain.UseCase
2024-12-19 17:36:57 +00:00
{
public class PresenceUseCase
{
public readonly IUserRepository _userRepository ;
public readonly IPresenceRepository _presenceRepository ;
private readonly IGroupRepository _groupRepository ;
public PresenceUseCase ( IPresenceRepository repositoryImpl ,
IUserRepository userRepositoryImpl ,
IGroupRepository groupRepositoryImpl )
{
_presenceRepository = repositoryImpl ;
_userRepository = userRepositoryImpl ;
_groupRepository = groupRepositoryImpl ;
}
2024-12-23 09:12:26 +00:00
public List < PresenceResponse > GetPresence ( int GroupId , DateOnly startData , DateOnly endData , int UserId ) // Метод для получения присутствия пользователей в группе за определенный период времени
2024-12-19 17:36:57 +00:00
{
var users = _userRepository . GetAllUser ( ) . Where ( x = > x . GroupId = = GroupId ) . ToList ( ) ;
var presence = _presenceRepository . GetPresence ( GroupId , startData , endData , UserId )
. Where ( x = > users . Any ( user = > user . UserId = = x . UserId ) )
. Select ( presence = > new PresenceResponse
{
User = new UserResponse
{
Id = presence . UserId ,
Group = new GroupResponse { Id = GroupId , Name = _groupRepository . GetGroupById ( GroupId ) . Name } ,
FIO = users . First ( user = > user . UserId = = presence . UserId ) . FIO ,
} ,
ClassNumber = presence . ClassNumber ,
Date = presence . Date ,
IsAttendence = presence . IsAttendence
} ) . ToList ( ) ;
return presence ;
}
2024-12-23 09:12:26 +00:00
public List < PresenceResponse > GetPresenceByGroup ( int groupId ) // Метод для получения присутствия по группе
2024-12-19 17:36:57 +00:00
{
var users = _userRepository . GetAllUser ( ) . Where ( x = > x . GroupId = = groupId ) . ToList ( ) ;
var presenceByGroup = _presenceRepository . GetPresenceByGroup ( groupId )
. Where ( x = > users . Any ( user = > user . UserId = = x . UserId ) )
. Select ( presence = > new PresenceResponse
{
User = new UserResponse
{
Id = presence . UserId ,
Group = new GroupResponse { Id = groupId , Name = _groupRepository . GetGroupById ( groupId ) . Name } ,
FIO = users . First ( user = > user . UserId = = presence . UserId ) . FIO ,
} ,
ClassNumber = presence . ClassNumber ,
Date = presence . Date ,
IsAttendence = presence . IsAttendence
} ) . ToList ( ) ;
return presenceByGroup ;
}
2024-12-23 09:12:26 +00:00
public List < Presence > GetPresenceByGroupAndDate ( int groupId , DateOnly date ) // Метод для получения присутствия по группе и дате
2024-12-19 17:36:57 +00:00
{
var users = _userRepository . GetAllUser ( ) . Where ( x = > x . GroupId = = groupId ) . ToList ( ) ;
var presenceByGroupAndDate = _presenceRepository . GetPresenceByGroupAndDate ( groupId , date )
. Where ( x = > users . Any ( user = > user . UserId = = x . UserId & & x . Date = = date ) )
2024-12-23 09:12:26 +00:00
. Select ( presence = > new Presence
2024-12-19 17:36:57 +00:00
{
User = new User
{
Id = presence . UserId ,
GroupId = new Group { Id = groupId , Name = _groupRepository . GetGroupById ( groupId ) . Name } ,
FIO = users . First ( user = > user . UserId = = presence . UserId ) . FIO ,
} ,
ClassNumber = presence . ClassNumber ,
Date = presence . Date ,
IsAttendence = presence . IsAttendence
} ) . ToList ( ) ;
return presenceByGroupAndDate ;
}
2024-12-23 09:12:26 +00:00
public bool UncheckAttendence ( int firstClass , int lastClass , DateOnly date , int userId ) // Метод для отмены отметки о посещении
2024-12-19 17:36:57 +00:00
{
return _presenceRepository . UncheckAttendence ( firstClass , lastClass , date , userId ) ;
}
2024-12-23 09:12:26 +00:00
public void AddPresence ( int firstClass , int lastClass , int groupId , DateOnly date ) //Метод для добавления посещения
2024-12-19 17:36:57 +00:00
{
var users = _userRepository . GetAllUser ( ) . Where ( x = > x . GroupId = = groupId ) . ToList ( ) ;
List < Presence > presenceList = new List < Presence > ( ) ;
for ( int i = firstClass ; i < lastClass ; i + + )
{
foreach ( var user in users )
{
2024-12-23 09:12:26 +00:00
Presence pres = new Presence
2024-12-19 17:36:57 +00:00
{
ClassNumber = i ,
Date = date ,
User = new User
{
Id = user . UserId ,
FIO = user . FIO ,
GroupId = new Group
{
Id = groupId ,
Name = _groupRepository . GetGroupById ( groupId ) . Name
}
}
} ;
presenceList . Add ( pres ) ;
}
}
}
2024-12-23 09:12:26 +00:00
public List < PresenceResponse > AddPresenceByDate ( String startDate , String endDate , int groupId ) // Метод для добавления посещаемости на заданный диапазон дат
2024-12-19 17:36:57 +00:00
{
var users = _userRepository . GetAllUser ( ) . Where ( x = > x . GroupId = = groupId ) . ToList ( ) ;
List < PresenceResponse > presenceList = new List < PresenceResponse > ( ) ;
for ( var currentDate = DateOnly . Parse ( startDate ) ; currentDate < = DateOnly . Parse ( endDate ) ; currentDate = currentDate . AddDays ( 1 ) )
{
for ( int i = 1 ; i < 8 ; i + + )
{
if ( currentDate . DayOfWeek = = DayOfWeek . Saturday | |
currentDate . DayOfWeek = = DayOfWeek . Sunday )
{
continue ;
}
foreach ( var user in users )
{
var presenceDao = new PresenceDao
{
ClassNumber = i ,
Date = currentDate ,
UserId = user . UserId ,
User = user ,
GroupId = groupId
} ;
_presenceRepository . AddPresence ( presenceDao ) ;
}
}
}
return presenceList ;
}
2024-12-23 09:12:26 +00:00
public Dictionary < string , int > GetPresenceStatsByGroup ( int groupId ) // Метод для получения статистики посещаемости по группе
2024-12-19 17:36:57 +00:00
{
var stats = new Dictionary < string , int > ( ) ;
// Получаем всех студентов группы
var users = _userRepository . GetAllUser ( ) . Where ( x = > x . GroupId = = groupId ) . ToList ( ) ;
stats [ "Количество студентов" ] = users . Count ;
// Получаем все записи посещаемости для группы
var presences = _presenceRepository . GetPresenceByGroup ( groupId ) ;
// Считаем количество уникальных занятий
var uniqueLessons = presences
. Select ( p = > new { p . Date , p . ClassNumber } )
. Distinct ( )
. Count ( ) ;
stats [ "Количество занятий" ] = uniqueLessons ;
// Считаем общую посещаемость
var totalAttendances = presences . Count ( p = > p . IsAttendence ) ;
var totalPossibleAttendances = users . Count * uniqueLessons ;
if ( totalPossibleAttendances > 0 )
{
var attendancePercentage = ( totalAttendances * 100 ) / totalPossibleAttendances ;
stats [ "Процент посещаемости" ] = attendancePercentage ;
}
else
{
stats [ "Процент посещаемости" ] = 0 ;
}
return stats ;
}
2024-12-23 09:12:26 +00:00
public void GenerateWeeklyPresence ( int firstClass , int lastClass , int groupId , DateOnly startDate ) // Метод для генерации посещаемости на неделю для заданной группы
2024-12-19 17:36:57 +00:00
{
var users = _userRepository . GetAllUser ( ) . Where ( x = > x . GroupId = = groupId ) . ToList ( ) ;
for ( int day = 0 ; day < 7 ; day + + )
{
var currentDate = startDate . AddDays ( day ) ;
2024-12-23 09:12:26 +00:00
// Пропускаем субботу и воскресенье
2024-12-19 17:36:57 +00:00
if ( currentDate . DayOfWeek = = DayOfWeek . Saturday | |
currentDate . DayOfWeek = = DayOfWeek . Sunday )
{
continue ;
}
for ( int classNum = firstClass ; classNum < = lastClass ; classNum + + )
{
foreach ( var user in users )
{
var presence = new PresenceDao
{
ClassNumber = classNum ,
Date = currentDate ,
UserId = user . UserId ,
User = user ,
GroupId = groupId
} ;
_presenceRepository . AddPresence ( presence ) ;
}
}
}
}
2024-12-23 09:12:26 +00:00
public bool DeletePresenceByGroup ( int groupId ) // Метод для удаления всех записей посещаемости по идентификатору группы
2024-12-19 17:36:57 +00:00
{
return _presenceRepository . DeletePresenceByGroup ( groupId ) ;
}
2024-12-23 09:12:26 +00:00
public bool DeletePresenceByDate ( DateOnly startData , DateOnly endData ) // Метод для удаления посещения по дате
2024-12-19 17:36:57 +00:00
{
2024-12-23 09:12:26 +00:00
return _presenceRepository . DeletePresenceByDate ( startData , endData ) ;
2024-12-19 17:36:57 +00:00
}
2024-12-23 09:12:26 +00:00
public bool DeletePresenceByUser ( int UserId ) // Метод для удаления посещения по юзеру
2024-12-19 17:36:57 +00:00
{
2024-12-23 09:12:26 +00:00
return _presenceRepository . DeletePresenceByUser ( UserId ) ;
2024-12-19 17:36:57 +00:00
}
}
}