public List<LivreBO> SelectByBibliotheque(String token, BibliothequeBO pBibliotheque)
 {
     if (!Autorization.Validate(token, Autorization.Role.CLIENT)) {
         return null;
     }
     try {
         return LivreBL.SelectByBibliotheque(pBibliotheque);
     } catch (Exception ex) {
         throw;
     }
 }
        public static List<AdministrateurBibliothequeBO> ByBibliotheque(BibliothequeBO pObjBibliotheque)
        {
            List<AdministrateurBibliothequeBO> administrateurBibliothequeResult;

            try {
                using (var proxyAdministrateurBibliotheque = new AdministrateurBibliothequeDAL(Util.GetConnection())) {
                    administrateurBibliothequeResult = proxyAdministrateurBibliotheque.AministrateurBibliothequeDAL_SelectByBiblioId(pObjBibliotheque.BibliothequeId).ToList();
                }
            } catch (Exception ex) {
                throw;
            }

            return administrateurBibliothequeResult;
        }
        public static List<LivreBO> SelectByBibliotheque(BibliothequeBO pBibliotheque)
        {
            List<LivreBO> result;

            try {
                using (var livreProxy = new LivreDAL(Util.GetConnection())) {
                    result = livreProxy.LivreDAL_SelectByBibliothequeId(pBibliotheque.BibliothequeId).ToList();
                    if (result.Count > 0) {
                        foreach (var oLivreBo in result) {
                            oLivreBo.RefLivre = RefLivreBL.SelectById(oLivreBo.RefLivreId);
                            oLivreBo.Bibliotheque = BibliothequeBL.SelectById(oLivreBo.BibliothequeId);
                        }
                    }
                }
            } catch (Exception ex) {
                throw;
            }
            return result;
        }
        private static string StatDemandeReservation(IEnumerable<BibliothequeBO> lstBibliotheque, BibliothequeBO objActualBibliotheque)
        {
            // Demande de réservation
            var lstReservation = CGlobalCache.LstDemandeReservationSelectAll;
            String demandeReservationAll = "", demandeReservationManaged = "", demandeReservationBibliotheque = "";
            demandeReservationAll += "Toutes les demandes de réservation: " + lstReservation.Count(xx => xx.Valide == 1).ToString(CultureInfo.InvariantCulture);
            var grpAllBibliotheque = lstReservation.Where(condition => condition.Valide == 1).GroupBy(xx => xx.Client.BibliothequeId).Select(group => new { bibliothequeId = group.Key, Count = group.Count() }).ToList();
            var query = lstBibliotheque.Join(grpAllBibliotheque, myBiblio => myBiblio.BibliothequeId, allBiblio => allBiblio.bibliothequeId, (biblio1, biblio2) => new { biblioId = biblio1.BibliothequeId, biblio2.Count });

            demandeReservationManaged += string.Format("dans vos bibliothèques: {0}", query.Sum(result => result.Count));
            if (objActualBibliotheque != null) {
                //var lstOldReservation = CGlobalCache.LstOldDemandeReservationByClient;
                //var grpAllOldBibliotheque = lstOldReservation.Where(condition => condition.Valide == 1).GroupBy(xx => xx.Client.BibliothequeId).Select(group => new { bibliothequeId = group.Key, Count = group.Count() });

                var newReservation = grpAllBibliotheque.FirstOrDefault(xx => xx.bibliothequeId == objActualBibliotheque.BibliothequeId);
                //var oldReservation = grpAllOldBibliotheque.FirstOrDefault(xx => xx.bibliothequeId == objActualBibliotheque.BibliothequeId);

                demandeReservationBibliotheque += "dans la bibliothèque " + objActualBibliotheque.BibliothequeName + ": " + ((newReservation != null)?newReservation.Count : 0).ToString(CultureInfo.InvariantCulture);
                //demandeReservationBibliothequeDepasse += "dépassée dans la bibliothèque " + objActualBibliotheque.BibliothequeName + ": " + ((oldReservation != null)?oldReservation.Count : 0).ToString(CultureInfo.InvariantCulture);
            } else {
                demandeReservationBibliotheque += "Vous n'avez pas choisi de bibliothèque à gérer";
            }

            return String.Format("{0}\n{1}\n{2}\n", demandeReservationAll,  demandeReservationManaged, demandeReservationBibliotheque);
        }
        private static string StatReservation(IEnumerable<BibliothequeBO> lstBibliotheque, BibliothequeBO objActualBibliotheque)
        {
            // Réservation
            String reservationAll = "", reservationManagedValid = "", reservationManagedUnValid = "", reservationBibliothequeValid = "", reservationBibliothequeUnValid = "";
            var lstEmprunt = CGlobalCache.LstEmpruntSelectAll;
            var bibliothequeBos = lstBibliotheque as IList<BibliothequeBO> ?? lstBibliotheque.ToList();

            var maxActionId = lstEmprunt.GroupBy(xx => xx.LivreId).Select(dd => new{LivreId = dd.Key, ActionId = dd.Max(qq => qq.ActionId)});
            var clientEmpruntEnCours = maxActionId.Select(dataInMaxActionResult => CGlobalCache.LstEmpruntSelectAll.ToList().Find(xx => xx.ActionId == dataInMaxActionResult.ActionId && xx.LivreId == dataInMaxActionResult.LivreId)).Where(result => result != null && result.State == "res").ToList();

            reservationAll += "Toutes les réservations: " + clientEmpruntEnCours.Count.ToString(CultureInfo.InvariantCulture);
            var grpAllBibliothequeValid = clientEmpruntEnCours.FindAll(xx => (DateTime.Now - xx.CreatedAt).Days <= 15).GroupBy(xx => xx.Livre.BibliothequeId).Select(group => new { bibliothequeId = group.Key, Count = group.Count() }).ToList();

            var queryValid = bibliothequeBos.Join(grpAllBibliothequeValid, myBiblio => myBiblio.BibliothequeId, allBiblio => allBiblio.bibliothequeId, (biblio1, biblio2) => new { biblioId = biblio1.BibliothequeId, biblio2.Count });

            var grpAllBibliothequeUnValid = clientEmpruntEnCours.FindAll(xx => (DateTime.Now - xx.CreatedAt).Days > 15).GroupBy(xx => xx.Livre.BibliothequeId).Select(group => new { bibliothequeId = group.Key, Count = group.Count() }).ToList();
            var queryUnValid = bibliothequeBos.Join(grpAllBibliothequeUnValid, myBiblio => myBiblio.BibliothequeId, allBiblio => allBiblio.bibliothequeId, (biblio1, biblio2) => new { biblioId = biblio1.BibliothequeId, biblio2.Count });

            reservationManagedValid += string.Format("dans vos bibliothèques: {0}", queryValid.Sum(result => result.Count));

            reservationManagedUnValid += string.Format("dans vos bibliothèques: {0}", queryUnValid.Sum(result => result.Count));

            if (objActualBibliotheque != null) {
                var resultValid = grpAllBibliothequeValid.FirstOrDefault(xx =>xx.bibliothequeId == objActualBibliotheque.BibliothequeId);
                reservationBibliothequeValid += String.Format("dans la bibliothèque {0}: {1}", objActualBibliotheque.BibliothequeName, ((resultValid != null)?resultValid.Count : 0).ToString(CultureInfo.InvariantCulture));

                var resultUnValid = grpAllBibliothequeUnValid.FirstOrDefault(xx =>xx.bibliothequeId == objActualBibliotheque.BibliothequeId);
                reservationBibliothequeUnValid += String.Format("dans la bibliothèque {0}: {1}", objActualBibliotheque.BibliothequeName, ((resultUnValid != null)?resultUnValid.Count : 0).ToString(CultureInfo.InvariantCulture));
            } else {
                reservationBibliothequeValid += "Vous n'avez pas choisi de bibliothèque à gérer";
                reservationBibliothequeUnValid += "Vous n'avez pas choisi de bibliothèque à gérer";
            }

            return String.Format("{0}\n\nValide:\n{1}\n{2}\n\nHors délais:\n{3}\n{4}\n",reservationAll, reservationManagedValid, reservationBibliothequeValid, reservationManagedUnValid, reservationBibliothequeUnValid);
        }
        private static string StatRefLivre(IEnumerable<BibliothequeBO> lstBibliotheque, BibliothequeBO objActualBibliotheque)
        {
            // Référence de livre
            var lstLivre = CGlobalCache.LstLivreSelectAll;
            String refLivreAll = "", refLivreManaged = "", refLivreBibliotheque = "";
            var reflivreUniq = lstLivre.GroupBy(xx => xx.RefLivreId).Select(grp => grp.First()).ToList();
            refLivreAll += "Toutes les références: " + reflivreUniq.Count().ToString(CultureInfo.InvariantCulture);
            var grpAllBibliotheque = reflivreUniq.GroupBy(yy => yy.BibliothequeId).Select(grp => new { bibliothequeId = grp.Key, Count = grp.Count() }).ToList();
            var query = lstBibliotheque.Join(grpAllBibliotheque, myBiblio => myBiblio.BibliothequeId, allBiblio => allBiblio.bibliothequeId, (biblio1, biblio2) => new { biblioId = biblio1.BibliothequeId, biblio2.Count });

            refLivreManaged += string.Format("dans vos bibliothèques: {0}", query.Sum(result => result.Count));
            if (objActualBibliotheque != null) {
                var result = grpAllBibliotheque.FirstOrDefault(xx => xx.bibliothequeId == objActualBibliotheque.BibliothequeId);
                refLivreBibliotheque += "dans la bibliothèque " + objActualBibliotheque.BibliothequeName + ": " + ((result != null)?result.Count : 0).ToString(CultureInfo.InvariantCulture) ;
            } else {
                refLivreBibliotheque += "Vous n'avez pas choisi de bibliothèque à gérer";
            }

            return refLivreAll + "\n" + refLivreManaged + "\n" + refLivreBibliotheque + "\n";
        }
        private static String StatLivre(IEnumerable<BibliothequeBO> lstBibliotheque, BibliothequeBO objActualBibliotheque)
        {
            // Livre
            var lstLivre = CGlobalCache.LstLivreSelectAll;
            String livreAll = "", livreManaged = "", livreBibliotheque = "";
            livreAll += "Tous les livres disponibles: " + lstLivre.Count.ToString(CultureInfo.InvariantCulture);
            var grpAllBibliotheque = lstLivre.GroupBy(xx => xx.BibliothequeId).Select(group => new { bibliothequeId = group.Key, Count = group.Count() }).ToList();
            var query = lstBibliotheque.Join(grpAllBibliotheque, myBiblio => myBiblio.BibliothequeId, allBiblio => allBiblio.bibliothequeId, (biblio1, biblio2) => new { biblioId = biblio1.BibliothequeId, biblio2.Count });

            livreManaged += string.Format("dans vos bibliothèques: {0}", query.Sum(result => result.Count));
            if (objActualBibliotheque != null) {
                var result = grpAllBibliotheque.FirstOrDefault(xx =>xx.bibliothequeId == objActualBibliotheque.BibliothequeId);
                livreBibliotheque += "dans la bibliothèque " + objActualBibliotheque.BibliothequeName + ": " + ((result != null)?result.Count : 0).ToString(CultureInfo.InvariantCulture) ;
            } else {
                livreBibliotheque += "Vous n'avez pas choisi de bibliothèque à gérer";
            }

            return String.Format("{0}\n{1}\n{2}\n", livreAll, livreManaged, livreBibliotheque);
        }
        private static string StatItem(BibliothequeBO objActualBibliotheque)
        {
            var lstItem = CGlobalCache.LstItemSelectByAministrateurId;
            String refItemManaged = "", refItemBibliotheque = "";

            var lstItemTotal = lstItem.GroupBy(xx => xx.AdministrateurId).Select(yy => new {yy.Key, Count = yy.Sum(d => d.Montant)}).ToList();
            var firstOrDefault = lstItemTotal.FirstOrDefault(xx => true);
            refItemManaged += string.Format("dans vos bibliothèques: {0:C}", ((null == firstOrDefault) ? 0 : firstOrDefault.Count));

            if (objActualBibliotheque != null) {
                var lstItemByBibliotheque = lstItem.GroupBy(xx => xx.Livre.BibliothequeId).Select(yy => new{yy.Key, Count = yy.Sum(d => d.Montant)});
                var result = lstItemByBibliotheque.FirstOrDefault(xx => xx.Key == objActualBibliotheque.BibliothequeId);
                refItemBibliotheque += String.Format("dans la bibliothèque {0}: {1:C}",  objActualBibliotheque.BibliothequeName, ((null == result)? 0 : result.Count));
            } else {
                refItemBibliotheque += "Vous n'avez pas choisi de bibliothèque à gérer";
            }

            return refItemManaged + "\n" + refItemBibliotheque + "\n";
        }
        private static string StatEmprunt(IEnumerable<BibliothequeBO> lstBibliotheque, BibliothequeBO objActualBibliotheque)
        {
            // Réservation
            var lstEmprunt = CGlobalCache.LstEmpruntSelectAll;

            var maxActionId = lstEmprunt.GroupBy(xx => xx.LivreId).Select(dd => new{LivreId = dd.Key, ActionId = dd.Max(qq => qq.ActionId)});

            var clientEmpruntEnCours = maxActionId.Select(dataInMaxActionResult => CGlobalCache.LstEmpruntSelectAll.ToList().Find(xx => xx.ActionId == dataInMaxActionResult.ActionId && xx.LivreId == dataInMaxActionResult.LivreId)).Where(result => result != null && result.State == "emp").ToList();

            String empruntAll = "", empruntManaged = "", empruntBibliotheque = "";

            empruntAll += "Tous les emprunts: " + clientEmpruntEnCours.Count.ToString(CultureInfo.InvariantCulture);
            var grpAllBibliotheque = clientEmpruntEnCours.GroupBy(xx => xx.Livre.BibliothequeId).Select(group => new { bibliothequeId = group.Key, Count = group.Count() }).ToList();
            var query = lstBibliotheque.Join(grpAllBibliotheque, myBiblio => myBiblio.BibliothequeId, allBiblio => allBiblio.bibliothequeId, (biblio1, biblio2) => new { biblioId = biblio1.BibliothequeId, biblio2.Count });

            empruntManaged += string.Format("dans vos bibliothèques: {0}", query.Sum(result => result.Count));
            if (objActualBibliotheque != null) {
                var result = grpAllBibliotheque.FirstOrDefault(xx =>xx.bibliothequeId == objActualBibliotheque.BibliothequeId);
                empruntBibliotheque += "dans la bibliothèque " + objActualBibliotheque.BibliothequeName + ": " + ((result != null)?result.Count : 0).ToString(CultureInfo.InvariantCulture) ;
            } else {
                empruntBibliotheque += "Vous n'avez pas choisi de bibliothèque à gérer";
            }

            return empruntAll + "\n" + empruntManaged + "\n" + empruntBibliotheque + "\n";
        }