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

/**
 *
 * @author sistem16user
 */
public class MySqlCursoDAO implements CursoDAO {

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

            String query = "SELECT curso.cod_fam,"
                    + "curso.cod_cur,"
                    + "curso.nom_cur, "
                    + "curso.cod_pla, "
                    + "curso.est_cur, "
                    + "plana.nom_pla, "
                    + "area.cod_are, "
                    + "area.nom_are, "
                    + "curso_familia.nom_fam "
                    + "FROM "
                    + "curso "
                    + "INNER JOIN plana ON plana.cod_pla = curso.cod_pla "
                    + "INNER JOIN area ON area.cod_are = plana.cod_are "
                    + "INNER JOIN curso_familia ON curso_familia.cod_fam = curso.cod_fam";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Curso curso = new Curso();
                curso.setCod_cur(rs.getInt("curso.cod_cur"));
                curso.setCod_pla(rs.getInt("curso.cod_pla"));
                curso.setNom_cur(rs.getString("curso.nom_cur"));
                curso.setEst_cur(rs.getInt("curso.est_cur"));
                curso.setCod_area(rs.getInt("area.cod_are"));
                curso.setNom_are(rs.getString("area.nom_are"));
                curso.setNom_pla(rs.getString("plana.nom_pla"));
                curso.setCod_fam(rs.getInt("cod_fam"));
                curso.setNom_fam(rs.getString("nom_fam"));
                lista.add(curso);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

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

            String query = "SELECT curso.cod_fam,"
                    + "curso.cod_cur,"
                    + "curso.nom_cur, "
                    + "curso.cod_pla, "
                    + "curso.est_cur, "
                    + "plana.nom_pla, "
                    + "area.cod_are, "
                    + "area.nom_are, "
                    + "curso_familia.nom_fam "
                    + "FROM "
                    + "curso "
                    + "INNER JOIN plana ON plana.cod_pla = curso.cod_pla "
                    + "INNER JOIN area ON area.cod_are = plana.cod_are "
                    + "INNER JOIN curso_familia ON curso_familia.cod_fam = curso.cod_fam "
                    + " WHERE plana.cod_pla = '" + codigoPlana + "' and area.cod_are = '" + codigoArea + "'";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Curso curso = new Curso();
                curso.setCod_cur(rs.getInt("curso.cod_cur"));
                curso.setCod_pla(rs.getInt("curso.cod_pla"));
                curso.setNom_cur(rs.getString("curso.nom_cur"));
                curso.setEst_cur(rs.getInt("curso.est_cur"));

                curso.setCod_area(rs.getInt("area.cod_are"));
                curso.setNom_are(rs.getString("area.nom_are"));
                curso.setNom_pla(rs.getString("plana.nom_pla"));
                curso.setCod_fam(rs.getInt("cod_fam"));
                curso.setNom_fam(rs.getString("nom_fam"));
                lista.add(curso);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

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

            String queryInsert = "INSERT INTO curso (nom_cur,cod_pla, cod_fam,est_cur) VALUES ('" + nombreCur + "','" + codigoPlana + "','" + codFam + "','1');";

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

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

            String queryInsert = "UPDATE curso SET est_cur='" + dato + "' WHERE cod_cur='" + codigoCur + "';";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

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

            String query = "SELECT cod_cur,nom_cur,cod_fam,cod_pla FROM curso WHERE est_cur='1' order by nom_cur";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Curso curso = new Curso();
                curso.setCod_cur(rs.getInt("cod_cur"));
                curso.setNom_cur(rs.getString("nom_cur"));
                curso.setCod_pla(rs.getInt("cod_pla"));
                lista.add(curso);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

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

            String query = "SELECT DISTINCT curso.cod_fam, curso_familia.nom_fam FROM curso INNER JOIN curso_familia ON curso_familia.cod_fam = curso.cod_fam";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Curso curso = new Curso();
                curso.setCod_fam(rs.getInt("curso.cod_fam"));
                curso.setNom_fam(rs.getString("curso_familia.nom_fam"));

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

    @Override
    public Curso NombreCurso(String cod) throws Exception {
        String base = "horario";

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

            String query = "SELECT area.cod_are,plana.color,plana.cod_pla,curso.cod_cur,curso.nom_cur, curso_familia.nom_fam, area.nom_are, plana.nom_pla FROM curso INNER JOIN plana ON plana.cod_pla = curso.cod_pla INNER JOIN area ON area.cod_are = plana.cod_are INNER JOIN curso_familia ON curso.cod_fam = curso_familia.cod_fam WHERE curso.cod_cur='" + cod + "'";
            System.out.println("(6)-- NombreCurso --->>>  " + query);
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                curso.setCod_cur(rs.getInt("cod_cur"));
                curso.setNom_cur(rs.getString("nom_cur"));
                curso.setNom_fam(rs.getString("nom_fam"));
                curso.setNom_are(rs.getString("nom_are"));
                curso.setNom_pla(rs.getString("nom_pla"));
                curso.setCod_pla(rs.getInt("cod_pla"));
                curso.setCod_area(rs.getInt("cod_are"));
                curso.setColor_plana(rs.getString("color"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return curso;
    }

    @Override
    public void Actualizar(String codCurso, String codigoArea, String codigoPlana, String codfamilia, String Nombre) throws Exception {
        String base = "horario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String queryInsert = "UPDATE curso set nom_cur='" + Nombre + "', cod_fam='" + codfamilia + "', cod_pla='" + codigoPlana + "' WHERE cod_cur='" + codCurso + "'";
            st.executeUpdate(queryInsert);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
    }

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

            String query = "SELECT curso.cod_cur,curso.nom_cur,nom_pla,area.nom_are "
                    + "FROM curso "
                    + "INNER JOIN plana on plana.cod_pla=curso.cod_pla "
                    + "INNER JOIN area on area.cod_are=plana.cod_are "
                    + "WHERE area.cod_are='" + codigoArea + "' and curso.est_cur='1'";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Curso curso = new Curso();
                curso.setCod_cur(rs.getInt("cod_cur"));
                curso.setNom_cur(rs.getString("nom_cur"));
                lista.add(curso);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }
		
		 @Override
    public ArrayList<Plana> cargarCursoDocente(String codDocente) throws Exception {
        ArrayList<Plana> lista = new ArrayList<Plana>();
        String base = "horario";
        Connection cnx = null;
				String nomPlana="";
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "select p.cod_pla,p.nom_pla \n" +
															"from area_docente ad  \n" +
															"INNER JOIN plana p ON p.cod_are\n" +
															"WHERE cod_doc='"+codDocente+"' and est_pla=1 and p.cod_are in (ad.cod_are,if(ad.cod_are = 2,3,if(ad.cod_are = 3,2,if(ad.cod_are = 1,4,if(ad.cod_are=4,1,0)))))";

            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"));				
                lista.add(plana);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }
		

    @Override
    public int cantidadCursoUso(String codigoCurso) 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 horario_curso_docente WHERE cod_cur='" + codigoCurso + "' and est_hcd=1";

            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                dato = rs.getInt("cant");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

}
