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

/**
 *
 * @author sistem02user
 */
public class MySqlZonaSedeDAO implements ZonaSedeDAO {

    @Override
    public ArrayList<ZonaSede> listarSedesZonas() throws Exception {

        ArrayList<ZonaSede> lista = new ArrayList<ZonaSede>();
        String base = "horario";

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

            String query = "SELECT\n"
                    + "	zona_sede.cod_zon_loc,\n"
                    + "	sede.des_loc,\n"
                    + "\n"
                    + "IF (\n"
                    + "	zona.nom_zon IS NULL,\n"
                    + "	'',\n"
                    + "	zona.nom_zon\n"
                    + ") AS nom_zon,\n"
                    + " sede.cod_loc,\n"
                    + "\n"
                    + "IF (\n"
                    + "	zona.cod_zon IS NULL,\n"
                    + "	'',\n"
                    + "	zona.cod_zon\n"
                    + ") AS cod_zon\n"
                    + "FROM\n"
                    + "	sede\n"
                    + "LEFT JOIN zona_sede ON zona_sede.cod_loc = sede.cod_loc\n"
                    + "LEFT JOIN zona ON zona.cod_zon = zona_sede.cod_zon\n"
                    + "WHERE\n"
                    + "	sede.cod_loc IN (SELECT cod_loc FROM sede WHERE est_loc = '1'\n"
                    + "	)\n"
                    + "AND sede.est_loc = '1'\n"
                    + "ORDER BY\n"
                    + "\n"
                    + "IF (\n"
                    + "	zona.nom_zon IS NULL,\n"
                    + "	sede.des_loc,\n"
                    + "	''\n"
                    + ") ASC,\n"
                    + "\n"
                    + "IF (\n"
                    + "	zona.nom_zon IS NOT NULL,\n"
                    + "	zona.nom_zon,\n"
                    + "	''\n"
                    + ") ASC;";
//            String query = "SELECT "
//                    + "zona_sede.cod_zon_loc, "
//                    + "sede.des_loc, "
//                    + "IF(zona.nom_zon IS NULL, '',zona.nom_zon) as nom_zon, "
//                    + "sede.cod_loc, "
//                    + "IF(zona.cod_zon IS NULL, '',zona.cod_zon) as cod_zon "
//                    + "FROM "
//                    + "sede "
//                    + "LEFT JOIN zona_sede ON zona_sede.cod_loc = sede.cod_loc "
//                    + "LEFT JOIN zona ON zona.cod_zon = zona_sede.cod_zon "
//                    + "WHERE sede.cod_loc IN (2,4,5,6,7,10,11,13,15,16,17,21,23,24,25,26,27,28,29,30,32,33,34,35,37,38,41,43) AND sede.est_loc='1' "
//                    + "ORDER BY "
//                    + "	IF(zona.nom_zon IS NULL,sede.des_loc,'') ASC, "
//                    + "	IF(zona.nom_zon IS NOT NULL,zona.nom_zon,'')ASC";
//            System.out.println("listarSedesZonas=> " + query);
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                ZonaSede zonaSede = new ZonaSede();
                zonaSede.setCodigoZonaSede(rs.getString("cod_zon_loc"));
                zonaSede.setCodigoSede(rs.getString("cod_loc"));
                zonaSede.setCodigoZona(rs.getString("cod_zon"));
                zonaSede.setNombreSede(rs.getString("des_loc"));
                zonaSede.setNombreZona(rs.getString("nom_zon"));
                lista.add(zonaSede);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public int registrarAsignacion(String codigoZona, String codigoSede) 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_sede (cod_zon, cod_loc, est_zon_loc) VALUES ('" + codigoZona + "', '" + codigoSede + "', '1')";

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

    @Override
    public int actualizarZonaSede(String codigoSede, 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_sede SET cod_zon='" + codigoZona + "' WHERE cod_loc='" + codigoSede + "'";

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

    @Override
    public ArrayList<ZonaSede> listarSedesZonas(String codigoZona) throws Exception {
        ArrayList<ZonaSede> lista = new ArrayList<ZonaSede>();
        String base = "horario";

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

            String query = "SELECT "
                    + "sede.des_loc "
                    + "FROM "
                    + "sede "
                    + "INNER JOIN zona_sede ON sede.cod_loc = zona_sede.cod_loc  "
                    + "AND zona_sede.cod_zon = '" + codigoZona + "'";
            ResultSet rs = st.executeQuery(query);
            System.out.println("listarSedesZonas 2 " + query);
            while (rs.next()) {
                ZonaSede zonaSede = new ZonaSede();
                zonaSede.setNombreSede(rs.getString("des_loc"));
                lista.add(zonaSede);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public ArrayList<ZonaSede> listarSedesConZona(String codigoZona) throws Exception {
        ArrayList<ZonaSede> lista = new ArrayList<ZonaSede>();
        String base = "horario";

        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "SELECT "
                    + "sede.des_loc, "
                    + "sede.cod_loc "
                    + "FROM "
                    + "zona_sede AS zs "
                    + "INNER JOIN sede ON sede.cod_loc = zs.cod_loc "
                    + "WHERE "
                    + "zs.cod_zon = '" + codigoZona + "' AND sede.est_loc='1'";
            ResultSet rs = st.executeQuery(query);
            System.out.println("listarSedesConZona " + query);
            while (rs.next()) {
                ZonaSede zonaSede = new ZonaSede();
                zonaSede.setNombreSede(rs.getString("des_loc"));
                zonaSede.setCodigoSede(rs.getString("cod_loc"));
                lista.add(zonaSede);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public int eliminarZonaSede(String codigoSede) throws Exception {
        int dato = 0;
        String base = "horario";

        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "DELETE FROM zona_sede WHERE zona_sede.cod_loc = '" + codigoSede + "'";
            dato = st.executeUpdate(query);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

}
