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

/**
 *
 * @author sistem02user
 */
public class MySqlZonaDistritoDAO implements ZonaDistritoDAO{

    @Override
    public ArrayList<ZonaDistrito> listarDistritosZonas(int codigoDepartamento, int codigoProvincia, int inicio,int cantidad) throws Exception {
        ArrayList<ZonaDistrito> lista = new ArrayList<ZonaDistrito>();
        String base = "horario";
Connection cnx = null;
        try{
         cnx = MySqlDAOFactory.obtenerConexion(base);
        Statement st = cnx.createStatement();
        
        String query = "SELECT " +
                        "zu.cod_zon_ubi, " +
                        "u.cod_dep_ubi, " +
                        "u.cod_pro_ubi, " +
                        "u.cod_dis_ubi, " +
                        "z.cod_zon, " +
                        "u.nom_dep_ubi, " +
                        "u.nom_pro_ubi, " +
                        "u.nom_dis_ubi, " +
                        "IF(z.nom_zon IS NULL , '' , z.nom_zon) AS nom_zon " +
                        "FROM " +
                        "ubigeo AS u " +
                        "LEFT JOIN zona_ubigeo AS zu ON u.cod_dep_ubi = zu.cod_dep  " +
                        "AND u.cod_pro_ubi = zu.cod_pro  " +
                        "AND u.cod_dis_ubi = zu.cod_dis AND zu.est_zon_ubi = '1' " +
                        "LEFT JOIN zona AS z ON z.cod_zon = zu.cod_zon " +
                        "WHERE " +
                        "u.cod_dep_ubi = '"+codigoDepartamento+"' AND " +
                        "u.cod_pro_ubi = '"+codigoProvincia+"' " +
                        "ORDER BY u.nom_dis_ubi " ;
//                        "IF (z.nom_zon IS NULL,u.nom_dis_ubi,'') ASC, " +
//                        "IF (z.nom_zon IS NOT NULL,z.nom_zon,'') ASC " +
                        
//                        "LIMIT "+inicio+","+cantidad+" ";
        ResultSet rs = st.executeQuery(query);
        while(rs.next()){
            ZonaDistrito zonaDistrito = new ZonaDistrito();
            zonaDistrito.setCodigoZonaDistrito(rs.getString("cod_zon_ubi"));
            zonaDistrito.setCodigoDepartamento(rs.getString("cod_dep_ubi"));
            zonaDistrito.setCodigoProvincia(rs.getString("cod_pro_ubi"));
            zonaDistrito.setCodigoDistrito(rs.getString("cod_dis_ubi"));
            zonaDistrito.setCodigoZona(rs.getString("cod_zon"));
            zonaDistrito.setNombreDepartamento(rs.getString("nom_dep_ubi"));
            zonaDistrito.setNombreProvincia(rs.getString("nom_pro_ubi"));
            zonaDistrito.setNombreDistrito(rs.getString("nom_dis_ubi"));
            zonaDistrito.setNombreZona(rs.getString("nom_zon"));
            lista.add(zonaDistrito);
        }
        } catch (Exception e){
            e.printStackTrace();
        } finally{
            cnx.close();
        }
        return lista;
    }

    @Override
    public int cantidadDistritoZonas(int codigoDepartamento, int codigoProvincia) throws Exception {
        int cantidad = 0;
        String base = "horario";
        
Connection cnx = null;
        try{
         cnx = MySqlDAOFactory.obtenerConexion(base);
        Statement st = cnx.createStatement();
        
        String query ="SELECT " +
                    "COUNT(*) as cantidad " +
                    "FROM " +
                    "ubigeo AS u " +
                    "LEFT JOIN zona_ubigeo AS zu ON u.cod_dep_ubi = zu.cod_dep  " +
                    "AND u.cod_pro_ubi = zu.cod_pro  " +
                    "AND u.cod_dis_ubi = zu.cod_dis AND zu.est_zon_ubi = '1' " +
                    "LEFT JOIN zona AS z ON z.cod_zon = zu.cod_zon " +
                    "WHERE " +
                    "u.cod_dep_ubi = '"+codigoDepartamento+"' AND " +
                    "u.cod_pro_ubi = '"+codigoProvincia+"'";
        ResultSet rs = st.executeQuery(query);
        
        if(rs.next()){
            cantidad= rs.getInt("cantidad");
        }
        } catch (Exception e){
            e.printStackTrace();
        } finally{
            cnx.close();
        }
        return cantidad;
    }

    @Override
    public int registrarAsignacionZonDist(String codigoZona, String codigoDepartemento, String codigoProvincia, String codigoDistrito) throws Exception {
        int dato = 0;
        String base = "horario";
        
Connection cnx = null;
        try{
         cnx = MySqlDAOFactory.obtenerConexion(base);
        Statement st = cnx.createStatement();
        String query = "INSERT INTO zona_ubigeo (cod_zon,cod_dep, cod_pro, cod_dis,est_zon_ubi) VALUES ('"+codigoZona+"', "
                + " '"+codigoDepartemento+"', '"+codigoProvincia+"', '"+codigoDistrito+"', '1')";
        
        dato = st.executeUpdate(query);
        } catch (Exception e){
            e.printStackTrace();
        } finally{
            cnx.close();
        }
        return dato;
    }

