/**
 * 
 */
package com.rjconsultores.ventaboletos.relatorios.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.rjconsultores.ventaboletos.enums.StatusRecargaEnum;
import com.rjconsultores.ventaboletos.relatorios.utilitarios.ArrayDataSource;
import com.rjconsultores.ventaboletos.relatorios.utilitarios.Relatorio;
import com.rjconsultores.ventaboletos.utilerias.DateUtil;
import com.rjconsultores.ventaboletos.web.utilerias.NamedParameterStatement;

public class RelatorioRecargaRvhub extends Relatorio {


	public RelatorioRecargaRvhub(Map<String, Object> parametros, Connection conexao) throws Exception {
		super(parametros, conexao);

		this.setCustomDataSource(new ArrayDataSource(this) {
			@Override
			public void initDados() throws Exception {

				Connection conexao = this.relatorio.getConexao();
				Map<String, Object> parametros = this.relatorio.getParametros();

				StringBuilder sql = new StringBuilder();
				
				sql.append(" SELECT "); 
				sql.append("   rc.created_at as dataVenda, ");
				sql.append("   pv.nombpuntoventa, ");
				sql.append("   rc.area_code as ddd, ");
				sql.append("   rc.cell_phone_number as telefone, ");
				sql.append("   ee.impingreso as valor, ");
				sql.append("   rc.status, ");
				sql.append("   tee.desctipoevento as descTipoEvento, ");
				sql.append("   rc.rvhub_id as rvhubId, ");
				sql.append("   u.cveusuario as usuario ");
				sql.append(" FROM recarga_celular rc ");
				sql.append("   left join evento_extra ee on ee.eventoextra_id = rc.eventoextra_id ");
				sql.append("   left join punto_venta pv on ee.puntoventa_id = pv.puntoventa_id ");
				sql.append("   left join tipo_evento_extra tee on tee.tipoeventoextra_id = ee.tipoeventoextra_id ");
				sql.append("   left join usuario u on rc.usuario_id = u.usuario_id ");
				sql.append(" where created_at >= :datInicial ");
				sql.append(" and created_at <= :datFinal ");				
				sql.append(" and ee.empresa_id = :empresa_id ");
				
				if (parametros.get("NUMPUNTOVENTA") != null && !parametros.get("NUMPUNTOVENTA").equals("-1")) {
					sql.append(" and ee.puntoventa_id in ( "+parametros.get("NUMPUNTOVENTA")+" ) ");
				}
				
				if (parametros.get("STATUS") != null ) {
					sql.append(" and rc.status in ( "+parametros.get("STATUS")+" ) ");
				}
				
				sql.append(" order by rc.created_at, ");
				sql.append(" pv.nombpuntoventa, ");
				sql.append(" rc.status, ");
				sql.append(" rc.area_code, ");
				sql.append(" rc.cell_phone_number ");
				
				NamedParameterStatement stmt = new NamedParameterStatement(conexao, sql.toString());

				stmt.setInt("empresa_id", Integer.valueOf(parametros.get("EMPRESA_ID").toString()));
				stmt.setTimestamp("datInicial", new Timestamp(DateUtil.inicioFecha((Date) parametros.get("datInicial")).getTime()));
				stmt.setTimestamp("datFinal", new Timestamp(DateUtil.fimFecha((Date) parametros.get("datFinal")).getTime()));

				ResultSet rset = stmt.executeQuery();

				while (rset.next()) {

					Map<String, Object> dataResult = new HashMap<String, Object>();
					
					dataResult.put("dataVenda", rset.getDate("dataVenda"));
					dataResult.put("nombPuntoVenta", rset.getString("nombpuntoventa"));
					dataResult.put("ddd", rset.getString("ddd"));
					dataResult.put("telefone", rset.getString("telefone"));
					dataResult.put("valor", rset.getBigDecimal("valor"));
					dataResult.put("descTipoEvento", rset.getString("descTipoEvento"));
					dataResult.put("rvhubId", rset.getString("rvhubId"));
					dataResult.put("usuario", rset.getString("usuario"));					
					dataResult.put("status", StatusRecargaEnum.buscarPorCodigo(rset.getString("STATUS")).name());					

					this.dados.add(dataResult);

				}

				this.resultSet = rset;
			}
		});
	}

	@Override
	protected void processaParametros() throws Exception {

	}

}
