计算跨多个季节类型的住宿总费用(票价)

Calculate Total Fee for Accommodation Stay Across Multiple Season Types (Fares)

我正在为客户开发一个住宿预订平台,该平台需要使用基于日期范围的动态定价。有多个定价范围,称为季节。例如,旺季、中季和淡季可以有不同级别的定价。

我还应该声明我正在使用 CakePHP - 尽管这不会真正影响它 - 所以我不一定会进行核心 MySQL 查询,但无论如何我都会非常感谢这些类型的答案,因为我也许能够在 Cake 中实现原始 MySQL 查询。

这是我的场景:

定价并不像每晚 x 那样简单,但现在这无关紧要。

假设高峰时段每晚 50 美元,中段时段每晚 25 美元,如何最好地计算定价?将日期范围和相应成本存储到数组(并遍历它们)的解决方案是否会比 returns 成本总和的原始 MySQL 查询更好?根据一年中的预订期,大约有 5 种季节类型,我认为返回最相关的季节类型可能很棘手。

非常感谢任何关于这一挑战的线索。

亲切的问候, 西蒙

我不想告诉你什么是 'best' 但我会给你一些建议

DROP TABLE IF EXISTS rates;

CREATE TABLE rates
(season_id INT NOT NULL PRIMARY KEY
,season_type VARCHAR(12) 
,season_start DATE NOT NULL
,season_end DATE NOT NULL 
,rate INT NOT NULL
);

INSERT INTO rates VALUES
(1,'Peak','2015-08-15','2015-08-30',50),
(1,'Mid' ,'2015-09-01','2015-09-30',25);


SELECT * FROM rates;
+-----------+-------------+--------------+------------+------+
| season_id | season_type | season_start | season_end | rate |
+-----------+-------------+--------------+------------+------+
|         1 | Peak        | 2015-08-15   | 2015-08-30 |   50 |
|         2 | Mid         | 2015-09-01   | 2015-09-30 |   25 |
+-----------+-------------+--------------+------------+------+

SELECT x.*
     , DATEDIFF(season_end,'2015-08-15') 
  FROM rates x 
 WHERE '2015-08-15' >= season_start
 UNION
SELECT x.*
     , DATEDIFF('2015-09-15',season_start) 
  FROM rates x 
 WHERE '2015-09-15' <= season_end;
+-----------+-------------+--------------+------------+------+-----------------------------------+
| season_id | season_type | season_start | season_end | rate | DATEDIFF(season_end,'2015-08-15') |
+-----------+-------------+--------------+------------+------+-----------------------------------+
|         1 | Peak        | 2015-08-15   | 2015-08-30 |   50 |                                15 |
|         2 | Mid         | 2015-09-01   | 2015-09-30 |   25 |                                14 |
+-----------+-------------+--------------+------------+------+-----------------------------------+

注意 8 月有 31 天