/*
 * 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.Area;
import pe.siso.horario.DAO.AreaDAO;

/**
 *
 * @author sistem16user
 */
public class MySqlAreaDAO implements AreaDAO {

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

            String query = "SELECT cod_are,nom_are,est_are FROM area WHERE est_are='1'";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Area area = new Area();
                area.setCodigoArea(rs.getInt("cod_are"));
                area.setNombreArea(rs.getString("nom_are"));
                area.setEstaArea(rs.getInt("est_are"));
                lista.add(area);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

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

            String queryConsulta = "SELECT count(*) as cant FROM area WHERE nom_are='" + nombreArea + "';";
            ResultSet rs = st.executeQuery(queryConsulta);

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

            if (dato == 0) {
                String queryInsert = "INSERT INTO area (nom_are,est_are) VALUES ('" + nombreArea + "','1');";
                st.executeUpdate(queryInsert);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

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

            String queryInsert = "UPDATE area SET est_are='" + dato + "' WHERE cod_are='" + codigoArea + "';";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        //System.out.println(queryInsert);
    }

    @Override
    public ArrayList<Area> cargarArea(int cod) throws Exception {
        ArrayList<Area> lista = new ArrayList<Area>();
        Connection cnx = null;
        try {
            String base = "horario";
            String codarea="";
	    cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
						if(cod != 0){
							codarea=" and cod_are ="+cod;
						}
						
            String query = "SELECT cod_are,nom_are,est_are from area WHERE est_are = '1'"+codarea;

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Area area = new Area();
                area.setCodigoArea(rs.getInt("cod_are"));
                area.setNombreArea(rs.getString("nom_are"));
                area.setEstaArea(rs.getInt("est_are"));

                lista.add(area);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;

    }

    @Override
    public Area NombreAreas(String cod) throws Exception {
        Connection cnx = null;
        Area area = new Area();
        try {
            String base = "horario";
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT nom_are FROM area where cod_are = '" + cod + "';";
            System.out.println(query);
            ResultSet rs = st.executeQuery(query);

            if (rs.next()) {
                area.setNombreArea(rs.getString("nom_are"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return area;
    }

    @Override
    public void Actualizar(String codigo, String nombreArea) throws Exception {
        Connection cnx = null;
        try {

            String base = "horario";
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String queryInsert = "UPDATE area set nom_are='" + nombreArea + "' where cod_are='" + codigo + "'";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

    @Override
    public int cantidadAreaUso(String codigoArea) throws Exception {
        int dato = 0;
        Connection cnx = null;
        try {

            String base = "horario";
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String queryConsulta = "SELECT COUNT(*) as cant from plana WHERE cod_are='" + codigoArea + "' and est_pla=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 Area datosArea(String codDocente) throws Exception {
        Connection cnx = null;
        Area area = new Area();
        try {
            String base = "horario";
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "SELECT  "
                    + "adoc.cod_are,  "
                    + "are.nom_are  "
                    + "FROM  "
                    + "area_docente AS adoc  "
                    + "INNER JOIN area AS are ON are.cod_are = adoc.cod_are  "
                    + "WHERE  "
                    + "adoc.cod_doc = '" + codDocente + "' AND  "
                    + "adoc.est_are_doc = 1 ;";
            System.out.println("query -->-->    " + query);
            ResultSet rs = st.executeQuery(query);

            if (rs.next()) {
                area.setCodigoArea(rs.getInt("cod_are"));
                area.setNombreArea(rs.getString("nom_are"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return area;
    }

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

            String queryInsert = "INSERT INTO area_docente(cod_doc, cod_are, est_are_doc) VALUES ('" + codigoDocente + "', '" + codigoArea + "', '1');";
            st.executeUpdate(queryInsert);

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

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

            String query = "SELECT\n" +
                    " adoc.cod_are,\n"
                    + "	are.nom_are\n"
                    + "FROM\n"
                    + "	area_docente AS adoc\n"
                    + "INNER JOIN area AS are ON are.cod_are = adoc.cod_are\n"
                    + "WHERE\n"
                    + "	adoc.cod_doc = '"+codDocente+"'\n"
                    + "AND adoc.est_are_doc = 1;";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Area area = new Area();
                area.setCodigoArea(rs.getInt("cod_are"));
                area.setNombreArea(rs.getString("nom_are"));
                
                lista.add(area);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

}
