﻿USE [eDosLocal];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

IF NOT EXISTS (SELECT * FROM sys.columns WHERE OBJECT_ID=OBJECT_ID('dbo.visit') AND name='eDosBatteryWarning')
begin
ALTER TABLE [dbo].[Visit] ADD eDosBatteryWarning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosDose07Warning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosDose07Alarm BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosRate07Warning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosRate07Alarm BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosDose10Warning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosDose10Alarm BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosRate10Warning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosRate10Alarm BIT null;
END 


IF NOT EXISTS (SELECT * FROM [sys].[columns] WHERE [object_id] = OBJECT_ID('dbo.visit') AND [name] = 'isFault')
BEGIN
    ALTER TABLE [dbo].[Visit] ADD [isFault] BIT NULL;
END;

GO
IF OBJECT_ID('dbo.PersonAlarms') IS NULL BEGIN
    CREATE TABLE [dbo].[PersonAlarms] ([PersID] INT NOT NULL,
    [VisitTime] DATETIME NOT NULL,
    [ID_Station] INT NOT NULL,
    [ID_Visit] INT NOT NULL,
    PRIMARY KEY NONCLUSTERED([PersID], [VisitTime]));
END;
GO
ALTER PROCEDURE [dbo].[VisitExit] @EPDVisitID INT,
    @HP07 FLOAT,
    @HP10 FLOAT,
    @HP07Peak FLOAT,
    @HP10Peak FLOAT,
    @HP07PeakTime DATETIME = NULL,
    @HP10PeakTime DATETIME = NULL,
    @HP07Total FLOAT,
    @HP10Total FLOAT,
    @K1 INT,
    @K2 INT,
    @K3 INT,
    @K4 INT,
    @K5 INT = NULL,
    @K6 INT = NULL,
    @SG INT,
    @BC INT,
    @FB INT,
    @HG INT,
    @QualityData INT,
    @alarmStatus INT,
    @otherAlarms INT,
    @errorStatus INT,
    @operatingStatus INT,
    @ENTRY_EPD_CLOCK DECIMAL(10, 0) NULL,
    @EXIT_EPD_CLOCK DECIMAL(10, 0) NULL,
    @PersID INT = NULL,
    @eDosBatteryWarning BIT = NULL,
    @eDosDose07Warning BIT = NULL,
    @eDosDose07Alarm BIT = NULL,
    @eDosRate07Warning BIT = NULL,
    @eDosRate07Alarm BIT = NULL,
    @eDosDose10Warning BIT = NULL,
    @eDosDose10Alarm BIT = NULL,
    @eDosRate10Warning BIT = NULL,
    @eDosRate10Alarm BIT = NULL,
    @isAlarm BIT = NULL,
    @isFault BIT = NULL
