CREATE OR REPLACE PROCEDURE RDS.sp_DispDirectFactDel(
    FileID UUID
)
    LANGUAGE plpgsql
AS
$$
    DECLARE
        p_FileID uuid := fileid;
BEGIN

    DELETE
    FROM RDS.T_DISP_DIRECT_FACT_DESTINATION d
        USING RDS.T_DISP_DIRECT_FACT f
    WHERE
          d.DispDirectFact = f.DispDirectFactID
      AND f.DispDirectFactFile = FileID;

    DELETE
    FROM RDS.T_DISP_DIRECT_FACT
    WHERE
        DispDirectFactFile = FileID;

    DELETE
    FROM RDS.T_DISP_DIRECT_STATUS s
        USING RDS.T_DISP_DIRECT d
            LEFT JOIN RDS.T_DISP_DIRECT_FACT ddf ON d.DispDirectID = ddf.DispDirect
    WHERE
          s.DispDirect = d.DispDirectID
      AND d.DispDirectFactFile = FileID
      AND ddf.DispDirectFactID IS NULL;

    DELETE
    FROM RDS.T_DISP_DIRECT_INTERVIEW i
        USING RDS.T_DISP_DIRECT d
            LEFT JOIN RDS.T_DISP_DIRECT_FACT ddf ON d.DispDirectID = ddf.DispDirect
    WHERE
          i.DispDirect = d.DispDirectID
      AND d.DispDirectFactFile = FileID
      AND ddf.DispDirectFactID IS NULL;

    DELETE
    FROM RDS.T_DISP_DIRECT_INFO i
        USING RDS.T_DISP_DIRECT d
            LEFT JOIN RDS.T_DISP_DIRECT_FACT ddf ON d.DispDirectID = ddf.DispDirect
    WHERE
          i.DispDirect = d.DispDirectID
      AND d.DispDirectFactFile = FileID
      AND ddf.DispDirectFactID IS NULL;

    DELETE
    FROM RDS.T_DISP_DIRECT_ANKETA1 a1
        USING RDS.T_DISP_DIRECT_QUESTIONING q, RDS.T_DISP_DIRECT d
            LEFT JOIN RDS.T_DISP_DIRECT_FACT ddf ON d.DispDirectID = ddf.DispDirect
    WHERE
          a1.DiapDirectQuestioningID = q.DiapDirectQuestioningID
      AND q.DispDirect = d.DispDirectID
      AND d.DispDirectFactFile = FileID
      AND ddf.DispDirectFactID IS NULL;

    DELETE
    FROM RDS.T_DISP_DIRECT_ANKETA2 a2
        USING RDS.T_DISP_DIRECT_QUESTIONING q, RDS.T_DISP_DIRECT d
            LEFT JOIN RDS.T_DISP_DIRECT_FACT ddf ON d.DispDirectID = ddf.DispDirect
    WHERE
          a2.DiapDirectQuestioningID = q.DiapDirectQuestioningID
      AND q.DispDirect = d.DispDirectID
      AND d.DispDirectFactFile = FileID
      AND ddf.DispDirectFactID IS NULL;

    DELETE
    FROM RDS.T_DISP_DIRECT_QUESTIONING q
        USING RDS.T_DISP_DIRECT d
            LEFT JOIN RDS.T_DISP_DIRECT_FACT ddf ON d.DispDirectID = ddf.DispDirect
    WHERE
          q.DispDirect = d.DispDirectID
      AND d.DispDirectFactFile = FileID
      AND ddf.DispDirectFactID IS NULL;

    DELETE
    FROM RDS.T_PATIENT_CONTACT pc
        USING RDS.T_DISP_DIRECT d
            LEFT JOIN RDS.T_DISP_DIRECT_FACT ddf ON d.DispDirectID = ddf.DispDirect
    WHERE
          pc.DispDirect = d.DispDirectID
      AND d.DispDirectFactFile = FileID
      AND ddf.DispDirectFactID IS NULL;

    DELETE
    FROM RDS.T_DISP_DIRECT d
    WHERE
          d.DispDirectFactFile = FileID
      AND NOT EXISTS (SELECT
                          1
                      FROM RDS.T_DISP_DIRECT_FACT ddf
                      WHERE
                          d.DispDirectID = ddf.DispDirect);


    DELETE
    FROM RDS.T_DISP_DIRECT_FACT_FILE f
    WHERE
        f.FileID = p_fileid;

END;
$$
;
