关于我的数据库设计和一些用例的进一步说明。
表设计
这是存储事件的主表(基本上基于 iCalendar 架构)。事件可以是典型事件,也可以是会议、公共假期等。
event (event_id (PK), dtstart, dtend, ... --other icalendar fields--)
如果特定类型的事件有我必须跟踪的额外信息,我会用另一张桌子装饰它。例如,存储电子休假特定信息的表。(total_days 不是作为要求的一部分的计算字段)
event_leave (event_id (PK/FK->event), total_days, leave_type_id (FK->leave_type))
请假类型表存储有关每个请假类型的一些信息。例如,应用程序是否需要批准/推荐等。除此之外,它还存储允许的最大结转。我假设最大结转不会经常改变。
leave_type (leave_type_id (PK), name, require_support, require_recommend, max_carry_forward)
用户被分成组,每个组将被赋予一些休假类型的可休假天数。存储在此表中的数据将每年填充一次(每年更新一次)。它只存储每个组的休假数,而不是每个用户。
leave_allocation (leave_allocation_id, year(PK), leave_type_id (PK/FK->leave_type), total_days, group_id)
接下来是存储结转信息的表。该表将每年为每个用户填充一次。该表将每年填充一次,因为动态计算并不容易。为用户计算 leave_carry_forward 的公式为:
leave_carry_forward(2009) = min(leave_allocation(2008) + leave_carry_forward(2007) - leave_taken(2008), maximum_carry_forward());
leave_carry_forward (leave_carry_forward_id, user_id, year, total_days)
一些示例用例和解决方案
计算余额 (WIP)
为了计算余额,我对声明如下的视图进行查询
DROP VIEW IF EXISTS leave_remaining_days;
CREATE OR REPLACE VIEW leave_remaining_days AS
SELECT year, user_id, leave_type_id, SUM(total_days) as total_days
FROM (
SELECT allocated.year, usr.uid AS "user_id", allocated.leave_type_id,
allocated.total_days
FROM users usr
JOIN app_event._leave_allocation allocated
ON allocated.group_id = usr.group_id
UNION
SELECT EXTRACT(year FROM event.dtstart) AS "year", event.user_id,
leave.leave_type_id, leave.total_days * -1 AS total_days
FROM app_event.event event
LEFT JOIN app_event.event_leave leave
ON event.event_id = leave.event_id
UNION
SELECT year, user_id, leave_type_id, total_days
FROM app_event._leave_carry_forward
) KKR
GROUP BY year, user_id, leave_type_id;
在年初填充 leave_allocation 表
public function populate_allocation($year) {
return $this->db->query(sprintf(
'INSERT INTO %s (%s)' .
"SELECT '%s' AS year, %s " .
'FROM %s ' .
'WHERE "year" = %s',
'event_allocation',
'year, leave_type_id, total_days ...', //(all the fields in the table)
empty($year) ? date('Y') : $year,
'leave_type_id, total_days, ..', //(all fields except year)
$this->__table,
empty($year) ? date('Y') - 1 : $year - 1
))
->count() > 0; // using the database query builder in Kohana PHP framework
}
在年初填充 leave_carry_forward 表
找出分配给用户的休假类型
我可能需要重命名这个视图(我不擅长命名东西......)。它实际上是一个用户的 leave_allocation 表。
DROP VIEW IF EXISTS user_leave_type;
CREATE OR REPLACE VIEW user_leave_type AS
SELECT la.year, usr.uid AS user_id, lt.leave_type_id, lt.max_carry_forward
FROM users usr
JOIN app_event._leave_allocation la
JOIN app_event._leave_type lt
ON la.leave_type_id = lt.leave_type_id
ON usr.group_id = la.group_id
实际查询
INSERT INTO leave_carry_forward (year, user_id, leave_type_id, total_days)
SELECT '{$this_year}' AS year, user_id, leave_type_id, MIN(carry_forward) AS total_days
FROM (
SELECT year, user_id, leave_type_id, total_days AS carry_forward
FROM leave_remaining_days
UNION
SELECT year, user_id, leave_type_id, max_carry_forward AS carry_forward
FROM user_leave_type
) KKR
WHERE year = {$last_year}
GROUP BY year, user_id, leave_type_id;