using System;
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 dalTempFaturaBImagePixBarras
    {
        string vFILTER = null;
        string vINNERJOIN = null;
        string vAND = null;
        string vORDERBY = null;

        #region Métodos

        public void Insert(entidadeTempFaturaBImagePixBarras objTempFaturaBImagePixBarras)
        {
            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 + "NUMNF  = ?NUMNF  , ";

                    vFILTER = vFILTER + "GRUPO = ?GRUPO , ";


                    if (objTempFaturaBImagePixBarras.IMAGEBARRAS != null)
                    {
                        vFILTER = vFILTER + "IMAGEBARRAS = ?IMAGEBARRAS  , ";
                    }

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.BARRAS))
                    {
                        vFILTER = vFILTER + "BARRAS = ?BARRAS          , ";
                    }

                    if (objTempFaturaBImagePixBarras.IMAGEPIX != null)
                    {
                        vFILTER = vFILTER + "IMAGEPIX = ?IMAGEPIX         , ";
                    }

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.PIX))
                    {
                        vFILTER = vFILTER + "PIX = ?PIX  , ";
                    }

                                                                        
                    vFILTER = vFILTER + "NOMEMAQUINA = ?NOMEMAQUINA      ";


                    cmd = new MySqlCommand(this.QueryInsert);


                    cmd.Parameters.AddWithValue("?NUMNF", objTempFaturaBImagePixBarras.NUMNF);
                    cmd.Parameters.AddWithValue("?GRUPO", objTempFaturaBImagePixBarras.GRUPO);

                    if (objTempFaturaBImagePixBarras.IMAGEBARRAS != null)
                    {
                        cmd.Parameters.AddWithValue("?IMAGEBARRAS", objTempFaturaBImagePixBarras.IMAGEBARRAS);
                    }

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.BARRAS))
                    {
                        cmd.Parameters.AddWithValue("?BARRAS", objTempFaturaBImagePixBarras.BARRAS.ToString().ToUpper());
                    }

                    if (objTempFaturaBImagePixBarras.IMAGEPIX != null)
                    {
                        cmd.Parameters.AddWithValue("?IMAGEPIX", objTempFaturaBImagePixBarras.IMAGEPIX);
                    }

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.PIX))
                    {
                        cmd.Parameters.AddWithValue("?PIX", objTempFaturaBImagePixBarras.PIX.ToString().ToUpper());
                    }


                    cmd.Parameters.AddWithValue("?NOMEMAQUINA", objTempFaturaBImagePixBarras.NOMEMAQUINA.ToString().ToUpper());


                    objGerenciadorTransacao.Execute(cmd);


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

        public void Update(entidadeTempFaturaBImagePixBarras objTempFaturaBImagePixBarras)
        {
            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 + "GRUPO = ?GRUPO , ";


                    if (objTempFaturaBImagePixBarras.IMAGEBARRAS != null)
                    {
                        vFILTER = vFILTER + "IMAGEBARRAS = ?IMAGEBARRAS  , ";
                    }

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.BARRAS))
                    {
                        vFILTER = vFILTER + "BARRAS = ?BARRAS          , ";
                    }

                    if (objTempFaturaBImagePixBarras.IMAGEPIX != null)
                    {
                        vFILTER = vFILTER + "IMAGEPIX = ?IMAGEPIX         , ";
                    }

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.PIX))
                    {
                        vFILTER = vFILTER + "PIX = ?PIX  , ";
                    }

                    //OUTROS                                                                         
                    vFILTER = vFILTER + "NOMEMAQUINA                                                    = ?NOMEMAQUINA      ";


                    vAND = vAND + "AND NUMNF  = ?NUMNF ";

                    cmd = new MySqlCommand(this.QueryUpdate);


                    cmd.Parameters.AddWithValue("?NUMNF", objTempFaturaBImagePixBarras.NUMNF);
                    cmd.Parameters.AddWithValue("?GRUPO", objTempFaturaBImagePixBarras.GRUPO);

                    if (objTempFaturaBImagePixBarras.IMAGEBARRAS != null)
                    {
                        cmd.Parameters.AddWithValue("?IMAGEBARRAS", objTempFaturaBImagePixBarras.IMAGEBARRAS);
                    }

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.BARRAS))
                    {
                        cmd.Parameters.AddWithValue("?BARRAS", objTempFaturaBImagePixBarras.BARRAS.ToString().ToUpper());
                    }

                    if (objTempFaturaBImagePixBarras.IMAGEPIX != null)
                    {
                        cmd.Parameters.AddWithValue("?IMAGEPIX", objTempFaturaBImagePixBarras.IMAGEPIX);
                    }

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.PIX))
                    {
                        cmd.Parameters.AddWithValue("?PIX", objTempFaturaBImagePixBarras.PIX.ToString().ToUpper());
                    }


                    cmd.Parameters.AddWithValue("?NOMEMAQUINA", objTempFaturaBImagePixBarras.NOMEMAQUINA.ToString().ToUpper());


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

        public void Delete(entidadeTempFaturaBImagePixBarras objTempFaturaBImagePixBarras)
        {

            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 NOMEMAQUINA = ?NOMEMAQUINA ";


                    cmd = new MySqlCommand(this.QueryDelete);


                    cmd.Parameters.AddWithValue("?NOMEMAQUINA", objTempFaturaBImagePixBarras.NOMEMAQUINA.ToString().ToUpper());


                    objGerenciadorTransacao.Execute(cmd);
                }


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

        public DataTable Select(entidadeTempFaturaBImagePixBarras objTempFaturaBImagePixBarras)
        {
            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 + "NUMNF, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "IMAGEBARRAS, ";
                    vFILTER = vFILTER + "BARRAS, ";
                    vFILTER = vFILTER + "IMAGEPIX, ";
                    vFILTER = vFILTER + "PIX, ";
                    vFILTER = vFILTER + "NOMEMAQUINA ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.NOMEMAQUINA))
                    {
                        vAND = vAND + "AND NOMEMAQUINA = ?NOMEMAQUINA ";
                    }

                    if (objTempFaturaBImagePixBarras.NUMNF != 0)
                    {
                        vAND = vAND + "AND NUMNF = ?NUMNF ";
                    }


                    cmd = new MySqlCommand(this.QuerySelect);

                    if (!String.IsNullOrWhiteSpace(objTempFaturaBImagePixBarras.NOMEMAQUINA))
                    {
                        cmd.Parameters.AddWithValue("?NOMEMAQUINA", objTempFaturaBImagePixBarras.NOMEMAQUINA.ToString().ToUpper());
                    }

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

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            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("TEMP_FATURA_B_IMAGE_PIX_BARRAS SET  ");
                this.sb.AppendLine(vFILTER);

                return sb.ToString();
            }
        }

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

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

                return sb.ToString();
            }
        }

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

                this.sb.AppendLine(vAND);
                this.sb.AppendLine(vORDERBY);

                return sb.ToString();
            }
        }

        #endregion

    }
}