AS BEGIN
    DECLARE
        @ID_Visit INT,
        @ID_Station INT,
        @ID_Station_Return INT;

    SET @ID_Station = @EPDVisitID % 100;
    SET @ID_Visit = @EPDVisitID / 100;

    IF @isAlarm IS NULL
        SET @isAlarm = CASE WHEN ISNULL(@alarmStatus, 0) <> 0
                                 OR (ISNULL(@otherAlarms, 0) & 0x7f) <> 0 --bit 7=battery low
                                 OR ISNULL(@eDosDose07Warning, 0) <> 0
                                 OR ISNULL(@eDosRate07Warning, 0) <> 0
                                 OR ISNULL(@eDosDose10Warning, 0) <> 0
                                 OR ISNULL(@eDosRate10Warning, 0) <> 0
                                 OR ISNULL(@eDosDose07Alarm, 0) <> 0
                                 OR ISNULL(@eDosRate07Alarm, 0) <> 0
                                 OR ISNULL(@eDosDose10Alarm, 0) <> 0
                                 OR ISNULL(@eDosRate10Alarm, 0) <> 0 THEN 1 ELSE 0 END;

    DECLARE @Now DATETIME = GETDATE();

    IF @ID_Station_Return IS NULL
        SELECT
            @ID_Station_Return = [ID_Station]
        FROM [dbo].[StationSettings]
        WHERE [PCNr] = HOST_NAME();

    DECLARE @isComplete BIT = 1;

    IF NOT EXISTS (SELECT
                       *
                   FROM [dbo].[Visit]
                   WHERE [ID_Visit] = @ID_Visit
                         AND [ID_Station] = @ID_Station)BEGIN
        SET @isComplete = 0;

        INSERT [dbo].[Visit]([ID_Station], [ID_Visit], [Inst_CODE], [isManualExit], [isDeleted], [ID_Station_Return], [isComplete], [ENTRY_EPD_CLOCK], [PersID])
        VALUES(@ID_Station, @ID_Visit, 0, 0, 0, @ID_Station_Return, 0, @ENTRY_EPD_CLOCK, @PersID);
    END;

    UPDATE
        [dbo].[Visit]
    SET
        [Hp07Dose] = @HP07,
        [Hp10Dose] = @HP10,
        [isComplete] = @isComplete,
        [ReturnTime] = @Now,
        [Hp07Peak] = @HP07Peak,
        [Hp10Peak] = @HP10Peak,
        [Hp07Time] = @HP07PeakTime,
        [Hp10Time] = @HP10PeakTime,
        [Hp07Total] = @HP07Total,
        [Hp10Total] = @HP10Total,
        [K1] = @K1,
        [K2] = @K2,
        [K3] = @K3,
        [K4] = @K4,
        [K5] = @K5,
        [K6] = @K6,
        [SG] = @SG,
        [BC] = @BC,
        [FB] = @FB,
        [HG] = @HG,
        [QualityData] = @QualityData,
        [alarmStatus] = @alarmStatus,
        [otherAlarms] = @otherAlarms,
        [errorStatus] = @errorStatus,
        [operatingStatus] = @operatingStatus,
        --[D1] = @D1,    [D2] = @D2,    [D3] = @D3,    [D4] = @D4,
        [isAlarm] = @isAlarm,
        [isManualExit] = 0,
        [ID_Station_Return] = @ID_Station_Return,
        [ENTRY_EPD_CLOCK] = @ENTRY_EPD_CLOCK,
        [EXIT_EPD_CLOCK] = @EXIT_EPD_CLOCK,
        [PersID] = CASE WHEN [PersID] IS NULL THEN @PersID ELSE [PersID] END,
        [eDosBatteryWarning] = @eDosBatteryWarning,
        [eDosDose07Warning] = @eDosDose07Warning,
        [eDosDose07Alarm] = @eDosDose07Alarm,
        [eDosRate07Warning] = @eDosRate07Warning,
        [eDosRate07Alarm] = @eDosRate07Alarm,
        [eDosDose10Warning] = @eDosDose10Warning,
        [eDosDose10Alarm] = @eDosDose10Alarm,
        [eDosRate10Warning] = @eDosRate10Warning,
        [eDosRate10Alarm] = @eDosRate10Alarm,
        [isFault] = @isFault
    WHERE [ID_Visit] = @ID_Visit
          AND [ID_Station] = @ID_Station;

    -- local- >PersonInfoSnapshot
    IF @isAlarm = 1 BEGIN
        IF @PersID IS NULL
            SELECT
                @PersID = [PersID]
            FROM [dbo].[Visit]
            WHERE [ID_Visit] = @ID_Visit
                  AND [ID_Station] = @ID_Station;

        UPDATE
            [dbo].[PersonInfoSnapshot]
        SET
            [VisitIDLocked] = @ID_Visit * 100 + @ID_Station
        WHERE [PersID] = @PersID;

        IF NOT EXISTS (SELECT
                           *
                       FROM [dbo].[PersonAlarms]
                       WHERE [PersID] = @PersID
                             AND [VisitTime] = @Now)
            INSERT [dbo].[PersonAlarms]([PersID], [VisitTime], [ID_Station], [ID_Visit])
            VALUES(@PersID, @Now, @ID_Station, @ID_Visit);
    END;
