USE [DocumedisLogs]
GO

/*
MDDOC-878

Purge of the table documedisLogs.dbo.Documedis_VaccinationCheck_LogData

Only records that are more than 7 months old and present as archived in [archivedRowsJournal]
will be purged.
All satellite tables are purged in order.

Deletion is made in batches to avoid locking.
Only 1 iteration is made, the job calling this procedure will handle the calls and be executed several times.
it will also handle the hours the deletion should run from / to
*/
CREATE OR ALTER PROCEDURE [purge_Documedis_ClinicalDecisionSupport_LogData] AS
BEGIN
    DECLARE @cutoff DATE = DATEADD(MONTH, -7, CURRENT_TIMESTAMP);
    DECLARE @batch INT = 5000;
    DECLARE @tbl_to_del TABLE (DocumedisClinicalDecisionSupportLogData_PK BIGINT NOT NULL );
    
    BEGIN TRANSACTION

    -- region populate filter table
    INSERT INTO @tbl_to_del([DocumedisClinicalDecisionSupportLogData_PK])
    SELECT TOP (@batch)
           [s].[DocumedisClinicalDecisionSupportLogData_PK]
    FROM [dbo].[Documedis_ClinicalDecisionSupport_LogData] [s]
    WHERE [s].[LogDateTime] < @cutoff
      AND EXISTS(
        SELECT 1
        FROM [archivedRowsJournal] [a]
        WHERE [a].[tableName] = 'Documedis_ClinicalDecisionSupport_LogData'
          AND [a].[tableRowId] = [s].[DocumedisClinicalDecisionSupportLogData_PK]
    );
    -- endregion

    -- region logcheck products
    DELETE [lcp]
    FROM @tbl_to_del [d]
        INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK]
        INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheckProduct] [lcp] ON [lc].[DocumedisClinicalDecisionSupportLogCheck_PK] = [lcp].[DocumedisClinicalDecisionSupportLogCheck_FK];
    PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogCheckProduct. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
    -- endregion

    -- region logcheck interactions
    DELETE [lci]
    FROM @tbl_to_del [d]
        INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK]
        INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheckInteraction] [lci] ON [lc].[DocumedisClinicalDecisionSupportLogCheck_PK] = [lci].[DocumedisClinicalDecisionSupportLogCheck_FK];
    PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogCheckInteraction. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
    -- endregion

    -- region logcheck
    DELETE [lc]
    FROM @tbl_to_del [d]
        INNER JOIN [Documedis_ClinicalDecisionSupport_LogCheck] [lc] ON [lc].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
    PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogCheck. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
    -- endregion

    -- region patientRisk
    DELETE [lr]
    FROM @tbl_to_del [d]
        INNER JOIN [Documedis_ClinicalDecisionSupport_LogDataPatientRisk] [lr] ON [lr].[DocumedisClinicalDecisionSupportLogData_FK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
    PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogDataPatientRisk. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
    -- endregion

    -- region logData
    DELETE ld
    FROM @tbl_to_del [d]
        INNER JOIN [Documedis_ClinicalDecisionSupport_LogData] [ld] ON [ld].[DocumedisClinicalDecisionSupportLogData_PK] = [d].[DocumedisClinicalDecisionSupportLogData_PK];
    PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - Deleted from Documedis_ClinicalDecisionSupport_LogData. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
    -- endregion

    -- region archivedRowsJournal
    DELETE j
    FROM [archivedRowsJournal] j
        INNER JOIN @tbl_to_del [d] ON [d].[DocumedisClinicalDecisionSupportLogData_PK] = [j].[tableRowId]
    WHERE [j].[tableName]='Documedis_ClinicalDecisionSupport_LogData';
    PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - deleted from archivedRowsJournal. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';
    -- endregion

    COMMIT TRANSACTION
END
