using EPAServeur.Context; using EPAServeur.Exceptions; using EPAServeur.IServices; using EPAServeur.Models.EP; using EPAServeur.Models.Formation; using EPAServeur.Models.SaisieChamp; using IO.Swagger.DTO; using IO.Swagger.Enum; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace EPAServeur.Services { public class DemandeFormationService : IDemandeFormationService { #region Variables /// /// Accès et gestion de la base de données /// private readonly EpContext epContext; /// /// Accès et service collaborateur /// private readonly ICollaborateurService collaborateurService; /// /// Nombre d'éléments min à afficher par page /// private readonly int minParPage = 5; /// /// Nom d'éléments max à affichar par page /// private readonly int maxParPage = 100; /// /// Nombre d'éléments à afficher par défaut par page /// private readonly int defaultParPage = 15; /// /// Numéro de page min à afficher par défaut /// private readonly int defaultNumPage = 1; /// /// Ordonnancement par défaut /// private readonly bool defaultAsc = true; #endregion #region Contructeurs /// /// Constructeur de la classe FormationService /// /// public DemandeFormationService(EpContext _epContext, ICollaborateurService _collaborateurService) { epContext = _epContext; collaborateurService = _collaborateurService; } #endregion #region Méthodes Service /// /// Récupérer la liste des origines des demandes de formation. /// /// public async Task> GetOriginesDemandeFormationAsync() { IEnumerable origineDemandes; IEnumerable origineDemandeDTOs; origineDemandes = await epContext.OrigineDemandeFormation.ToListAsync(); origineDemandeDTOs = origineDemandes.Select(origineFormation => GetOrigineDemandeFormationDTO(origineFormation)); return origineDemandeDTOs; } /// /// Récupérer la liste des demandes de formation. /// /// Liste des états des demandes à afficher /// liste des ids des BU auxquelles les données sont rattachées /// Indique si les données sont récupérées dans l'ordre croissant ou non /// Numéro de la page du tableau à afficher /// Nombre d’élément maximum à afficher dans le tableau /// Texte permettant de filtrer les données /// Colonne du tableau sur lequel le tri devra être effectué /// Date à partir de laquelle les données son récupérées /// Date jusqu'à laquelle les données sont récupérées /// public async Task> GetDemandesFormationAsync(List etatsDemande, List idBUs, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { IQueryable query; IEnumerable demandeFormations; IEnumerable demandeFormationDTOs; IEnumerable collaborateurDTOs; query = epContext.DemandeFormation .Include(demandeFormation => demandeFormation.Ep) .Include(demandeFormation => demandeFormation.ParticipationFormation) .ThenInclude(participationFormation => participationFormation.Formation); query = EtatsDemandeFilter(query, etatsDemande); query = IdBUsFilter(query, idBUs); query = DateFilter(query, dateDebut, dateFin); query = OrderByColumn(query, asc, tri); query = SkipAndTake(query, parPage, numPage); demandeFormations = await query.ToListAsync(); collaborateurDTOs = await GetCollaborateurDTOs(demandeFormations); demandeFormationDTOs = demandeFormations.Select(demandeFormation => GetDemandeFormationDTO(demandeFormation, collaborateurDTOs)); return demandeFormationDTOs; } /// /// Récupérer le nombre total de demandes de formation. /// /// Liste des états des demandes à afficher /// liste des ids des BU auxquelles les données sont rattachées /// Texte permettant de filtrer les données /// Date à partir de laquelle les données son récupérées /// Date jusqu'à laquelle les données sont récupérées /// public async Task> GetDemandesFormationCountAsync(List etatsDemande, List idBUs, string texte, DateTime? dateDebut, DateTime? dateFin) { throw new NotImplementedException(); } /// /// Créer demande de formation pour un collaborateur. /// /// /// public async Task AddDemandeFormationAsync(DemandeFormationDTO demandeFormationDTO) { throw new NotImplementedException(); } /// /// Répondre à une demande de formation. /// /// /// /// public async Task UpdateDemandeFormationAsync(long idDemandeFormation, DemandeFormationDTO demandeFormationDTO) { throw new NotImplementedException(); } /// /// Supprimer une demande de formation. /// /// /// public async Task DeleteDemandeFormationAsync(long idDemandeFormation) { throw new NotImplementedException(); } #endregion #region Méthodes Privée /// /// Vérifier si un objet DemandeFormationDTO est valide pour une mise à jour /// /// /// Un objet DemandeFormationDTO est valide si l'objet n'est pas null, si le libellé, la descritpion, /// la date de demande de début et la valeur permettant de dire si la demande a été créé par une RH ou non ne sont pas null. /// /// /// true si l'objet est valide, false sinon private void IsDemandeFormationValide(DemandeFormationDTO demande) { // Vérifier que la demande de formation n'est pas null if (demande == null) throw new DemandeFormationInvalidException("Aucune évaluation n'a été reçue."); // Vérifier que la demande de formation a bien un libellé if (string.IsNullOrWhiteSpace(demande.Libelle)) throw new DemandeFormationInvalidException("Le libellé de la demande de formation doit contenir au moins 1 caractère."); // Vérifier que la demande de formation a bien une description if (string.IsNullOrWhiteSpace(demande.Description)) throw new DemandeFormationInvalidException("La description de la demande de formation doit contenir au moins 1 caractère."); // Vérifier que la demande de formation a bien une valeur permettant de dire s'il s'agit d'une demande créée par une RH ou non if (!demande.DemandeRH.HasValue) throw new DemandeFormationInvalidException("Impossible de répondre à une demande de formation sans savoir si la demande a été créé par une RH ou non."); // Vérifier que la demande de formation a bien une date de demande if (!demande.DateDemande.HasValue) throw new DemandeFormationInvalidException("Une date de demande de formation est requise."); } /// /// Ajouter un ordonnancement croissant ou décroissant sur colonne /// /// /// /// /// private IQueryable OrderByColumn(IQueryable query, bool? asc, string columnName) { if (!asc.HasValue) asc = defaultAsc; if (string.IsNullOrWhiteSpace(columnName)) { if (asc.Value) return query.OrderBy(p => p.Libelle); else return query.OrderByDescending(p => p.Libelle); } switch (columnName.ToLower()) { case "businessunit": if (asc.Value) return query.OrderBy(d => d.Libelle); else return query.OrderByDescending(p => p.Libelle); default: if (asc.Value) return query.OrderBy(p => p.Libelle); else return query.OrderByDescending(p => p.Libelle); } } /// /// Ajouter un filtre pour récupérer les demandes de formation en fonction de plusieurs états de demande /// /// /// /// private IQueryable EtatsDemandeFilter(IQueryable query, List etatsDemande) { if (etatsDemande != null && etatsDemande.Count > 0) return query.Where(demandeFormation => etatsDemande.Contains(demandeFormation.Etat)); else return query; } /// /// Ajouter un filtre pour récupérer les demandes de formation en fonction de l'id BU des collaborateurs /// /// /// /// private IQueryable IdBUsFilter(IQueryable query, List idBus) { if (idBus != null && idBus.Count > 0) return query.Where(demandeFormation => idBus.Contains(demandeFormation.Ep.IdBu)); else return query; } /// /// Ajouter un filtre pour récupérer les formations en fonction d'un intitulé /// /// /// /// private IQueryable IntituleFilter(IQueryable query, string intitule) { if (!string.IsNullOrWhiteSpace(intitule)) return query.Where(formation => formation.Intitule.ToLower().Contains(intitule.ToLower())); else return query; } /// /// Ajouter un filtre pour récupérer les formations en fonction d'un intervalle de date /// /// /// /// /// private IQueryable DateFilter(IQueryable query, DateTime? dateDebut, DateTime? dateFin) { if (dateDebut.HasValue && dateFin.HasValue) return query.Where(demandeFormation => demandeFormation.DateDemande >= dateDebut.Value && demandeFormation.DateDemande <= dateFin.Value.AddDays(1)); else if (!dateDebut.HasValue && dateFin.HasValue) return query.Where(demandeFormation => demandeFormation.DateDemande <= dateFin.Value.AddDays(1)); else if (dateDebut.HasValue && !dateFin.HasValue) return query.Where(demandeFormation => demandeFormation.DateDemande >= dateDebut.Value); else return query; } /// /// Ajouter une pagination /// /// /// /// /// private IQueryable SkipAndTake(IQueryable query, int? parPage, int? numPage) { int skip, take; if (!parPage.HasValue || parPage.Value < minParPage || parPage.Value > maxParPage) parPage = defaultParPage; if (!numPage.HasValue || numPage.Value <= 0) numPage = defaultNumPage; skip = (numPage.Value - 1) * parPage.Value; take = parPage.Value; return query.Skip(skip).Take(take); } #region Object to DTO /// /// Récuperer un objet OrigineDemandeFormationDTO en fonction d'un objet OrigineDemande /// /// /// private OrigineDemandeFormationDTO GetOrigineDemandeFormationDTO(OrigineDemande origineDemande) { if (origineDemande == null) return null; OrigineDemandeFormationDTO origineDemandeFormationDTO = new OrigineDemandeFormationDTO() { Id = origineDemande.IdOrigineDemande, Libelle = origineDemande.Libelle }; return origineDemandeFormationDTO; } /// /// Récuperer un objet DemandeFormationDTO en fonction d'un objet DemandeFormation et d'une liste de CollaborateurDTO /// /// /// private DemandeFormationDTO GetDemandeFormationDTO(DemandeFormation demandeFormation, IEnumerable collaborateurDTOs) { if (demandeFormation == null) return null; if (collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; DemandeFormationDTO demandeFormationDTO = new DemandeFormationDTO() { Id = demandeFormation.IdDemandeFormation, Libelle = demandeFormation.Libelle, Description = demandeFormation.Description, DemandeRH = demandeFormation.DemandeRH, DateDemande = demandeFormation.DateDemande, EtatDemande = demandeFormation.Etat, CommentaireRefus = demandeFormation.CommentaireRefus, DateDerniereReponse = demandeFormation.DateDerniereReponse, //Origine = GetOrigineDemandeFormationDTO(demandeFormation.OrigineFormation), // Voir avec Yanael Collaborateur = GetCollaborateurDTO(demandeFormation, collaborateurDTOs), Ep = GetEpInformationDTO(demandeFormation.Ep, collaborateurDTOs), Formation = GetFormationDTO(demandeFormation.ParticipationFormation.Formation, collaborateurDTOs) }; return demandeFormationDTO; } /// /// Récuperer un objet FormationDTO avec des participations en fonction d'un objet Formation et d'une liste de CollaborateurDTO /// /// /// private FormationDTO GetFormationDTO(Formation formation, IEnumerable collaborateurDTOs) { if (formation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; FormationDTO formationDTO = new FormationDTO() { Id = formation.IdFormation, Intitule = formation.Intitule, IdAgence = formation.IdAgence, DateDebut = formation.DateDebut, DateFin = formation.DateFin, Heure = formation.Heure, Jour = formation.Jour, Organisme = formation.Organisme, EstCertifiee = formation.EstCertifiee, EstRealisee = formation.EstRealisee, Origine = GetOrigineFormationDTO(formation.Origine), Statut = GetStatutFormationDTO(formation.Statut), Mode = GetModeFormationDTO(formation.ModeFormation), Type = GetTypeFormationDTO(formation.TypeFormation), Participations = GetParticipationsFormationDTO(formation.ParticipationsFormation, collaborateurDTOs) }; return formationDTO; } /// /// Récuperer un objet OrigineFormationDTO en fonction d'un objet OrigineFormation /// /// /// private OrigineFormationDTO GetOrigineFormationDTO(OrigineFormation origineFormation) { if (origineFormation == null) return null; OrigineFormationDTO origineFormationDTO = new OrigineFormationDTO() { Id = origineFormation.IdOrigineFormation, Libelle = origineFormation.Libelle }; return origineFormationDTO; } /// /// Récuperer un objet StatutFormationDTO en fonction d'un objet StatutFormation /// /// /// private StatutFormationDTO GetStatutFormationDTO(StatutFormation statutFormation) { if (statutFormation == null) return null; StatutFormationDTO statutFormationDTO = new StatutFormationDTO() { Id = statutFormation.IdStatutFormation, Libelle = statutFormation.Libelle }; return statutFormationDTO; } /// /// Récuperer un objet ModeFormationDTO en fonction d'un objet ModeFormation /// /// /// private ModeFormationDTO GetModeFormationDTO(ModeFormation modeFormation) { if (modeFormation == null) return null; ModeFormationDTO modeFormationDTO = new ModeFormationDTO() { Id = modeFormation.IdModeFormation, Libelle = modeFormation.Libelle }; return modeFormationDTO; } /// /// Récuperer un objet TypeFormationDTO en fonction d'un objet TypeFormation /// /// /// private TypeFormationDTO GetTypeFormationDTO(TypeFormation typeFormation) { if (typeFormation == null) return null; TypeFormationDTO typeFormationDTO = new TypeFormationDTO() { Id = typeFormation.IdTypeFormation, Libelle = typeFormation.Libelle }; return typeFormationDTO; } /// /// Récuperer une liste de ParticipationFormationDTO en fonction d'une liste de ParticipationFormation et d'une liste de CollaborateurDTO. Retourne null s'il n'y a aucune participation ou aucun collaborateur. /// /// /// private List GetParticipationsFormationDTO(List participationsFormation, IEnumerable collaborateurDTOs) { List participationFormationDTOs; if (participationsFormation == null || participationsFormation.Count == 0 || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; participationFormationDTOs = participationsFormation.Select(participationFormation => GetParticipationFormationDTO(participationFormation, collaborateurDTOs)) .OrderBy(participationFormation => participationFormation.Collaborateur.Nom) .ThenBy(participationFormation => participationFormation.Collaborateur.Prenom).ToList(); return participationFormationDTOs; } /// /// Récuperer un objet ParticipationFormationDTO en fonction d'un objet ParticipationFormation et d'une liste de CollaborateurDTO /// /// /// private ParticipationFormationDTO GetParticipationFormationDTO(ParticipationFormation participationFormation, IEnumerable collaborateurDTOs) { if (participationFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; ParticipationFormationDTO participationFormationDTO = new ParticipationFormationDTO() { Id = participationFormation.IdParticipationFormation, DateCreation = participationFormation.DateCreation, Intitule = participationFormation.Formation.Intitule, DateDebut = participationFormation.Formation.DateDebut, Statut = GetStatutFormationDTO(participationFormation.Formation.Statut), Collaborateur = GetCollaborateurDTO(participationFormation, collaborateurDTOs), Ep = GetEpInformationDTO(participationFormation.DemandeFormation.Ep, collaborateurDTOs) }; return participationFormationDTO; } /// /// Récupère un objet CollaborateurDTO en fonction d'un objet ParticipationFormation et d'une liste de CollaborateurDTO /// /// /// private CollaborateurDTO GetCollaborateurDTO(ParticipationFormation participationFormation, IEnumerable collaborateurDTOs) { if (participationFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; return collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == participationFormation.DemandeFormation.Ep.IdCollaborateur); } /// /// Récupère un objet CollaborateurDTO en fonction d'un objet DemandeFormation et d'une liste de CollaborateurDTO /// /// /// private CollaborateurDTO GetCollaborateurDTO(DemandeFormation demandeFormation, IEnumerable collaborateurDTOs) { if (demandeFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; return collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == demandeFormation.Ep.IdCollaborateur); } /// /// Récuperer une liste de CollaborateurDTO contenant les collaborateurs et les référents. Retourne null s'il n'y a aucune demande de formation. /// /// /// private async Task> GetCollaborateurDTOs(IEnumerable demandeFormations) { if (demandeFormations == null || !demandeFormations.Any()) return null; List guids = demandeFormations.SelectMany(demandeFormation => new[] { (Guid?)demandeFormation.Ep.IdCollaborateur, demandeFormation.Ep.IdReferent }).ToList(); return await collaborateurService.GetCollaborateurDTOsAsync(guids); ; } /// /// Récuperer une liste de CollaborateurDTO contenant les collaborateurs et les référents. Retourne null s'il n'y a aucune participation. /// /// /// private async Task> GetCollaborateurDTOs(IEnumerable participationsFormation) { if (participationsFormation == null || !participationsFormation.Any()) return null; List guids = participationsFormation.SelectMany(participationFormation => new[] { (Guid?)participationFormation.DemandeFormation.Ep.IdCollaborateur, participationFormation.DemandeFormation.Ep.IdReferent }).ToList(); return await collaborateurService.GetCollaborateurDTOsAsync(guids); ; } /// /// Récupère un objet EpInformationDTO en fonction d'un objet Ep et d'une liste de CollaborateurDTO /// /// /// private EpInformationDTO GetEpInformationDTO(Ep ep, IEnumerable collaborateurDTOs) { CollaborateurDTO collaborateur; CollaborateurDTO referent; if (ep == null) return null; if (collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; collaborateur = collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdCollaborateur); referent = collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdReferent); EpInformationDTO epInformationDTO = new EpInformationDTO() { Id = ep.IdEP, Type = ep.TypeEP, Statut = ep.Statut, DateDisponibilite = ep.DateDisponibilite, DatePrevisionnelle = ep.DatePrevisionnelle, Obligatoire = ep.Obligatoire, Collaborateur = collaborateur, Referent = referent, }; return epInformationDTO; } #endregion #region DTO to Object /// /// Récuperer un objet Saisie en fonction d'un objet SaisieDTO /// /// /// private Saisie GetSaisie(SaisieDTO saisieDTO) { if (saisieDTO == null) return null; Saisie saisie = new Saisie() { //IdSaisie = saisieDTO.Id.Value, Note = saisieDTO.Note, Texte = saisieDTO.Texte, Champ = GetChamp(saisieDTO.Champ), TypeSaisie = saisieDTO.TypeSaisie }; return saisie; } /// /// Récuperer un objet Champ en fonction d'un objet ChampDTO /// /// /// private Champ GetChamp(ChampDTO champDTO) { if (champDTO == null) return null; Champ champ = new Champ() { IdChamp = champDTO.Id.Value, Texte = champDTO.Texte, Section = champDTO.Section, SousSection = champDTO.Soussection, Ordre = champDTO.Ordre.Value, TypeChamp = champDTO.TypeChamp, TypeSaisie = champDTO.TypeSaisie }; return champ; } #endregion #endregion } }