using EPAServeur.Context; using EPAServeur.IServices; using IO.Swagger.ApiCollaborateur; using IO.Swagger.DTO; using IO.Swagger.ModelCollaborateur; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; namespace EPAServeur.Services { public class CollaborateurService : ICollaborateurService { private readonly ICollaborateurApi collaborateurApi; public CollaborateurService(ICollaborateurApi _collaborateurApi) { collaborateurApi = _collaborateurApi; } public CollaborateurDTO GetCollaborateurById(Guid? id) { Collaborateur collaborateur = collaborateurApi.ChercherCollabId(id); if (collaborateur == null) return null; return GetCollaborateurDTO(collaborateur); } public CollaborateurDTO GetCollaborateurByMail(string mail) { Collaborateur collaborateur = collaborateurApi.ChercherCollabMail(mail); if (collaborateur == null) return null; return GetCollaborateurDTO(collaborateur); } public List GetCollaborateurs(bool? asc, int? numPage, int? parPage, List fonctions, int? idAgence, int? idBU, String texte, string tri) { if (texte == null) texte = ""; else texte = texte.ToLower(); List collaborateurs; List collaborateursDTO; if(idBU != null) collaborateurs = collaborateurApi.ChercherCollabBU(idBU); else if( idAgence != null) collaborateurs = collaborateurApi.ChercherCollabAgence(idAgence); else collaborateurs = collaborateurApi.ChercherCollab(); if (collaborateurs == null) return new List(); int skip = (numPage.Value - 1) * parPage.Value; int take = parPage.Value; collaborateursDTO = (from c in collaborateurs where (c.Nom + " " + c.Prenom).ToLower().Contains(texte) || (c.Prenom + " " + c.Nom).ToLower().Contains(texte) select GetCollaborateurDTO(c)).Skip(skip).Take(take).ToList(); return collaborateursDTO; } public List GetCollaborateursByReferent(Guid? idReferent, bool? asc, int? numPage, int? parPage, String texte, string tri) { if (texte == null) texte = ""; else texte = texte.ToLower(); Collaborateur referent = collaborateurApi.ChercherCollabId(idReferent); if (referent == null) return null; List collaborateurs = collaborateurApi.ChercherCollabRef(idReferent); int skip = (numPage.Value - 1) * parPage.Value; int take = parPage.Value; List collaborateursDTO = (from c in collaborateurs where (c.Nom + " " + c.Prenom).ToLower().Contains(texte) || (c.Prenom + " " + c.Nom).ToLower().Contains(texte) select GetCollaborateurDTO(c)).Skip(skip).Take(take).ToList(); return collaborateursDTO; } public ProfilDTO GetProfilByMail(string mail) { Collaborateur collaborateur = collaborateurApi.ChercherCollabMail(mail); if (collaborateur == null) return null; return GetProfilDTO(collaborateur); } public ProfilDTO GetProfilById(Guid? idCollaborateur) { Collaborateur collaborateur = collaborateurApi.ChercherCollabId(idCollaborateur); if (collaborateur == null) return null; return GetProfilDTO(collaborateur); } //Object to DTO private ProfilDTO GetProfilDTO(Collaborateur collaborateur) { ProfilDTO profilDTO = new ProfilDTO() { Id = collaborateur.Id, DateArrivee = collaborateur.DateArrivee, MailApside = collaborateur.MailApside, Nom = collaborateur.Nom, Prenom = collaborateur.Prenom, BusinessUnit = GetBusinessUnitDTO(collaborateur.BusinessUnit) }; return profilDTO; } private AgenceDTO GetAgenceDTO(Agence agence) { if (agence == null) return null; AgenceDTO agenceDTO = new AgenceDTO() { Id = agence.Id, Nom = agence.Nom, Bu = new List() }; agenceDTO.Bu = agence.Bus.Select(bu => new BusinessUnitDTO() { Id = bu.Id, Nom = bu.Nom }).ToList(); return agenceDTO; } private BusinessUnitDTO GetBusinessUnitDTO(BU businessUnit) { if (businessUnit == null) return null; BusinessUnitDTO businessUnitDTO = new BusinessUnitDTO() { Id = businessUnit.Id, Nom = businessUnit.Nom, Agence = GetAgenceDTO(businessUnit.Agence) }; return businessUnitDTO; } private CollaborateurDTO GetCollaborateurDTO(Collaborateur collaborateur) { CollaborateurDTO collaborateurDTO = new CollaborateurDTO() { Id = collaborateur.Id, Prenom = collaborateur.Prenom, Nom = collaborateur.Nom, MailApside = collaborateur.MailApside, DateArrivee = collaborateur.DateArrivee, }; collaborateurDTO.BusinessUnit = GetBusinessUnitDTO(collaborateur.BusinessUnit); collaborateurDTO.Referent = GetReferentDTO(collaborateur.Referent); return collaborateurDTO; } private ReferentDTO GetReferentDTO(Collaborateur referent) { if (referent == null) return null; ReferentDTO referentDTO = new ReferentDTO() { Id = referent.Id, Prenom = referent.Prenom, Nom = referent.Nom, MailApside = referent.MailApside }; return referentDTO; } } }