/*
 * 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.asistenciaAniversario.MySqlDAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.json.Json;
import org.json.JSONObject;
import pe.siso.asistenciaAniversario.Beans.Asistencia;
import pe.siso.asistenciaAniversario.DAO.AsistenciaDAO;

/**
 *
 * @author fcelestino
 */
public class MySqlAsistenciaDAO implements AsistenciaDAO {

	@Override
	public Asistencia consultarAsistencia(String busqueda) throws Exception {
		Asistencia asis = new Asistencia();
		String base = "nuevo";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "";

		return asis;
	}

	@Override
	public ArrayList<Asistencia> consultarExistencia(String busqueda) throws Exception {
		ArrayList<Asistencia> lista = new ArrayList<Asistencia>();
		String base = "nuevo";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = " SELECT codper,convert(cast(CONVERT(REPLACE(apellido,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as apellido, "
			+ "convert(cast(CONVERT(REPLACE(nombre,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as nombre ,dni,fotocheck,retirado, ifnull(cant, 0) as cant   "
			+ " FROM personal   "
			+ " WHERE   "
			+ " retirado='0' AND  "
			+ "(  "
			+ "	codper = '" + busqueda + "' OR  "
			+ "	apellido LIKE '%" + busqueda + "%' OR  "
			+ "	fotocheck ='" + busqueda + "' OR  "
			+ "	dni='" + busqueda + "'  "
			+ ") "
			+ "     ORDER BY  apellido, nombre ";
		System.out.println("QUERY EXISTENCIA --> " + query);
		ResultSet rs = st.executeQuery(query);
		while (rs.next()) {
			Asistencia asi = new Asistencia();
			asi.setCodigoPersonal(rs.getString("codper"));
			asi.setApellidos(rs.getString("apellido"));
			asi.setNombres(rs.getString("nombre"));
			asi.setDni(rs.getString("dni"));
			asi.setFotocheck(rs.getString("fotocheck"));
			asi.setCantidadNinios(rs.getString("cant"));
			lista.add(asi);
		}
		return lista;

	}

	@Override
	public int registrarAsistencia(String codigoPersona) throws Exception {
		int dato = 0;
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String queryConslta = " select count(cod_per) as cant FROM asistencia where cod_per='" + codigoPersona + "' AND DATE_FORMAT(mar_asi,'%Y-%m-%d')=CURDATE() ";
		ResultSet rs = st.executeQuery(queryConslta);
		if (rs.next()) {
			dato = rs.getInt("cant");
		}
		if (dato != 1) {
			String query = " INSERT INTO asistencia (cod_per,mar_asi,ali_asi,est_asi) VALUES ('" + codigoPersona + "',CURRENT_TIMESTAMP(),'0','1')";
			dato = st.executeUpdate(query);
		}

		return dato;
	}

	@Override
	public Asistencia consultarEstadosAsistencia(String codPerAsi) throws Exception {
		Asistencia asis = new Asistencia();
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = " SELECT cod_asi,cod_per,DATE_FORMAT(mar_asi, '%h:%i %p') as mar_asi, IFNULL(ali_asi, 0) as ali_asi,est_asi FROM asistencia WHERE cod_per='" + codPerAsi + "' AND DATE_FORMAT(mar_asi,'%Y-%m-%d')=CURDATE() ";
		System.out.println("query- -  " + query);
		ResultSet rs = st.executeQuery(query);
		if (rs.next()) {
			asis.setCodigoAsistencia(rs.getString("cod_asi"));
			asis.setCodigoPersonal(rs.getString("cod_per"));
			asis.setHoraMarcacion(rs.getString("mar_asi"));
			asis.setEstadoAlimentacion(rs.getString("ali_asi"));
			asis.setEstadoAsistencia(rs.getString("est_asi"));
		}
		System.out.println("ASISTENCIA ---> " + asis.getEstadoAlimentacion());
		return asis;
	}

