/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.siso.horario.MySqlDAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import pe.siso.horario.Beans.Nivel;
import pe.siso.horario.DAO.NivelDAO;

/**
 *
 * @author fcelestino
 */
public class MySqlNivelDAO implements NivelDAO {

    @Override
    public ArrayList<Nivel> listarNivel() throws Exception {
        ArrayList<Nivel> lista = new ArrayList<Nivel>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT nivel.codnivel,nivel.desnivel,nivel.codcentro,nivel.estado,nivel.orden,centro.descentro,tipo_centro.destipocentro FROM nivel "
                    + "INNER JOIN centro on centro.codcentro=nivel.codcentro "
                    + "INNER JOIN tipo_centro on tipo_centro.codtipocentro=centro.codtipocentro ";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Nivel nivel = new Nivel();
                nivel.setCodigoNivel(rs.getInt("codnivel"));
                nivel.setCodigoCentro(rs.getInt("codcentro"));
                nivel.setNombreNivel(rs.getString("desnivel"));
                nivel.setEstadoNivel(rs.getInt("estado"));
                nivel.setOrdenNivel(rs.getInt("orden"));
                nivel.setNombreCentro(rs.getString("descentro"));
                nivel.setNombreTipoCentro(rs.getString("destipocentro"));
                lista.add(nivel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public ArrayList<Nivel> listarNivel(String codigoTipoCentro, String codigoCentro) throws Exception {
        ArrayList<Nivel> lista = new ArrayList<Nivel>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT nivel.codnivel,nivel.desnivel,nivel.codcentro,nivel.estado,nivel.orden,centro.descentro,tipo_centro.destipocentro FROM nivel "
                    + "INNER JOIN centro on centro.codcentro=nivel.codcentro "
                    + "INNER JOIN tipo_centro on tipo_centro.codtipocentro=centro.codtipocentro "
                    + "WHERE tipo_centro.codtipocentro='" + codigoTipoCentro + "' and centro.codcentro='" + codigoCentro + "' ";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Nivel nivel = new Nivel();
                nivel.setCodigoNivel(rs.getInt("codnivel"));
                nivel.setCodigoCentro(rs.getInt("codcentro"));
                nivel.setNombreNivel(rs.getString("desnivel"));
                nivel.setEstadoNivel(rs.getInt("estado"));
                nivel.setOrdenNivel(rs.getInt("orden"));
                nivel.setNombreCentro(rs.getString("descentro"));
                nivel.setNombreTipoCentro(rs.getString("destipocentro"));
                lista.add(nivel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public void registrarNivel(String codigoCentro, String nombreNivel) throws Exception {
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String queryInsert = "INSERT INTO nivel (desnivel,codcentro,estado,orden) VALUES ('" + nombreNivel + "','" + codigoCentro + "','1','1');";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

    @Override
    public void cambiarEstado(String dato, String codigoNivel) throws Exception {
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String queryInsert = "UPDATE nivel SET estado='" + dato + "' WHERE codnivel='" + codigoNivel + "';";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

    @Override
    public ArrayList<Nivel> cargarNivel(String codigoCentro) throws Exception {
        ArrayList<Nivel> lista = new ArrayList<Nivel>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT nivel.codnivel,nivel.desnivel "
                    + "FROM nivel "
                    + "INNER JOIN centro on centro.codcentro=nivel.codcentro "
                    + "WHERE nivel.codcentro='" + codigoCentro + "' ;";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Nivel nivel = new Nivel();
                nivel.setCodigoNivel(rs.getInt("codnivel"));
                nivel.setNombreNivel(rs.getString("desnivel"));
                lista.add(nivel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public ArrayList<Nivel> cargatNivelDocente() throws Exception {
        ArrayList<Nivel> lista = new ArrayList<Nivel>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT cod_niv_gen,nom_niv_gen,est_niv_gen FROM nivel_docente WHERE est_niv_gen='1';";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Nivel nivel = new Nivel();
                nivel.setCodigoNivelDocente(rs.getInt("cod_niv_gen"));
                nivel.setNombreNivelDocente(rs.getString("nom_niv_gen"));
                nivel.setEstadoNivelDocente(rs.getInt("est_niv_gen"));
                lista.add(nivel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public ArrayList<Nivel> cargatNivel() throws Exception {
        ArrayList<Nivel> lista = new ArrayList<Nivel>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT codnivel,desnivel FROM nivel WHERE estado='1' ORDER BY orden";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Nivel nivel = new Nivel();
                nivel.setCodigoNivel(rs.getInt("codnivel"));
                nivel.setNombreNivel(rs.getString("desnivel"));
                lista.add(nivel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public Nivel nombreNivel(String codNivel) throws Exception {
        String base = "horario";

        Nivel nivel = new Nivel();
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "SELECT nivel.desnivel,nivel.codnivel,centro.descentro,tipo_centro.destipocentro,tipo_centro.codtipocentro,centro.codcentro FROM nivel INNER JOIN centro ON nivel.codcentro = centro.codcentro INNER JOIN tipo_centro ON tipo_centro.codtipocentro = centro.codtipocentro WHERE codnivel='" + codNivel + "'";

            ResultSet rs = st.executeQuery(query);
            System.out.println(query);
            while (rs.next()) {

                nivel.setCodigoNivel(rs.getInt("codnivel"));
                nivel.setNombreNivel(rs.getString("desnivel"));
                nivel.setCodigoCentro(rs.getInt("codcentro"));
                nivel.setNombreCentro(rs.getString("descentro"));

                nivel.setNombreTipoCentro(rs.getString("destipocentro"));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return nivel;

    }

    @Override
    public void actualizar(String codNivel, String nombre) throws Exception {
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String queryInsert = "update nivel set desnivel='" + nombre + "' WHERE codnivel='" + codNivel + "'";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

    @Override
    public ArrayList<Nivel> cargarNivel(String codigoSede, String codigoTipoCentro, String codigoCentro) throws Exception {
        ArrayList<Nivel> lista = new ArrayList<Nivel>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT DISTINCT aula.codlocal,nivel.codnivel,nivel.desnivel "
                    + "from aula "
                    + "INNER JOIN tipo_centro on tipo_centro.codtipocentro=aula.codtipocentro "
                    + "INNER JOIN centro on centro.codcentro=aula.cocentro "
                    + "INNER JOIN nivel on nivel.codnivel=aula.codnivel "
                    + "WHERE nivel.codnivel not in (1,2) and aula.codlocal='" + codigoSede + "' AND tipo_centro.codtipocentro='" + codigoTipoCentro + "' and centro.codcentro='" + codigoCentro + "' and nivel.estado='1'";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Nivel nivel = new Nivel();
                nivel.setCodigoNivel(rs.getInt("codnivel"));
                nivel.setNombreNivel(rs.getString("desnivel"));
                lista.add(nivel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public ArrayList<Nivel> cargarNivelSec(String codigoCentro) throws Exception {
        ArrayList<Nivel> lista = new ArrayList<Nivel>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT nivel.codnivel,nivel.desnivel "
                    + "FROM nivel "
                    + "INNER JOIN centro on centro.codcentro=nivel.codcentro "
                    + "WHERE nivel.codcentro='" + codigoCentro + "' and nivel.codnivel not in (1,2);";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Nivel nivel = new Nivel();
                nivel.setCodigoNivel(rs.getInt("codnivel"));
                nivel.setNombreNivel(rs.getString("desnivel"));
                lista.add(nivel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public int cantidadUsoNivel(String codNivel) throws Exception {
        int dato = 0;
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "select count(*) as cant from tipo_grado WHERE codnivel='" + codNivel + "' and estado=1 ";
            ResultSet rs = st.executeQuery(query);

            if (rs.next()) {
                dato = rs.getInt("cant");

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;

    }

    @Override
    public ArrayList<Nivel> listarNivel(String codCentro) throws Exception {
        ArrayList<Nivel> lista = new ArrayList<Nivel>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT nivel.codnivel,nivel.desnivel,nivel.codcentro,nivel.estado,nivel.orden,centro.descentro,tipo_centro.destipocentro FROM nivel "
                    + "INNER JOIN centro on centro.codcentro=nivel.codcentro "
                    + "INNER JOIN tipo_centro on tipo_centro.codtipocentro=centro.codtipocentro "
                    + "WHERE nivel.codcentro='" + codCentro + "'  ";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Nivel nivel = new Nivel();
                nivel.setCodigoNivel(rs.getInt("codnivel"));
                nivel.setCodigoCentro(rs.getInt("codcentro"));
                nivel.setNombreNivel(rs.getString("desnivel"));
                nivel.setEstadoNivel(rs.getInt("estado"));
                nivel.setOrdenNivel(rs.getInt("orden"));
                nivel.setNombreCentro(rs.getString("descentro"));
                nivel.setNombreTipoCentro(rs.getString("destipocentro"));
                lista.add(nivel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

}
