322 lines
16 KiB
Java
322 lines
16 KiB
Java
package com.rjconsultores.ventaboletos.service.impl;
|
||
|
||
import java.math.BigDecimal;
|
||
import java.util.ArrayList;
|
||
import java.util.Calendar;
|
||
import java.util.Collections;
|
||
import java.util.Comparator;
|
||
import java.util.List;
|
||
|
||
import org.apache.log4j.Logger;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import com.rjconsultores.ventaboletos.dao.RutaDAO;
|
||
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
||
import com.rjconsultores.ventaboletos.entidad.Parada;
|
||
import com.rjconsultores.ventaboletos.entidad.Ruta;
|
||
import com.rjconsultores.ventaboletos.entidad.RutaCombinacion;
|
||
import com.rjconsultores.ventaboletos.entidad.RutaEmpresa;
|
||
import com.rjconsultores.ventaboletos.entidad.RutaSecuencia;
|
||
import com.rjconsultores.ventaboletos.entidad.TramoServicio;
|
||
import com.rjconsultores.ventaboletos.entidad.Via;
|
||
import com.rjconsultores.ventaboletos.exception.BusinessException;
|
||
import com.rjconsultores.ventaboletos.service.RutaCombinacionService;
|
||
import com.rjconsultores.ventaboletos.service.RutaSecuenciaService;
|
||
import com.rjconsultores.ventaboletos.service.RutaService;
|
||
import com.rjconsultores.ventaboletos.service.TramoRutaService;
|
||
import com.rjconsultores.ventaboletos.service.TramoService;
|
||
import com.rjconsultores.ventaboletos.service.TramoServicioService;
|
||
import com.rjconsultores.ventaboletos.utilerias.DateUtil.Hora;
|
||
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
|
||
import com.rjconsultores.ventaboletos.vo.esquemaoperacional.RutaTramoVO;
|
||
import com.rjconsultores.ventaboletos.vo.esquemaoperacional.SecuenciaRutaTramoVO;
|
||
|
||
@Service("tramoRutaService")
|
||
public class TramoRutaServiceImpl implements TramoRutaService {
|
||
|
||
private static Logger log = Logger.getLogger(TramoRutaServiceImpl.class);
|
||
@Autowired
|
||
private RutaService rutaService;
|
||
@Autowired
|
||
private RutaCombinacionService rutaCombinacionService;
|
||
@Autowired
|
||
private RutaSecuenciaService rutaSecuenciaService;
|
||
@Autowired
|
||
private TramoService tramoService;
|
||
@Autowired
|
||
private TramoServicioService tramoServicioService;
|
||
@Autowired
|
||
private RutaDAO rutaDAO;
|
||
|
||
/**
|
||
* Hace la validaci<63>n de los datos del rutaTramoVO
|
||
* @param rutaTramoVO
|
||
* @throws BusinessException
|
||
*/
|
||
private void validarGenerarTramosRutas(RutaTramoVO rutaTramoVO) throws BusinessException {
|
||
if (rutaTramoVO.getLsSecuenciaRutaTramoVO().isEmpty()) {
|
||
throw new BusinessException("tramoRutaServiceImpl.msg.secuenciaObligatorio");
|
||
}
|
||
if ((rutaTramoVO.getPrefixo() == null) || (rutaTramoVO.getPrefixo().isEmpty())) {
|
||
throw new BusinessException("tramoRutaServiceImpl.msg.prefixoObligatorio");
|
||
}
|
||
if (rutaTramoVO.getLsEmpresa().isEmpty()) {
|
||
throw new BusinessException("tramoRutaServiceImpl.msg.empresaObligatoria");
|
||
}
|
||
|
||
if (rutaTramoVO.getLsCombinacionRutaTramoVO().isEmpty()) {
|
||
throw new BusinessException("tramoRutaServiceImpl.msg.combinacionObligatorio");
|
||
}
|
||
for (SecuenciaRutaTramoVO s : rutaTramoVO.getLsCombinacionRutaTramoVO()) {
|
||
if (s.getTramoId() != null) {
|
||
continue;
|
||
}
|
||
if ((s.getOrigen() == null) || (s.getDestino() == null) || (s.getVia() == null)
|
||
|| (s.getKmReal() == null) || (s.getSecuencia() == null) || (s.getTiempoRecorrido() == null)) {
|
||
throw new BusinessException("tramoRutaServiceImpl.msg.cambosObligatoriosCombinacion", new Object[]{s.getOrigen(),
|
||
s.getDestino()});
|
||
}
|
||
}
|
||
for (SecuenciaRutaTramoVO s : rutaTramoVO.getLsSecuenciaRutaTramoVO()) {
|
||
if (s.getTramoId() != null) {
|
||
continue;
|
||
}
|
||
if ((s.getOrigen() == null) || (s.getDestino() == null) || (s.getVia() == null)
|
||
|| (s.getKmReal() == null) || (s.getSecuencia() == null) || (s.getTiempoRecorrido() == null)) {
|
||
throw new BusinessException("tramoRutaServiceImpl.msg.cambosObligatoriosSecuencia", new Object[]{s.getOrigen(),
|
||
s.getDestino()});
|
||
}
|
||
}
|
||
}
|
||
|
||
@Transactional(rollbackFor = BusinessException.class)
|
||
@Override
|
||
public Ruta generarTramosRutas(RutaTramoVO rutaTramoVO) throws BusinessException {
|
||
|
||
this.validarGenerarTramosRutas(rutaTramoVO);
|
||
|
||
// Genero los tramos y tramos servicios de la combinacion que no existen
|
||
// Aqui si genera tambien los tramos de la secuencia
|
||
for (SecuenciaRutaTramoVO s : rutaTramoVO.getLsCombinacionRutaTramoVO()) {
|
||
log.debug("Origen:" + s.getOrigen() + ";Destino:" + s.getDestino() + ";Via:" + s.getVia());
|
||
boolean fueGenerado = tramoServicioService.generarTramoTiempo(s.getOrigen(), s.getDestino(), s.getVia(), s.getKmReal(),
|
||
s.getTiempoRecorrido().getFecha(), rutaTramoVO.getClaseServicio());
|
||
log.debug("Fue Generado:" + fueGenerado);
|
||
}
|
||
|
||
// Genero la relacion de ruta con empresa
|
||
List<RutaEmpresa> lsRutaEmpresa = new ArrayList<RutaEmpresa>();
|
||
for (Empresa e : rutaTramoVO.getLsEmpresa()) {
|
||
RutaEmpresa rutaEmpresa = new RutaEmpresa();
|
||
rutaEmpresa.setEmpresa(e);
|
||
rutaEmpresa.setActivo(Boolean.TRUE);
|
||
rutaEmpresa.setFecmodif(Calendar.getInstance().getTime());
|
||
rutaEmpresa.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
|
||
|
||
lsRutaEmpresa.add(rutaEmpresa);
|
||
}
|
||
|
||
// Genero la ruta
|
||
Ruta ruta = new Ruta();
|
||
ruta.setDescruta(rutaTramoVO.getDescRuta());
|
||
ruta.setClaseServicio(rutaTramoVO.getClaseServicio());
|
||
ruta.setOrgaoConcedente(rutaTramoVO.getOrgaoConcedente());
|
||
ruta.setIndNombreObligatorio(rutaTramoVO.getSolicitaNombrePasajero());
|
||
ruta.setVentaOffLine(rutaTramoVO.getVentaHandHeld());
|
||
ruta.setLsRutaEmpresa(lsRutaEmpresa);
|
||
ruta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
|
||
ruta.setActivo(Boolean.TRUE);
|
||
ruta.setFecmodif(new java.util.Date());
|
||
ruta.setPrefixo(rutaTramoVO.getPrefixo());
|
||
ruta.setNumRuta(rutaTramoVO.getNumRuta());
|
||
|
||
ruta = rutaDAO.suscribir(ruta);
|
||
|
||
// Genero la secuencia de la ruta
|
||
for (SecuenciaRutaTramoVO s : rutaTramoVO.getLsSecuenciaRutaTramoVO()) {
|
||
RutaSecuencia rutaSecuencia = new RutaSecuencia();
|
||
rutaSecuencia.setNumsecuencia(s.getSecuencia().shortValue());
|
||
rutaSecuencia.setTramo(tramoService.obtenerTramotPorOrigemDestinoVia(s.getOrigen(), s.getDestino(), s.getVia()));
|
||
rutaSecuencia.setRuta(ruta);
|
||
rutaSecuencia = rutaSecuenciaService.suscribir(rutaSecuencia);
|
||
}
|
||
|
||
for (SecuenciaRutaTramoVO s : rutaTramoVO.getLsCombinacionRutaTramoVO()) {
|
||
RutaCombinacion rutaCombinacion = new RutaCombinacion();
|
||
rutaCombinacion.setIndventa(Boolean.TRUE);
|
||
rutaCombinacion.setRuta(ruta);
|
||
rutaCombinacion.setTramo(tramoService.obtenerTramotPorOrigemDestinoVia(s.getOrigen(), s.getDestino(), s.getVia()));
|
||
|
||
rutaCombinacion = rutaCombinacionService.suscribir(rutaCombinacion);
|
||
}
|
||
|
||
return ruta;
|
||
}
|
||
|
||
@Override
|
||
public RutaTramoVO generarRutaRegreso(RutaTramoVO rutaTramoVO) {
|
||
RutaTramoVO rutaTramoRegresoVO = new RutaTramoVO();
|
||
rutaTramoRegresoVO.setClaseServicio(rutaTramoVO.getClaseServicio());
|
||
rutaTramoRegresoVO.setOrgaoConcedente(rutaTramoVO.getOrgaoConcedente());
|
||
rutaTramoRegresoVO.setSolicitaNombrePasajero(rutaTramoVO.getSolicitaNombrePasajero());
|
||
rutaTramoRegresoVO.setVentaHandHeld(rutaTramoVO.getVentaHandHeld());
|
||
rutaTramoRegresoVO.setLsEmpresa(rutaTramoVO.getLsEmpresa());
|
||
rutaTramoRegresoVO.setPrefixo(rutaTramoVO.getPrefixo());
|
||
if (rutaTramoVO.getDescRuta() != null) {
|
||
rutaTramoRegresoVO.setDescRuta(rutaTramoVO.getDescRuta());
|
||
} else {
|
||
rutaTramoRegresoVO.setDescRuta("-");
|
||
}
|
||
|
||
List<SecuenciaRutaTramoVO> lsSecuenciaRutaTramoRegresoVO = new ArrayList<SecuenciaRutaTramoVO>();
|
||
List<SecuenciaRutaTramoVO> lsSecuenciaRutaTramoIdaVO = new ArrayList<SecuenciaRutaTramoVO>(rutaTramoVO.getLsSecuenciaRutaTramoVO());
|
||
Collections.sort(lsSecuenciaRutaTramoIdaVO, new Comparator<SecuenciaRutaTramoVO>() {
|
||
|
||
@Override
|
||
public int compare(SecuenciaRutaTramoVO o1, SecuenciaRutaTramoVO o2) {
|
||
return o2.getSecuencia().compareTo(o1.getSecuencia());
|
||
}
|
||
});
|
||
|
||
int secuencia = 0;
|
||
|
||
for (SecuenciaRutaTramoVO s : lsSecuenciaRutaTramoIdaVO) {
|
||
TramoServicio tramoServicio = tramoServicioService.buscar(s.getOrigen(), s.getDestino(), s.getVia(), rutaTramoVO.getClaseServicio());
|
||
if (tramoServicio != null) {
|
||
s.setTramoId(tramoServicio.getTramo().getTramoId());
|
||
s.setTramoServicioId(tramoServicio.getTramoservicioId());
|
||
|
||
lsSecuenciaRutaTramoRegresoVO.add(new SecuenciaRutaTramoVO(s.getDestino(), s.getOrigen(), s.getVia(), ++secuencia, s.getKmReal(), s.getTiempoRecorrido(),
|
||
s.getTramoId(), s.getTramoServicioId()));
|
||
} else {
|
||
lsSecuenciaRutaTramoRegresoVO.add(new SecuenciaRutaTramoVO(s.getDestino(), s.getOrigen(), s.getVia(), ++secuencia, s.getKmReal(), s.getTiempoRecorrido()));
|
||
}
|
||
}
|
||
rutaTramoRegresoVO.setLsSecuenciaRutaTramoVO(lsSecuenciaRutaTramoRegresoVO);
|
||
|
||
List<SecuenciaRutaTramoVO> lsCombinacionRutaTramoRegresoVO = new ArrayList<SecuenciaRutaTramoVO>();
|
||
List<SecuenciaRutaTramoVO> lsCombinacionTramoIdaVO = new ArrayList<SecuenciaRutaTramoVO>(rutaTramoVO.getLsCombinacionRutaTramoVO());
|
||
Collections.sort(lsCombinacionTramoIdaVO, new Comparator<SecuenciaRutaTramoVO>() {
|
||
|
||
@Override
|
||
public int compare(SecuenciaRutaTramoVO o1, SecuenciaRutaTramoVO o2) {
|
||
return o2.getSecuencia().compareTo(o1.getSecuencia());
|
||
}
|
||
});
|
||
|
||
secuencia = 0;
|
||
for (SecuenciaRutaTramoVO s : lsCombinacionTramoIdaVO) {
|
||
TramoServicio tramoServicio = tramoServicioService.buscar(s.getOrigen(), s.getDestino(), s.getVia(), rutaTramoVO.getClaseServicio());
|
||
if (tramoServicio != null) {
|
||
s.setTramoId(tramoServicio.getTramo().getTramoId());
|
||
s.setTramoServicioId(tramoServicio.getTramoservicioId());
|
||
|
||
lsCombinacionRutaTramoRegresoVO.add(new SecuenciaRutaTramoVO(s.getDestino(), s.getOrigen(), s.getVia(), ++secuencia, s.getKmReal(), s.getTiempoRecorrido(),
|
||
s.getTramoId(), s.getTramoServicioId()));
|
||
} else {
|
||
lsCombinacionRutaTramoRegresoVO.add(new SecuenciaRutaTramoVO(s.getDestino(), s.getOrigen(), s.getVia(), ++secuencia, s.getKmReal(), s.getTiempoRecorrido()));
|
||
}
|
||
|
||
}
|
||
rutaTramoRegresoVO.setLsCombinacionRutaTramoVO(lsCombinacionRutaTramoRegresoVO);
|
||
|
||
return rutaTramoRegresoVO;
|
||
}
|
||
|
||
@Override
|
||
public List<SecuenciaRutaTramoVO> generarCombinacion(RutaTramoVO rutaTramoVO) {
|
||
List<SecuenciaRutaTramoVO> lsSecuenciaRutaTramoVO = new ArrayList<SecuenciaRutaTramoVO>(rutaTramoVO.getLsSecuenciaRutaTramoVO());
|
||
List<SecuenciaRutaTramoVO> lsSecuenciaCombinacionRutaTramoVO = new ArrayList<SecuenciaRutaTramoVO>();
|
||
|
||
Collections.sort(lsSecuenciaRutaTramoVO, new Comparator<SecuenciaRutaTramoVO>() {
|
||
|
||
@Override
|
||
public int compare(SecuenciaRutaTramoVO o1, SecuenciaRutaTramoVO o2) {
|
||
return o1.getSecuencia().compareTo(o2.getSecuencia());
|
||
}
|
||
});
|
||
// Collections.sort(lsSecuenciaCombinacionRutaTramoVO, new
|
||
// Comparator<SecuenciaRutaTramoVO>() {
|
||
//
|
||
// @Override
|
||
// public int compare(SecuenciaRutaTramoVO o1, SecuenciaRutaTramoVO o2)
|
||
// {
|
||
// return o1.getSecuencia().compareTo(o2.getSecuencia());
|
||
// }
|
||
// });
|
||
|
||
int qtdTramos = lsSecuenciaRutaTramoVO.size();
|
||
int secuencia = 0;
|
||
for (int i = 0; i < qtdTramos; i++) {
|
||
SecuenciaRutaTramoVO secuenciaInicial = lsSecuenciaRutaTramoVO.get(i);
|
||
Parada origen = secuenciaInicial.getOrigen();
|
||
BigDecimal totalKmsReal = secuenciaInicial.getKmReal();
|
||
Hora totalTiempoRecorrido = secuenciaInicial.getTiempoRecorrido();
|
||
boolean primeraCombinacion = true;
|
||
int j = i + 1;
|
||
for (; j < (qtdTramos); j++) {
|
||
Parada destino = lsSecuenciaRutaTramoVO.get(j).getOrigen();
|
||
|
||
Via via = secuenciaInicial.getVia();
|
||
// if (primeraCombinacion) {
|
||
// via = secuenciaInicial.getVia();
|
||
// }
|
||
|
||
SecuenciaRutaTramoVO nuevaSecuencia = new SecuenciaRutaTramoVO(origen, destino, via, ++secuencia,
|
||
totalKmsReal, totalTiempoRecorrido);
|
||
|
||
if (via != null) {
|
||
TramoServicio tramoServicio = tramoServicioService.buscar(origen, destino, via, rutaTramoVO.getClaseServicio());
|
||
if (tramoServicio != null) {
|
||
log.debug("TRAMO EXISTE");
|
||
|
||
nuevaSecuencia.setKmReal(tramoServicio.getTramo().getKmReal());
|
||
nuevaSecuencia.setTiempoRecorrido(new Hora(tramoServicio.getTiemporecorrido()));
|
||
nuevaSecuencia.setTramoId(tramoServicio.getTramo().getTramoId());
|
||
nuevaSecuencia.setTramoServicioId(tramoServicio.getTramoservicioId());
|
||
|
||
}
|
||
}
|
||
lsSecuenciaCombinacionRutaTramoVO.add(nuevaSecuencia);
|
||
|
||
totalKmsReal = totalKmsReal.add(lsSecuenciaRutaTramoVO.get(j).getKmReal());
|
||
totalTiempoRecorrido = totalTiempoRecorrido.sumar(lsSecuenciaRutaTramoVO.get(j).getTiempoRecorrido());
|
||
|
||
log.debug("Origen:" + nuevaSecuencia.getOrigen() + ";Destino:" + nuevaSecuencia.getDestino() + ";Via:"
|
||
+ nuevaSecuencia.getVia() + ";KmsReal:" + nuevaSecuencia.getKmReal() + ";hora:" + nuevaSecuencia.getTiempoRecorrido());
|
||
|
||
primeraCombinacion = false;
|
||
}
|
||
Parada destino = lsSecuenciaRutaTramoVO.get(qtdTramos - 1).getDestino();
|
||
|
||
Via via = secuenciaInicial.getVia();
|
||
// if (primeraCombinacion) {
|
||
// via = secuenciaInicial.getVia();
|
||
// }
|
||
|
||
SecuenciaRutaTramoVO nuevaSecuencia = new SecuenciaRutaTramoVO(origen, destino, via, ++secuencia,
|
||
totalKmsReal, totalTiempoRecorrido);
|
||
if (via != null) {
|
||
TramoServicio tramoServicio = tramoServicioService.buscar(origen, destino, via, rutaTramoVO.getClaseServicio());
|
||
if (tramoServicio != null) {
|
||
log.debug("TRAMO EXISTE");
|
||
|
||
nuevaSecuencia.setKmReal(tramoServicio.getTramo().getKmReal());
|
||
nuevaSecuencia.setTiempoRecorrido(new Hora(tramoServicio.getTiemporecorrido()));
|
||
nuevaSecuencia.setTramoId(tramoServicio.getTramo().getTramoId());
|
||
nuevaSecuencia.setTramoServicioId(tramoServicio.getTramoservicioId());
|
||
}
|
||
}
|
||
|
||
lsSecuenciaCombinacionRutaTramoVO.add(nuevaSecuencia);
|
||
log.debug("Origen:" + nuevaSecuencia.getOrigen() + ";Destino:" + nuevaSecuencia.getDestino() + ";Via:"
|
||
+ nuevaSecuencia.getVia() + ";KmsReal:" + nuevaSecuencia.getKmReal() + ";hora:" + nuevaSecuencia.getTiempoRecorrido());
|
||
|
||
}
|
||
|
||
return lsSecuenciaCombinacionRutaTramoVO;
|
||
}
|
||
}
|