Генерация свободного времени между 2 датами в блоках по длине в зависимости от задачи - PullRequest
0 голосов
/ 04 октября 2019

Я пытаюсь генерировать бесплатные блоки для рабочих и работать нормально, если рабочее время с 08:00 до 16:00. Но когда я меняю рабочее время с 07:30 до 15:30, это не работает так, как я хочу. Если продолжительность моей задачи составляет 1 час / 60 минут, первое доступное время будет 8.00, что должно было быть 7:30. Любые предложения о том, как я могу решить эту проблему?

Вывод:

ожидается, начнется в 7:30, 8:30 и т. Д.

хранимая процедура

GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ProcedureGenerateFreetimeForOneStakeholder]
(
    @EmployeeId uniqueidentifier,
    @NumberOfDaysBeforeBooking int,
    @NumberOfDays int,
    @Rebuild int 
)
AS
DECLARE @workHours TABLE (
    [CompanyKeyValuePairSettingsId] uniqueidentifier,
    [Value] nvarchar(249)
)
DECLARE @CompanyId uniqueidentifier = (select top 1 CompanyId from tblStakeholder where StakeholderId = @EmployeeId)
SET @NumberOfDaysBeforeBooking =(select [value] FROM tblCompanyToCompanyKeyValuePairSettings WHERE CompanyId = @CompanyId AND CompanyKeyValuePairSettingsId = '06B546A9-C47E-4A8B-AE13-E3F9F0C87DEC')
SET @NumberOfDays =(select [value] FROM tblCompanyToCompanyKeyValuePairSettings WHERE CompanyId = @CompanyId AND CompanyKeyValuePairSettingsId = '87C6406F-BC47-4F42-B58B-450C9F4EBC9E')

-- Insert user work time to @workHours
INSERT INTO @workHours ([CompanyKeyValuePairSettingsId], [Value]) (SELECT s.[CompanyKeyValuePairSettingsId], s.[Value] FROM [dbo].[tblCompanyToCompanyKeyValuePairSettings] as s
INNER JOIN [dbo].[tblCompanyKeyValuePairSettings] as p on s.[CompanyKeyValuePairSettingsId] = p.CompanyKeyValuePairSettingsId
WHERE p.[Section] = 'workingHours' and [CompanyId] = @CompanyId and p.[CompanyKeyValuePairSettingsId] not in (
SELECT [IsOverwriteTo]
FROM [dbo].[tblCompanyUserKeyValuePairSettings]
INNER JOIN [dbo].[tblCompanyUserToStakeholderIdKeyValuePairSettings] on [tblCompanyUserKeyValuePairSettings].[CompanyUserKeyValuePairSettingsId] = [tblCompanyUserToStakeholderIdKeyValuePairSettings].[CompanyUserKeyValuePairSettingsId]
WHERE Section = 'WorkingHours' AND IsOverwriteTo is not null and StakeholderId = @EmployeeId))

INSERT INTO @workHours ([CompanyKeyValuePairSettingsId], [Value] ) (SELECT [IsOverwriteTo], [tblCompanyUserToStakeholderIdKeyValuePairSettings].[Value]
FROM [dbo].[tblCompanyUserKeyValuePairSettings]
INNER JOIN [dbo].[tblCompanyUserToStakeholderIdKeyValuePairSettings] on [tblCompanyUserKeyValuePairSettings].[CompanyUserKeyValuePairSettingsId] = [tblCompanyUserToStakeholderIdKeyValuePairSettings].[CompanyUserKeyValuePairSettingsId]
WHERE Section = 'WorkingHours' AND IsOverwriteTo is not null and StakeholderId = @EmployeeId)

declare @TimeSlots table (
    StartTime [nchar](5) NOT NULL, 
    EndTime [nchar](5) NOT NULL
)


DECLARE @Today DateTime  = CONVERT (date, DATEADD(day, 0, GETDATE()))
DECLARE @FirstDay DateTime  = CONVERT (date, DATEADD(day, @NumberOfDaysBeforeBooking, GETDATE()))
DECLARE @Last DateTime  = CONVERT (date, DATEADD(day, @NumberOfDays,GETDATE()))
DECLARE @current DateTime  = CONVERT (date, DATEADD(day, 0, GETDATE()))

DECLARE @MinuteBeginDay int
DECLARE @MinuteEndDay int

DECLARE @MinuteAssignmentBegin int
DECLARE @MinuteAssignmentEnd int = 1440;


