presence/domain/UseCase/PresenceUseCase.cs
2024-11-11 14:49:39 +03:00

246 lines
10 KiB
C#

using System.Security.Cryptography.X509Certificates;
using Demo.Data.Repository;
using Demo.Domain.Models;
namespace Demo.Domain.UseCase
{
public class PresenceUseCase : IPresenceUseCase
{
private readonly IUserRepository _repositoryUserImpl;
private readonly IPresenceRepository _repositoryPresenceImpl;
private readonly IGroupRepository _repositoryGroupImpl;
public PresenceUseCase(IPresenceRepository repositoryPresenceImpl, IUserRepository repositoryUserImpl, IGroupRepository repositoryGroupImpl)
{
_repositoryPresenceImpl = repositoryPresenceImpl;
_repositoryUserImpl = repositoryUserImpl;
_repositoryGroupImpl = repositoryGroupImpl;
}
public List<Presence> GetPresenceByGroup(int groupID){
var usersByGroup = _repositoryUserImpl.GetAllUser()
.Where(user => user.GroupID == groupID).ToList();
var presenceByGroup = _repositoryPresenceImpl.GetAllPresences()
.Where(x => usersByGroup.Any(user => user.Guid == x.UserGuid))
.Select(presence => new Presence{
User = new User{
Guid = presence.UserGuid,
Group = new Group{
ID = groupID,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == groupID).Name
},
FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO,
},
LessonNumber = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance
}).ToList();
return presenceByGroup;
}
public List<Presence> GetFalsePresenceByGroup(int groupID){
var usersByGroup = _repositoryUserImpl.GetAllUser()
.Where(user => user.GroupID == groupID).ToList();
var presenceByGroup = _repositoryPresenceImpl.GetAllPresences()
.Where(x => usersByGroup.Any(user => user.Guid == x.UserGuid) && x.IsAttedance == false)
.Select(presence => new Presence{
User = new User{
Guid = presence.UserGuid,
Group = new Group{
ID = groupID,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == groupID).Name
},
FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO,
},
LessonNumber = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance
}).ToList();
return presenceByGroup;
}
public List<Presence> GetPresenceByUser(Guid userGuid){
var user = _repositoryUserImpl.GetUserByGuid(userGuid);
var presenceByUser = _repositoryPresenceImpl.GetAllPresences()
.Where(user => userGuid == user.UserGuid)
.Select(presence => new Presence{
User = new User{
Guid = user.Guid,
Group = new Group{
ID = user.GroupID,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == user.GroupID).Name
},
FIO = user.FIO
},
LessonNumber = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance
}).ToList();
return presenceByUser;
}
public List<Presence> GetFalsePresenceByUser(Guid userGuid){
var user = _repositoryUserImpl.GetUserByGuid(userGuid);
var presenceByUser = _repositoryPresenceImpl.GetAllPresences()
.Where(user => userGuid == user.UserGuid && user.IsAttedance == false)
.Select(presence => new Presence{
User = new User{
Guid = user.Guid,
Group = new Group{
ID = user.GroupID,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == user.GroupID).Name
},
FIO = user.FIO
},
LessonNumber = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance
}).ToList();
return presenceByUser;
}
public List<Presence> GetPresenceByGroupByTime(int groupID, DateOnly date){
var usersByGroup = _repositoryUserImpl.GetAllUser()
.Where(user => user.GroupID == groupID).ToList();
var presenceByGroup = _repositoryPresenceImpl.GetAllPresences()
.Where(x => usersByGroup.Any(user => user.Guid == x.UserGuid && x.Date == date))
.Select(presence => new Presence{
User = new User{
Guid = presence.UserGuid,
Group = new Group{
ID = groupID,
Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == groupID).Name
},
FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO,
},
LessonNumber = presence.LessonNumber,
Date = presence.Date,
IsAttedance = presence.IsAttedance
}).ToList();
return presenceByGroup;
}
public PresenceGet GetPresencebyAll(int GroupID, DateOnly? start = null, DateOnly? end = null, Guid userGuid = default){
IEnumerable<UserLocalEntity>? users = null;
if (userGuid == default)
{
users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == GroupID);
} else {
users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == GroupID && x.Guid == userGuid);
}
var usersFor = new List<UserForPresence>();
foreach(var user in users){
IEnumerable<PresenceLocalEntity> presences;
if (userGuid != default){
presences = _repositoryPresenceImpl.GetAllPresences().Where(x => x.UserGuid == userGuid).ToList();
}else{
presences = _repositoryPresenceImpl.GetAllPresences().ToList();
}
foreach(var presence in presences)
{
if (start.HasValue && end.HasValue)
{
if (presence.Date >= start && presence.Date <= end)
{
usersFor.Add(new UserForPresence{
FIO = user.FIO, lesson_number = presence.LessonNumber, date = presence.Date, isAttendance = presence.IsAttedance
});
}
} else
{
usersFor.Add(new UserForPresence{
FIO = user.FIO, lesson_number = presence.LessonNumber, date = presence.Date, isAttendance = presence.IsAttedance
});
}
}
}
var presenceGet = new PresenceGet{
gruop_name = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(x => x.ID == GroupID).Name,
users = usersFor
};
return presenceGet;
}
public bool DeletePresence(){
return _repositoryPresenceImpl.DeletePresence();
}
public bool DeletePresenceByGroup(int groupID){
return _repositoryPresenceImpl.DeletePresenceByGroup(groupID);
}
public bool DeletePresenceByUser(Guid userGuid){
return _repositoryPresenceImpl.DeletePresenceByUser(userGuid);
}
public bool DeletePresenceByRange(DateOnly start, DateOnly end)
{
return _repositoryPresenceImpl.DeletePresenceByRange(start, end);
}
public bool IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){
_repositoryPresenceImpl.IsAttedance(firstLesson, lastLesson, date, UserGuid);
return true;
}
public bool GeneratePresence(int firstLesson, int lastLesson, int groupID, DateOnly date)
{
var usersByGroup = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == groupID).ToList();
var groupName = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == groupID).Name;
List<Presence> presenceList = new List<Presence>();
for (int i = firstLesson; i <= lastLesson; i++)
{
foreach (UserLocalEntity user in usersByGroup)
{
presenceList.Add(new Presence
{
User = new User
{
Group = new Group
{
ID = groupID,
Name = groupName
},
FIO = user.FIO,
Guid = user.Guid
},
IsAttedance = true,
Date = date,
LessonNumber = i
});
}
}
var presenceLocalEntity = presenceList.Select(x => new PresenceLocalEntity
{
UserGuid = x.User.Guid,
IsAttedance = x.IsAttedance,
LessonNumber = x.LessonNumber,
Date = x.Date
}).ToList();
_repositoryPresenceImpl.GeneratePresence(presenceLocalEntity);
return true;
}
public bool GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date){
for (int i = 0; i < 8; i++){
GeneratePresence(firstLesson, lastLesson, groupID, date.AddDays(i));
}
return true;
}
}
}