	@Override
	public ArrayList<Asistencia> consultarDatosComision(String codper) throws Exception {
		ArrayList<Asistencia> lista = new ArrayList<Asistencia>();
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = " SELECT aniversario.cod_ani, aniversario.cod_per, detalle_aniversario.cod_com,  "
			+ "dias.fec_dia, detalle_aniversario.cod_tip, tipo_asistencia.nom_asi, detalle_aniversario.cod_hor,  "
			+ "detalle_aniversario.can_fun, DATE_FORMAT(horarios.ini_hor, '%h:%i %p') as ini_hor, DATE_FORMAT(horarios.fin_hor, '%h:%i %p') as fin_hor, horarios.des_hor,  "
			+ "horarios.alm_hor, horarios.lon_hor, horarios.cen_hor, comision.nom_com,  "
			+ "comision_responsables.cod_per as resp  "
			+ "FROM aniversario  "
			+ "INNER JOIN detalle_aniversario ON detalle_aniversario.cod_ani = aniversario.cod_ani  "
			+ "INNER JOIN dias ON dias.cod_dia = detalle_aniversario.cod_dia  "
			+ "LEFT JOIN horarios ON horarios.cod_hor = detalle_aniversario.cod_hor  "
			+ "LEFT JOIN tipo_asistencia ON tipo_asistencia.cod_asi = detalle_aniversario.cod_tip  "
			+ "INNER JOIN comision ON comision.cod_com = detalle_aniversario.cod_com  "
			+ "INNER JOIN comision_responsables ON comision_responsables.cod_com = comision.cod_com  "
			+ "WHERE aniversario.cod_per='" + codper + "' AND dias.fec_dia = CURDATE()";
		System.out.println("query  consulta de datos alv  :: -> " + query);
		ResultSet rs = st.executeQuery(query);
		while (rs.next()) {
			Asistencia asi = new Asistencia();
			asi.setCodigoPersonal(rs.getString("cod_per"));
			asi.setCodigoComision(rs.getString("cod_com"));
			asi.setDiaEvento(rs.getString("fec_dia"));
			asi.setCodigoTipoAsis(rs.getString("cod_tip"));
			asi.setNombreAsis(rs.getString("nom_asi"));
			asi.setCodHorario(rs.getString("cod_hor"));
			asi.setCadenaFunciones(rs.getString("can_fun"));
			asi.setHorarioInicio(rs.getString("ini_hor"));
			asi.setHorarioFinal(rs.getString("fin_hor"));
			asi.setDesayuno(rs.getString("des_hor"));
			asi.setAlmuerzo(rs.getString("alm_hor"));
			asi.setLonche(rs.getString("lon_hor"));
			asi.setCena(rs.getString("cen_hor"));
			asi.setComision(rs.getString("nom_com"));
			asi.setCodigoPersonalResponsable(rs.getString("resp"));
			lista.add(asi);
		}
		return lista;

	}

	@Override
	public String consultarDatosResponsable(String codResponsable) throws Exception {
		String responsables = "";
		String base = "nuevo";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = " SELECT codper,apellido,nombre   "
			+ " FROM personal   "
			+ " WHERE codper in (" + codResponsable + ") "
			+ "     ORDER BY  apellido, nombre ";
		ResultSet rs = st.executeQuery(query);
		while (rs.next()) {
			if (responsables.equals("")) {
				responsables = rs.getString("apellido") + ", " + rs.getString("nombre");
			} else {
				responsables += ";" + rs.getString("apellido") + ", " + rs.getString("nombre");
			}

		}

		return responsables;
	}

	@Override
	public int registrarEntragaAlimentacion(String codAsis) throws Exception {
		int dato = 0;
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "UPDATE asistencia SET ali_asi='1' WHERE cod_asi='" + codAsis + "' ";
		dato = st.executeUpdate(query);
		return dato;
	}

	@Override
	public int quitarAsistencia(String codAsis) throws Exception {
		int dato = 0;
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "DELETE FROM asistencia WHERE cod_asi='" + codAsis + "' ";
		dato = st.executeUpdate(query);
		return dato;
	}

	@Override
	public int quitarAlimentacion(String codAsis) throws Exception {
		int dato = 0;
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "UPDATE asistencia SET ali_asi='0' WHERE cod_asi='" + codAsis + "' ";
		dato = st.executeUpdate(query);
		return dato;
	}