END;

GO
ALTER PROCEDURE [export].[VisitsSelect]
AS
SELECT
    [ID_Station],
    [ID_Visit],
    [Inst_CODE],
    [PersID],
    [ID_Task],
    [EntryTime],
    [ReturnTime],
    [Hp10Dose],
    [Hp07Dose],
    [Hp10Total],
    [Hp07Total],
    [Hp10Peak],
    [Hp07Peak],
    [Hp10Time],
    [Hp07Time],
    [ID_EPD],
    [QualityData],
    [K1],
    [K2],
    [K3],
    [K4],
    [K5],
    [K6],
    [SG],
    [BC],
    [FB],
    [HG],
    [alarmStatus],
    [otherAlarms],
    [errorStatus],
    [operatingStatus],
    [isBG],
    [isComplete],
    [isDeleted],
    [isAlarm],
    [isAlarmCleared],
    [isManualExit],
    [ID_Station_Return],
    [ENTRY_EPD_CLOCK],
    [EXIT_EPD_CLOCK],
    [TIME_LOST],
    [VisitStatus] = CONVERT(TINYINT, CASE WHEN [EntryTime] IS NOT NULL
                                               AND [ReturnTime] IS NOT NULL THEN 3
                                     WHEN [EntryTime] IS NOT NULL THEN 1
                                     WHEN [ReturnTime] IS NOT NULL THEN 2 ELSE 0 END),
    [syncedEntry],
    [syncedExit],
    [epdType],
    [epdID],
    [eDosBatteryWarning],
    [eDosDose07Warning],
    [eDosDose07Alarm],
    [eDosRate07Warning],
    [eDosRate07Alarm],
    [eDosDose10Warning],
    [eDosDose10Alarm],
    [eDosRate10Warning],
    [eDosRate10Alarm]
FROM [dbo].[Visit]
WHERE([syncedEntry] = 0 OR [syncedExit] = 0);
GO
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[dbo].[StationSettings]') AND name='RestartComFirst')
begin
ALTER TABLE [dbo].[StationSettings] ADD [RestartComFirst] BIT;
ALTER TABLE [dbo].[StationSettings] ADD [OnlyMK2] BIT;
END
GO
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[dbo].[StationSettings]') AND name='AllowMultipleEPD')
ALTER TABLE [dbo].[StationSettings] ADD AllowMultipleEPD BIT NULL;
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[dbo].[StationSettings]') AND name='EPDRetries')
ALTER TABLE [dbo].[StationSettings] ADD EPDRetries BIT NULL;
GO
ALTER PROCEDURE [dbo].[StationSettingsSelect] AS
SELECT
    [ID_Station],
    [PCNr],
    [ComPort],
    [Inst_CODE1],
    [Inst_CODE2],
    [Inst_Name1],
    [Inst_Name2],
    [TestCSN],
    [Reader],
    [useTestButtons] = ISNULL([useTestButtons], 0),
    [useBeep] = ISNULL([useBeep], 22),
    [checkUser] = ISNULL([checkUser], 0),
    [EPDTimeout] = ISNULL([EPDTimeout], 10000),
    [EPDRetries] = ISNULL([EPDREtries], 1),
    [RestartCom] = ISNULL([RestartCom], 0),
    [RestartComFirst] = ISNULL([RestartComFirst], 0),
    [OnlyMK2] = ISNULL([OnlyMK2], 0),
    [AllowMultipleEPD] = ISNULL([AllowMultipleEPD], 0)
