How to manage rooms availability based on days or months occupation

烂漫一生 提交于 2019-12-20 04:13:58

问题


I'm developing a Rails 3 application (with MySQL as RDMS) to manage availability/occupancy for some Rooms (it's an hotel-like app) In my case, the problem is I've to keep up in the system 2 different requirements because one room can be rented for:

  1. Some months (for example 6 monts)
  2. Some days/weeks/weekend

Clearly the app has to show for each room in a given date, if it is available or not, and which user is renting; and on the other side, one user can search one room for a given date range. The date range can be some fixed date or some months range (from September to March for instance) and I know this is something not related with the DataBase deisgn but I've to keep in mind.

To get this system working in a efficient way, I thought to create a table YEAR composite by a reference to the Room and an array (representing 1 year) of 365 elements when each element could be 0 or 1, when 0 means available and 1 means rented.

Adopting this solution I'll have to face with some problems:

  1. Return very quick results when an user is searching for a room during some months.
  2. Managing 'cross-years'ranges (for instance, from 20th of December of 2012 to 13rd of January of 2013)
  3. Leap years

and the last thing, I'm tying to avoid SQL procedure or mid-night processes (if it's possible)

I'm sure there are better ways to design this problem in SQL... Could some of you help me? or giving me some hint?


回答1:


Keep a list of room reservations which have a starting date and ending date. Don't try to model availability slots or think of your reservation database like a spreadsheet. This will just lead you to pointless complexity. Date ranges are easy to work with.

The most important thing to know is how to detect overlapping date ranges in your queries. This is the basis for testing whether a room is already reserved or whether it is free. Let's say you have a RESERVATION table and you want to find reservations that overlap a given date range: @FromDate and @ToDate. Your WHERE clause for finding overlapping reservations looks like this:

WHERE RESERVATION.start_date < @ToDate 
  AND RESERVATION.end_date > @FromDate

Available rooms won't have conflicts (i.e. WHERE NOT IN...) and unavailable rooms will have a conflict.



来源:https://stackoverflow.com/questions/10280333/how-to-manage-rooms-availability-based-on-days-or-months-occupation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!