/*
 * 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 pe.siso.asistenciaAniversario.Beans.Conteo;
import pe.siso.asistenciaAniversario.DAO.ConteoDAO;

/**
 *
 * @author sistem13user
 */
public class MySqlConteoDAO implements ConteoDAO {

    @Override
    public int registrarTicket(String tipo) throws Exception {
        int dato = 0;
        String base = "aniversario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = " INSERT INTO conteo_ticket (tip_con, fec_con, est_con) VALUES ('" + tipo + "', CURRENT_TIMESTAMP(), 1); ";
            dato = st.executeUpdate(query);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

    @Override
    public String consultarEntradas() throws Exception {
        String dato = "";
        String base = "aniversario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = " SELECT GROUP_CONCAT(CONCAT(tip_con,'-',cont)) AS TOTALES FROM (  "
                    + "SELECT 1 AS 'cod', tip_con, count(tip_con) as cont FROM conteo_ticket  "
                    + "GROUP BY tip_con  "
                    + ") tabla  "
                    + "GROUP BY tabla.cod ";
            ResultSet rs = st.executeQuery(query);
            if (rs.next()) {
                dato = rs.getString("TOTALES");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;

    }

    @Override
    public int totalTicket() throws Exception {
        int dato = 0;
        String base = "aniversario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = " SELECT COUNT(tip_con) as cant FROM conteo_ticket WHERE tip_con <> 'NINGUNO' ";
            ResultSet rs = st.executeQuery(query);
            if (rs.next()) {
                dato = rs.getInt("cant");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return dato;
    }

    @Override
    public ArrayList<Conteo> consultarTotalEntradasPorTipo() throws Exception {
        ArrayList<Conteo> lista = new ArrayList<Conteo>();
        String base = "aniversario";
        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = " SELECT tip_con,COUNT(tip_con) as cant FROM conteo_ticket GROUP BY tip_con ORDER BY cod_con ";
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Conteo con = new Conteo();
                con.setTipo(rs.getString("tip_con"));
                con.setCantidad(rs.getInt("cant"));
                lista.add(con);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

}
