简体   繁体   English

使用C#在SQL表中创建“每月时间表/时间表日历”

[英]Using C# to create a “monthly timetable/schedule calender” in a SQL table

For context I am creating a database for a vet clinic. 对于上下文,我正在为兽医诊所创建数据库。

I have a SQL table in Microsoft SQL Server Management studio 2014 with relational model (schema) as follows: ScheduleSlot(s̲l̲o̲t̲I̲D̲, room, date, startTime, endTime, appointment_id, staff_id) that i would like to partially pre fill. 我在Microsoft SQL Server Management Studio 2014中有一个带有关系模型(schema)的SQL表,如下所示:我想部分预先填充的ScheduleSlot(slloottIdD̲,房间,日期,startTime,endTime,约会ID,staff_id)

  • appointment_id and staff_id are foeign_keys that can and should be null for the purpose of this "empty calendar" 约会ID和职员ID是foeign_key,为此“空日历”的目的可以并且应该为null
  • The vent clinic is only open 6 days a week and is closed on Sunday. 发泄诊所每周仅开放6天,周日关闭。
  • Each possible appointment is half an hour. 每个可能的约会都是半小时。
  • The hours of operation are 8:00am - 5:00pm. 营业时间为8:00 am-5:00 pm。

I would like to create a diary structure that allows you for each day to define the possible slots that a vet might be available for. 我想创建一个日记结构,该日记结构允许您每天定义兽医可能可用的位置。

For example I would like to produce something like this for all 26 not Sunday days in May 2016: 例如,我想为2016年5月的所有26个非星期日天生成这样的内容:

slotId  date        startTime   endTime room    appointment_id  staff_id
1       5/2/2016    8:00        8:30    1       null            null
2       5/2/2016    8:00        8:30    2       null            null
3       5/2/2016    8:00        8:30    3       null            null
4       5/2/2016    8:00        8:30    4       null            null
5       5/2/2016    8:30        9:00    1       null            null
6       5/2/2016    8:30        9:00    2       null            null
7       5/2/2016    8:30        9:00    3       null            null
8       5/2/2016    8:30        9:00    4       null            null
9       5/2/2016    9:00        9:30    1       null            null
10      5/2/2016    9:00        9:30    2       null            null
11      5/2/2016    9:00        9:30    3       null            null
12      5/2/2016    9:30        9:30    4       null            null
...     ...         ...         ...     ...     ...             ...

For reference there should be 72(18 intervals x 4 room posiblites) slots per day for any month, and for the month May 2016 around 1872 slots (72 x 26). 作为参考,任何月份,以及2016年5月每月应有72个(18个间隔x 4个房间posiblites)插槽,约1872个插槽(72 x 26)。


My possible solution a using a modulo (loop) that checks if i % 7 == 0 and skips it? 我可能的解决方案是使用模(循环)检查i%7 == 0并跳过它? - The for loop could be passed a parameter as the first date and work until the end of the month. -for循环可以传递参数作为第一个日期,并一直工作到月底。

Don't do this . 不要这样做

Just have an appointments table that stores when appointments are scheduled. 只是有一个约会表,用于存储安排约会的时间。

SQL should not generally be responsible for business rules. SQL通常不应对业务规则负责。

Do this in your middle tier, or wherever you are handling your business logic, prior to handing it to the database for storage. 在将其交给数据库进行存储之前,请在中间层或任何要处理业务逻辑的地方进行此操作。

If on any given day, you only have 2 appointments, you should only have 2 rows for that day. 如果在给定的一天中您只有2个约会,那么该天您应该只有2行。 Not a bunch of rows with all available timeslots and NULLs. 没有一堆具有所有可用时隙和NULL的行。 That is a waste of space and needless overhead. 那是浪费空间和不必要的开销。

If you want to force integrity from the database side, to ensure that nothing invalid is getting through from your data-access layer (such as appointments outside of business hours, or overlapping appointments), you can employ triggers and check constraints that check for valid logic, or send everything through a stored procedure that does the same. 如果要从数据库端强制完整性,以确保没有任何无效数据从数据访问层通过(例如,工作时间以外的约会或重叠的约会),则可以使用触发器并检查检查是否有效的约束逻辑,或通过执行相同操作的存储过程发送所有内容。

As @Patrick said Don't do this , because after a while you have lots of garbage data you should remove it.this kind of garbage would be make some trouble in reporting. 就像@Patrick所说的那样不要这样做 ,因为过一会儿您应该删除大量垃圾数据后,这种垃圾将给报告带来麻烦。

So, If I were you, I would create a table for scheduling like rooms,the duration,start time,end time,... and when i want to insert new appointment, i try to create new appointment by the schedule rule and the last appointment. 因此,如果我是您,我将创建一个用于安排时间的表格,例如房间,持续时间,开始时间,结束时间...。当我想插入新约会时,我尝试通过时间表规则和最后一次约会。

for example the last appointment is in 5/2/2016 at 13:00 in room #1, and in schedule table i know the duration of meeting is half and hour, 例如最后一次约会是在2016年5月2日1号房间的13:00,在时间表表中,我知道会议的持续时间是一个半小时,

so i create new appointment for room #1 in 5/2/2016 at 14:30 所以我在5/2/2016 14:30为#1房间创建了新约会

Also you can find lots of solution to do this in the best way. 您也可以找到许多解决方案,以最好的方式做到这一点。

If you really want to prefill a table with the desired values, you could use this 如果您确实想用所需的值预填充表,则可以使用此方法

DECLARE @desiredMonth int = 5;
DECLARE @desiredYear int = 2016;

SELECT StartDate, 
DATEADD(minute, 30, StartDate) AS EndDate, 
rooms AS Room 
FROM
(
    SELECT 
    /* Construct StartDate */
    dateadd(minute, minutes, dateadd(hour, hours, dateadd(day, days - 1, dateadd(month, @desiredMonth - 1, dateadd(year, @desiredYear - 1900, 0))))) AS StartDate,
    minutes,
    rooms
    FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12), (13), (14), (15), (16), (17), (18), (19), (20), (21), (22), (23), (24), (25), (26), (27), (28), (29), (30), (31)) AS W(days)
    CROSS JOIN (VALUES (8), (9), (10), (12), (12), (13), (14), (15), (16)) AS X(hours)
    CROSS JOIN (VALUES (00), (30)) AS Y(minutes)
    CROSS JOIN (VALUES (1), (2), (3), (4)) AS Z(rooms)
    /* Check, that only days are included for desired month */
    WHERE DATEPART(m, dateadd(hour, hours, dateadd(day, days - 1, dateadd(month, @desiredMonth - 1, dateadd(year, @desiredYear - 1900, 0))))) = @desiredMonth
) AS SubQuery01

But I agree with the other opinions, that usually you should not do such stuff. 但是我同意其他意见,即通常不应该这样做。 Maybe it's better for using in a view? 也许在视图中使用更好?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM