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

        #region Métodos

        public DataTable Select(entidadeParcelamento objParcelamento)
        {
            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 + "ID, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "CODIGO, ";
                    vFILTER = vFILTER + "VALOR, ";
                    vFILTER = vFILTER + "DATA_REFERENCIA, ";
                    vFILTER = vFILTER + "ID_PARCELA, ";
                    vFILTER = vFILTER + "NUMERO_PARCELA, ";
                    vFILTER = vFILTER + "REFERENCIA, ";
                    vFILTER = vFILTER + "DT_CADASTRO, ";
                    vFILTER = vFILTER + "ANOMES_FAT, ";
                    vFILTER = vFILTER + "FLG  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = vAND + " AND ZONA = ?ZONA ";
                    vAND = vAND + " AND LIVRO = ?LIVRO ";
                    vAND = vAND + " AND CONTA = ?CONTA ";
                    vAND = vAND + " AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + " AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + " AND YEAR(DATA_REFERENCIA) = YEAR(?DATA_REFERENCIA) ";
                    vAND = vAND + " AND MONTH(DATA_REFERENCIA) = MONTH(?DATA_REFERENCIA) ";

                    //vAND = vAND + " AND ANOMES_FAT = ?ANOMES_FAT";

                    vAND = vAND + " AND ((FLG = '' OR FLG IS NULL) OR (FLG = ?FLG AND ANOMES_FAT = ?ANOMES_FAT )) ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?ZONA", objParcelamento.ZONA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?LIVRO", objParcelamento.LIVRO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONTA", objParcelamento.CONTA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?ANOMES_FAT", objParcelamento.ANOMES_FAT);
                    cmd.Parameters.AddWithValue("?CONS_ID", objParcelamento.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objParcelamento.UNID_ID);
                    cmd.Parameters.AddWithValue("?DATA_REFERENCIA", objParcelamento.DATA_REFERENCIA);
                    cmd.Parameters.AddWithValue("?FLG", objParcelamento.FLG.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

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

        public void Update(entidadeParcelamento objParcelamento)
        {
            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 + "           FLG              = ?FLG     , ";
                    vFILTER = vFILTER + "           ANOMES_FAT       = ?ANOMES_FAT   , ";
                    vFILTER = vFILTER + "           CONS_ID          = ?CONS_ID  , ";
                    vFILTER = vFILTER + "           UNID_ID          = ?UNID_ID    ";

                    vAND = vAND + " AND  ID = ?ID ";


                    cmd = new MySqlCommand(this.QueryUpdate);

                    cmd.Parameters.AddWithValue("?FLG", objParcelamento.FLG.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?ANOMES_FAT", objParcelamento.ANOMES_FAT);
                    cmd.Parameters.AddWithValue("?CONS_ID", objParcelamento.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objParcelamento.UNID_ID);
                    cmd.Parameters.AddWithValue("?ID", objParcelamento.ID);


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

                return sb.ToString();
            }
        }

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

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

                return sb.ToString();
            }
        }

        #endregion

    }
}
