1 | //package com.example.rezevirajmasa.demo.bootstrap;
|
---|
2 | //
|
---|
3 | //import com.example.rezevirajmasa.demo.model.Reservation;
|
---|
4 | //import com.example.rezevirajmasa.demo.model.Restaurant;
|
---|
5 | //import com.example.rezevirajmasa.demo.model.TableEntity;
|
---|
6 | //import com.example.rezevirajmasa.demo.service.ReservationService;
|
---|
7 | //import com.example.rezevirajmasa.demo.service.RestaurantService;
|
---|
8 | //import com.example.rezevirajmasa.demo.service.TableService;
|
---|
9 | //import jakarta.annotation.PostConstruct;
|
---|
10 | //import org.springframework.beans.factory.annotation.Autowired;
|
---|
11 | //import org.springframework.boot.ApplicationArguments;
|
---|
12 | //import org.springframework.stereotype.Component;
|
---|
13 | //
|
---|
14 | //import java.time.LocalDate;
|
---|
15 | //import java.time.LocalDateTime;
|
---|
16 | //import java.time.LocalTime;
|
---|
17 | //import java.time.format.DateTimeFormatter;
|
---|
18 | //import java.util.ArrayList;
|
---|
19 | //import java.util.List;
|
---|
20 | //
|
---|
21 | //@Component
|
---|
22 | //public class DataInitialization {
|
---|
23 | // private final RestaurantService restaurantService;
|
---|
24 | // private final TableService tableService;
|
---|
25 | // private final ReservationService reservationService;
|
---|
26 | //
|
---|
27 | // public DataInitialization(RestaurantService restaurantService, TableService tableService, ReservationService reservationService) {
|
---|
28 | // this.restaurantService = restaurantService;
|
---|
29 | // this.tableService = tableService;
|
---|
30 | // this.reservationService = reservationService;
|
---|
31 | // }
|
---|
32 | //
|
---|
33 | //// @PostConstruct
|
---|
34 | // public void initData() {
|
---|
35 | // for(Restaurant restaurant : restaurantService.listall()) {
|
---|
36 | // extendTimeSlotsIndefinitely(restaurant);
|
---|
37 | // }
|
---|
38 | // }
|
---|
39 | //
|
---|
40 | // private void extendTimeSlotsIndefinitely(Restaurant restaurant) {
|
---|
41 | // while (true) {
|
---|
42 | // String[] hours = restaurant.getOperatingHours().split("-");
|
---|
43 | // LocalTime startTime = LocalTime.parse(hours[0], DateTimeFormatter.ofPattern("HH:mm"));
|
---|
44 | // LocalTime endTime = LocalTime.parse(hours[1], DateTimeFormatter.ofPattern("HH:mm"));
|
---|
45 | //
|
---|
46 | // // Get the current date and time
|
---|
47 | // LocalDateTime currentDateTime = LocalDateTime.now().with(startTime);
|
---|
48 | //
|
---|
49 | // // Calculate the end date (current date + 3 days)
|
---|
50 | // LocalDateTime endDate = LocalDateTime.now().with(endTime).plusDays(3);
|
---|
51 | //
|
---|
52 | // List<TableEntity> tables = restaurant.getTablesList();
|
---|
53 | // for (TableEntity table : tables) {
|
---|
54 | // List<LocalDateTime> existingTimeSlots = table.getTimeSlots();
|
---|
55 | // List<Reservation> existingReservations = reservationService.findReservationsByTableAndDateRange(table, currentDateTime, endDate);
|
---|
56 | // List<LocalDateTime> newTimeSlots = generateTimeSlots(currentDateTime, endDate, existingReservations, existingTimeSlots, startTime, endTime);
|
---|
57 | // existingTimeSlots.addAll(newTimeSlots);
|
---|
58 | // tableService.saveTable(table);
|
---|
59 | // }
|
---|
60 | //
|
---|
61 | // // Sleep for a period before generating time slots again
|
---|
62 | // try {
|
---|
63 | // // Sleep for 24 hours
|
---|
64 | // Thread.sleep(24 * 60 * 60 * 1000);
|
---|
65 | // } catch (InterruptedException e) {
|
---|
66 | // // Handle interrupted exception
|
---|
67 | // break;
|
---|
68 | // }
|
---|
69 | // }
|
---|
70 | // }
|
---|
71 | //
|
---|
72 | // private List<LocalDateTime> generateTimeSlots(LocalDateTime startDate, LocalDateTime endDate, List<Reservation> existingReservations, List<LocalDateTime> existingTimeSlots, LocalTime startTime, LocalTime endTime) {
|
---|
73 | // List<LocalDateTime> timeSlots = new ArrayList<>();
|
---|
74 | // LocalDateTime currentDateTime = startDate;
|
---|
75 | // LocalTime currentTime = startTime;
|
---|
76 | // LocalDate currentDate = LocalDate.now(); // Get the current date
|
---|
77 | //
|
---|
78 | // // Generate time slots until the end date
|
---|
79 | // while (currentDateTime.isBefore(endDate)) {
|
---|
80 | // // Ensure the current date is within the restaurant's opening hours
|
---|
81 | // if (currentDateTime.toLocalTime().isBefore(startTime)) {
|
---|
82 | // currentDateTime = currentDateTime.plusDays(1).with(startTime); // Move to the next day and set the start time
|
---|
83 | // continue; // Skip generating time slots for this day
|
---|
84 | // }
|
---|
85 | //
|
---|
86 | // final LocalDateTime currentTimeSlotStart = currentDateTime; // Declare a final local variable
|
---|
87 | //
|
---|
88 | // // Check if the current time slot overlaps with existing reservations
|
---|
89 | // boolean overlapsReservations = existingReservations.stream()
|
---|
90 | // .anyMatch(reservation -> isOverlapping(currentTimeSlotStart, currentTimeSlotStart.plusMinutes(15), reservation.getCheckInTime(), reservation.getCheckOutTime()));
|
---|
91 | // boolean overlapsExistingTimeSlots = existingTimeSlots.stream()
|
---|
92 | // .anyMatch(existingSlot -> isOverlapping(currentTimeSlotStart, currentTimeSlotStart.plusMinutes(15), existingSlot, existingSlot.plusMinutes(15)));
|
---|
93 | //
|
---|
94 | // if (!overlapsReservations && !overlapsExistingTimeSlots) {
|
---|
95 | // timeSlots.add(currentDateTime);
|
---|
96 | // }
|
---|
97 | //
|
---|
98 | // // Move to the next time slot (add 15 minutes
|
---|
99 | // currentDateTime = currentDateTime.plusMinutes(15);
|
---|
100 | // }
|
---|
101 | //
|
---|
102 | // return timeSlots;
|
---|
103 | // }
|
---|
104 | //
|
---|
105 | // private boolean isOverlapping(LocalDateTime start1, LocalDateTime end1, LocalDateTime start2, LocalDateTime end2) {
|
---|
106 | // return start1.isBefore(end2) && start2.isBefore(end1);
|
---|
107 | // }
|
---|
108 | //} |
---|