From caec1c0390f328f8d07935d6b062bea3ad6dcedc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Mon, 22 Feb 2021 14:08:37 +0100 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentatino=20du=20code=20pour=20r?= =?UTF-8?q?=C3=A9cup=C3=A9rer=20les=20EP=20en=20cours=20et=20les=20EP=20si?= =?UTF-8?q?gnes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EPAServeur/IServices/IEpInformationService.cs | 16 +- EPAServeur/Services/EpInformationService.cs | 172 ++++++++++++++++-- 2 files changed, 167 insertions(+), 21 deletions(-) diff --git a/EPAServeur/IServices/IEpInformationService.cs b/EPAServeur/IServices/IEpInformationService.cs index 411d749..3ca26f6 100644 --- a/EPAServeur/IServices/IEpInformationService.cs +++ b/EPAServeur/IServices/IEpInformationService.cs @@ -8,24 +8,24 @@ namespace EPAServeur.IServices { public interface IEpInformationService { - Task> GetEPEnCours(List idBUs, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); - Task GetEPEnCoursCount(List idBUs, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); + Task> GetEPEnCours(List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); + Task GetEPEnCoursCount(List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); Task> GetEPEnCoursCollaborateurParticipant(Guid? idCollaborateur); - Task> GetEPEnCoursReferent(Guid? idReferent, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); + Task> GetEPEnCoursReferent(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); - Task GetEPEnCoursReferentCount(Guid? idReferent, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); + Task GetEPEnCoursReferentCount(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); - Task> GetEPSignes(List idBUs, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); + Task> GetEPSignes(List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); - Task GetEPSignesCount(List idBUs, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); + Task GetEPSignesCount(List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); Task> GetEPSignesCollaborateur(Guid? idCollaborateur); - Task> GetEPSignesReferent(Guid? idReferent, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); + Task> GetEPSignesReferent(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); - Task GetEPSignesReferentCount(Guid? idReferent, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); + Task GetEPSignesReferentCount(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin); Task GetProchainEPCollaborateur(Guid? idCollaborateur); diff --git a/EPAServeur/Services/EpInformationService.cs b/EPAServeur/Services/EpInformationService.cs index 87ce8fb..27ea38d 100644 --- a/EPAServeur/Services/EpInformationService.cs +++ b/EPAServeur/Services/EpInformationService.cs @@ -1,6 +1,8 @@ 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; @@ -10,43 +12,99 @@ 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 - public async Task> GetEPEnCours(List idBUs, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) + + #region Services async + public async Task> GetEPEnCours(List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { - throw new NotImplementedException(); + IEnumerable eps = context.Ep.Where(ep => idBUs.Contains(ep.IdBu)).Where(ep => estEpEnCours(ep.Statut)); + eps = TriDate(eps, dateDebut, dateFin); + + List guids = eps.SelectMany(ep => new Guid?[] { ep.IdReferent, ep.IdCollaborateur }).ToList(); + + IEnumerable collaborateurDTOs = await collaborateurService.GetCollaborateurDTOsAsync(guids); + + IEnumerable epDTOs = eps.Select(ep => EpToEpDTO(ep, collaborateurDTOs)); + + 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); } - public async Task> GetEPEnCoursCollaborateurParticipant(Guid? idCollaborateur) + + + public async Task GetEPEnCoursCount(List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { - throw new NotImplementedException(); + IEnumerable eps = context.Ep.Where(ep => idBUs.Contains(ep.IdBu)).Where(ep => estEpEnCours(ep.Statut)); + eps = TriDate(eps, dateDebut, dateFin); + + + + List guids = eps.SelectMany(ep => new Guid?[] { ep.IdReferent, ep.IdCollaborateur }).ToList(); + + IEnumerable collaborateurDTOs = await collaborateurService.GetCollaborateurDTOsAsync(guids); + + IEnumerable epDTOs = eps.Select(ep => EpToEpDTO(ep, collaborateurDTOs)); + + epDTOs = epDTOs.Where(e => TriTexte(e, texte)); + + return epDTOs.Count(); } - public async Task GetEPEnCoursCount(List idBUs, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) + public async Task> GetEPEnCoursCollaborateurParticipant(Guid? idCollaborateur) { throw new NotImplementedException(); } - public async Task> GetEPEnCoursReferent(Guid? idReferent, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) + public async Task> GetEPEnCoursReferent(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { throw new NotImplementedException(); } - public async Task GetEPEnCoursReferentCount(Guid? idReferent, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) + public async Task GetEPEnCoursReferentCount(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { throw new NotImplementedException(); } - public async Task> GetEPSignes(List idBUs, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) + public async Task> GetEPSignes(List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { - throw new NotImplementedException(); + IEnumerable eps = context.Ep.Where(ep => idBUs.Contains(ep.IdBu)).Where(ep => ep.Statut == StatutEp.Signe); + eps = TriDate(eps, dateDebut, dateFin); + + List guids = eps.SelectMany(ep => new Guid?[] { ep.IdReferent, ep.IdCollaborateur }).ToList(); + + IEnumerable collaborateurDTOs = await collaborateurService.GetCollaborateurDTOsAsync(guids); + + IEnumerable epDTOs = eps.Select(ep => EpToEpDTO(ep, collaborateurDTOs)); + + 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); } public async Task> GetEPSignesCollaborateur(Guid? idCollaborateur) @@ -54,17 +112,28 @@ namespace EPAServeur.Services throw new NotImplementedException(); } - public async Task GetEPSignesCount(List idBUs, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) + public async Task GetEPSignesCount(List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { - throw new NotImplementedException(); + IEnumerable eps = context.Ep.Where(ep => idBUs.Contains(ep.IdBu)).Where(ep => ep.Statut == StatutEp.Signe); + eps = TriDate(eps, dateDebut, dateFin); + + List guids = eps.SelectMany(ep => new Guid?[] { ep.IdReferent, ep.IdCollaborateur }).ToList(); + + IEnumerable collaborateurDTOs = await collaborateurService.GetCollaborateurDTOsAsync(guids); + + IEnumerable epDTOs = eps.Select(ep => EpToEpDTO(ep, collaborateurDTOs)); + + epDTOs = epDTOs.Where(e => TriTexte(e, texte)); + + return epDTOs.Count(); } - public async Task> GetEPSignesReferent(Guid? idReferent, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) + public async Task> GetEPSignesReferent(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { throw new NotImplementedException(); } - public async Task GetEPSignesReferentCount(Guid? idReferent, bool asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) + public async Task GetEPSignesReferentCount(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { throw new NotImplementedException(); } @@ -73,5 +142,82 @@ namespace EPAServeur.Services { throw new NotImplementedException(); } + #endregion + + #region méthodes privées + private IEnumerable TriDate(IEnumerable 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 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 TriColonne(IEnumerable 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 "date": + epDTOs = epDTOs.OrderBy(e => e.DatePrevisionnelle); + break; + case "typeep": + epDTOs = epDTOs.OrderBy(e => e.Type); + break; + case "etatep": + epDTOs = epDTOs.OrderBy(e => e.Statut); + break; + } + return epDTOs; + } + + private IEnumerable SkipAndTake(IEnumerable 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).Contains(texte) || (epDTOs.Collaborateur.Prenom + " " + epDTOs.Collaborateur.Nom).Contains(texte); + } + + #endregion } }