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

/**
 *
 * @author fcelestino
 */
public class MySqlAreaDocenteDAO implements AreaDocenteDAO {

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

            String query = "SELECT COUNT(*) as cant FROM area_docente WHERE cod_doc='" + codigoDocente + "' and est_are_doc='1'";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                cantidad = rs.getInt("cant");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return cantidad;

    }

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

            String query = "INSERT INTO area_docente (cod_doc,cod_are,est_are_doc) VALUES ('" + codigoDocente + "','" + codigoArea + "','1')";

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

    }

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

            String query = "SELECT COUNT(*) as cant FROM area_docente WHERE cod_doc='" + codigoDocente + "' and cod_are='" + codigoArea + "' and est_are_doc='1'";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                cantidad = rs.getInt("cant");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return cantidad;
    }

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

            String query = "SELECT cod_are_doc,cod_doc,cod_are,est_are_doc FROM area_docente WHERE cod_doc IN (" + codigosDocente + ")  and est_are_doc='1'";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                AreaDocente areaDocente = new AreaDocente();
                areaDocente.setCodigoAreaDocente(rs.getInt("cod_are_doc"));
                areaDocente.setCodigoDocente(rs.getString("cod_doc"));
                areaDocente.setCodigoArea(rs.getInt("cod_are"));
                areaDocente.setEstado(rs.getInt("est_are_doc"));
                lista.add(areaDocente);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public ArrayList<AreaDocente> cargarDocentesDisponibles(String codigoTurno, String dia, String codigoCurso) throws Exception {
        ArrayList<AreaDocente> lista = new ArrayList<AreaDocente>();
        String cadenaCodigosDocente = "0";
        String base = "horario";
        String base2 = "sacoolive3";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            //String query = "SELECT codDoc FROM disponibilidad_docente WHERE diaDis LIKE '%"+codigoTurno+"#"+dia+"%'";
            String query = "SELECT dis.codDoc FROM disponibilidad_docente as dis "
                    + "INNER JOIN area_docente as ad on ad.cod_doc=dis.codDoc "
                    + "INNER JOIN plana on plana.cod_are=ad.cod_are "
                    + "INNER JOIN curso on curso.cod_pla=plana.cod_pla "
                    + "WHERE curso.cod_cur='" + codigoCurso + "' and "
                    + " diaDis LIKE '%" + codigoTurno + "#" + dia + "%'";
            ResultSet rs = st.executeQuery(query);
            System.out.println("docenteee --->>> " + query);
            while (rs.next()) {
                cadenaCodigosDocente += "," + rs.getString("codDoc");

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

        Connection cnx2 = null;
        try {
            cnx2 = MySqlDAOFactory.obtenerConexion(base2);
            Statement st2 = cnx2.createStatement();
            String query2 = "SELECT codprofesor, nombre, apellido from profesor WHERE codprofesor in (" + cadenaCodigosDocente + ");";
            ResultSet rs2 = st2.executeQuery(query2);
            System.out.println(query2);
            while (rs2.next()) {
                AreaDocente areaDocente = new AreaDocente();
                areaDocente.setCodigoDocente(rs2.getString("codprofesor"));
                areaDocente.setNombreDocente(rs2.getString("nombre"));
                areaDocente.setApellidoDocente(rs2.getString("apellido"));
                lista.add(areaDocente);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx2.close();
        }
        return lista;
    }

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

            String query = "DELETE from area_docente WHERE cod_doc='" + codigoDocente + "' and cod_are='" + codigoArea + "'";

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

    @Override
    public ArrayList<AreaDocente> cargarDocentesDisponibles(String codigoTurno, String dia, String codigoCurso, String periodo) throws Exception {
        ArrayList<AreaDocente> lista = new ArrayList<AreaDocente>();
        String cadenaCodigosDocente = "0";
        String base = "horario";
        String base2 = "sacoolive3";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            //String query = "SELECT codDoc FROM disponibilidad_docente WHERE diaDis LIKE '%"+codigoTurno+"#"+dia+"%'";
            String query = "SELECT dis.codDoc FROM disponibilidad_docente as dis "
                    + "INNER JOIN area_docente as ad on ad.cod_doc=dis.codDoc "
                    + "INNER JOIN plana on plana.cod_are=ad.cod_are "
                    + "INNER JOIN curso on curso.cod_pla=plana.cod_pla "
                    + "WHERE curso.cod_cur='" + codigoCurso + "' and "
                    + "  dis.cod_per='" + periodo + "' and ";

            if (codigoTurno.equals("4")) {
                query += " (diaDis LIKE '%1#" + dia + "%' or diaDis LIKE '%2#" + dia + "%' ) ";
            } else if (codigoTurno.equals("5")) {
                query += " (diaDis LIKE '%2#" + dia + "%' or diaDis LIKE '%3#" + dia + "%' ) ";
            } else if (codigoTurno.equals("6")) {
                query += " (diaDis LIKE '%1#" + dia + "%' or diaDis LIKE '%2#" + dia + "%' or diaDis LIKE '%3#" + dia + "%') ";
            } else {
							  if(dia.equals("")){
									 query += " diaDis LIKE '%" + codigoTurno + "#%' ";
								}else{
									 query += " diaDis LIKE '%" + codigoTurno + "#" + dia + "%' ";
								}
               
            }

            ResultSet rs = st.executeQuery(query);
            System.out.println("1 docenteee2 --->>> " + query);
            while (rs.next()) {
                cadenaCodigosDocente += "," + rs.getString("codDoc");

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

        Connection cnx2 = null;
        try {
            cnx2 = MySqlDAOFactory.obtenerConexion(base2);
            Statement st2 = cnx2.createStatement();
            String query2 = "SELECT codprofesor, nombre, apellido from profesor WHERE codprofesor in (" + cadenaCodigosDocente + ");";
            ResultSet rs2 = st2.executeQuery(query2);
            System.out.println(query2);
            while (rs2.next()) {
                AreaDocente areaDocente = new AreaDocente();
                areaDocente.setCodigoDocente(rs2.getString("codprofesor"));
                areaDocente.setNombreDocente(rs2.getString("nombre"));
                areaDocente.setApellidoDocente(rs2.getString("apellido"));
                lista.add(areaDocente);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx2.close();
        }
        return lista;
    }

    @Override
    public ArrayList<AreaDocente> cargarDocentesDisponibles2(String codigoTurno, String dia, String codigoCurso, String periodo) throws Exception {
        ArrayList<AreaDocente> lista = new ArrayList<AreaDocente>();
        String cadenaCodigosDocente = "0";
        String base = "horario";
        String base2 = "sacoolive3";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            //String query = "SELECT codDoc FROM disponibilidad_docente WHERE diaDis LIKE '%"+codigoTurno+"#"+dia+"%'";
            String query = "SELECT dis.codDoc FROM disponibilidad_docente as dis "
                    + "INNER JOIN area_docente as ad on ad.cod_doc=dis.codDoc "
                    + "INNER JOIN plana on plana.cod_are=ad.cod_are "
                    + "INNER JOIN curso on curso.cod_pla=plana.cod_pla "
                    + "WHERE curso.cod_cur='" + codigoCurso + "' and "
                    + "  dis.cod_per='" + periodo + "' and ";

            if (codigoTurno.equals("4")) {
                query += " (diaDis LIKE '%1#" + dia + "%' and diaDis LIKE '%2#" + dia + "%' ) ";
            } else if (codigoTurno.equals("5")) {
                query += " (diaDis LIKE '%2#" + dia + "%' and diaDis LIKE '%3#" + dia + "%' ) ";
            } else if (codigoTurno.equals("6")) {
                query += " (diaDis LIKE '%1#" + dia + "%' and diaDis LIKE '%2#" + dia + "%' and diaDis LIKE '%3#" + dia + "%') ";
            } else {
                query += " diaDis LIKE '%" + codigoTurno + "#" + dia + "%' ";
            }

            ResultSet rs = st.executeQuery(query);
            System.out.println("2 docenteee2 --->>> " + query);
            while (rs.next()) {
                cadenaCodigosDocente += "," + rs.getString("codDoc");

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

        Connection cnx2 = null;
        try {
            cnx2 = MySqlDAOFactory.obtenerConexion(base2);
            Statement st2 = cnx2.createStatement();
            String query2 = " SELECT codprofesor, nombre, apellido from profesor WHERE codprofesor in (" + cadenaCodigosDocente + ") and estadoprofe='Y' "
                    + " ORDER BY apellido ;";
            ResultSet rs2 = st2.executeQuery(query2);
            System.out.println(query2);
            while (rs2.next()) {
                AreaDocente areaDocente = new AreaDocente();
                areaDocente.setCodigoDocente(rs2.getString("codprofesor"));
                areaDocente.setNombreDocente(rs2.getString("nombre"));
                areaDocente.setApellidoDocente(rs2.getString("apellido"));
                lista.add(areaDocente);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

}
