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

237 lines
8.8 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
/// <summary>
/// Accès et gestion de la base de données
/// </summary>
private readonly EpContext context;
/// <summary>
/// Accès et service collaborateur
/// </summary>
private readonly ICollaborateurService collaborateurService;
/// <summary>
/// Accès au service permettant de transformer un modèle en dto
/// </summary>
private readonly ITransformDTO transformDTO;
/// <summary>
/// Nombre d'éléments min à afficher par page
/// </summary>
private readonly int minParPage = 5;
/// <summary>
/// Nom d'éléments max à affichar par page
/// </summary>
private readonly int maxParPage = 100;
/// <summary>
/// Nombre d'éléments à afficher par défaut par page
/// </summary>
private readonly int defaultParPage = 15;
#endregion
#region constructeur
public EpInformationService(EpContext context, ICollaborateurService collaborateurService, ITransformDTO _transformDTO)
{
this.context = context;
this.collaborateurService = collaborateurService;
this.transformDTO = _transformDTO;
}
#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, string texte, 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, string texte, 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, string texte, 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, string texte, 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 => ep.Statut.Equals(StatutEp.Cree) || EstEpEnCours(ep.Statut)).FirstOrDefault();
if (ep == null)
return null;
IEnumerable<CollaborateurDTO> collaborateurDTOs = await collaborateurService.GetCollaborateurDTOsAsync(new List<Guid?>() { ep.IdReferent, ep.IdCollaborateur });
return transformDTO.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 collaborateurService.GetCollaborateurDTOsAsync(eps);
IEnumerable<EpInformationDTO> epDTOs = eps.Select(ep => transformDTO.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 collaborateurService.GetCollaborateurDTOsAsync(eps);
IEnumerable<EpInformationDTO> epDTOs = eps.Select(ep => transformDTO.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 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());
}
#endregion
}
}