﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using System.Data.SqlClient;
using Softv.Entities;
using Softv.Providers;
using SoftvConfiguration;
using Globals;

namespace Softv.DAO
{
    /// <summary>
    /// Class                   : Softv.DAO.TipServData
    /// Generated by            : Class Generator (c) 2014
    /// Description             : TipServ Data Access Object
    /// File                    : TipServDAO.cs
    /// Creation date           : 04/05/2016
    /// Creation time           : 06:40 p. m.
    ///</summary>
    public class TipServData : TipServProvider
    {
        /// <summary>
        ///</summary>
        /// <param name="TipServ"> Object TipServ added to List</param>
        public override int AddTipServ(TipServEntity entity_TipServ)
        {
            int result = 0;
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServAdd", connection);

                AssingParameter(comandoSql, "@Clv_TipSer", null, pd: ParameterDirection.Output, IsKey: true);

                AssingParameter(comandoSql, "@Concepto", entity_TipServ.Concepto);

                AssingParameter(comandoSql, "@Habilitar", entity_TipServ.Habilitar);

                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    result = ExecuteNonQuery(comandoSql);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error adding TipServ " + ex.Message, ex);
                }
                finally
                {
                    connection.Close();
                }
                result = (int)comandoSql.Parameters["@IdTipServ"].Value;
            }
            return result;
        }

        /// <summary>
        /// Deletes a TipServ
        ///</summary>
        /// <param name="">  Clv_TipSer to delete </param>
        public override int DeleteTipServ(int? Clv_TipSer)
        {
            int result = 0;
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServDelete", connection);

                AssingParameter(comandoSql, "@Clv_TipSer", Clv_TipSer);

                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    result = ExecuteNonQuery(comandoSql);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error deleting TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Edits a TipServ
        ///</summary>
        /// <param name="TipServ"> Objeto TipServ a editar </param>
        public override int EditTipServ(TipServEntity entity_TipServ)
        {
            int result = 0;
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServEdit", connection);

                AssingParameter(comandoSql, "@Clv_TipSer", entity_TipServ.Clv_TipSer);

                AssingParameter(comandoSql, "@Concepto", entity_TipServ.Concepto);

                AssingParameter(comandoSql, "@Habilitar", entity_TipServ.Habilitar);

                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    result = int.Parse(ExecuteNonQuery(comandoSql).ToString());

                }
                catch (Exception ex)
                {
                    throw new Exception("Error updating TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Gets all TipServ
        ///</summary>
        public override List<TipServEntity> GetTipServ()
        {
            List<TipServEntity> TipServList = new List<TipServEntity>();
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServGet", connection);
                IDataReader rd = null;
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    rd = ExecuteReader(comandoSql);

                    while (rd.Read())
                    {
                        TipServList.Add(GetTipServFromReader(rd));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting data TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                    if (rd != null)
                        rd.Close();
                }
            }
            return TipServList;
        }

        /// <summary>
        /// Gets all TipServ by List<int>
        ///</summary>
        public override List<TipServEntity> GetTipServ(List<int> lid)
        {
            List<TipServEntity> TipServList = new List<TipServEntity>();
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {
                DataTable IdDT = BuildTableID(lid);

                SqlCommand comandoSql = CreateCommand("Softv_TipServGetByIds", connection);
                AssingParameter(comandoSql, "@IdTable", IdDT);

                IDataReader rd = null;
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    rd = ExecuteReader(comandoSql);

                    while (rd.Read())
                    {
                        TipServList.Add(GetTipServFromReader(rd));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting data TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                    if (rd != null)
                        rd.Close();
                }
            }
            return TipServList;
        }

        /// <summary>
        /// Gets TipServ by
        ///</summary>
        public override TipServEntity GetTipServById(int? Clv_TipSer)
        {
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServGetById", connection);
                TipServEntity entity_TipServ = null;


                AssingParameter(comandoSql, "@Clv_TipSer", Clv_TipSer);

                IDataReader rd = null;
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    rd = ExecuteReader(comandoSql, CommandBehavior.SingleRow);
                    if (rd.Read())
                        entity_TipServ = GetTipServFromReader(rd);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting data TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                    if (rd != null)
                        rd.Close();
                }
                return entity_TipServ;
            }

        }


        public override List<TipServEntity> GetTipServByClv_TipSer(int? Clv_TipSer)
        {
            List<TipServEntity> TipServList = new List<TipServEntity>();
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServGetByClv_TipSer", connection);

                AssingParameter(comandoSql, "@Clv_TipSer", Clv_TipSer);
                IDataReader rd = null;
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    rd = ExecuteReader(comandoSql);

                    while (rd.Read())
                    {
                        TipServList.Add(GetTipServFromReader(rd));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting data TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                    if (rd != null)
                        rd.Close();
                }
            }
            return TipServList;
        }


        /// <summary>
        ///Get TipServ
        ///</summary>
        public override SoftvList<TipServEntity> GetPagedList(int pageIndex, int pageSize)
        {
            SoftvList<TipServEntity> entities = new SoftvList<TipServEntity>();
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServGetPaged", connection);

                AssingParameter(comandoSql, "@pageIndex", pageIndex);
                AssingParameter(comandoSql, "@pageSize", pageSize);
                IDataReader rd = null;
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    rd = ExecuteReader(comandoSql);
                    while (rd.Read())
                    {
                        entities.Add(GetTipServFromReader(rd));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting data TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                    if (rd != null)
                        rd.Close();
                }
                entities.totalCount = GetTipServCount();
                return entities ?? new SoftvList<TipServEntity>();
            }
        }

        /// <summary>
        ///Get TipServ
        ///</summary>
        public override SoftvList<TipServEntity> GetPagedList(int pageIndex, int pageSize, String xml)
        {
            SoftvList<TipServEntity> entities = new SoftvList<TipServEntity>();
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServGetPagedXml", connection);

                AssingParameter(comandoSql, "@pageSize", pageSize);
                AssingParameter(comandoSql, "@pageIndex", pageIndex);
                AssingParameter(comandoSql, "@xml", xml);
                IDataReader rd = null;
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    rd = ExecuteReader(comandoSql);
                    while (rd.Read())
                    {
                        entities.Add(GetTipServFromReader(rd));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting data TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                    if (rd != null)
                        rd.Close();
                }
                entities.totalCount = GetTipServCount(xml);
                return entities ?? new SoftvList<TipServEntity>();
            }
        }

        /// <summary>
        ///Get Count TipServ
        ///</summary>
        public int GetTipServCount()
        {
            int result = 0;
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServGetCount", connection);
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    result = (int)ExecuteScalar(comandoSql);


                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting data TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                }
            }
            return result;
        }


        /// <summary>
        ///Get Count TipServ
        ///</summary>
        public int GetTipServCount(String xml)
        {
            int result = 0;
            using (SqlConnection connection = new SqlConnection(SoftvSettings.Settings.TipServ.ConnectionString))
            {

                SqlCommand comandoSql = CreateCommand("Softv_TipServGetCountXml", connection);

                AssingParameter(comandoSql, "@xml", xml);
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    result = (int)ExecuteScalar(comandoSql);


                }
                catch (Exception ex)
                {
                    throw new Exception("Error getting data TipServ " + ex.Message, ex);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                }
            }
            return result;
        }

        #region Customs Methods

        #endregion
    }
}
