presence/Domain/UseCase/PresenceUseCase.cs

108 lines
4.4 KiB
C#
Raw Normal View History

2024-12-06 08:51:13 +00:00
using presence.Data.LocalData.Entity;
using presence.Data.ReportsHistory;
using presence.Data.Repository;
using presence.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace presence.Domain.UseCase
{
public class PresenceUseCase
{
private readonly IPresenceRepository _presenceRepositoryImpl;
private readonly IGroupRepository _groupRepositoryImpl;
private readonly IUserRepository _userRepositoryImpl;
public List<Presence> GetPresenceByGroup(int groupId)
{
var users = _userRepositoryImpl.GetAllUser().Where(x => x.GroupID == groupId).ToList();
var presenceByGroup = _presenceRepositoryImpl.GetPresenceByGroup(groupId)
.Where(x => users.Any(user => user.UserID == x.UserDaoUserId))
.Select(presence => new Presence
{
User = new User
{
UserID = presence.UserDaoUserId,
UserGroup = new Group
{
ID = groupId,
Name = _groupRepositoryImpl.GetAllGroup().First(group => group.ID == groupId).Name
},
UserFIO = users.First(user => user.UserID == presence.UserDaoUserId).UserFIO,
},
ClassNum = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance,
}).ToList();
return (List<Presence>)presenceByGroup;
}
public List<Presence> GetPresenceByGroupAndDate(int groupId, DateOnly date)
{
var users = _userRepositoryImpl.GetAllUser().Where(x => x.GroupID == groupId).ToList();
var presenceByGroup = _presenceRepositoryImpl.GetPresenceByGroup(groupId)
.Where(x => users.Any(user => user.UserID == x.UserDaoUserId && x.Date == date))
.Select(presence => new Presence
{
User = new User
{
UserID = presence.UserDaoUserId,
UserGroup = new Group
{
ID = groupId,
Name = _groupRepositoryImpl.GetAllGroup().First(group => group.ID == groupId).Name
},
UserFIO = users.First(user => user.UserID == presence.UserDaoUserId).UserFIO,
},
ClassNum = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance,
}).ToList();
return presenceByGroup;
}
public bool UnCheckAttendance(int firstClass, int lastClass, DateOnly date, int userID)
{
return _presenceRepositoryImpl.UnCheckAttendence(firstClass, lastClass, date, userID);
}
public Dictionary<string, int> GetPresenceStatsByGroup(int groupId)
{
var stats = new Dictionary<string, int>();
// Получаем всех студентов группы
var users = _userRepositoryImpl.GetAllUser().Where(x => x.GroupID == groupId).ToList();
stats["Количество студентов"] = users.Count;
// Получаем все записи посещаемости для группы
var presences = _presenceRepositoryImpl.GetPresenceByGroup(groupId);
// Считаем количество уникальных занятий
var uniqueLessons = presences
.Select(p => new { p.Date, p.LessonNumber })
.Distinct()
.Count();
stats["Количество занятий"] = uniqueLessons;
// Считаем общую посещаемость
var totalAttendances = presences.Count(p => p.IsAttedance);
var totalPossibleAttendances = users.Count * uniqueLessons;
if (totalPossibleAttendances > 0)
{
var attendancePercentage = (totalAttendances * 100) / totalPossibleAttendances;
stats["Процент посещаемости"] = attendancePercentage;
}
else
{
stats["Процент посещаемости"] = 0;
}
return stats;
}
}
}