package com.rjconsultores.ventaboletos.relatorios.impl;

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

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

public class RelatorioTabelaPreco extends Relatorio {

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

		this.setCustomDataSource(new ArrayDataSource(this) {

			public void initDados() throws Exception {
				Connection conexao = this.relatorio.getConexao();
				NamedParameterStatement stmt = new NamedParameterStatement(conexao, getSql());
				ResultSet rset = stmt.executeQuery();

				while (rset.next()) {
					Map<String, Object> dataResult = new HashMap<String, Object>();
				
					dataResult.put("origem", rset.getString("origem"));
					dataResult.put("destino", rset.getString("destino"));
					dataResult.put("tarifa", rset.getBigDecimal("tarifa"));
					dataResult.put("pedagio", rset.getBigDecimal("pedagio"));
					dataResult.put("taxaEmbarque", rset.getBigDecimal("taxaEmbarque"));
					dataResult.put("seguro", rset.getBigDecimal("seguro"));
					dataResult.put("outros", rset.getBigDecimal("outros"));
					dataResult.put("tipoClasse", rset.getString("tipoClasse"));
					dataResult.put("linha", rset.getString("linha"));
					dataResult.put("empresa", rset.getString("empresa"));
					dataResult.put("dataVigenciaInicial", rset.getDate("dataVigenciaInicial"));
					dataResult.put("dataVigenciaFinal", rset.getDate("dataVigenciaFinal"));
					dataResult.put("idLinha", rset.getInt("idLinha"));
					dataResult.put("idVigencia", rset.getInt("idVigencia"));
					
					this.dados.add(dataResult);
				}

				this.resultSet = rset;
			}
		});
	}

	@Override
	protected void processaParametros() throws Exception {
	}

	private String getSql() {
		StringBuilder sql = new StringBuilder();

		sql.append("SELECT pOrigem.DESCPARADA                                 as origem, ");
		sql.append("       pDestino.DESCPARADA                                as destino, ");
		sql.append("       COALESCE(t.precio,0)                               as tarifa, ");
		sql.append("       COALESCE(t.IMPORTEPEDAGIO, 0)                      as pedagio, ");
		sql.append("       COALESCE(t.IMPORTETAXAEMBARQUE, 0)                 as taxaEmbarque, ");
		sql.append("       COALESCE(t.IMPORTESEGURO, 0)                       as seguro, ");
		sql.append("       COALESCE(t.IMPORTEOUTROS, 0)                       as outros, ");
		sql.append("       cs.DESCCLASE                                       as tipoClasse, ");
		sql.append("       r.DESCRUTA                                         as linha, ");
		sql.append("       e.NOMBEMPRESA                                      as empresa, ");
		sql.append("       vt.FECINICIOVIGENCIA                               as dataVigenciaInicial, ");
		sql.append("       vt.FECFINVIGENCIA                                  as dataVigenciaFinal, ");
		sql.append("       vt.VIGENCIATARIFA_ID                               as idVigencia, ");
		sql.append("       vt.FECINICIOVIGENCIA                               as dataVigenciaInicial, ");
		sql.append("       r.RUTA_ID                                          as idLinha ");
		sql.append("FROM TARIFA t ");
		sql.append("INNER JOIN PARADA     pOrigem ON t.ORIGEN_ID           = pOrigem.PARADA_ID ");
		sql.append("INNER JOIN PARADA    pDestino ON t.DESTINO_ID          = pDestino.PARADA_ID ");
		sql.append("INNER JOIN CLASE_SERVICIO  cs ON cs.CLASESERVICIO_ID   = t.CLASESERVICIO_ID ");
		sql.append("INNER JOIN RUTA             r ON r.RUTA_ID             = t.RUTA_ID ");
		sql.append("INNER JOIN MARCA            m ON m.MARCA_ID            = t.MARCA_ID ");
		sql.append("INNER JOIN EMPRESA          e ON e.EMPRESA_ID          = m.EMPRESA_ID ");
		sql.append("INNER JOIN VIGENCIA_TARIFA vt ON vt.VIGENCIATARIFA_ID  = t.VIGENCIATARIFA_ID ");
		sql.append("INNER JOIN RUTA_COMBINACION rc ON rc.RUTA_ID           = r.RUTA_ID ");
		sql.append("INNER JOIN TRAMO            tr ON tr.TRAMO_ID          = rc.TRAMO_ID ");
		sql.append("INNER JOIN ORGAO_CONCEDENTE  o ON o.ORGAOCONCEDENTE_ID = r.ORGAOCONCEDENTE_ID ");
		sql.append("WHERE rc.INDVENTA = 1 ");
		sql.append("AND rc.ACTIVO = 1 ");
		sql.append("AND tr.ACTIVO = 1 ");
		sql.append("AND tr.ORIGEN_ID = t.ORIGEN_ID ");
		sql.append("AND tr.DESTINO_ID = t.DESTINO_ID ");
		
		if(parametros.get("VIGENCIA_ID") != null){
			sql.append(" AND vt.VIGENCIATARIFA_ID = " + parametros.get("VIGENCIA_ID"));
		}
		
		if (parametros.get("EMPRESA_ID") != null) {
			sql.append(" AND e.empresa_id = " + parametros.get("EMPRESA_ID"));
		}
		
		if (parametros.get("LINHAS_ID") != null) {
			sql.append(" AND r.RUTA_ID IN (" + parametros.get("LINHAS_ID") + ")");
		}
		
		if(parametros.get("ORGAO_CONCEDENTE_ID") != null){
			sql.append(" AND  r.ORGAOCONCEDENTE_ID = " + parametros.get("ORGAO_CONCEDENTE_ID"));
		}
		
		sql.append( "ORDER BY r.RUTA_ID, vt.FECINICIOVIGENCIA ");
		
        return sql.toString();
	}

}