    @Override
    public int actualizarZonaDistrito(String codDepartamento, String codProvincia, String codigoDistrito, String codigoZona) throws Exception {
        int dato = 0;
        String base = "horario";
        
Connection cnx = null;
        try{
         cnx = MySqlDAOFactory.obtenerConexion(base);
        Statement st = cnx.createStatement();
        String query = "UPDATE zona_ubigeo SET cod_zon='"+codigoZona+"' WHERE cod_dep='"+codDepartamento+"' "
                + "AND cod_pro ='"+codProvincia+"' AND cod_dis='"+codigoDistrito+"' ";   
        System.out.println("actualizarZonaDistrito "+query);
        dato = st.executeUpdate(query);
        } catch (Exception e){
            e.printStackTrace();
        } finally{
            cnx.close();
        }
        return dato;
    }

    @Override
    public ZonaDistrito consultarZona(String codigoDocente) throws Exception {
        String base = "horario";
        
        ZonaDistrito zonaDistrito = new ZonaDistrito();
Connection cnx = null;
        try{
         cnx = MySqlDAOFactory.obtenerConexion(base);
        Statement st = cnx.createStatement();
        String query = "SELECT " +
                        "u.nom_dep_ubi, " +
                        "u.nom_pro_ubi, " +
                        "u.nom_dis_ubi, " +
                        "zona.nom_zon " +
                        "FROM " +
                        "datos_docente AS ddoc " +
                        "LEFT JOIN ubigeo AS u ON ddoc.cod_dep = u.cod_dep_ubi  " +
                        "AND ddoc.cod_dis = u.cod_dis_ubi AND ddoc.cod_prov = u.cod_pro_ubi " +
                        "LEFT JOIN zona_docente ON ddoc.cod_doc = zona_docente.cod_doc AND zona_docente.est_zon_doc ='1' " +
                        "LEFT JOIN zona ON zona_docente.cod_zon = zona.cod_zon " +
                        "WHERE ddoc.cod_doc ='"+codigoDocente+"' ";
        
        ResultSet rs = st.executeQuery(query);
        System.out.println(" consultarZona >>>> "+query);
        if(rs.next()){
            zonaDistrito.setNombreDepartamento(rs.getString("nom_dep_ubi"));
            zonaDistrito.setNombreProvincia(rs.getString("nom_pro_ubi"));
            zonaDistrito.setNombreDistrito(rs.getString("nom_dis_ubi"));
            zonaDistrito.setNombreZona(rs.getString("nom_zon"));
        }   
        } catch (Exception e){
            e.printStackTrace();
        } finally{
            cnx.close();
        }
        return zonaDistrito;
    }

    @Override
    public int actualizarZonaDocentes(String codDepartamento, String codProvincia, String codigoDistrito, String codigoZona) throws Exception {
        int dato = 0;
        String base = "horario";
        
Connection cnx = null;
        try{
         cnx = MySqlDAOFactory.obtenerConexion(base);
        Statement st = cnx.createStatement();
        String codigoDocente = "";
        String consultar = "SELECT " +
                            "cod_doc " +
                            "FROM " +
                            "datos_docente AS dd " +
                            "WHERE " +
                            "dd.cod_dep ='"+codDepartamento+"' AND " +
                            "dd.cod_prov='"+codProvincia+"' AND " +
                            "dd.cod_dis='"+codigoDistrito+"'";
         System.out.println("DDDDDDDDDDDDDDD "+consultar);
        ResultSet rs = st.executeQuery(consultar);
        String cadena = "";
        while(rs.next()){
            codigoDocente = rs.getString("cod_doc");
            if(cadena.equals("")){
                cadena = codigoDocente;
            }else{
                cadena +=","+codigoDocente;
            }
        }
        if(!cadena.equals("")){
            String codzona = "";
            String[] array = cadena.split(",");
            System.out.println("array "+array.length);
            for(int i = 0; i<array.length;i++){
                String codDocente = array[i];
                
                String queryCons = " SELECT cod_zon FROM zona_docente WHERE cod_doc = '"+codDocente+"'";
                
                ResultSet rs2 = st.executeQuery(queryCons);
                
                if(rs2.next()){
                    codzona = rs2.getString("cod_zon");
                }
                
                System.out.println("AAAAAAAAAAAAAA "+codzona);
                
                if(!codzona.equals("")){
                    String update = "UPDATE zona_docente SET cod_zon='"+codigoZona+"' WHERE cod_doc = '"+codDocente+"'";
                    System.out.println("actualizarZonaDocentes actualizar "+update);
                    dato = st.executeUpdate(update);
                }else{
                    String insert = "INSERT INTO zona_docente (cod_zon,cod_dep,cod_pro, cod_dis,  cod_doc,est_zon_doc) VALUES "
                            + "     ('"+codigoZona+"', '"+codDepartamento+"','"+codProvincia+"', '"+codigoDistrito+"', '"+codDocente+"' , '1')";
                    System.out.println("actualizarZonaDocentes insertar "+insert);
                    dato = st.executeUpdate(insert);
                }
            }
        }
       
        } catch (Exception e){
            e.printStackTrace();
        } finally{
            cnx.close();
        }
        
        return dato;
        
    }
    
}
