presence/data/Repository/SQLPresenceRepositoryImpl.cs

218 lines
8.2 KiB
C#
Raw Permalink Normal View History

2024-11-11 09:07:11 +00:00
using data.RemoteData.RemoteDataBase;
using data.RemoteData.RemoteDataBase.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using domain.Models;
using Microsoft.EntityFrameworkCore;
namespace data.Repository
{
public class SQLPresenceRepositoryImpl : IPresenceRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLPresenceRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public void SavePresence(List<PresenceLocalEntity> presences)
{
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;
}
}
_remoteDatabaseContext.SaveChanges();
}
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)
{
return _remoteDatabaseContext.PresenceDaos.Include(user => user.UserDao)
2024-11-13 09:00:26 +00:00
.Where(p => p.UserDao != null && p.UserDao.GroupID == groupId) // Проверяем на null
2024-11-11 09:07:11 +00:00
.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();
}
public void MarkUserAsAbsent(Guid userGuid, int firstLessonNumber, int lastLessonNumber)
{
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)
{
2024-11-13 09:00:26 +00:00
presence.IsAttedance = false;
2024-11-11 09:07:11 +00:00
}
}
}
public DateOnly? GetLastDateByGroupId(int groupId)
{
2024-11-13 09:00:26 +00:00
// Проверим наличие записей о посещаемости в бд
2024-11-11 09:07:11 +00:00
var lastDate = _remoteDatabaseContext.PresenceDaos
.Where(p => p.UserDao.GroupID == groupId)
.OrderByDescending(p => p.Date)
.Select(p => p.Date)
.FirstOrDefault();
return lastDate == default ? (DateOnly?)null : lastDate;
}
public GroupPresenceSummary GetGeneralPresenceForGroup(int groupId)
{
var presences = _remoteDatabaseContext.PresenceDaos
.Where(p => p.UserDao.GroupID == groupId)
.OrderBy(p => p.Date).ThenBy(p => p.LessonNumber)
.ToList();
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
var distinctLessonDates = presences
.Select(p => new { p.Date, p.LessonNumber })
.Distinct()
.ToList();
int lessonCount = distinctLessonDates.Count;
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
var userGuids = presences
.Select(p => p.UserGuid)
.Distinct()
.ToHashSet();
double totalAttendance = presences.Count(p => p.IsAttedance);
double totalPossibleAttendance = userGuids.Count * lessonCount;
var userAttendances = userGuids.Select(userGuid =>
{
var userPresences = presences.Where(p => p.UserGuid == userGuid).ToList();
double attended = userPresences.Count(p => p.IsAttedance);
double missed = userPresences.Count(p => !p.IsAttedance);
return new UserAttendance
{
UserGuid = userGuid,
Attended = attended,
Missed = missed,
AttendanceRate = (attended / (attended + missed)) * 100
};
}).ToList();
2024-11-13 09:00:26 +00:00
// Рассчитываем процент посещаемости в заданной группе
2024-11-11 09:07:11 +00:00
double totalAttendancePercentage = (totalAttendance / totalPossibleAttendance) * 100;
return new GroupPresenceSummary
{
UserCount = userGuids.Count,
LessonCount = lessonCount,
TotalAttendancePercentage = totalAttendancePercentage,
UserAttendances = userAttendances
};
}
2024-11-13 09:00:26 +00:00
public bool UpdateAttention(Guid UserGuid, int groupId, int firstLesson, int lastLesson, DateOnly date, bool isAttendance)
2024-11-11 09:07:11 +00:00
{
var presences = _remoteDatabaseContext.PresenceDaos
.Where(p => p.UserGuid == UserGuid && p.UserDao.GroupID == groupId &&
p.LessonNumber >= firstLesson && p.LessonNumber <= lastLesson && p.Date == date)
.ToList();
if (presences.Any())
{
foreach (var presence in presences)
{
presence.IsAttedance = isAttendance;
}
_remoteDatabaseContext.SaveChanges();
2024-11-13 09:00:26 +00:00
return true;
2024-11-11 09:07:11 +00:00
}
2024-11-13 09:00:26 +00:00
return false; // анлак
2024-11-11 09:07:11 +00:00
}
public List<PresenceDao> GetAttendanceByGroup(int groupId)
{
2024-11-13 09:00:26 +00:00
2024-11-11 09:07:11 +00:00
var userGuidsInGroup = _remoteDatabaseContext.Users
.Where(u => u.GroupID == groupId)
.Select(u => u.Guid)
.ToList();
// Фильтруем посещаемость по пользователям из этой группы
return _remoteDatabaseContext.PresenceDaos
.Where(p => userGuidsInGroup.Contains(p.UserGuid))
.Select(p => new PresenceDao
{
UserGuid = p.UserGuid,
Id = p.Id,
Date = p.Date,
LessonNumber = p.LessonNumber,
IsAttedance = p.IsAttedance
})
.ToList();
}
}
}