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

        #region Métodos

        public void Insert(entidadeContrato objContrato)
        {
            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 + "REFERENCIA  = ?REFERENCIA , ";
                    vFILTER = vFILTER + "CODIGO      = ?CODIGO , ";
                    vFILTER = vFILTER + "DESCRI      = ?DESCRI , ";
                    vFILTER = vFILTER + "VALOR       = ?VALOR , ";
                    vFILTER = vFILTER + "TA          = ?TA , ";
                    vFILTER = vFILTER + "CONS_ID     = ?CONS_ID , ";
                    vFILTER = vFILTER + "UNID_ID     = ?UNID_ID ";

                    cmd = new MySqlCommand(this.QueryInsert);

                    cmd.Parameters.AddWithValue("?REFERENCIA", objContrato.REFERENCIA);
                    cmd.Parameters.AddWithValue("?CODIGO", objContrato.CODIGO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?DESCRI", objContrato.DESCRI.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?VALOR", objContrato.VALOR);
                    cmd.Parameters.AddWithValue("?TA", objContrato.TA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONS_ID", objContrato.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objContrato.UNID_ID);


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

        public void Update(entidadeContrato objContrato)
        {
            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 + "REFERENCIA  = ?REFERENCIA , ";
                    vFILTER = vFILTER + "CODIGO      = ?CODIGO , ";
                    vFILTER = vFILTER + "DESCRI      = ?DESCRI , ";
                    vFILTER = vFILTER + "VALOR       = ?VALOR , ";
                    vFILTER = vFILTER + "TA          = ?TA , ";
                    vFILTER = vFILTER + "CONS_ID     = ?CONS_ID , ";
                    vFILTER = vFILTER + "UNID_ID     = ?UNID_ID ";
               
                    vAND = vAND + "AND CONS_ID    =  ?CONS_ID_OLD ";
                    vAND = vAND + "AND UNID_ID    = ?UNID_ID_OLD ";
                    vAND = vAND + "AND REFERENCIA =  ?REFERENCIA_OLD ";
                    vAND = vAND + "AND CODIGO     =  ?CODIGO_OLD  ";

                    cmd = new MySqlCommand(this.QueryUpdate);

                    cmd.Parameters.AddWithValue("?REFERENCIA", objContrato.REFERENCIA);
                    cmd.Parameters.AddWithValue("?CODIGO", objContrato.CODIGO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?DESCRI", objContrato.DESCRI.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?VALOR", objContrato.VALOR);
                    cmd.Parameters.AddWithValue("?TA", objContrato.TA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONS_ID", objContrato.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objContrato.UNID_ID);
                    cmd.Parameters.AddWithValue("?CONS_ID_OLD", objContrato.CONS_ID_OLD);
                    cmd.Parameters.AddWithValue("?UNID_ID_OLD", objContrato.UNID_ID_OLD);
                    cmd.Parameters.AddWithValue("?REFERENCIA_OLD", objContrato.REFERENCIA_OLD);
                    cmd.Parameters.AddWithValue("?CODIGO_OLD", objContrato.CODIGO_OLD.ToString().ToUpper());

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

        public DataTable Select(entidadeContrato objContrato)
        {
            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 + "REFERENCIA, ";
                    vFILTER = vFILTER + "CODIGO, ";
                    vFILTER = vFILTER + "DESCRI, ";
                    vFILTER = vFILTER + "VALOR ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY REFERENCIA DESC, CODIGO ASC ";


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

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


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

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

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

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

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

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

                    cmd = new MySqlCommand(this.QuerySelect);

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

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

                    if (objContrato.REFERENCIA != Convert.ToDateTime("01/01/0001"))
                    {
                        cmd.Parameters.AddWithValue("?REFERENCIA", objContrato.REFERENCIA);
                    }

                    if (objContrato.CODIGO != string.Empty)
                    {
                        cmd.Parameters.AddWithValue("?CODIGO", objContrato.CODIGO.ToString().ToUpper());
                    }


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public DataTable Select2(entidadeContrato objContrato)
        {
            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 + "REFERENCIA, ";
                    vFILTER = vFILTER + "CODIGO, ";
                    vFILTER = vFILTER + "DESCRI, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "VALOR ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY REFERENCIA DESC, CODIGO ASC ";


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

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


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

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

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

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

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

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

                    cmd = new MySqlCommand(this.QuerySelect);

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

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

                    if (objContrato.REFERENCIA != Convert.ToDateTime("01/01/0001"))
                    {
                        cmd.Parameters.AddWithValue("?REFERENCIA", objContrato.REFERENCIA);
                    }

                    if (objContrato.CODIGO != string.Empty)
                    {
                        cmd.Parameters.AddWithValue("?CODIGO", objContrato.CODIGO.ToString().ToUpper());
                    }


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public void Delete(entidadeContrato objContrato)
        {
            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 CONS_ID    = ?CONS_ID ";
                    vAND = vAND + "AND UNID_ID    = ?UNID_ID ";
                    vAND = vAND + "AND REFERENCIA = ?REFERENCIA ";
                    vAND = vAND + "AND CODIGO     = ?CODIGO ";

                    cmd = new MySqlCommand(this.QueryDelete);

                    cmd.Parameters.AddWithValue("?CONS_ID", objContrato.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objContrato.UNID_ID);
                    cmd.Parameters.AddWithValue("?REFERENCIA", objContrato.REFERENCIA);
                    cmd.Parameters.AddWithValue("?CODIGO", objContrato.CODIGO.ToString().ToUpper());

                    objGerenciadorTransacao.Execute(cmd);
                }
            }
            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("CONTRATOS SET  ");
                this.sb.AppendLine(vFILTER);

                return sb.ToString();
            }
        }

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

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

                return sb.ToString();
            }
        }

        #endregion




    }
}
