277 lines
9.4 KiB
C#
277 lines
9.4 KiB
C#
|
using Demo.Data.LocalData;
|
|||
|
using Demo.Data.RemoteData.RemoteDataBase;
|
|||
|
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
|||
|
using Demo.domain.Models;
|
|||
|
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> 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();
|
|||
|
|
|||
|
if (presences.Any())
|
|||
|
{
|
|||
|
// Обновляем значение IsAttendance для всех найденных записей
|
|||
|
foreach (var presence in presences)
|
|||
|
{
|
|||
|
presence.IsAttedance = isAttendance;
|
|||
|
}
|
|||
|
|
|||
|
_remoteDatabaseContext.SaveChanges(); // Сохраняем изменения в базе данных
|
|||
|
Console.WriteLine($"Статус посещаемости для пользователя {userId} с {firstLesson} по {lastLesson} урок обновлён.");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Console.WriteLine($"Посещаемость для пользователя ID: {userId} на дату {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 presences = _remoteDatabaseContext.PresenceDaos.Where(p=>p.GroupId==groupId).OrderBy(p=>p.LessonNumber).ToList();
|
|||
|
int lesId = 0;
|
|||
|
int lesNum = 1;
|
|||
|
double att = 0;
|
|||
|
int countAllLes = 0;
|
|||
|
DateOnly date= DateOnly.MinValue;
|
|||
|
List<int> usersId = new List<int>();
|
|||
|
|
|||
|
foreach (var presence in presences)
|
|||
|
{
|
|||
|
if (!usersId.Contains(presence.UserId))
|
|||
|
{
|
|||
|
usersId.Add(presence.UserId);
|
|||
|
}
|
|||
|
if (presence.Date != date)
|
|||
|
{
|
|||
|
date = presence.Date;
|
|||
|
lesId++;
|
|||
|
lesNum = presence.LessonNumber;
|
|||
|
}
|
|||
|
if (presence.LessonNumber != lesNum && date == presence.Date)
|
|||
|
{
|
|||
|
lesNum = presence.LessonNumber;
|
|||
|
countAllLes++;
|
|||
|
lesId++;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
if (presence.IsAttedance)
|
|||
|
{
|
|||
|
att++;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
Console.WriteLine($"Человек в группе: {usersId.Count()}, " +
|
|||
|
$"Количество проведённых занятий: {lesId}, " +
|
|||
|
$"Общий процент посещаемости группы: {att/usersId.Count()/lesNum*100}%");
|
|||
|
List<Temp> a = new List<Temp>();
|
|||
|
List<int> ids = new List<int>();
|
|||
|
double ok = 0;
|
|||
|
double skip = 0;
|
|||
|
int userId = 0;
|
|||
|
foreach (var user in usersId)
|
|||
|
{
|
|||
|
var users = _remoteDatabaseContext.PresenceDaos.Where(p => p.UserId == user);
|
|||
|
foreach (var usera in users)
|
|||
|
{
|
|||
|
userId = usera.UserId;
|
|||
|
if (!ids.Contains(usera.UserId))
|
|||
|
{
|
|||
|
skip = 0;
|
|||
|
ok = 0;
|
|||
|
ids.Add(userId);
|
|||
|
a.Add(new Temp { UserId = userId, ok = ok, skip = skip });
|
|||
|
userId = usera.UserId;
|
|||
|
if (usera.IsAttedance)
|
|||
|
{
|
|||
|
a.First(a => a.UserId == usera.UserId).ok = ok += 1;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
a.First(a => a.UserId == usera.UserId).skip = skip += 1;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (usera.IsAttedance)
|
|||
|
{
|
|||
|
a.First(a => a.UserId == usera.UserId).ok=ok+=1;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
a.First(a => a.UserId == usera.UserId).skip=skip+=1;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
foreach(var user in a)
|
|||
|
{
|
|||
|
if(user.ok / (user.skip + user.ok) * 100 < 40)
|
|||
|
{
|
|||
|
Console.ForegroundColor = ConsoleColor.Red;
|
|||
|
}
|
|||
|
Console.WriteLine($"ID Пользователя: {user.UserId}, " +
|
|||
|
$"Посетил: {user.ok}, " +
|
|||
|
$"Пропустил: {user.skip}, " +
|
|||
|
$"Процент посещаемости: {user.ok/(user.skip+user.ok)*100}%");
|
|||
|
Console.ForegroundColor= ConsoleColor.White;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//double skip=0;
|
|||
|
//double ok=0;
|
|||
|
//List<Temp> users = new List<Temp>();
|
|||
|
//foreach (var presence in presences)
|
|||
|
//{
|
|||
|
// int id = presence.UserId;
|
|||
|
// var attendance=_remoteDatabaseContext.PresenceDaos.Where(p=>p.UserId==id).ToList();
|
|||
|
// int userid = 0;
|
|||
|
// int lessons=0;
|
|||
|
// foreach (var user in attendance)
|
|||
|
// {
|
|||
|
// skip = 0;
|
|||
|
// ok = 0;
|
|||
|
// lessons = 0;
|
|||
|
// if (user.IsAttedance)
|
|||
|
// {
|
|||
|
// ok++;
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// skip++;
|
|||
|
// }
|
|||
|
// lessons++;
|
|||
|
// userid=user.UserId;
|
|||
|
// }
|
|||
|
// if (ok != 0)
|
|||
|
// {
|
|||
|
// Console.WriteLine($"{userid} Посетил: {ok}, Пропустил: {skip}, Процент посещаемости: {lessons / ok * 100}%");
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// Console.WriteLine($"{userid} Посетил: {ok}, Пропустил: {skip}, Процент посещаемости: 0%");
|
|||
|
// }
|
|||
|
// lessons = 0;
|
|||
|
//}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|