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 dalLogradouro
    {

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


        #region Métodos

        public void Insert(entidadeLogradouro objLogradouro)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;
            int idCod = 0;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "COUNT(*) ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = " ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                    if (dt.Rows.Count != 0)
                    {
                        idCod = Convert.ToInt32(dt.Rows[0].ItemArray[0].ToString());
                    }
                    else
                    {
                        idCod = 0;
                    }

                    if (idCod != 0)
                    {
                        cmd = null;

                        idCod = 0;

                        vFILTER = " ";

                        vFILTER = vFILTER + "MAX(CODNUM) AS CODNUM ";

                        vINNERJOIN = " ";

                        vORDERBY = " ";

                        vAND = " ";

                        cmd = new MySqlCommand(this.QuerySelect);

                        idCod = (int)objGerenciadorTransacao.ExecuteScalar(cmd);

                        idCod = idCod + 1;

                    }
                    else
                    {
                        idCod = idCod + 1;
                    }


                    objLogradouro.CODNUM = idCod;

                    objLogradouro.COD = objLogradouro.entidadeMUNIC.CODUF.ToString("00") + objLogradouro.CODCID.ToString("0000") + objLogradouro.entidadeDISTRITO.CODNUM.ToString("00") + objLogradouro.CODBAI.ToString("000") + idCod.ToString("0000");

                    cmd = null;

                    vFILTER = " ";

                    vFILTER = vFILTER + "COD = ?COD, NOME = ?NOME, CEP = ?CEP, CODNUM = ?CODNUM, CODCID = ?CODCID, CODBAI = ?CODBAI  ";

                    cmd = new MySqlCommand(this.QueryInsert);

                    cmd.Parameters.AddWithValue("?COD", objLogradouro.COD.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?NOME", objLogradouro.NOME.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CEP", objLogradouro.CEP.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CODNUM", objLogradouro.CODNUM);
                    cmd.Parameters.AddWithValue("?CODCID", objLogradouro.CODCID);
                    cmd.Parameters.AddWithValue("?CODBAI", objLogradouro.CODBAI);


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

        public void Insert2(entidadeLogradouro objLogradouro)
        {
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;
            int idCod = 0;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    //vFILTER = vFILTER + "MAX(CODNUM) AS CODNUM ";

                    //vINNERJOIN = " ";

                    //vORDERBY = " ";

                    //vAND = " ";

                    //cmd = new MySqlCommand(this.QuerySelect);

                    //idCod = (int)objGerenciadorTransacao.ExecuteScalar(cmd);

                    //idCod = idCod + 1;

                    //objLogradouro.CODNUM = idCod;

                    objLogradouro.COD = objLogradouro.entidadeMUNIC.CODUF.ToString("00") + objLogradouro.CODCID.ToString("0000") + objLogradouro.entidadeDISTRITO.CODNUM.ToString("00") + objLogradouro.CODBAI.ToString("000") + objLogradouro.CODNUM.ToString("0000");

                    cmd = null;

                    vFILTER = " ";

                    vFILTER = vFILTER + "COD = ?COD, NOME = ?NOME, CEP = ?CEP, CODNUM = ?CODNUM, CODCID = ?CODCID, CODBAI = ?CODBAI  ";

                    cmd = new MySqlCommand(this.QueryInsert);

                    cmd.Parameters.AddWithValue("?COD", objLogradouro.COD.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?NOME", objLogradouro.NOME.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CEP", objLogradouro.CEP.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CODNUM", objLogradouro.CODNUM);
                    cmd.Parameters.AddWithValue("?CODCID", objLogradouro.CODCID);
                    cmd.Parameters.AddWithValue("?CODBAI", objLogradouro.CODBAI);


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

        public void Update(entidadeLogradouro objLogradouro)
        {
            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 + "COD = ?COD, NOME = ?NOME, CEP = ?CEP, CODNUM = ?CODNUM, CODCID = ?CODCID, CODBAI = ?CODBAI, TIPOLOGRA = ?TIPOLOGRA, TITULOLOGRA = ?TITULOLOGRA ";

                    vAND = vAND + "AND COD = ?COD ";

                    cmd = new MySqlCommand(this.QueryUpdate);

                    cmd.Parameters.AddWithValue("?COD", objLogradouro.COD.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?NOME", objLogradouro.NOME.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CEP", objLogradouro.CEP.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CODNUM", objLogradouro.CODNUM);
                    cmd.Parameters.AddWithValue("?CODCID", objLogradouro.CODCID);
                    cmd.Parameters.AddWithValue("?CODBAI", objLogradouro.CODBAI);
                    cmd.Parameters.AddWithValue("?TIPOLOGRA", objLogradouro.TIPOLOGRA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?TITULOLOGRA", objLogradouro.TITULOLOGRA.ToString().ToUpper());

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

        public void Delete(entidadeLogradouro objLogradouro)
        {
            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 = " ";

                    vAND = vAND + "AND COD = ?COD ";

                    cmd = new MySqlCommand(this.QueryDelete);

                    cmd.Parameters.AddWithValue("?COD", objLogradouro.COD.ToString());

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

        public DataTable Select(entidadeLogradouro objLogradouro)
        {
            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 + "LOGRA.COD       AS CODEND,  ";
                    vFILTER = vFILTER + "LOGRA.NOME      AS NOMLOG,  ";
                    vFILTER = vFILTER + "LOGRA.CEP       AS CEPLOG,  ";
                    vFILTER = vFILTER + "LOGRA.CODNUM    AS IDLOG,   ";
                    vFILTER = vFILTER + "BAIRRO.NOME     AS NOMBAI,  ";
                    vFILTER = vFILTER + "BAIRRO.CODNUM   AS IDBAI, ";
                    vFILTER = vFILTER + "DISTRITO.NOME   AS NOMDIS, ";
                    vFILTER = vFILTER + "DISTRITO.CODNUM AS IDDIS,  ";
                    vFILTER = vFILTER + "MUNIC.NOME      AS NOMCID, ";
                    vFILTER = vFILTER + "MUNIC.CODNUM    AS IDCID, ";
                    vFILTER = vFILTER + "UF.UF           AS NOMUF, ";
                    vFILTER = vFILTER + "UF.CODNUM       AS IDUF ";

                    vINNERJOIN = ", BAIRRO, DISTRITO, MUNIC, UF ";

                    vORDERBY = "ORDER BY UF.UF ASC, MUNIC.NOME ASC, DISTRITO.NOME ASC, BAIRRO.NOME ASC, LOGRA.NOME ASC  ";

                    vAND = vAND + "AND LOGRA.CODBAI         = BAIRRO.CODNUM  ";
                    vAND = vAND + "AND LOGRA.CODCID         = MUNIC.CODNUM  ";
                    vAND = vAND + "AND BAIRRO.CODDIST       = DISTRITO.CODNUM ";
                    vAND = vAND + "AND DISTRITO.CODCID      = MUNIC.CODNUM ";
                    vAND = vAND + "AND MUNIC.CODUF          = UF.CODNUM ";
                    vAND = vAND + "AND LEFT(LOGRA.COD,11)   = LEFT(BAIRRO.COD  ,11) ";
                    vAND = vAND + "AND LEFT(LOGRA.COD, 8)   = LEFT(DISTRITO.COD, 8) ";
                    vAND = vAND + "AND LEFT(LOGRA.COD, 6)   = LEFT(MUNIC.COD   , 6) ";
                    vAND = vAND + "AND LEFT(LOGRA.COD, 2)   = LEFT(UF.COD      , 2) ";
                    vAND = vAND + "AND LEFT(BAIRRO.COD,8)   = LEFT(DISTRITO.COD, 8) ";
                    vAND = vAND + "AND LEFT(BAIRRO.COD,6)   = LEFT(MUNIC.COD   , 6) ";
                    vAND = vAND + "AND LEFT(BAIRRO.COD,2)   = LEFT(UF.COD      , 2) ";
                    vAND = vAND + "AND LEFT(DISTRITO.COD,6) = LEFT(MUNIC.COD   , 6) ";
                    vAND = vAND + "AND LEFT(DISTRITO.COD,2) = LEFT(UF.COD      , 2) ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectCount(entidadeLogradouro objLogradouro)
        {
            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 + "COUNT(*) ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = "AND LEFT(LOGRA.COD,?COD0) = ?COD1  ";


                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?COD0", objLogradouro.COD.ToString().Length);
                    cmd.Parameters.AddWithValue("?COD1", objLogradouro.COD.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectOthersTables(entidadeLogradouro objLogradouro)
        {
            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 + "CODNUM, ";
                    vFILTER = vFILTER + "NOME ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY NOME ASC ";

                    vAND = vAND + "AND LEFT(COD,?COD0) = ?COD1 ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?COD0", objLogradouro.COD.ToString().Length);
                    cmd.Parameters.AddWithValue("?COD1", objLogradouro.COD.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectOthersTables_1(entidadeLogradouro objLogradouro)
        {
            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 + "COD, ";
                    vFILTER = vFILTER + "NOME, ";
                    vFILTER = vFILTER + "CEP ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY NOME ASC ";

                    vAND = vAND + "AND LEFT(COD,?COD0) = ?COD1 ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?COD0", objLogradouro.COD.ToString().Length);
                    cmd.Parameters.AddWithValue("?COD1", objLogradouro.COD.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectOthersTables_2(entidadeLogradouro objLogradouro)
        {
            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 + "COD, ";
                    vFILTER = vFILTER + "NOME, ";
                    vFILTER = vFILTER + "CEP ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY NOME ASC ";

                    vAND = vAND + "AND COD = ?COD0 ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?COD0", objLogradouro.COD.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectOthersTables_3(entidadeLogradouro objLogradouro)
        {
            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 + "COD, ";
                    vFILTER = vFILTER + "NOME, ";
                    vFILTER = vFILTER + "CEP ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY NOME ASC ";

                    vAND = vAND + "AND CODBAI = ?COD0 ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?COD0", objLogradouro.CODBAI.ToString().ToUpper());

                    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("LOGRA SET  ");
                this.sb.AppendLine(vFILTER);

                return sb.ToString();
            }
        }

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

                this.sb.AppendLine("UPDATE  ");
                this.sb.AppendLine("LOGRA 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 LOGRA ");
                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("LOGRA  ");
                this.sb.AppendLine(vINNERJOIN);
                this.sb.AppendLine("WHERE 1 = 1  ");
                this.sb.AppendLine(vAND);
                this.sb.AppendLine(vORDERBY);

                return sb.ToString();
            }
        }

        #endregion

    }
}
