using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using fatd_faturamento.ENTIDADE;
using fatd_faturamento.DAL.Conexao;

namespace fatd_faturamento.DAL.Persistencia
{
    public class dalIndiceCM
    {

        string vFILTER = null;
        string vINNERJOIN = null;
        string vAND = null;
        string vORDERBY = null;

        #region Métodos

        public void Insert(entidadeIndiceCM objIndiceCM)
        {
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "INDICE    = ?INDICE,  ";
                    vFILTER = vFILTER + "VLRINDICE = ?VLRINDICE,  ";
                    vFILTER = vFILTER + "MESANO    = ?MESANO   ";

                    cmd = new MySqlCommand(this.QueryInsert);

                    cmd.Parameters.AddWithValue("?INDICE", objIndiceCM.INDICE.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?VLRINDICE", objIndiceCM.VLRINDICE);
                    cmd.Parameters.AddWithValue("?MESANO", objIndiceCM.MESANO);

                    objGerenciadorTransacao.Execute(cmd);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public void Update(entidadeIndiceCM objIndiceCM)
        {
            MySqlCommand cmd = null;

            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "INDICE    = ?INDICE,  ";
                    vFILTER = vFILTER + "VLRINDICE = ?VLRINDICE,  ";
                    vFILTER = vFILTER + "MESANO    = ?MESANO   ";

                    vAND = vAND + "AND CODIGO = ?CODIGO    ";
                    vAND = vAND + "AND MESANO = ?MESANO_OLD  ";

                    cmd = new MySqlCommand(this.QueryUpdate);

                    cmd.Parameters.AddWithValue("?INDICE", objIndiceCM.INDICE.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?VLRINDICE", objIndiceCM.VLRINDICE);
                    cmd.Parameters.AddWithValue("?MESANO", objIndiceCM.MESANO);
                    cmd.Parameters.AddWithValue("?CODIGO", objIndiceCM.CODIGO);
                    cmd.Parameters.AddWithValue("?MESANO_OLD", objIndiceCM.MESANO_OLD);

                    objGerenciadorTransacao.Execute(cmd);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public void Delete(entidadeIndiceCM objIndiceCM)
        {
            MySqlCommand cmd = null;

            IGerenciadorTransacao objGerenciadorTransacao = null;

            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vORDERBY = " ";

                    if (objIndiceCM.CODIGO != 0)
                    {
                        if (vAND != String.Empty)
                        { vAND = vAND + "AND CODIGO  = ?CODIGO "; }
                        else
                        { vAND = "AND CODIGO = ?CODIGO "; }

                    }
                    else
                    {
                        if (vAND != String.Empty)
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (objIndiceCM.MESANO != Convert.ToDateTime("01/01/0001"))
                    {
                        if (vAND != String.Empty)
                        { vAND = vAND + "AND MESANO = ?MESANO "; }
                        else
                        { vAND = "AND MESANO = ?MESANO "; }

                    }
                    else
                    {
                        if (vAND != String.Empty)
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    cmd = new MySqlCommand(this.QueryDelete);

                    if (objIndiceCM.CODIGO != 0)
                    {
                        cmd.Parameters.AddWithValue("?CODIGO", objIndiceCM.CODIGO);
                    }

                    if (objIndiceCM.MESANO != Convert.ToDateTime("01/01/0001"))
                    {
                        cmd.Parameters.AddWithValue("?MESANO", objIndiceCM.MESANO);
                    }
                    objGerenciadorTransacao.Execute(cmd);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select(entidadeIndiceCM objIndiceCM)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "CODIGO, ";
                    vFILTER = vFILTER + "INDICE, ";
                    vFILTER = vFILTER + "VLRINDICE, ";
                    vFILTER = vFILTER + "MESANO ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY MESANO DESC, INDICE ASC ";

                    vAND = " ";

                    cmd = new MySqlCommand(this.QuerySelect);


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        #endregion


        #region " Comandos SQL "

        StringBuilder sb = new StringBuilder();

        protected string QueryInsert
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine("INSERT INTO  ");
                this.sb.AppendLine("INDICECM SET  ");
                this.sb.AppendLine(vFILTER);

                return sb.ToString();
            }
        }

        protected string QueryUpdate
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine("UPDATE  ");
                this.sb.AppendLine("INDICECM SET  ");
                this.sb.AppendLine(vFILTER);
                this.sb.AppendLine("WHERE 1 = 1 ");
                this.sb.AppendLine(vAND);

                return sb.ToString();
            }
        }

        protected string QueryDelete
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine("DELETE FROM INDICECM ");
                this.sb.AppendLine("WHERE 1 = 1 ");
                this.sb.AppendLine(vAND);
                this.sb.AppendLine(vORDERBY);

                return sb.ToString();
            }
        }

        protected string QuerySelect
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine("SELECT ");
                this.sb.AppendLine(vFILTER);
                this.sb.AppendLine("FROM ");
                this.sb.AppendLine("INDICECM  ");
                this.sb.AppendLine(vINNERJOIN);
                this.sb.AppendLine("WHERE 1 = 1  ");
                this.sb.AppendLine(vAND);
                this.sb.AppendLine(vORDERBY);

                return sb.ToString();
            }
        }

        #endregion

    }
}