FROM [dbo].[StationSettings]
WHERE [PCNr] = HOST_NAME();
GO
ALTER PROCEDURE [dbo].[StationSettingsUpdate] @ComPort INT = NULL,
    @Inst_CODE1 INT = NULL,
    @Inst_CODE2 INT = NULL,
    @Inst_Name1 VARCHAR(50) = NULL,
    @Inst_Name2 VARCHAR(50) = NULL,
    @TestCSN VARCHAR(50) = NULL,
    @Reader VARCHAR(70) = NULL,
    @useTestButtons BIT = 0,
    @useBeep TINYINT = 22,
    @checkUser BIT = 0,
    @EPDTimeout INT = 10000,
    @EPDRetries INT = 1,
    @RestartCom BIT = 0,
    @RestartComFirst BIT = 0,
    @OnlyMK2 BIT = 0,
    @AllowMultipleEPD BIT = 0
AS
IF NOT EXISTS (SELECT * FROM [dbo].[StationSettings] WHERE [PCNr] = HOST_NAME())
    INSERT [dbo].[StationSettings]([PCNr], [ComPort], [Inst_CODE1], [Inst_CODE2], [Inst_Name1], [Inst_Name2], [TestCSN], [Reader], [useTestButtons], [useBeep], [checkUser], [EPDREtries], [RestartCom], [RestartComFirst], [OnlyMK2], [AllowMultipleEPD])
    VALUES(HOST_NAME(), @ComPort, @Inst_CODE1, @Inst_CODE2, @Inst_Name1, @Inst_Name2, @TestCSN, @Reader, @useTestButtons, @useBeep, @checkUser, @EPDRetries, @RestartCom, @RestartComFirst, @OnlyMK2, @AllowMultipleEPD);
ELSE
    UPDATE
        [dbo].[StationSettings]
    SET
        [ComPort] = @ComPort,
        [Inst_CODE1] = @Inst_CODE1,
        [Inst_CODE2] = @Inst_CODE2,
        [Inst_Name1] = @Inst_Name1,
        [Inst_Name2] = @Inst_Name2,
        [TestCSN] = @TestCSN,
        [Reader] = @Reader,
        [useTestButtons] = @useTestButtons,
        [useBeep] = @useBeep,
        [checkUser] = @checkUser,
        [EPDTimeout] = @EPDTimeout,
        [EPDREtries] = @EPDRetries,
        [RestartCom] = @RestartCom,
        [RestartComFirst] = @RestartComFirst,
        [OnlyMK2] = @OnlyMK2,
        [AllowMultipleEPD]=@AllowMultipleEPD 
    WHERE [PCNr] = HOST_NAME();
go


CREATE OR ALTER PROCEDURE [dbo].[VisitInfo] @EPDVisitID INT = 51905,
    @hasExited BIT = 0 OUT,
    @isManualExit BIT = 0 OUT,
    @name VARCHAR(100) = NULL OUT
AS
--DECLARE @EPDVisitID INT= 51905
BEGIN
    SET NOCOUNT ON;

    DECLARE
        @ID_Visit INT,
        @ID_Station INT,
        @PersID INT;

    SET @ID_Station = @EPDVisitID % 100;
    SET @ID_Visit = @EPDVisitID / 100;
    SET @hasExited = 0;
    SET @isManualExit = 0;

    SELECT
        @hasExited = CONVERT(BIT, CASE WHEN [ReturnTime] IS NOT NULL OR [isManualExit] = 1 THEN 1 ELSE 0 END),
        @isManualExit = ISNULL([isManualExit], 0),
        @PersID = @PersID
    FROM [dbo].[Visit]
    WHERE [ID_Visit] = @ID_Visit
          AND [ID_Station] = @ID_Station;

    SET NOCOUNT OFF;

    SELECT [hasExited] = @hasExited;

    SELECT
        @name = ISNULL([name] + ' ' + ISNULL([callingname], [firstname]), '?')
    FROM [dbo].[PersonAll]
    WHERE [persid] = @PersID;

    RETURN CASE WHEN ISNULL(@hasExited, 0) = 1 THEN 1 ELSE 0 END + CASE WHEN ISNULL(@isManualExit, 0) = 1 THEN 2 ELSE 0 END;
END;

GO

GRANT EXECUTE ON [dbo].[VisitInfo] TO [eDosStation];
GO