/*
 * 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.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import pe.siso.horario.Beans.Zona;
import pe.siso.horario.DAO.ZonaDAO;

/**
 *
 * @author sistem02user
 */
public class MysqlZonaDAO implements ZonaDAO {

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

            String query = "SELECT zona.cod_zon, zona.nom_zon, zona.est_zon FROM zona ";

            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Zona zona = new Zona();
                zona.setCodigoZona(rs.getString("cod_zon"));
                zona.setNombreZona(rs.getString("nom_zon"));
                zona.setEstadoZona(rs.getInt("est_zon"));
                lista.add(zona);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public int registrarZonas(String nombreZona) throws Exception {

        int dato = 0;
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String queryConsulta = "SELECT count(*) as cant FROM zona WHERE nom_zon='" + nombreZona + "';";
            ResultSet rs = st.executeQuery(queryConsulta);

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

            if (dato == 0) {
                String queryInsert = "INSERT INTO zona (nom_zon,est_zon) VALUES ('" + nombreZona + "','1');";
                st.executeUpdate(queryInsert);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;

    }

    @Override
    public Zona NombreZonas(String codigoZona) throws Exception {
        String base = "horario";

        Zona zona = new Zona();
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT nom_zon FROM zona where cod_zon = '" + codigoZona + "';";
            System.out.println(query);
            ResultSet rs = st.executeQuery(query);
            if (rs.next()) {
                zona.setNombreZona(rs.getString("nom_zon"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return zona;
    }

    @Override
    public int actualizarZona(String codigoZona, String nombreZona) throws Exception {
        int dato = 0;
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String queryInsert = "UPDATE zona set nom_zon='" + nombreZona + "' where cod_zon='" + codigoZona + "'";
            dato = st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

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

            String queryConsulta = "SELECT COUNT(*) as cant FROM zona_sede WHERE cod_zon='" + codigoZona + "' and est_zon_loc=1";
            ResultSet rs = st.executeQuery(queryConsulta);
            while (rs.next()) {
                dato = rs.getInt("cant");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

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

            String queryInsert = "UPDATE zona SET est_zon='" + dato + "' WHERE cod_zon='" + codigoZona + "';";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

    @Override
    public ArrayList<Zona> listarZonasActivas() throws Exception {
        ArrayList<Zona> lista = new ArrayList<Zona>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "SELECT zona.cod_zon, zona.nom_zon, zona.est_zon FROM zona WHERE zona.est_zon='1' ORDER BY zona.nom_zon ";

            System.out.println("listarZonasActivas " + query);
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Zona zona = new Zona();
                zona.setCodigoZona(rs.getString("cod_zon"));
                zona.setNombreZona(rs.getString("nom_zon"));
                zona.setEstadoZona(rs.getInt("est_zon"));
                lista.add(zona);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public int registrarZonaDocente(String codDocente, int codDepartamento, int codProvincia, int codDistrito) throws SQLException {
        int dato = 0;
        int codigoZona = 0;
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String queryConsultar = "SELECT "
                    + "zu.cod_zon "
                    + "FROM "
                    + "zona_ubigeo AS zu "
                    + "INNER JOIN zona AS z ON z.cod_zon = zu.cod_zon "
                    + "WHERE zu.cod_dep = '" + codDepartamento + "' AND zu.cod_pro='" + codProvincia + "' AND zu.cod_dis='" + codDistrito + "' ";

            ResultSet rs = st.executeQuery(queryConsultar);
            if (rs.next()) {
                codigoZona = rs.getInt("cod_zon");
            }
            String insert = "";
            if (codigoZona == 0) {
                insert = "INSERT INTO zona_docente (cod_zon, cod_doc, est_zon_doc) VALUES ('0', '" + codDocente + "', '1')";
            } else {
                insert = "INSERT INTO zona_docente (cod_zon, cod_doc, est_zon_doc) VALUES ('" + codigoZona + "', '" + codDocente + "', '1')";
            }

            dato = st.executeUpdate(insert);
            System.out.println("registrarZonaDocente " + insert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

    @Override
    public int consultarDocentesAsignadosZona(String codDepartamento, String codProvincia, String codigoDistrito, String codigoZona) throws Exception {
        int cantidad = 0;
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "SELECT "
                    + "COUNT(*) as cantidad "
                    + "FROM "
                    + "zona_docente "
                    + "WHERE "
                    + "zona_docente.cod_dep = '"+codDepartamento+"' AND "
                    + "zona_docente.cod_pro = '"+codProvincia+"' AND "
                    + "zona_docente.cod_dis = '"+codigoDistrito+"'";
            System.out.println("consultarDocentesAsignadosZona " + query);
            ResultSet rs = st.executeQuery(query);
            if (rs.next()) {
                cantidad = rs.getInt("cantidad");
            }

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

    @Override
    public int eliminarDocentesAsignadosZona(String codDepartamento, String codProvincia, String codigoDistrito, String codigoZona) throws Exception {
        int dato = 0;
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "DELETE FROM zona_docente WHERE cod_dep ='" + codDepartamento + "' AND cod_pro ='" + codProvincia + "'"
                    + " AND cod_dis='" + codigoDistrito + "' AND cod_zon='" + codigoZona + "'";
            System.out.println("eliminarDocentesAsignadosZona " + query);
            dato = st.executeUpdate(query);

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

        return dato;
    }

    @Override
    public int eliminarZonaUbigeo(String codDepartamento, String codProvincia, String codigoDistrito, String codigoZona) throws Exception {
        int dato = 0;
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "DELETE FROM zona_ubigeo WHERE cod_dep ='" + codDepartamento + "' AND cod_pro ='" + codProvincia + "'"
                    + " AND cod_dis='" + codigoDistrito + "' AND cod_zon='" + codigoZona + "'";
            System.out.println("eliminarZonaUbigeo " + query);
            dato = st.executeUpdate(query);

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

    @Override
    public int cantidadZonaDocente() throws Exception {
        int cantidad = 0;
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "SELECT COUNT(*) AS cantidad FROM zona_docente";
            ResultSet rs = st.executeQuery(query);
            if (rs.next()) {
                cantidad = rs.getInt("cantidad");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return cantidad;
    }

}
