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

/**
 *
 * @author sistem02user
 */
public class MySqlPlanaDocenteDAO implements PlanaDocenteDAO {

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

            String query = "SELECT\n"
                    + "	cod_pla_doc,\n"
                    + "	cod_doc,\n"
                    + "	cod_pla,\n"
                    + "	cod_per\n"
                    + "FROM\n"
                    + "	plana_docente\n"
                    + "WHERE\n"
                    + "	cod_doc IN (" + codigosDocente + ")\n"
                    + "AND cod_per = '" + codigoPeriodo + "'";
            System.out.println("consultarAsignadosPlanaDocente => " + query);
            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                PlanaDocente planaDocente = new PlanaDocente();
                planaDocente.setCodigoPlanaDocente(rs.getInt("cod_pla_doc"));
                planaDocente.setCodigoDocente(rs.getString("cod_doc"));
                planaDocente.setCodigoPlana(rs.getInt("cod_pla"));
                planaDocente.setCodigoPeriodo(rs.getInt("cod_per"));
                lista.add(planaDocente);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

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

            String query = "DELETE \n"
                    + "FROM plana_docente \n"
                    + "WHERE (cod_doc='"+codigoDocente+"' AND cod_per = '"+codigoPeriodo+"' AND cod_pla='"+codigoPlana+"')";
            System.out.println("quitarPlanaDocente = "+query);
            dato  = st.executeUpdate(query);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

}
