/*
 * 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.Plana;
import pe.siso.horario.Beans.UbigeoDocente;
import pe.siso.horario.DAO.PlanaDAO;

/**
 *
 * @author sistem16user
 */
public class MySqlPlanaDAO implements PlanaDAO {

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

            String query = "SELECT plana.color,plana.cod_pla, plana.nom_pla, plana.cod_are, plana.est_pla, area.nom_are  FROM plana INNER JOIN area ON area.cod_are = plana.cod_are";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("plana.cod_pla"));
                plana.setNom_pla(rs.getString("plana.nom_pla"));
                plana.setEst_plana(rs.getInt("plana.est_pla"));
                plana.setCod_area(rs.getInt("plana.cod_are"));
                plana.setNom_are(rs.getString("area.nom_are"));
                plana.setColor(rs.getString("plana.color"));
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

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

            String queryInsert = "UPDATE plana SET est_pla='" + dato + "' WHERE cod_pla='" + codigoPlana + "';";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

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

            String queryInsert = "INSERT INTO plana (nom_pla,cod_are,est_pla) VALUES ('" + nombrePlana + "','" + CodigoArea + "','1');";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

    @Override
    public ArrayList<Plana> listarPlana(String codigoArea) throws Exception {

        ArrayList<Plana> lista = new ArrayList<Plana>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT plana.color,plana.cod_pla, plana.nom_pla, plana.cod_are, plana.est_pla, area.nom_are  FROM plana INNER JOIN area ON area.cod_are = plana.cod_are WHERE area.cod_are = '" + codigoArea + "' ";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("plana.cod_pla"));
                plana.setNom_pla(rs.getString("plana.nom_pla"));
                plana.setEst_plana(rs.getInt("plana.est_pla"));
                plana.setCod_area(rs.getInt("plana.cod_are"));
                plana.setNom_are(rs.getString("area.nom_are"));
                plana.setColor(rs.getString("plana.color"));

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

    @Override
    public ArrayList<Plana> cargarPlana(String codigoArea) throws Exception {

        ArrayList<Plana> lista = new ArrayList<Plana>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT cod_pla, nom_pla, est_pla, cod_are from plana  WHERE cod_are='" + codigoArea + "' and est_pla='1';";
            System.out.println(" cargarPlana ===> "+query);
            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("cod_pla"));
                plana.setNom_pla(rs.getString("nom_pla"));
                plana.setEst_plana(rs.getInt("est_pla"));
                plana.setCod_area(rs.getInt("cod_are"));
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }
		
        @Override
	public ArrayList<Plana> cargarPlanaMatematica() throws Exception {
		
	ArrayList<Plana> lista = new ArrayList<Plana>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT cod_pla, nom_pla, est_pla, cod_are from plana  WHERE cod_are='1' and est_pla='1';";
            System.out.println(" cargarPlana ===> "+query);
            ResultSet rs = st.executeQuery(query);
             
            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("cod_pla"));
                plana.setNom_pla(rs.getString("nom_pla"));
                plana.setEst_plana(rs.getInt("est_pla"));
                plana.setCod_area(rs.getInt("cod_are"));
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
		}
		
		
        @Override
	public ArrayList<Plana> cargarPlanaComunicacion() throws Exception {
			    ArrayList<Plana> lista = new ArrayList<Plana>();
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT cod_pla, nom_pla, est_pla, cod_are from plana  WHERE cod_are='2' and est_pla='1';";
            System.out.println(" cargarPlana ===> "+query);
            ResultSet rs = st.executeQuery(query);
             
            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("cod_pla"));
                plana.setNom_pla(rs.getString("nom_pla"));
                plana.setEst_plana(rs.getInt("est_pla"));
                plana.setCod_area(rs.getInt("cod_are"));
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
	}

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

            String query = "SELECT cod_pla, nom_pla, est_pla, cod_are from plana  WHERE cod_are='3' and est_pla='1';";
            System.out.println(" cargarPlana ===> "+query);
            ResultSet rs = st.executeQuery(query);
             
            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("cod_pla"));
                plana.setNom_pla(rs.getString("nom_pla"));
                plana.setEst_plana(rs.getInt("est_pla"));
                plana.setCod_area(rs.getInt("cod_are"));
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
	}

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

            String query = "SELECT cod_pla, nom_pla, est_pla, cod_are from plana  WHERE cod_are='4' and est_pla='1';";
            System.out.println(" cargarPlana ===> "+query);
            ResultSet rs = st.executeQuery(query);
             
            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("cod_pla"));
                plana.setNom_pla(rs.getString("nom_pla"));
                plana.setEst_plana(rs.getInt("est_pla"));
                plana.setCod_area(rs.getInt("cod_are"));
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
	}

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

            String query = "SELECT cod_pla, nom_pla, est_pla, cod_are from plana  WHERE cod_are='5' and est_pla='1';";
            System.out.println(" cargarPlana ===> "+query);
            ResultSet rs = st.executeQuery(query);
             
            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("cod_pla"));
                plana.setNom_pla(rs.getString("nom_pla"));
                plana.setEst_plana(rs.getInt("est_pla"));
                plana.setCod_area(rs.getInt("cod_are"));
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
	}
		

		
		
		

    @Override
    public Plana NombrePlana(String cod) throws Exception {
        String base = "horario";

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

            String query = "SELECT plana.color,plana.nom_pla,area.cod_are, area.nom_are FROM plana INNER JOIN area ON plana.cod_are = area.cod_are where plana.cod_pla= '" + cod + "';";

            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                plana.setNom_pla(rs.getString("nom_pla"));
                plana.setNom_are(rs.getString("nom_are"));
                plana.setCod_area(rs.getInt("cod_are"));
                plana.setColor(rs.getString("color"));

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

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

            String query = "SELECT plana.nom_pla, area.nom_are FROM plana INNER JOIN area ON plana.cod_are = area.cod_are where plana.cod_pla= '" + cod + "';";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Plana plana = new Plana();
                plana.setNom_pla(rs.getString("plana.nom_pla"));
                plana.setNom_are(rs.getString("area.nom_are"));
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

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

            String queryInsert = "UPDATE plana set nom_pla='" + nombre + "', cod_are='" + codigoArea + "',color='" + color + "'  WHERE cod_pla='" + codigoPlana + "'";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

    @Override
    public void registrarPlana(String nombrePlana, String CodigoArea, String color) throws Exception {
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String codigo = "";
            String queryInsert = "INSERT INTO plana (nom_pla,cod_are,color,est_pla) VALUES ('" + nombrePlana + "','" + CodigoArea + "','" + color + "','1');";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

    @Override
    public int cantidadUsoPlana(String codigoPlana) 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 curso WHERE cod_pla='" + codigoPlana + "' and est_cur=1";
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                dato = rs.getInt("cant");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

    @Override
    public String codigoPlanaRegistrar(String nombrePlana, String CodigoArea, String color) throws Exception {
        String base = "horario";

        String codigo = "";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String queryInsert = "INSERT INTO plana (nom_pla,cod_are,color,est_pla) VALUES ('" + nombrePlana + "','" + CodigoArea + "','" + color + "','1');";
            st.executeUpdate(queryInsert);

            String query = "select cod_pla from plana";
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return codigo;
    }

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

            String queryInsert = "INSERT INTO plana_docente (\n"
                    + "	cod_pla,\n"
                    + "	cod_doc,\n"
                    + "	cod_per\n"
                    + ")\n"
                    + "VALUES\n"
                    + "	('"+codigoPlana+"', '"+codigoDocente+"', '"+codigoPeriodo+"');";
            System.out.println("registrarPlanaDocente====> "+queryInsert);
            st.executeUpdate(queryInsert);

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

    @Override
    public ArrayList<Plana> listarPlanaAsiste(String codDocente, String codPer,String codArea) throws Exception {
        ArrayList<Plana> lista = new ArrayList<Plana>();
        String base = "horario";

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

            String query = "SELECT p.cod_pla,p.nom_pla,\n" +
                            "(SELECT est_plana FROM plana_docente pd WHERE pd.cod_doc='"+codDocente+"' and pd.cod_per='"+codPer+"' and pd.est_plana=1 and pd.cod_pla=p.cod_pla) estado\n" +
                            "from plana p \n" +
                            "WHERE p.cod_are='"+codArea+"'";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Plana plana = new Plana();
                plana.setCod_pla(rs.getInt("cod_pla"));
                plana.setNom_pla(rs.getString("nom_pla"));
                plana.setEst_plana(rs.getInt("estado"));
                lista.add(plana);
            }
            System.out.println("lista planaAsiste ---->"+lista);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }



	
}