	@Override
	public ArrayList<Asistencia> consultarExistenciaAsistencia(String busqueda, int inicio, int cantidad) throws Exception {
		ArrayList<Asistencia> lista = new ArrayList<Asistencia>();
		String base1 = "aniversario";
		Connection cnx1 = MySqlDAOFactory.obtenerConexion(base1);
		Statement st1 = cnx1.createStatement();
		String queryCod = " SELECT  cod_per FROM asistencia WHERE DATE_FORMAT(mar_asi,'%Y-%m-%d')=CURDATE() AND est_asi = '1'  ";
		ResultSet rs1 = st1.executeQuery(queryCod);
		String codPerAsis = "";
		while (rs1.next()) {
			if (codPerAsis.equals("")) {
				codPerAsis = rs1.getString("cod_per");
			} else {
				codPerAsis += "," + rs1.getString("cod_per");
			}
		}

		String base = "nuevo";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "";
		if (busqueda.equals("TODO")) {
			query = " SELECT codper,convert(cast(CONVERT(REPLACE(apellido,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as apellido,convert(cast(CONVERT(REPLACE(nombre,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as nombre,dni,fotocheck,retirado, IFNULL(cant, 0) as cant   "
				+ " FROM personal   "
				+ " WHERE   "
				+ " codper in (" + codPerAsis + ")   and "
				+ " retirado='0'   "
				+ " ORDER BY  apellido, nombre LIMIT " + inicio + "," + cantidad + "";
		} else {
			query = " SELECT codper,convert(cast(CONVERT(REPLACE(apellido,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as apellido,convert(cast(CONVERT(REPLACE(nombre,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as nombre,dni,fotocheck,retirado, IFNULL(cant, 0) as cant   "
				+ " FROM personal   "
				+ " WHERE   "
				+ " codper in (" + codPerAsis + ")   and "
				+ " retirado='0' AND  "
				+ "(  "
				+ "	codper = '" + busqueda + "' OR  "
				+ "	apellido LIKE '%" + busqueda + "%' OR  "
				+ "	fotocheck ='" + busqueda + "' OR  "
				+ "	dni='" + busqueda + "'  "
				+ ") "
				+ "     ORDER BY  apellido, nombre LIMIT " + inicio + "," + cantidad + " ";
		}

		System.out.println("-x-x-> " + query);
		ResultSet rs = st.executeQuery(query);
		while (rs.next()) {
			Asistencia asi = new Asistencia();
			asi.setCodigoPersonal(rs.getString("codper"));
			asi.setApellidos(rs.getString("apellido"));
			asi.setNombres(rs.getString("nombre"));
			asi.setDni(rs.getString("dni"));
			asi.setFotocheck(rs.getString("fotocheck"));
			asi.setCantidadNinios(rs.getString("cant"));
			lista.add(asi);
		}
		return lista;
	}

	@Override
	public int cantidadExistenciaAsistencia(String busqueda) throws Exception {
		int cant = 0;
		String base1 = "aniversario";
		Connection cnx1 = MySqlDAOFactory.obtenerConexion(base1);
		Statement st1 = cnx1.createStatement();
		String queryCod = " SELECT  cod_per FROM asistencia WHERE DATE_FORMAT(mar_asi,'%Y-%m-%d')=CURDATE() AND est_asi = '1'  ";
		ResultSet rs1 = st1.executeQuery(queryCod);
		String codPerAsis = "";
		while (rs1.next()) {
			if (codPerAsis.equals("")) {
				codPerAsis = rs1.getString("cod_per");
			} else {
				codPerAsis += "," + rs1.getString("cod_per");
			}
		}

		String base = "nuevo";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "";
		if (busqueda.equals("TODO")) {
			query = " SELECT count(codper) as cant   "
				+ " FROM personal   "
				+ " WHERE   "
				+ " codper in (" + codPerAsis + ")   and "
				+ " retirado='0'   ";
		} else {
			query = " SELECT count(codper) as cant   "
				+ " FROM personal   "
				+ " WHERE   "
				+ " codper in (" + codPerAsis + ")   and "
				+ " retirado='0' AND  "
				+ "(  "
				+ "	codper = '" + busqueda + "' OR  "
				+ "	apellido LIKE '%" + busqueda + "%' OR  "
				+ "	fotocheck ='" + busqueda + "' OR  "
				+ "	dni='" + busqueda + "'  "
				+ ")  ";
		}

		System.out.println("-cant-> " + query);
		ResultSet rs = st.executeQuery(query);
		if (rs.next()) {
			cant = rs.getInt("cant");
		}
		return cant;
	}

