You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Digitalisation_EPA_Serveur/EPAServeur/Services/EpInformationService.cs

234 lines
9.0 KiB

using EPAServeur.Context;
using EPAServeur.IServices;
using EPAServeur.Models.EP;
using IO.Swagger.DTO;
using IO.Swagger.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Services
{
public class EpInformationService : IEpInformationService
{
#region Variables
private EpContext context;
private ICollaborateurService collaborateurService;
private int? minParPage = 5;
private int? maxParPage = 100;
private int? defaultParPage = 15;
#endregion
#region constructeur
public EpInformationService(EpContext context, ICollaborateurService collaborateurService)
{
this.context = context;
this.collaborateurService = collaborateurService;
}
#endregion
#region Services async
public async Task<IEnumerable<EpInformationDTO>> GetEPEnCours(List<long?> idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => idBUs.Contains(ep.IdBu)).AsEnumerable().Where(ep => EstEpEnCours(ep.Statut));
return await GetEpInformationDTOs(eps, asc, numPage, parPage, texte, tri, dateDebut, dateFin);
}
public async Task<int> GetEPEnCoursCount(List<long?> idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => idBUs.Contains(ep.IdBu)).AsEnumerable().Where(ep => EstEpEnCours(ep.Statut));
return await GetEpInformationDTOsCount(eps, texte, dateDebut, dateFin);
}
public async Task<IEnumerable<EpInformationDTO>> GetEPEnCoursCollaborateurParticipant(Guid? idCollaborateur)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<EpInformationDTO>> GetEPEnCoursReferent(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => ep.IdReferent.Equals(idReferent)).AsEnumerable().Where(ep => EstEpEnCours(ep.Statut));
return await GetEpInformationDTOs(eps, asc, numPage, parPage, texte, tri, dateDebut, dateFin);
}
public async Task<int> GetEPEnCoursReferentCount(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => ep.IdReferent.Equals(idReferent)).AsEnumerable().Where(ep => EstEpEnCours(ep.Statut));
return await GetEpInformationDTOsCount(eps, texte, dateDebut, dateFin);
}
public async Task<IEnumerable<EpInformationDTO>> GetEPSignes(List<long?> idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => idBUs.Contains(ep.IdBu)).Where(ep => ep.Statut == StatutEp.Signe);
return await GetEpInformationDTOs(eps, asc, numPage, parPage, texte, tri, dateDebut, dateFin);
}
public async Task<IEnumerable<EpInformationDTO>> GetEPSignesCollaborateur(Guid? idCollaborateur)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => ep.IdCollaborateur.Equals(idCollaborateur)).Where(ep => ep.Statut.Equals(StatutEp.Signe));
return await GetEpInformationDTOs(eps, true, 1, maxParPage, "", "", null, null);
}
public async Task<int> GetEPSignesCount(List<long?> idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => idBUs.Contains(ep.IdBu)).Where(ep => ep.Statut == StatutEp.Signe);
return await GetEpInformationDTOsCount(eps, texte, dateDebut, dateFin);
}
public async Task<IEnumerable<EpInformationDTO>> GetEPSignesReferent(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => ep.IdReferent.Equals(idReferent)).Where(ep => ep.Statut.Equals(StatutEp.Signe));
return await GetEpInformationDTOs(eps, asc, numPage, parPage, texte, tri, dateDebut, dateFin);
}
public async Task<int> GetEPSignesReferentCount(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
IEnumerable<Ep> eps = context.Ep.Where(ep => ep.IdReferent.Equals(idReferent)).Where(ep => ep.Statut.Equals(StatutEp.Signe));
return await GetEpInformationDTOsCount(eps, texte, dateDebut, dateFin);
}
public async Task<EpInformationDTO> GetProchainEPCollaborateur(Guid? idCollaborateur)
{
Ep ep = context.Ep.Where(ep => ep.IdCollaborateur.Equals(idCollaborateur)).OrderBy(ep => ep.DateDisponibilite).AsEnumerable().Where(ep => EstEpEnCours(ep.Statut)).FirstOrDefault();
if (ep == null)
return null;
IEnumerable<CollaborateurDTO> collaborateurDTOs = await collaborateurService.GetCollaborateurDTOsAsync(new List<Guid?>() { ep.IdReferent, ep.IdCollaborateur });
return EpToEpDTO(ep, collaborateurDTOs);
}
#endregion
#region méthodes privées
private async Task<IEnumerable<EpInformationDTO>> GetEpInformationDTOs(IEnumerable<Ep> eps, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
eps = TriDate(eps, dateDebut, dateFin);
IEnumerable<CollaborateurDTO> collaborateurDTOs = await GetCollaborateurDTOs(eps);
IEnumerable<EpInformationDTO> epDTOs = eps.Select(ep => EpToEpDTO(ep, collaborateurDTOs));
if (!string.IsNullOrEmpty(texte))
epDTOs = epDTOs.Where(e => TriTexte(e, texte));
epDTOs = TriColonne(epDTOs, tri);
if (asc != null && asc.HasValue && !asc.Value)
epDTOs = epDTOs.Reverse();
return SkipAndTake(epDTOs, numPage, parPage);
}
private async Task<int> GetEpInformationDTOsCount(IEnumerable<Ep> eps, string texte, DateTime? dateDebut, DateTime? dateFin)
{
eps = TriDate(eps, dateDebut, dateFin);
IEnumerable<CollaborateurDTO> collaborateurDTOs = await GetCollaborateurDTOs(eps);
IEnumerable<EpInformationDTO> epDTOs = eps.Select(ep => EpToEpDTO(ep, collaborateurDTOs));
if(!string.IsNullOrEmpty(texte))
epDTOs = epDTOs.Where(e => TriTexte(e, texte));
return epDTOs.Count();
}
private IEnumerable<Ep> TriDate(IEnumerable<Ep> eps, DateTime? dateDebut, DateTime? dateFin)
{
if (dateDebut.HasValue && dateDebut.Value != null)
eps = eps.Where(ep => ep.DatePrevisionnelle >= dateDebut.Value);
if (dateFin.HasValue && dateFin.Value != null)
eps = eps.Where(ep => ep.DatePrevisionnelle < dateFin.Value.AddDays(1));
return eps;
}
private EpInformationDTO EpToEpDTO(Ep ep, IEnumerable<CollaborateurDTO> collaborateurDTOs)
{
return new EpInformationDTO()
{
Id = ep.IdEP,
Collaborateur = collaborateurDTOs.Where(c => c.Id.Equals(ep.IdCollaborateur)).FirstOrDefault(),
DateDisponibilite = ep.DateDisponibilite,
DatePrevisionnelle = ep.DatePrevisionnelle,
Obligatoire = ep.Obligatoire,
Referent = collaborateurDTOs.FirstOrDefault(c => c.Id.Equals(ep.IdReferent)),
Statut = ep.Statut,
Type = ep.TypeEP
};
}
private IEnumerable<EpInformationDTO> TriColonne(IEnumerable<EpInformationDTO> epDTOs, string tri)
{
switch (tri)
{
case "agence":
epDTOs = epDTOs.OrderBy(e => e.Collaborateur.BusinessUnit.Nom);
break;
case "collaborateur":
epDTOs = epDTOs.OrderBy(e => e.Collaborateur.Nom + e.Collaborateur.Prenom);
break;
case "referent":
epDTOs = epDTOs.OrderBy(e => e.Referent.Nom + e.Referent.Prenom);
break;
case "dateentretien":
epDTOs = epDTOs.OrderBy(e => e.DatePrevisionnelle);
break;
case "typeep":
epDTOs = epDTOs.OrderBy(e => e.Type);
break;
case "statutep":
epDTOs = epDTOs.OrderBy(e => e.Statut);
break;
case "datearrivee":
epDTOs = epDTOs.OrderBy(e => e.Collaborateur.DateArrivee);
break;
case "datedisponibilite":
epDTOs = epDTOs.OrderBy(e => e.DateDisponibilite);
break;
}
return epDTOs;
}
private IEnumerable<EpInformationDTO> SkipAndTake(IEnumerable<EpInformationDTO> epDTOs, int? numPage, int? parPage)
{
if(numPage == null ||!numPage.HasValue)
numPage = 1;
if (parPage == null || !parPage.HasValue || parPage < minParPage || parPage > maxParPage)
parPage = defaultParPage;
int skip = (numPage.Value - 1) * parPage.Value;
return epDTOs.Skip(skip).Take(parPage.Value);
}
private bool EstEpEnCours(StatutEp statut)
{
return statut != StatutEp.Annule && statut != StatutEp.Cree && statut != StatutEp.Rejete && statut != StatutEp.Signe;
}
private bool TriTexte(EpInformationDTO epDTOs, string texte)
{
return (epDTOs.Collaborateur.Nom + " " + epDTOs.Collaborateur.Prenom).ToLower().Contains(texte.ToLower()) || (epDTOs.Collaborateur.Prenom + " " + epDTOs.Collaborateur.Nom).ToLower().Contains(texte.ToLower());
}
private async Task<IEnumerable<CollaborateurDTO>> GetCollaborateurDTOs(IEnumerable<Ep> eps)
{
List<Guid?> guids = eps.SelectMany(ep => new Guid?[] { ep.IdReferent, ep.IdCollaborateur }).Distinct().ToList();
return await collaborateurService.GetCollaborateurDTOsAsync(guids);
}
#endregion
}
}