using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CG67.FicheCollege.Domaine;
using CG67.FicheCollege.Interface;

namespace CG67.FicheCollege.Entrepot
{
    public class EntrepotLogement : EntrepotBase, IEntrepotLogement
    {
        public Logement GetByCodeRNE(string codeRNE)
        {
            Logement resultat = new Logement();
            using (SqlConnection connexion = new SqlConnection(this.ChaineDeConnexion))
            {
                try
                {
                    connexion.Open();
                    using (SqlCommand command = connexion.CreateCommand())
                    {
                        command.CommandText = "SELECT FonctionLocataire FROM Logement INNER JOIN Logement_Contact ON (Logement.Id = Logement_Contact.IdLogement) WHERE (Logement.CodeRNE= @RNE)";
                        command.Parameters.AddWithValue("@RNE", codeRNE);
                        using (SqlDataReader dr = command.ExecuteReader())
                        {
                            int NbrAgents = 0;
                            int NbrLogements = 0;
                            while (dr.Read())
                           {
                               NbrLogements = NbrLogements + 1 ;
                       
                              if (dr["FonctionLocataire"].ToString().Contains("ATC"))
                                  NbrAgents=NbrAgents + 1 ;
                            }

                            resultat.NbrLogements = Convert.ToInt16(NbrLogements.ToString());
                              resultat.NbrAgentsLoges = Convert.ToInt16(NbrAgents.ToString());
                           
                            }
                          }
                   }
                
                catch 
                {
                    throw;
                }
                finally
                {
                    if (connexion.State == ConnectionState.Open)
                        connexion.Close();
                }
            }
            return resultat;
        }
        }
    }

