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 EntrepotInvestissement : EntrepotBase,IEntrepotInvestissement
    {
        public IList<Investissement> GetByCodeRNEAndAnnee(string codeRNE, int annee)
        {
            IList<Investissement> resultat = new List<Investissement>();
            using (SqlConnection connexion = new SqlConnection(this.ChaineDeConnexion))
            {
                try
                {
                    connexion.Open();
                    using (SqlCommand command = connexion.CreateCommand())
                    {
                        //on récupère les ATC.
                        command.CommandText = "Select * from InvestissementImmo Where CodeRNE = @RNE AND Annee >= @Annee";
                        command.Parameters.AddWithValue("@RNE", codeRNE);
                        command.Parameters.AddWithValue("@Annee", annee);
                        using (SqlDataReader dr = command.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                resultat.Add(new Investissement(Convert.ToInt16(dr["ANNEE"].ToString()),dr["TypeOp"].ToString(), dr["Avancement"].ToString(), dr["Commentaire"].ToString(), dr["MontantOp"].ToString(), dr["MontantCP"].ToString(), dr["Status"].ToString(), dr["DateLivraison"].ToString()));
                            }
                        }
                    }
                }
                catch (Exception erreurInterne)
                {
                    throw new Exception(" " + erreurInterne);
                }
                finally
                {
                    if (connexion.State == ConnectionState.Open)
                        connexion.Close();
                }
            }
            return resultat;
        }
    }
}