	@Override
	public ArrayList<Asistencia> reporteAsistenciaAlimentacion() throws Exception {
		ArrayList<Asistencia> lista = new ArrayList<Asistencia>();
		String base1 = "aniversario";
		Connection cnx1 = MySqlDAOFactory.obtenerConexion(base1);
		Statement st1 = cnx1.createStatement();
		String query = " SELECT  "
			+ "comision.cod_com,  "
			+ "comision.nom_com,  "
			+ "COUNT(detalle_aniversario.cod_tip) as cantComi,  "
			+ "COUNT(asistencia.est_asi) as cantAsis,  "
			+ "(COUNT(detalle_aniversario.cod_tip)-COUNT(asistencia.est_asi)) as noAsis,  "
			+ "if(SUM(asistencia.ali_asi) is null, '0',SUM(asistencia.ali_asi) )as canAli,  "
			+ "if(COUNT(asistencia.est_asi)-SUM(asistencia.ali_asi) is null,'0',COUNT(asistencia.est_asi)-SUM(asistencia.ali_asi)) AS noAli  "
			+ "FROM  "
			+ "comision  "
			+ "LEFT JOIN dias ON dias.fec_dia= CURDATE()  "
			+ "LEFT JOIN detalle_aniversario ON detalle_aniversario.cod_com = comision.cod_com AND detalle_aniversario.cod_tip = '1'  AND dias.cod_dia = detalle_aniversario.cod_dia "
			+ "LEFT JOIN aniversario ON aniversario.cod_ani = detalle_aniversario.cod_ani   "
			+ "LEFT JOIN asistencia ON asistencia.cod_per = aniversario.cod_per AND DATE_FORMAT(asistencia.mar_asi,'%Y-%m-%d')= CURDATE()  "
			+ "WHERE  "
			+ "comision.est_com='1'  "
			+ "GROUP BY comision.cod_com  "
			+ "ORDER BY comision.nom_com";
		ResultSet rs1 = st1.executeQuery(query);
		while (rs1.next()) {
			Asistencia asi = new Asistencia();
			asi.setComision(rs1.getString("nom_com"));
			asi.setCantidadColaboradores(rs1.getString("cantComi"));
			asi.setCantidadAsistentes(rs1.getString("cantAsis"));
			asi.setCantidadFaltantes(rs1.getString("noAsis"));
			asi.setCantidadEntregados(rs1.getString("canAli"));
			asi.setCantidadSinEntregados(rs1.getString("noAli"));
			lista.add(asi);
		}

		return lista;
	}

