presence/data/Repository/SQLPresenceRepositoryImpl.cs
2024-11-13 12:00:26 +03:00

218 lines
8.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
.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();
}
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)
{
presence.IsAttedance = false;
}
}
}
public DateOnly? GetLastDateByGroupId(int groupId)
{
// Проверим наличие записей о посещаемости в бд
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();
var distinctLessonDates = presences
.Select(p => new { p.Date, p.LessonNumber })
.Distinct()
.ToList();
int lessonCount = distinctLessonDates.Count;
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();
// Рассчитываем процент посещаемости в заданной группе
double totalAttendancePercentage = (totalAttendance / totalPossibleAttendance) * 100;
return new GroupPresenceSummary
{
UserCount = userGuids.Count,
LessonCount = lessonCount,
TotalAttendancePercentage = totalAttendancePercentage,
UserAttendances = userAttendances
};
}
public bool UpdateAttention(Guid UserGuid, int groupId, int firstLesson, int lastLesson, DateOnly date, bool isAttendance)
{
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();
return true;
}
return false; // анлак
}
public List<PresenceDao> GetAttendanceByGroup(int groupId)
{
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();
}
}
}