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)
|
|
|
|
|
{
|
2024-11-08 06:28:46 +00:00
|
|
|
|
foreach (var presence in presences)
|
|
|
|
|
{
|
|
|
|
|
// Проверяем, существует ли запись с указанными датой, пользователем и номером занятия
|
|
|
|
|
var existing = _remoteDatabaseContext.PresenceDaos.FirstOrDefault(p =>
|
|
|
|
|
p.Date == DateOnly.FromDateTime(presence.Date) &&
|
|
|
|
|
p.UserGuid == presence.UserGuid &&
|
|
|
|
|
p.LessonNumber == presence.LessonNumber);
|
|
|
|
|
|
|
|
|
|
if (existing == null)
|
|
|
|
|
{
|
|
|
|
|
// Добавляем запись, если её не существует
|
|
|
|
|
_remoteDatabaseContext.PresenceDaos.Add(new PresenceDao
|
|
|
|
|
{
|
|
|
|
|
Date = DateOnly.FromDateTime(presence.Date),
|
|
|
|
|
IsAttedance = presence.IsAttedance,
|
|
|
|
|
LessonNumber = presence.LessonNumber,
|
|
|
|
|
UserGuid = presence.UserGuid
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Обновляем запись, если она уже существует
|
|
|
|
|
existing.IsAttedance = presence.IsAttedance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Сохраняем все изменения в базе данных
|
2024-11-01 07:40:53 +00:00
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
|
}
|
2024-11-08 06:28:46 +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-07 11:46:50 +00:00
|
|
|
|
.OrderBy(p => p.Date).ThenBy(p => p.LessonNumber)
|
2024-11-05 18:36:49 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2024-11-07 11:46:50 +00:00
|
|
|
|
// Уникальные пары "дата + номер занятия"
|
|
|
|
|
var distinctLessonDates = presences
|
|
|
|
|
.Select(p => new { p.Date, p.LessonNumber })
|
2024-11-05 18:36:49 +00:00
|
|
|
|
.Distinct()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2024-11-07 11:46:50 +00:00
|
|
|
|
int lessonCount = distinctLessonDates.Count;
|
2024-11-05 18:36:49 +00:00
|
|
|
|
|
2024-11-07 11:46:50 +00:00
|
|
|
|
// Считаем количество уникальных пользователей в группе
|
|
|
|
|
var userGuids = presences
|
|
|
|
|
.Select(p => p.UserGuid)
|
|
|
|
|
.Distinct()
|
|
|
|
|
.ToHashSet();
|
2024-11-05 18:36:49 +00:00
|
|
|
|
|
2024-11-07 11:46:50 +00:00
|
|
|
|
// Подсчитываем общее количество посещений и общее количество возможных посещений
|
|
|
|
|
double totalAttendance = presences.Count(p => p.IsAttedance);
|
|
|
|
|
double totalPossibleAttendance = userGuids.Count * lessonCount;
|
2024-11-05 18:36:49 +00:00
|
|
|
|
|
2024-11-06 09:07:50 +00:00
|
|
|
|
var userAttendances = userGuids.Select(userGuid =>
|
2024-11-05 18:36:49 +00:00
|
|
|
|
{
|
2024-11-07 11:46:50 +00:00
|
|
|
|
var userPresences = presences.Where(p => p.UserGuid == userGuid).ToList();
|
2024-11-05 18:36:49 +00:00
|
|
|
|
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,
|
2024-11-07 11:46:50 +00:00
|
|
|
|
AttendanceRate = (attended / (attended + missed)) * 100
|
2024-11-06 09:07:50 +00:00
|
|
|
|
};
|
|
|
|
|
}).ToList();
|
2024-11-05 18:36:49 +00:00
|
|
|
|
|
2024-11-07 11:46:50 +00:00
|
|
|
|
// Рассчитываем общий процент посещаемости группы
|
|
|
|
|
double totalAttendancePercentage = (totalAttendance / totalPossibleAttendance) * 100;
|
|
|
|
|
|
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,
|
2024-11-07 11:46:50 +00:00
|
|
|
|
TotalAttendancePercentage = totalAttendancePercentage,
|
2024-11-06 09:07:50 +00:00
|
|
|
|
UserAttendances = userAttendances
|
|
|
|
|
};
|
2024-11-05 18:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-06 09:07:50 +00:00
|
|
|
|
|
2024-11-07 11:46:50 +00:00
|
|
|
|
|
2024-11-08 07:56:52 +00:00
|
|
|
|
public bool UpdateAtt(Guid UserGuid, int groupId, int firstLesson, int lastLesson, DateOnly date, bool isAttendance)
|
2024-11-05 18:36:49 +00:00
|
|
|
|
{
|
|
|
|
|
var presences = _remoteDatabaseContext.PresenceDaos
|
2024-11-08 07:56:52 +00:00
|
|
|
|
.Where(p => p.UserGuid == UserGuid && p.UserDao.GroupID == groupId &&
|
|
|
|
|
p.LessonNumber >= firstLesson && p.LessonNumber <= lastLesson && p.Date == date)
|
2024-11-05 18:36:49 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (presences.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var presence in presences)
|
|
|
|
|
{
|
|
|
|
|
presence.IsAttedance = isAttendance;
|
|
|
|
|
}
|
2024-11-08 07:56:52 +00:00
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
|
return true; // Успех
|
2024-11-05 18:36:49 +00:00
|
|
|
|
}
|
2024-11-08 07:56:52 +00:00
|
|
|
|
return false; // Данные не найдены
|
2024-11-05 18:36:49 +00:00
|
|
|
|
}
|
2024-11-07 07:35:57 +00:00
|
|
|
|
public List<PresenceDao> GetAttendanceByGroup(int groupId)
|
|
|
|
|
{
|
2024-11-07 11:46:50 +00:00
|
|
|
|
// Получаем пользователей указанной группы
|
|
|
|
|
var userGuidsInGroup = _remoteDatabaseContext.Users
|
|
|
|
|
.Where(u => u.GroupID == groupId)
|
|
|
|
|
.Select(u => u.Guid)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
// Фильтруем посещаемость по пользователям из этой группы
|
2024-11-07 07:35:57 +00:00
|
|
|
|
return _remoteDatabaseContext.PresenceDaos
|
2024-11-07 11:46:50 +00:00
|
|
|
|
.Where(p => userGuidsInGroup.Contains(p.UserGuid))
|
2024-11-07 07:35:57 +00:00
|
|
|
|
.Select(p => new PresenceDao
|
|
|
|
|
{
|
|
|
|
|
UserGuid = p.UserGuid,
|
|
|
|
|
Id = p.Id,
|
|
|
|
|
Date = p.Date,
|
|
|
|
|
LessonNumber = p.LessonNumber,
|
|
|
|
|
IsAttedance = p.IsAttedance
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 11:46:50 +00:00
|
|
|
|
|
2024-11-01 07:40:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|