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

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

        #region Métodos

        public void Insert(entidadeDistrito objDistrito)
        {
            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;
                    }

                    objDistrito.CODNUM = idCod;

                    objDistrito.COD = objDistrito.entidadeMUNIC.CODUF.ToString("00") + objDistrito.CODCID.ToString("0000") + idCod.ToString("00");

                    cmd = null;

                    vFILTER = " ";

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


                    cmd = new MySqlCommand(this.QueryInsert);

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

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

        public void Insert2(entidadeDistrito objDistrito)
        {
            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;

                    //objDistrito.CODNUM = idCod;

                    objDistrito.COD = objDistrito.entidadeMUNIC.CODUF.ToString("00") + objDistrito.CODCID.ToString("0000") + objDistrito.CODNUM.ToString("00");

                    cmd = null;

                    vFILTER = " ";

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


                    cmd = new MySqlCommand(this.QueryInsert);

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

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

        public void Update(entidadeDistrito objDistrito)
        {
            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, CODNUM = ?CODNUM, CODCID = ?CODCID ";

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

                    cmd = new MySqlCommand(this.QueryUpdate);

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

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

        public void Delete(entidadeDistrito objDistrito)
        {
            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", objDistrito.COD.ToString());

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

        public DataTable Select(entidadeDistrito objDistrito)
        {
            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 + "DISTRITO.COD AS CODEND, ";
                    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 = ", MUNIC, UF ";

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

                    vAND = vAND + "AND DISTRITO.CODCID = MUNIC.CODNUM ";
                    vAND = vAND + "AND MUNIC.CODUF = UF.CODNUM ";
                    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 Select2(entidadeDistrito objDistrito)
        {
            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 + "DISTRITO.COD AS CODEND, ";
                    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 = ", MUNIC, UF ";

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

                    vAND = vAND + "AND DISTRITO.CODCID = MUNIC.CODNUM ";
                    vAND = vAND + "AND MUNIC.CODUF = UF.CODNUM ";
                    vAND = vAND + "AND LEFT(DISTRITO.COD,6) = LEFT(MUNIC.COD, 6) ";
                    //vAND = vAND + "AND LEFT(MUNIC.CODUF,2) = LEFT(UF.COD, 2) ";
                    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(entidadeDistrito objDistrito)
        {
            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 = vAND + "AND LEFT(DISTRITO.COD,?COD0) = ?COD1 ";

                    cmd = new MySqlCommand(this.QuerySelect);

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

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectOthersTables(entidadeDistrito objDistrito)
        {
            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", objDistrito.COD.ToString().Length);
                    cmd.Parameters.AddWithValue("?COD1", objDistrito.COD.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectOthersTables_1(entidadeDistrito objDistrito)
        {
            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 ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY NOME ASC  ";

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


                    cmd = new MySqlCommand(this.QuerySelect);

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

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectOthersTables_2(entidadeDistrito objDistrito)
        {
            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 ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY NOME ASC  ";

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

                    cmd = new MySqlCommand(this.QuerySelect);
                    cmd.Parameters.AddWithValue("?COD0", objDistrito.COD.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable SelectOthersTables_3(entidadeDistrito objDistrito)
        {
            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 ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY NOME ASC  ";

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

                    cmd = new MySqlCommand(this.QuerySelect);
                    cmd.Parameters.AddWithValue("?COD0", objDistrito.CODCID.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("DISTRITO SET  ");
                this.sb.AppendLine(vFILTER);

                return sb.ToString();
            }
        }

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

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

                return sb.ToString();
            }
        }

        #endregion
    }
}