if(@Rebuild = 1)
Begin
DELETE FROM [dbo].[tblEmployeeFreetime] WHERE [StakeholderId] = @EmployeeId
End
if(@Rebuild = 0)
Begin
 DELETE FROM [dbo].[tblEmployeeFreetime] WHERE [StakeholderId] = @EmployeeId and [StartTime] <= @FirstDay 
End
-- Insert blocked time
WHILE (@current <= @FirstDay)
BEGIN
    INSERT INTO @TimeSlots (StartTime,EndTime) values ('23:59','23:59')

        set @MinuteBeginDay = (select CONVERT(INT, (SELECT SUBSTRING((select top (1) StartTime from @TimeSlots), 1, 2))) * 60 + CONVERT(INT, (SELECT SUBSTRING((select top (1) StartTime from @TimeSlots), 4, 5))))
        set @MinuteEndDay = (select CONVERT(INT, (SELECT SUBSTRING((select top (1) EndTime from @TimeSlots), 1, 2))) * 60 + CONVERT(INT, (SELECT SUBSTRING((select top (1) EndTime from @TimeSlots), 4, 5))))

        set @MinuteAssignmentBegin = 30
        While (@MinuteAssignmentBegin <  @MinuteAssignmentEnd - @MinuteAssignmentBegin)  
        Begin
                INSERT INTO  [dbo].[tblEmployeeFreetime] ([StakeholderId], StartTime,EndTime,[AssignmentLength]) 
                (select @EmployeeId, @current , DATEADD(minute, @MinuteEndDay, @current),@MinuteAssignmentBegin)

                SET @MinuteAssignmentBegin = @MinuteAssignmentBegin + 30
        End
        DELETE FROM @TimeSlots

    Set  @current = CONVERT (date, DATEADD(day, 1, @current))
End

-- Insert free time on workdays and weekends
SET @current   = CONVERT (date, DATEADD(day, 1, (SELECT TOP (1) [EndTime] FROM [dbo].[tblEmployeeFreetime] where [StakeholderId] = @EmployeeId order by [EndTime] desc)))
Declare @SetTime datetime
while (@current <= @Last)
Begin
        declare @str  VARCHAR(MAX) 
        set @str = FORMAT(@current, 'dddd')
        -----
        declare @StartSlot nvarchar(5) = (SELECT w.Value FROM @workHours as w
            INNER JOIN tblCompanyKeyValuePairSettings as s on w.[CompanyKeyValuePairSettingsId] = s.[CompanyKeyValuePairSettingsId] where s.Name Like @str + 'StartTime%')
        declare @EndSlot nvarchar(5) =  (SELECT w.Value FROM @workHours as w
            INNER JOIN tblCompanyKeyValuePairSettings as s on w.[CompanyKeyValuePairSettingsId] = s.[CompanyKeyValuePairSettingsId] where s.Name Like @str + 'EndTime%')

        INSERT INTO @TimeSlots (StartTime,EndTime) VALUES (@StartSlot,@EndSlot)

        ----

        set @MinuteBeginDay = (select CONVERT(INT, (SELECT SUBSTRING((select top (1) StartTime from @TimeSlots), 1, 2))) * 60 + CONVERT(INT, (SELECT SUBSTRING((select top (1) StartTime from @TimeSlots), 4, 5))))
        set @MinuteEndDay = (select CONVERT(INT, (SELECT SUBSTRING((select top (1) EndTime from @TimeSlots), 1, 2))) * 60 + CONVERT(INT, (SELECT SUBSTRING((select top (1) EndTime from @TimeSlots), 4, 5))))

        set @MinuteAssignmentBegin = 30
        While (@MinuteAssignmentBegin <  @MinuteAssignmentEnd - @MinuteAssignmentBegin)  
        Begin
                INSERT INTO  [dbo].[tblEmployeeFreetime]  ([StakeholderId], StartTime,EndTime,[AssignmentLength]) (select @EmployeeId, @current , DATEADD(minute, @MinuteBeginDay, @current),@MinuteAssignmentBegin)

                if @StartSlot != @EndSlot
                begin
                    set @SetTime = DATEADD(minute, @MinuteEndDay - @MinuteAssignmentBegin, @current)
                    INSERT INTO  [dbo].[tblEmployeeFreetime]  ([StakeholderId], StartTime,EndTime,[AssignmentLength]) (select @EmployeeId, @SetTime, DATEADD(minute, 1439, @current),@MinuteAssignmentBegin)
                end
                set @MinuteAssignmentBegin = @MinuteAssignmentBegin + 30
        End
        delete from @TimeSlots

     Set  @current = CONVERT (date, DATEADD(day, 1, @current))
End
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...