	@Override
	public ArrayList<Asistencia> reporteAlimentacion() throws Exception {
		ArrayList<Asistencia> lista = new ArrayList<Asistencia>();
		String base1 = "aniversario";
		Connection cnx1 = MySqlDAOFactory.obtenerConexion(base1);
		Statement st1 = cnx1.createStatement();
		String query = " SELECT  "
			+ "comision.cod_com,  "
			+ "comision.nom_com,  "
			+ "detalle_aniversario.cod_dia,  "
			+ "detalle_aniversario.cod_tip,  "
			+ "detalle_aniversario.cod_hor,  "
			+ "DATE_FORMAT(dias.fec_dia, '%d %b %Y') AS fec_dia,  "
			+ "	IF(horarios.des_hor is not null,SUM(horarios.des_hor),'0') AS des_hor,  "
			+ "	IF(horarios.alm_hor is not null,SUM(horarios.alm_hor),'0') AS alm_hor,  "
			+ "	IF(horarios.lon_hor is not null,SUM(horarios.lon_hor),'0') AS lon_hor,  "
			+ "	IF(horarios.cen_hor is not null,SUM(horarios.cen_hor),'0') AS cen_hor,  "
			+ "	IF(sum(asistencia.ali_asi) is NULL,'0',sum(asistencia.ali_asi)) as emtregado,  "
			+ "if(sum(asistencia.ali_asi) is null ,'0',sum( if( horarios.des_hor=1 && asistencia.ali_asi=1,1,0))) as  des,  "
			+ "if(sum(asistencia.ali_asi) is null ,'0',sum( if( horarios.alm_hor=1 && asistencia.ali_asi=1,1,0))) as  alm,  "
			+ "if(sum(asistencia.ali_asi) is null ,'0',sum( if( horarios.lon_hor=1 && asistencia.ali_asi=1,1,0))) as  lon,  "
			+ "if(sum(asistencia.ali_asi) is null ,'0',sum( if( horarios.cen_hor=1 && asistencia.ali_asi=1,1,0))) as  cen,  "
			+ "if(SUM(asistencia.est_asi) is null,'0',SUM(asistencia.est_asi)) as asistieron  "
			+ "FROM  "
			+ "comision  "
			+ "LEFT JOIN dias ON  dias.fec_dia = CURDATE()   "
			+ "LEFT JOIN detalle_aniversario ON detalle_aniversario.cod_com = comision.cod_com AND detalle_aniversario.cod_tip = '1'  AND dias.cod_dia = detalle_aniversario.cod_dia   "
			+ "LEFT JOIN horarios ON horarios.cod_hor = detalle_aniversario.cod_hor  "
			+ "LEFT JOIN aniversario ON aniversario.cod_ani = detalle_aniversario.cod_ani  "
			+ "LEFT JOIN asistencia ON asistencia.cod_per = aniversario.cod_per AND DATE_FORMAT(asistencia.mar_asi,'%Y-%m-%d')= CURDATE()  and asistencia.est_asi='1'  "
			+ "WHERE comision.est_com='1'    "
			+ "GROUP BY comision.cod_com  "
			+ "ORDER BY comision.nom_com";
		System.out.println("queryquery --->>> " + query);
		ResultSet rs1 = st1.executeQuery(query);
		while (rs1.next()) {
			Asistencia asi = new Asistencia();
			asi.setCodigoComision(rs1.getString("cod_com"));
			asi.setComision(rs1.getString("nom_com"));
			asi.setDesayuno(rs1.getString("des_hor"));
			asi.setAlmuerzo(rs1.getString("alm_hor"));
			asi.setLonche(rs1.getString("lon_hor"));
			asi.setCena(rs1.getString("cen_hor"));
			asi.setCantidadEntregados(rs1.getString("emtregado"));
			asi.setDesayunoEn(rs1.getString("des"));
			asi.setAlmuerzoEn(rs1.getString("alm"));
			asi.setLoncheEn(rs1.getString("lon"));
			asi.setCenaEn(rs1.getString("cen"));
			asi.setCantidadAsistentes(rs1.getString("asistieron"));
			lista.add(asi);
		}

		return lista;
	}

	@Override
	public int codigoDia() throws Exception {
		int cod_dia = 0;
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String queryCod = " SELECT cod_dia FROM dias WHERE fec_dia=CURDATE() ";
		ResultSet rs = st.executeQuery(queryCod);
		if (rs.next()) {
			cod_dia = rs.getInt("cod_dia");
		}
		return cod_dia;
	}

	@Override
	public int registrarExtras(String codDia, String resp, String des, String alm, String lon, String cen) throws Exception {
		int dato = 0;
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "INSERT INTO alimentacion_extra (cod_dia, nom_resp , des_ext , alm_ext , lon_ext , cen_ext , est_ext) "
			+ "VALUES ('" + codDia + "', '" + resp + "' , '" + des + "' , '" + alm + "' , '" + lon + "' , '" + cen + "' , '1') ";
		dato = st.executeUpdate(query);
		return dato;
	}

	@Override
	public ArrayList<Asistencia> personalActivo() throws Exception {
		ArrayList<Asistencia> lista = new ArrayList<Asistencia>();
		String base = "nuevo";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "SELECT codper,convert(cast(CONVERT(REPLACE(apellido,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as apellido, "
			+ "convert(cast(CONVERT(REPLACE(nombre,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as nombre "
			+ "FROM personal "
			+ "WHERE retirado=0 "
			+ "ORDER BY apellido,nombre ";
		ResultSet rs = st.executeQuery(query);
		while (rs.next()) {
			Asistencia asi = new Asistencia();
			asi.setCodigoPersonal(rs.getString("codper"));
			asi.setApellidos(rs.getString("apellido"));
			asi.setNombres(rs.getString("nombre"));
			lista.add(asi);
		}

		return lista;
	}

	@Override
	public ArrayList<Asistencia> reporteExtras() throws Exception {
		ArrayList<Asistencia> lista = new ArrayList<Asistencia>();
		int cod_dia = 0;
		String base = "aniversario";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String queryCod = " SELECT cod_dia FROM dias WHERE fec_dia=CURDATE() ";
		ResultSet rs = st.executeQuery(queryCod);
		if (rs.next()) {
			cod_dia = rs.getInt("cod_dia");
		}

		String query = " SELECT nom_resp,des_ext,alm_ext,lon_ext,cen_ext FROM alimentacion_extra WHERE cod_dia='" + cod_dia + "' ";
		ResultSet rs1 = st.executeQuery(query);
		while (rs1.next()) {
			Asistencia asi = new Asistencia();
			asi.setNombres(rs1.getString("nom_resp"));
			asi.setDesayuno(rs1.getString("des_ext"));
			asi.setAlmuerzo(rs1.getString("alm_ext"));
			asi.setLonche(rs1.getString("lon_ext"));
			asi.setCena(rs1.getString("cen_ext"));
			lista.add(asi);
		}
		return lista;
	}

	@Override
	public ArrayList<Asistencia> consultarExistenciaAsistencia(String busqueda) throws Exception {
		ArrayList<Asistencia> lista = new ArrayList<Asistencia>();
		String base1 = "aniversario";
		Connection cnx1 = MySqlDAOFactory.obtenerConexion(base1);
		Statement st1 = cnx1.createStatement();
		String queryCod = " SELECT  cod_per FROM asistencia WHERE DATE_FORMAT(mar_asi,'%Y-%m-%d')=CURDATE() AND est_asi = '1'  ";
		ResultSet rs1 = st1.executeQuery(queryCod);
		String codPerAsis = "";
		while (rs1.next()) {
			if (codPerAsis.equals("")) {
				codPerAsis = rs1.getString("cod_per");
			} else {
				codPerAsis += "," + rs1.getString("cod_per");
			}
		}

		String base = "nuevo";
		Connection cnx = MySqlDAOFactory.obtenerConexion(base);
		Statement st = cnx.createStatement();
		String query = "";
		if (busqueda.equals("TODO")) {
			query = " SELECT codper,convert(cast(CONVERT(REPLACE(apellido,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as apellido,convert(cast(CONVERT(REPLACE(nombre,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as nombre,dni,fotocheck,retirado   "
				+ " FROM personal   "
				+ " WHERE   "
				+ " codper in (" + codPerAsis + ")   and "
				+ " retirado='0'   "
				+ " ORDER BY  apellido, nombre ";
		} else {
			query = " SELECT codper,convert(cast(CONVERT(REPLACE(apellido,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as apellido,convert(cast(CONVERT(REPLACE(nombre,'Ñ','Ã‘') USING latin1)as BINARY) USING utf8) as nombre,dni,fotocheck,retirado   "
				+ " FROM personal   "
				+ " WHERE   "
				+ " codper in (" + codPerAsis + ")   and "
				+ " retirado='0' AND  "
				+ "(  "
				+ "	codper = '" + busqueda + "' OR  "
				+ "	apellido LIKE '%" + busqueda + "%' OR  "
				+ "	fotocheck ='" + busqueda + "' OR  "
				+ "	dni='" + busqueda + "'  "
				+ ") "
				+ "     ORDER BY  apellido, nombre  ";
		}

		System.out.println("-x-x-> " + query);
		ResultSet rs = st.executeQuery(query);
		while (rs.next()) {
			Asistencia asi = new Asistencia();
			asi.setCodigoPersonal(rs.getString("codper"));
			asi.setApellidos(rs.getString("apellido"));
			asi.setNombres(rs.getString("nombre"));
			asi.setDni(rs.getString("dni"));
			asi.setFotocheck(rs.getString("fotocheck"));
			lista.add(asi);
		}
		return lista;
	}

}
