2024-11-08 16:52:52 +00:00
|
|
|
|
using Demo.Data.Exceptions;
|
|
|
|
|
using Demo.Data.LocalData;
|
|
|
|
|
using Demo.Data.RemoteData.RemoteDataBase;
|
|
|
|
|
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
|
|
|
|
using Demo.domain.Models;
|
|
|
|
|
using DocumentFormat.OpenXml.InkML;
|
|
|
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
UserId = p.UserId,
|
|
|
|
|
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.UserId == p.UserId)).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Реализация метода для получения всех данных по группе
|
|
|
|
|
public List<PresenceDao> GetPresenceByGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
return _remoteDatabaseContext.PresenceDaos.Where(p => p.GroupId == groupId)
|
|
|
|
|
.OrderBy(p => p.Date)
|
|
|
|
|
.ThenBy(p=>p.UserId).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SavePresence(List<PresenceDao> presences)
|
|
|
|
|
{
|
|
|
|
|
_remoteDatabaseContext.PresenceDaos.AddRange(presences.Select(it => new PresenceDao
|
|
|
|
|
{
|
|
|
|
|
Date = it.Date,
|
|
|
|
|
IsAttedance = it.IsAttedance,
|
|
|
|
|
LessonNumber = it.LessonNumber,
|
|
|
|
|
UserId = it.UserId,
|
|
|
|
|
GroupId = it.GroupId
|
|
|
|
|
}));
|
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateAtt(int userId, int groupId, int firstLesson, int lastLesson, DateOnly date, bool isAttendance)
|
|
|
|
|
{
|
|
|
|
|
// Находим все записи по UserId, GroupId, LessonNumber (в диапазоне) и дате
|
|
|
|
|
var presences = _remoteDatabaseContext.PresenceDaos
|
|
|
|
|
.Where(p => p.UserId == userId
|
|
|
|
|
&& p.GroupId == groupId
|
|
|
|
|
&& p.LessonNumber >= firstLesson
|
|
|
|
|
&& p.LessonNumber <= lastLesson
|
|
|
|
|
&& p.Date == date)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
// Обновляем значение IsAttendance для всех найденных записей
|
|
|
|
|
foreach (var presence in presences)
|
|
|
|
|
{
|
|
|
|
|
presence.IsAttedance = isAttendance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_remoteDatabaseContext.SaveChanges(); // Сохраняем изменения в базе данных
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 GroupAttendanceStatistics GetGeneralPresenceForGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
var presences = _remoteDatabaseContext.PresenceDaos.Where(p => p.GroupId == groupId).OrderBy(p => p.LessonNumber).ToList();
|
2024-11-14 07:53:42 +00:00
|
|
|
|
if (presences.Count != 0)
|
2024-11-08 16:52:52 +00:00
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
var dates = _remoteDatabaseContext.PresenceDaos;
|
|
|
|
|
var distDates = dates.Select(p => p.Date).Distinct().ToList();
|
|
|
|
|
int lesId = 0;
|
|
|
|
|
int lesNum = 1;
|
|
|
|
|
double att = 0;
|
|
|
|
|
int days = -1;
|
|
|
|
|
int countAllLes = 0;
|
|
|
|
|
DateOnly date = DateOnly.MinValue;
|
|
|
|
|
List<int> usersId = new List<int>();
|
|
|
|
|
|
|
|
|
|
foreach (var presence in presences)
|
2024-11-08 16:52:52 +00:00
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
if (!usersId.Contains(presence.UserId))
|
|
|
|
|
{
|
|
|
|
|
usersId.Add(presence.UserId);
|
|
|
|
|
}
|
|
|
|
|
if (presence.Date != date)
|
|
|
|
|
{
|
|
|
|
|
date = presence.Date;
|
|
|
|
|
lesId++;
|
|
|
|
|
lesNum = presence.LessonNumber;
|
|
|
|
|
days++;
|
|
|
|
|
}
|
|
|
|
|
if (presence.LessonNumber != lesNum && date == presence.Date)
|
|
|
|
|
{
|
|
|
|
|
lesNum = presence.LessonNumber;
|
|
|
|
|
countAllLes++;
|
|
|
|
|
lesId++;
|
|
|
|
|
}
|
2024-11-08 16:52:52 +00:00
|
|
|
|
|
|
|
|
|
|
2024-11-14 07:53:42 +00:00
|
|
|
|
if (presence.IsAttedance)
|
|
|
|
|
{
|
|
|
|
|
att++;
|
|
|
|
|
}
|
2024-11-08 16:52:52 +00:00
|
|
|
|
|
2024-11-14 07:53:42 +00:00
|
|
|
|
}
|
|
|
|
|
List<UserAttendance> a = new List<UserAttendance>();
|
|
|
|
|
List<int> ids = new List<int>();
|
|
|
|
|
double ok = 0;
|
|
|
|
|
double skip = 0;
|
|
|
|
|
int userId = 0;
|
|
|
|
|
foreach (var user in usersId)
|
2024-11-08 16:52:52 +00:00
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
var users = _remoteDatabaseContext.PresenceDaos.Where(p => p.UserId == user);
|
|
|
|
|
foreach (var usera in users)
|
2024-11-08 16:52:52 +00:00
|
|
|
|
{
|
|
|
|
|
userId = usera.UserId;
|
2024-11-14 07:53:42 +00:00
|
|
|
|
if (!ids.Contains(usera.UserId))
|
2024-11-08 16:52:52 +00:00
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
skip = 0;
|
|
|
|
|
ok = 0;
|
|
|
|
|
ids.Add(userId);
|
|
|
|
|
a.Add(new UserAttendance { UserId = userId, Attended = ok, Missed = skip });
|
|
|
|
|
userId = usera.UserId;
|
|
|
|
|
if (usera.IsAttedance)
|
|
|
|
|
{
|
|
|
|
|
a.First(a => a.UserId == usera.UserId).Attended = ok += 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
a.First(a => a.UserId == usera.UserId).Missed = skip += 1;
|
|
|
|
|
}
|
2024-11-08 16:52:52 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
if (usera.IsAttedance)
|
|
|
|
|
{
|
|
|
|
|
a.First(a => a.UserId == usera.UserId).Attended = ok += 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
a.First(a => a.UserId == usera.UserId).Missed = skip += 1;
|
|
|
|
|
}
|
2024-11-08 16:52:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 07:53:42 +00:00
|
|
|
|
var statistics = new GroupAttendanceStatistics
|
|
|
|
|
{
|
|
|
|
|
UserCount = usersId.Count,
|
|
|
|
|
TotalLessons = (int)(a[0].Attended + a[0].Missed),
|
|
|
|
|
AttendancePercentage = Math.Round(att / usersId.Count / lesNum / distDates.Count() * 100, 2)
|
|
|
|
|
};
|
2024-11-08 16:52:52 +00:00
|
|
|
|
|
2024-11-14 07:53:42 +00:00
|
|
|
|
foreach (var user in a)
|
2024-11-08 16:52:52 +00:00
|
|
|
|
{
|
2024-11-14 07:53:42 +00:00
|
|
|
|
statistics.UserAttendanceDetails.Add(new UserAttendance
|
|
|
|
|
{
|
|
|
|
|
UserId = user.UserId,
|
|
|
|
|
Attended = user.Attended,
|
|
|
|
|
Missed = user.Missed,
|
|
|
|
|
AttendanceRate = Math.Round(user.Attended / (user.Missed + user.Attended) * 100, 2)
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-11-08 16:52:52 +00:00
|
|
|
|
|
2024-11-14 07:53:42 +00:00
|
|
|
|
return statistics;
|
|
|
|
|
}
|
|
|
|
|
var statistic = new GroupAttendanceStatistics
|
|
|
|
|
{
|
|
|
|
|
UserCount = 0,
|
|
|
|
|
TotalLessons = 0,
|
|
|
|
|
AttendancePercentage = 0
|
|
|
|
|
};
|
|
|
|
|
return statistic;
|
2024-11-08 16:52:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|