Hello!
I have a table with bookings for some rooms:
CREATE TABLE `booking` ( `id` int(11) NOT NULL, `room_id` int(11) DEFAULT NULL, `client_id` int(11) DEFAULT NULL, `timestamp_from` int(11) DEFAULT NULL, `timestamp_to` int(11) DEFAULT NULL, `status_id` int(11) DEFAULT NULL, `guests` int(11) DEFAULT NULL, `type` int(11) DEFAULT NULL, `tariff` int(11) DEFAULT NULL, `comment` text DEFAULT NULL, `created_by` int(11) DEFAULT NULL, `updated_by` int(11) DEFAULT NULL, `created_at` int(11) DEFAULT NULL, `updated_at` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
also a table of statuses:
CREATE TABLE `booking_status` ( `id` int(11) NOT NULL, `booking_id` int(11) DEFAULT NULL, `status` int(11) DEFAULT NULL, `created_by` int(11) DEFAULT NULL, `updated_by` int(11) DEFAULT NULL, `created_at` int(11) DEFAULT NULL, `updated_at` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
The task is as follows:
I need an SQL query that would take parameters:
- timestamp_from - start time of search
- timestamp_to - end time of search
- duration - duration (in seconds)
- slot_interval - slot interval (in seconds)
- by_room - whether to consider rooms
In response, the query should return all possible combinations of available "slots" for booking (available - the time for which there is currently no active booking for a specific room).
A slot is a time interval with a duration of duration, which has a start time, an end time, and a room number.
It should be noted that there are many rooms, and it is not necessary that if there is a booking record, all rooms are occupied at that time.
Also, it should be taken into account that bookings can be deleted (booking_status.status = 0), in which case we consider it as available time, and slots can be generated for it.
And importantly, if there is already an active booking, the slots should be generated taking into account a 30-minute interval before and after the already active booking.
The by_room parameter determines whether to return a specific room or not. If yes, the query should return all slots for each room separately. If not, the query should return completely available slots (for example, if there is a booking for one room at a certain time, but not for another - the query should return that slot as available)
Example (considering that we have only ONE room, and no others):
I have two bookings for ONE room:
1) 20.05.2024 10:00 - 20.05.2024 14:00
2) 20.05.2024 18:00 - 20.05.2024 22:00
I enter the following data in the query:
timestamp_from - 20.05.2024 10:00 (naturally in timestamp)
timestamp_to - 20.05.2024 23:00 (naturally in timestamp)
duration - 7200 (2 hours in seconds)
slot_interval - 1800 (30 minutes)
by_room - true
In response, I should get such an array (all dates should be in timestamps!!!):
room_id: 1, start: 20.05.2024 14:30, end: 20.05.2024 16:30;
room_id: 1, start: 20.05.2024 15:00, end: 20.05.2024 17:00;
room_id: 1, start: 20.05.2024 15:30, end: 20.05.2024 17:30;
If there was a deleted booking in any of these intervals, it would not be considered!!!!
If we had more rooms available, and there were no bookings on them - there would be plenty of slots available for the whole day.
Payment will be made after all tests from my side and adjustments from the performer's side.
The use of various AI is not allowed.