219 lines
8.5 KiB
Java
219 lines
8.5 KiB
Java
package com.rjconsultores.ventaboletos.dao.hibernate;
|
||
|
||
import java.sql.Connection;
|
||
import java.sql.SQLException;
|
||
import java.sql.Statement;
|
||
import java.util.List;
|
||
|
||
import org.hibernate.Query;
|
||
import org.hibernate.SQLQuery;
|
||
import org.hibernate.SessionFactory;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.beans.factory.annotation.Qualifier;
|
||
import org.springframework.stereotype.Repository;
|
||
|
||
import com.rjconsultores.ventaboletos.dao.TarifaOficialDAO;
|
||
import com.rjconsultores.ventaboletos.dao.sqlbuilder.SQLBuilder;
|
||
import com.rjconsultores.ventaboletos.entidad.TarifaOficial;
|
||
import com.rjconsultores.ventaboletos.entidad.VigenciaTarifa;
|
||
|
||
@Repository("tarifaOficialDAO")
|
||
public class TarifaOficialHibernateDAO extends GenericHibernateDAO<TarifaOficial, Integer> implements TarifaOficialDAO {
|
||
|
||
private static Logger log = LoggerFactory.getLogger(TarifaOficialHibernateDAO.class);
|
||
|
||
@Autowired
|
||
private SQLBuilder sqlBuilder;
|
||
|
||
@Autowired
|
||
public TarifaOficialHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
|
||
setSessionFactory(factory);
|
||
}
|
||
|
||
@Override
|
||
public Integer gerarTarifaPorCoeficiente(Integer rutaId, Integer usuarioId, Integer orgaoConcedenteId, Integer emrpesaId) {
|
||
|
||
String sql = sqlBuilder.getSQLGerarTarifaOficial(rutaId, usuarioId, orgaoConcedenteId, emrpesaId);
|
||
|
||
int qtd = getSession().createSQLQuery(sql).executeUpdate();
|
||
|
||
return qtd;
|
||
}
|
||
|
||
@Override
|
||
public Integer gerarTarifaArtesp(Integer rutaId, Integer usuarioId, Integer orgaoConcedenteId, Integer emrpesaId) {
|
||
|
||
String sql = sqlBuilder.getSQLGerarTarifaOficialArtesp(rutaId, usuarioId, orgaoConcedenteId, emrpesaId);
|
||
|
||
int qtd = getSession().createSQLQuery(sql).executeUpdate();
|
||
|
||
return qtd;
|
||
}
|
||
|
||
@Override
|
||
public Integer gerarTabelaZerada(Integer rutaId, Integer usuarioId, Integer orgaoConcedenteId, Integer emrpesaId) {
|
||
|
||
String sql = sqlBuilder.getSQLGerarTabelaZerada(rutaId, usuarioId, orgaoConcedenteId, emrpesaId);
|
||
|
||
int qtd = getSession().createSQLQuery(sql).executeUpdate();
|
||
|
||
return qtd;
|
||
}
|
||
|
||
@Override
|
||
public Integer atualizarTarifaCoeficiente(Integer rutaId, Integer usuarioId, Integer orgaoConcedenteId) {
|
||
|
||
String sql = sqlBuilder.getSQLAtualizarTarifaOficial(rutaId, usuarioId, orgaoConcedenteId);
|
||
|
||
int qtd = getSession().createSQLQuery(sql).executeUpdate();
|
||
|
||
return qtd;
|
||
}
|
||
|
||
public void limparTarifasOficiais() {
|
||
Query query = getSession().createQuery("DELETE FROM TarifaOficial");
|
||
query.executeUpdate();
|
||
}
|
||
|
||
private void apagarTarifasInativas(VigenciaTarifa vigenciaTarifa){
|
||
StringBuilder sb = new StringBuilder("");
|
||
sb.append("select ");
|
||
sb.append(" tar.tarifaId ");
|
||
sb.append("from ");
|
||
sb.append(" Tarifa tar,TarifaOficial tao ");
|
||
sb.append("where ");
|
||
sb.append(" tar.activo=0 ");
|
||
sb.append(" and tao.activo = 1 ");
|
||
sb.append(" and tar.tramo=tao.tramo ");
|
||
sb.append(" and tar.marca=tao.marca ");
|
||
sb.append(" and tar.claseServicio =tao.claseServicio ");
|
||
sb.append(" and tar.moneda=tao.moneda ");
|
||
sb.append(" and tar.orgaoConcedente=tao.orgaoConcedente ");
|
||
sb.append(" and tar.ruta=tao.ruta ");
|
||
sb.append(" and tar.vigenciaTarifa.vigenciatarifaId = :vigenciaId ");
|
||
|
||
Query query = getSession().createQuery(sb.toString());
|
||
query.setParameter("vigenciaId", vigenciaTarifa.getVigenciatarifaId());
|
||
|
||
List<Integer> list = query.list();
|
||
|
||
if (!list.isEmpty()){
|
||
query = getSession().createQuery("DELETE FROM TarifaTipoptovta WHERE tarifa.tarifaId in (:ids)");
|
||
query.setParameterList("ids", list);
|
||
int qtd = query.executeUpdate();
|
||
log.info("qtd TarifaTipoptovta apagada = " + qtd);
|
||
|
||
query = getSession().createQuery("DELETE FROM TarifaCategoria WHERE tarifa.tarifaId in (:ids)");
|
||
query.setParameterList("ids", list);
|
||
qtd = query.executeUpdate();
|
||
log.info("qtd TarifaCategoria apagada = " + qtd);
|
||
|
||
query = getSession().createQuery("DELETE FROM Tarifa WHERE activo = 0 and tarifaId in (:ids)");
|
||
query.setParameterList("ids", list);
|
||
qtd = query.executeUpdate();
|
||
log.info("qtd Tarifa apagada = " + qtd);
|
||
|
||
|
||
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void copiarParaTarifa(VigenciaTarifa vigenciaTarifa, Integer usuarioId, Boolean calculaPegagio, Boolean calculaTarifa, Boolean calculaTaxaEmbarque) {
|
||
|
||
|
||
//Apago antes as tarifas que podem estar como activo =0
|
||
apagarTarifasInativas(vigenciaTarifa);
|
||
|
||
// Insiro as tarifas que n<>o existem
|
||
SQLQuery querySQL = getSession().createSQLQuery(sqlBuilder.getSQLInserirTarifaPelaTarifaOficial(vigenciaTarifa.getVigenciatarifaId(), usuarioId));
|
||
querySQL.executeUpdate();
|
||
// Atualizo o pre<72>o e o componente dos pre<72>os que j<> existem
|
||
querySQL = getSession().createSQLQuery(sqlBuilder.getSQLAtualizarTarifaPorTarifaOfical(vigenciaTarifa.getVigenciatarifaId(), usuarioId, calculaPegagio, calculaTarifa, calculaTaxaEmbarque));
|
||
querySQL.executeUpdate();
|
||
}
|
||
|
||
@Override
|
||
public void atualizarTaxaEmbarque(Integer rutaId, Integer usuarioId, Integer orgaoConcedenteId, Integer empresaId) {
|
||
|
||
// Atualizo a taxa de embarque de acordo a parada e km
|
||
SQLQuery query = getSession().createSQLQuery(sqlBuilder.getAtualizarTaxaEmbarquePorKmParada(rutaId, usuarioId, orgaoConcedenteId, empresaId));
|
||
query.executeUpdate();
|
||
|
||
// Atualizo a taxa de embarque de acordo a km do orgao
|
||
query = getSession().createSQLQuery(sqlBuilder.getSQLAtualizarTaxaEmbarquePorKmOrgao(rutaId, usuarioId, orgaoConcedenteId, empresaId));
|
||
query.executeUpdate();
|
||
|
||
// Atualizo a taxa de embarque de acordo a parada e valor fixo
|
||
query = getSession().createSQLQuery(sqlBuilder.getSQLAtualizarTaxaEmbarquePorParadaFixo(rutaId, usuarioId, orgaoConcedenteId, empresaId));
|
||
query.executeUpdate();
|
||
}
|
||
|
||
@Override
|
||
public void atualizarSeguroPorKm(Integer rutaId, Integer orgaoId, Integer usuarioId) {
|
||
SQLQuery query = getSession().createSQLQuery(sqlBuilder.getSQLAtualizarSeguroPorKm(rutaId, usuarioId, orgaoId));
|
||
query.executeUpdate();
|
||
}
|
||
|
||
@Override
|
||
public void atualizarSeguroPorTarifa(Integer rutaId, Integer orgaoId, Integer usuarioId) {
|
||
SQLQuery query = getSession().createSQLQuery(sqlBuilder.getSQLAtualizarSeguroPorTarifa(rutaId, usuarioId, orgaoId));
|
||
query.executeUpdate();
|
||
}
|
||
|
||
@Override
|
||
public void aplicarArredondamentoTarifa(Integer orgaoConcedenteId, Integer usuarioId, Boolean taxaEmbarque) {
|
||
StringBuilder sql = new StringBuilder();
|
||
sql.append(" UPDATE TarifaOficial tao ");
|
||
sql.append(" SET tao.precio = FN_ARREDONDAMENTO_TARIFA(tao.precio,tao.orgaoConcedente.orgaoConcedenteId,tao.importeseguro,tao.importetaxaembarque,tao.importepedagio,tao.importeoutros,:txEmbarque), ");
|
||
sql.append(" tao.activo = true , ");
|
||
sql.append(" tao.fecmodif= CURRENT_TIMESTAMP(), ");
|
||
sql.append(" tao.usuarioId =:usuarioId ");
|
||
|
||
if (orgaoConcedenteId != null) {
|
||
sql.append(" WHERE tao.orgaoConcedente.orgaoConcedenteId = :orgao ");
|
||
}
|
||
|
||
Query query = getSession().createQuery(sql.toString());
|
||
if (orgaoConcedenteId != null) {
|
||
query.setParameter("orgao", orgaoConcedenteId);
|
||
}
|
||
query.setInteger("txEmbarque", taxaEmbarque ? 1 : 0);
|
||
query.setParameter("usuarioId", usuarioId);
|
||
query.executeUpdate();
|
||
}
|
||
|
||
public void atualizarPedagio() {
|
||
StringBuilder sql = new StringBuilder();
|
||
sql.append(" merge into tarifa_oficial ");
|
||
sql.append(" using ");
|
||
sql.append(" ( ");
|
||
sql.append(" WITH tarifas_ofic as ( ");
|
||
sql.append(" select tarifaoficial_id, ORGAOCONCEDENTE_ID, ruta_id , origen_id , DESTINO_ID from tarifa_oficial ");
|
||
sql.append(" ), ");
|
||
sql.append(" peaje_tarifas as ( ");
|
||
sql.append(" select * from peaje p inner join tarifas_ofic tof on ");
|
||
sql.append(" p.ORGAOCONCEDENTE_ID = tof.ORGAOCONCEDENTE_ID and p.ruta_id = tof.ruta_id ");
|
||
sql.append(" and p.ORIGEN_ID = tof.ORIGEN_ID and p.DESTINO_ID = tof.DESTINO_ID and p.activo = 1 ");
|
||
sql.append(" ) ");
|
||
sql.append(" select pf.tarifaoficial_id , sum(importepeaje) as soma from peaje_tarifas pf Group by pf.tarifaoficial_id ");
|
||
sql.append(" ");
|
||
sql.append(" ) tarifa_importe ");
|
||
sql.append(" on (tarifa_oficial.tarifaoficial_id = tarifa_importe.tarifaoficial_id) ");
|
||
sql.append(" when matched then ");
|
||
sql.append(" update set ");
|
||
sql.append(" importepedagio = tarifa_importe.soma ");
|
||
|
||
Connection conn = getSession().connection();
|
||
Statement stmt;
|
||
try {
|
||
stmt = conn.createStatement();
|
||
stmt.executeUpdate(sql.toString());
|
||
} catch (SQLException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
} |