251 lines
10 KiB
C#
251 lines
10 KiB
C#
using Demo.Data.LocalData;
|
||
using Demo.Data.RemoteData.RemoteDataBase;
|
||
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
||
using Demo.domain.Models;
|
||
using DocumentFormat.OpenXml.InkML;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Linq;
|
||
using System.Runtime.CompilerServices;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||
|
||
namespace Demo.Data.Repository
|
||
{
|
||
public class SQLPresenceRepositoryImpl : IPresenceRepository
|
||
{
|
||
private readonly RemoteDatabaseContext _remoteDatabaseContext;
|
||
|
||
public SQLPresenceRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
|
||
{
|
||
_remoteDatabaseContext = remoteDatabaseContext;
|
||
}
|
||
|
||
|
||
public List<PresenceDao> GetAttendanceByGroup(int groupId)
|
||
{
|
||
// Получаем записи посещаемости для указанной группы
|
||
return _remoteDatabaseContext.PresenceDaos
|
||
.Where(p => p.GroupId == groupId)
|
||
.Select(p => new PresenceDao
|
||
{
|
||
UserGuid = p.UserGuid,
|
||
GroupId = p.GroupId,
|
||
Date = p.Date,
|
||
LessonNumber = p.LessonNumber,
|
||
IsAttedance = p.IsAttedance
|
||
})
|
||
.ToList();
|
||
}
|
||
|
||
|
||
public List<PresenceDao> GetPresenceForAbsent(DateTime date, int GroupId)
|
||
{
|
||
return _remoteDatabaseContext.PresenceDaos.Where(p => p.GroupId == GroupId && p.Date == DateOnly.FromDateTime(date)).ToList();
|
||
}
|
||
public List<PresenceDao> GetPresenceByDateAndGroup(DateTime date, int groupId)
|
||
{
|
||
return _remoteDatabaseContext.PresenceDaos.Where(p => p.Date == DateOnly.FromDateTime(date) &&
|
||
_remoteDatabaseContext.Users.Any(u => u.GroupId == groupId && u.UserGuid == p.UserGuid)).ToList();
|
||
}
|
||
|
||
// Реализация метода для получения всех данных по группе
|
||
public List<PresenceDao> GetPresenceByGroup(int groupId)
|
||
{
|
||
return _remoteDatabaseContext.PresenceDaos.Where(p => p.GroupId == groupId)
|
||
.OrderBy(p => p.Date)
|
||
.ThenBy(p=>p.UserGuid).ToList();
|
||
}
|
||
|
||
public void SavePresence(List<PresenceDao> presences)
|
||
{
|
||
_remoteDatabaseContext.PresenceDaos.AddRange(presences.Select(it => new PresenceDao
|
||
{
|
||
Date = it.Date,
|
||
IsAttedance = it.IsAttedance,
|
||
LessonNumber = it.LessonNumber,
|
||
UserGuid = it.UserGuid,
|
||
GroupId = it.GroupId
|
||
}));
|
||
_remoteDatabaseContext.SaveChanges();
|
||
}
|
||
|
||
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
|
||
&& p.GroupId == groupId
|
||
&& 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} уроки не найдена.");
|
||
}
|
||
}
|
||
|
||
public DateOnly? GetLastDateByGroupId(int groupId)
|
||
{
|
||
// Проверяем наличие записей о посещаемости в базе данных для данной группы.
|
||
var lastDate = _remoteDatabaseContext.PresenceDaos
|
||
.Where(p => p.GroupId == groupId)
|
||
.OrderByDescending(p => p.Date)
|
||
.Select(p => p.Date)
|
||
.FirstOrDefault();
|
||
|
||
return lastDate == default ? (DateOnly?)null : lastDate;
|
||
}
|
||
|
||
public List<PresenceDao> PresenceSort(List<PresenceDao> presences)
|
||
{
|
||
presences=_remoteDatabaseContext.PresenceDaos.OrderBy(p=>p.Date).ToList();
|
||
return presences;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
public void GetGeneralPresenceForGroup(int groupId)
|
||
{
|
||
var attendanceRecords = _remoteDatabaseContext.PresenceDaos
|
||
.Where(p => p.GroupId == groupId)
|
||
.OrderBy(p => p.LessonNumber)
|
||
.ToList();
|
||
|
||
var uniqueDates = _remoteDatabaseContext.PresenceDaos
|
||
.Select(p => p.Date)
|
||
.Distinct()
|
||
.ToList();
|
||
|
||
int sessionId = 0;
|
||
int lessonNum = 1;
|
||
double attendedSessions = 0;
|
||
int totalDays = -1;
|
||
int totalLessons = 0;
|
||
DateOnly currentDate = DateOnly.MinValue;
|
||
List<Guid> uniqueUserGuids = new List<Guid>();
|
||
|
||
foreach (var record in attendanceRecords)
|
||
{
|
||
if (!uniqueUserGuids.Contains(record.UserGuid))
|
||
{
|
||
uniqueUserGuids.Add(record.UserGuid);
|
||
}
|
||
|
||
if (record.Date != currentDate)
|
||
{
|
||
currentDate = record.Date;
|
||
sessionId++;
|
||
lessonNum = record.LessonNumber;
|
||
totalDays++;
|
||
}
|
||
|
||
if (record.LessonNumber != lessonNum && currentDate == record.Date)
|
||
{
|
||
lessonNum = record.LessonNumber;
|
||
totalLessons++;
|
||
sessionId++;
|
||
}
|
||
|
||
if (record.IsAttedance)
|
||
{
|
||
attendedSessions++;
|
||
}
|
||
}
|
||
|
||
Console.WriteLine(attendanceRecords.Count);
|
||
Console.WriteLine($"Количество студентов в группе: {uniqueUserGuids.Count}, " +
|
||
$"Количество проведённых занятий: {sessionId}, " +
|
||
$"Общий процент посещаемости группы: {attendedSessions / uniqueUserGuids.Count / lessonNum / uniqueDates.Count * 100}%");
|
||
|
||
List<AttendanceSummary> attendanceSummaryList = new List<AttendanceSummary>();
|
||
List<Guid> processedUserGuids = new List<Guid>();
|
||
double attendedCount = 0;
|
||
double missedCount = 0;
|
||
Guid currentUserGuid = Guid.Empty;
|
||
|
||
foreach (var userGuid in uniqueUserGuids)
|
||
{
|
||
var userAttendanceRecords = _remoteDatabaseContext.PresenceDaos
|
||
.Where(p => p.UserGuid == userGuid);
|
||
|
||
foreach (var record in userAttendanceRecords)
|
||
{
|
||
currentUserGuid = record.UserGuid;
|
||
|
||
if (!processedUserGuids.Contains(currentUserGuid))
|
||
{
|
||
missedCount = 0;
|
||
attendedCount = 0;
|
||
processedUserGuids.Add(currentUserGuid);
|
||
attendanceSummaryList.Add(new AttendanceSummary { UserGuid = currentUserGuid, Attended = attendedCount, Missed = missedCount });
|
||
|
||
if (record.IsAttedance)
|
||
{
|
||
attendanceSummaryList.First(a => a.UserGuid == currentUserGuid).Attended = attendedCount += 1;
|
||
}
|
||
else
|
||
{
|
||
attendanceSummaryList.First(a => a.UserGuid == currentUserGuid).Missed = missedCount += 1;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (record.IsAttedance)
|
||
{
|
||
attendanceSummaryList.First(a => a.UserGuid == currentUserGuid).Attended = attendedCount += 1;
|
||
}
|
||
else
|
||
{
|
||
attendanceSummaryList.First(a => a.UserGuid == currentUserGuid).Missed = missedCount += 1;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
foreach (var summary in attendanceSummaryList)
|
||
{
|
||
double attendancePercentage = summary.Attended / (summary.Missed + summary.Attended) * 100;
|
||
|
||
if (attendancePercentage < 40)
|
||
{
|
||
Console.ForegroundColor = ConsoleColor.Red;
|
||
}
|
||
|
||
Console.WriteLine($"ID Пользователя: {summary.UserGuid}, " +
|
||
$"Посетил: {summary.Attended}, " +
|
||
$"Пропустил: {summary.Missed}, " +
|
||
$"Процент посещаемости: {attendancePercentage}%");
|
||
Console.ForegroundColor = ConsoleColor.White;
|
||
}
|
||
}
|
||
|
||
|
||
public class AttendanceSummary
|
||
{
|
||
public Guid UserGuid { get; set; }
|
||
public double Attended { get; set; }
|
||
public double Missed { get; set; }
|
||
}
|
||
}
|
||
}
|