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.repository.ReservationRepository;
|
---|
7 | import com.example.rezevirajmasa.demo.repository.TableRepository;
|
---|
8 | import jakarta.annotation.PostConstruct;
|
---|
9 | import jakarta.annotation.PreDestroy;
|
---|
10 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
11 | import org.springframework.cglib.core.Local;
|
---|
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.Collections;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.stream.Collectors;
|
---|
22 |
|
---|
23 | @Component
|
---|
24 | public class TimeSlotManagementService {
|
---|
25 | @Autowired
|
---|
26 | private TableRepository tableRepository;
|
---|
27 | @Autowired
|
---|
28 | private ReservationRepository reservationRepository;
|
---|
29 |
|
---|
30 | @PostConstruct
|
---|
31 | public void initializeTimeSlots() {
|
---|
32 | LocalDate startDate = LocalDate.now();
|
---|
33 | initializeTimeSlotsForNextDays(startDate);
|
---|
34 | }
|
---|
35 |
|
---|
36 | private void initializeTimeSlotsForNextDays(LocalDate startDate) {
|
---|
37 | LocalDate currentDate = startDate;
|
---|
38 | for (int i = 0; i < 3; i++) {
|
---|
39 | Iterable<TableEntity> tableEntities = tableRepository.findAll();
|
---|
40 | for (TableEntity tableEntity : tableEntities) {
|
---|
41 | initializeTimeSlotsForTable(tableEntity, currentDate);
|
---|
42 | tableRepository.save(tableEntity);
|
---|
43 | }
|
---|
44 | currentDate = currentDate.plusDays(1);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private void initializeTimeSlotsForTable(TableEntity tableEntity, LocalDate date) {
|
---|
49 | // Get existing time slots for the table
|
---|
50 | List<LocalDateTime> existingTimeSlots = tableEntity.getTimeSlots().stream()
|
---|
51 | .filter(slot -> slot.toLocalDate().isEqual(date)) // Filter time slots for the specified date
|
---|
52 | .toList();
|
---|
53 |
|
---|
54 | // If existing time slots are not null and not empty, return without initializing new ones
|
---|
55 | if (!existingTimeSlots.isEmpty()) {
|
---|
56 | return;
|
---|
57 | }
|
---|
58 |
|
---|
59 | Restaurant restaurant = tableEntity.getRestaurant();
|
---|
60 | String[] hours = restaurant.getOperatingHours().split("-");
|
---|
61 | LocalTime startTime = LocalTime.parse(hours[0], DateTimeFormatter.ofPattern("HH:mm"));
|
---|
62 | LocalTime endTime = LocalTime.parse(hours[1], DateTimeFormatter.ofPattern("HH:mm"));
|
---|
63 |
|
---|
64 | List<LocalDateTime> reservationCheckIns = getReservationCheckInsAfterNow(date);
|
---|
65 |
|
---|
66 | tableEntity.initializeTimeSlots(startTime, endTime, Collections.singletonList(date), reservationCheckIns);
|
---|
67 | }
|
---|
68 |
|
---|
69 | private List<LocalDateTime> getReservationCheckInsAfterNow(LocalDate date) {
|
---|
70 | LocalDateTime currentTime = LocalDateTime.now();
|
---|
71 | List<Reservation> reservations = reservationRepository.findByCheckInTimeAfterAndCheckInTimeBefore(
|
---|
72 | LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
|
---|
73 | List<LocalDateTime> checkIns = new ArrayList<>();
|
---|
74 | for (Reservation reservation : reservations) {
|
---|
75 | if (reservation.getCheckInTime().isAfter(currentTime)) {
|
---|
76 | checkIns.add(reservation.getCheckInTime());
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return checkIns;
|
---|
80 | }
|
---|
81 | } |
---|