Commit d22c9817 authored by Ololw's avatar Ololw
Browse files

MAJ vehicules entre 2 dates + flex

parent 097a1c72
......@@ -8,5 +8,5 @@ import com.mycompany.myapp.domain.Vehicule;
public interface VehiculeRepositoryCustom
{
public List<Vehicule> findAllAvailableVehicules(long id, Instant startDate, Instant endDate);
public List<Vehicule>test(long idAgence, Instant startDate, Instant endDate, int nbJours);
public List<Vehicule> availableVehiculesFlex(long idAgence, Instant startDate, Instant endDate, int nbJours);
}
package com.mycompany.myapp.repository;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
......@@ -13,95 +14,179 @@ import com.mycompany.myapp.domain.Vehicule;
public class VehiculeRepositoryImpl implements VehiculeRepositoryCustom
{
@PersistenceContext
EntityManager entityManager;
EntityManager entityManager;
// Retourne les vehicules disponibles d'une agence entre deux dates
public List<Vehicule> findAllAvailableVehiculesrate(long idAgence, Instant startDate, Instant endDate)
{
Query query = entityManager.createNativeQuery("" + "SELECT * FROM mytransport.vehicule as vehicule "
+ "WHERE vehicule.agence_id = ? " + "AND NOT EXISTS( "
+ "SELECT id FROM mytransport.reservation as reservation WHERE reservation.vehicule_id = vehicule.id) "
+ "UNION " + "SELECT * FROM mytransport.vehicule as vehi " + "WHERE vehi.agence_id = ? "
+ "AND vehi.id NOT IN ( " + "SELECT DISTINCT id FROM mytransport.reservation "
+ "WHERE mytransport.reservation.start_date BETWEEN ? AND ? "
+ "OR mytransport.reservation.end_date BETWEEN ? AND ? " + ") " + "", Vehicule.class);
query.setParameter(1, idAgence);
query.setParameter(2, idAgence);
query.setParameter(3, startDate);
query.setParameter(4, endDate);
query.setParameter(5, startDate);
query.setParameter(6, endDate);
//Retourne les vehicules disponibles d'une agence entre deux dates
public List<Vehicule> findAllAvailableVehicules(long idAgence, Instant startDate, Instant endDate)
{
Query query = entityManager.createNativeQuery(""
+ "SELECT * FROM mytransport.vehicule as vehicule "
+ "WHERE vehicule.agence_id = ? "
+ "AND NOT EXISTS( "
+ "SELECT id FROM mytransport.reservation as reservation WHERE reservation.vehicule_id = vehicule.id) "
+ "UNION "
+ "SELECT * FROM mytransport.vehicule as vehi "
+ "WHERE vehi.agence_id = ? "
+ "AND vehi.id NOT IN ( "
+ "SELECT DISTINCT id FROM mytransport.reservation "
+ "WHERE mytransport.reservation.start_date BETWEEN ? AND ? "
+ "OR mytransport.reservation.end_date BETWEEN ? AND ? "
+ ") "
+ "", Vehicule.class);
query.setParameter(1, idAgence);
query.setParameter(2, idAgence);
query.setParameter(3, startDate);
query.setParameter(4, endDate);
query.setParameter(5, startDate);
query.setParameter(6, endDate);
return query.getResultList();
}
private List<Vehicule> listeVehicules(List<Reservation> reservations, Instant startDate , Instant endDate)
public List<Vehicule> findAllAvailableVehicules(long idAgence, Instant startDate, Instant endDate)
{
List<Vehicule> vehicules = new ArrayList<Vehicule>();
int compteurJours = 0;
Reservation reservation = reservations.get(0);
return vehicules;
List<Vehicule> vehicules;
List<Vehicule> vehiculesAEnvoyer = new ArrayList<Vehicule>();
List<Reservation> reservations;
Vehicule vehicule;
// On recupere les vehicules de l'agence idAgence
Query query = entityManager.createNativeQuery(
"" + "SELECT * FROM mytransport.vehicule as vehicule " + "WHERE vehicule.agence_id = ?",
Vehicule.class);
query.setParameter(1, idAgence);
vehicules = query.getResultList();
// Pour chaque vehicule
for (int i = 0; i < vehicules.size(); i++)
{
vehicule = vehicules.get(i);
// On recupere les reservations
/*query = entityManager.createNativeQuery(
"" + "SELECT * FROM mytransport.reservation as reservation " + "WHERE reservation.vehicule_id = ? "
+ "AND (reservation.start_date BETWEEN ? AND ? OR reservation.end_date BETWEEN ? AND ?) "
+ "ORDER BY reservation.start_date",
Reservation.class);*/
query = entityManager.createNativeQuery(
"" + "SELECT * FROM mytransport.reservation as reservation " + "WHERE reservation.vehicule_id = ? "
+ "AND (? BETWEEN reservation.start_date AND reservation.end_date OR ? BETWEEN reservation.start_date AND reservation.end_date) ",
Reservation.class);
query.setParameter(1, vehicule.getId());
query.setParameter(2, startDate);
query.setParameter(3, endDate);
reservations = query.getResultList();
if (reservations.isEmpty())
vehiculesAEnvoyer.add(vehicule);
}
return vehiculesAEnvoyer;
}
public List<Vehicule>test(long idAgence, Instant startDate, Instant endDate, int nbJours)
public boolean disponibiliteFlex(Instant startDate, Instant endDate, int nbJours, List<Reservation> reservations)
{
List<Vehicule> vehicules;
//On récupère les reservations pour les véhicules de cette agence, triés par id véhicule puis par startDate
//SELECT * FROM mytransport.reservation as reservation WHERE (reservation.start_date BETWEEN '2018-12-04 00:00:00' AND '2018-12-27 00:00:00' OR reservation.end_date BETWEEN '2018-12-04 00:00:00' AND '2018-12-27 00:00:00') AND EXISTS ( SELECT id from mytransport.vehicule as vehicule WHERE vehicule.agence_id = 2 AND reservation.vehicule_id = vehicule.id ) ORDER BY reservation.vehicule_id, reservation.start_date
Query query = entityManager.createNativeQuery(""
+ "SELECT * FROM mytransport.reservation as reservation "
+ "WHERE (reservation.start_date BETWEEN ? AND ? OR reservation.end_date BETWEEN ? AND ?) "
+ "AND EXISTS "
+ "( "
+ " SELECT id from mytransport.vehicule as vehicule WHERE vehicule.agence_id = ? "
+ "AND reservation.vehicule_id = vehicule.id "
+ ") "
+ "ORDER BY reservation.vehicule_id, reservation.start_date "
+ "", Reservation.class);
query.setParameter(1, startDate);
query.setParameter(2, endDate);
query.setParameter(3, startDate);
query.setParameter(4, endDate);
query.setParameter(5, idAgence);
List<Reservation> reservations = query.getResultList();
System.out.println("\n\n"+reservations+"\n\n");
//Cas où il n'y a pas de réservation -> tous les véhicules de l'agence sont disponibles
if(reservations.isEmpty())
Reservation reservation;
Reservation reservationNext;
if (reservations.isEmpty())
return true;
for (int i = 0; i < reservations.size(); i++)
{
query = entityManager.createNativeQuery("SELECT * FROM mytransport.vehicule as vehicule WHERE vehicule.agence_id = ?", Vehicule.class);
query.setParameter(1, idAgence);
vehicules = query.getResultList();
//TODO : retourner la liste contenant tous les véhicules de l'agence
reservation = reservations.get(i);
// Premiere reservation entre startDate et endDate
// Si il y a plus de nbJours de differences, alors le vehicule est disponible
if (i == 0)
{
Instant dateLimiteDebut = reservation.getStartDate().minus(nbJours, ChronoUnit.DAYS);
if (dateLimiteDebut.compareTo(startDate) > 0)
{
return true;
}
}
// Derniere reservation entre startDate et endDate
// Si il y a plus de nbJours de differences, alors le vehicule est disponible
if (i == reservations.size() - 1)
{
Instant dateLimiteFin = reservation.getEndDate().plus(nbJours, ChronoUnit.DAYS);
if (dateLimiteFin.compareTo(endDate) < 0)
{
return true;
}
}
// Sinon, on regarde si le vehicule est disponible entre deux reservations
else
{
reservationNext = reservations.get(i + 1);
if (reservation.getEndDate().plus(nbJours, ChronoUnit.DAYS)
.compareTo(reservationNext.getStartDate()) < 0)
{
return true;
}
}
}
else
// Si on est sorti de la boucle, aucun cas le vehicule n'est pas disponible
return false;
}
public List<Vehicule> availableVehiculesFlex(long idAgence, Instant startDate, Instant endDate, int nbJours)
{
List<Vehicule> vehicules;
List<Vehicule> vehiculesDisponible = new ArrayList<Vehicule>();
Vehicule vehicule;
List<Reservation> reservations;
// On recupere les vehicules de l'agence idAgence
Query query = entityManager.createNativeQuery(
"" + "SELECT * FROM mytransport.vehicule as vehicule " + "WHERE vehicule.agence_id = ?",
Vehicule.class);
query.setParameter(1, idAgence);
vehicules = query.getResultList();
// Pour chaque vehicule
for (int i = 0; i < vehicules.size(); i++)
{
vehicules = new ArrayList<Vehicule>();
vehicule = vehicules.get(i);
// On recupere les reservations
/*query = entityManager.createNativeQuery(
"" + "SELECT * FROM mytransport.reservation as reservation " + "WHERE reservation.vehicule_id = ? "
+ "AND (reservation.start_date BETWEEN ? AND ? OR reservation.end_date BETWEEN ? AND ?) "
+ "ORDER BY reservation.start_date",
Reservation.class);*/
query = entityManager.createNativeQuery(
"" + "SELECT * FROM mytransport.reservation as reservation " + "WHERE reservation.vehicule_id = ? "
+ "AND ( ? BETWEEN reservation.start_date AND reservation.end_date OR ? BETWEEN reservation.start_date AND reservation.end_date) "
+ "ORDER BY reservation.start_date",
Reservation.class);
query.setParameter(1, vehicule.getId());
query.setParameter(2, startDate);
query.setParameter(3, endDate);
reservations = query.getResultList();
if (disponibiliteFlex(startDate, endDate, nbJours, reservations))
{
vehiculesDisponible.add(vehicule);
}
}
return vehicules;
return vehiculesDisponible;
}
}
......@@ -16,6 +16,7 @@ import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
......@@ -156,11 +157,35 @@ public class VehiculeResource {
public List<Vehicule> getAllVehiculesAvailable(@RequestParam("id") long id, @RequestParam("startDate") Instant startDate, @RequestParam("endDate") Instant endDate) {
log.debug("REST request to get avaible Vehicules of agence "+ id + " between " + startDate + " to " + endDate);
vehiculeRepository.test(id, startDate,endDate,1);
if( endDate.compareTo(startDate) < 0 )
{
return null;
}
return vehiculeRepository.findAllAvailableVehicules(id, startDate, endDate);
}
/**
* GET /vehiculesAvailableFlex/:id/:startDate/:endDate/:nbJours : get all available vehicules, flexed for nbJours between startDate and endDate.
* @param id the id of the agency, startDate la date de début de réservation, endDate la date finale de la réservation, nbJours le nombre de jours disponible d'affilée entre endDate et startDate
* @return the ResponseEntity with status 200 (OK) and the list of vehicules in body
*/
@RequestMapping(value="/vehiculesAvailableFlex",params= {"id", "startDate", "endDate", "nbJours"},method = RequestMethod.GET)
@Timed
public List<Vehicule> getAllVehiculesAvailableFlex(@RequestParam("id") long id, @RequestParam("startDate") Instant startDate, @RequestParam("endDate") Instant endDate, @RequestParam("nbJours") int nbJours) {
log.debug("REST request to get avaible Vehicules of agence "+ id + " between " + startDate + " to " + endDate + "for" + nbJours);
if( endDate.minus(nbJours, ChronoUnit.DAYS).compareTo(startDate) < 0 )
{
return null;
}
return vehiculeRepository.availableVehiculesFlex(id, startDate, endDate, nbJours);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment