CREATE OR REPLACE PROCEDURE RDS.sp_RDS_FTLOAD_DloAccessPost(
     Xml xml DEFAULT NULL,
     IsNew int DEFAULT 1,
     ID uuid DEFAULT NULL,
     QueueName character varying DEFAULT NULL,
     dateTime timestamp DEFAULT NULL,
     "user" uuid DEFAULT NULL
) AS $$
DECLARE
    v_Result text;
    v_ContentEncoding varchar(255) := 'utf-8';
    v_Type varchar(255) := 'DLO_ACCESSED_POSTS_FTLOAD';

    p_Xml xml := Xml;
    p_IsNew integer := IsNew;
    p_ID uuid := ID;
    p_QueueName varchar(100) := QueueName;
    p_dateTime timestamp := dateTime;
    p_User uuid := "user";

BEGIN

    p_dateTime = coalesce(p_dateTime, CURRENT_TIMESTAMP(0));

    SELECT
    XMLELEMENT
    (
        NAME "DATA",
        (SELECT
                XMLAGG(
                    (SELECT XMLELEMENT(
                        NAME "DLO_ACCESSED_POSTS",
                        XMLFOREST
                        (
                            p.DloAccessedPostsID as "ID",
                            p.PostCode as "POST_CODE",
                            p.LastName as "LAST_NAME",
                            p.FirstName as "FIRST_NAME",
                            p.FatherName as "FATHER_NAME",
                            p.Category as "CATEGORY",
                            to_char(p.BeginAccessDate, 'DD.MM.YYYY') as "BEGIN_ACCESS_DATE",
                            to_char(p.EndAccessDate, 'DD.MM.YYYY') as "END_ACCESS_DATE",
                            to_char(p.BeginWorkDate, 'DD.MM.YYYY') as "BEGIN_WORK_DATE",
                            to_char(p.CertificateDate, 'DD.MM.YYYY') as "CERTIFICATE_DATE",
                            p.MedicalSpecialityCode as "MEDICAL_SPECIALITY_CODE",
                            to_char(p.ExpulsionDate, 'DD.MM.YYYY') AS "EXPULSION_DATE",
                            p.Snils as "SNILS",
                            p.PostType as "POST_TYPE",
                            p.Speciality as "SPECIALITY",
                            p.LpuMain as "LPU_MAIN",
                            e1.IdEhr as "EhrRus7PJSAM6GJM",
                            rrb1.CurrVersion as "EhrRus7PJSAM6GJMVer",
                            e2.IdEhr as "EhrRusLS9R0Q4VR4",
                            rrb2.CurrVersion as "EhrRusLS9R0Q4VR4Ver"
                        )
                     ))
                )
        FROM RDS.T_DLO_ACCESSED_POSTS p
        LEFT JOIN EHR.T_EHR_RUS_7PJSAM6GJM e1 on p.EhrRus7pjsam6gjm = e1.EhrRusId
        LEFT JOIN EHR.T_EHR_RUS_REF_BOOKS RRB1 ON RRB1.EhrRusRefBooksID = e1.EhrRusRefBooks
        LEFT JOIN EHR.T_EHR_RUS_LS9R0Q4VR4 e2 on p.EhrRusLs9r0q4vr4 = e2.EhrRusId
        LEFT JOIN EHR.T_EHR_RUS_REF_BOOKS RRB2 ON RRB2.EhrRusRefBooksID = e2.EhrRusRefBooks
        )
    )::text
    INTO v_Result;

    INSERT INTO RDS.T_RABBIT_SEND_OBJECT
    (
        RabbitSendObjectID,
        QueueName,
        Content,
        MessageID,
        Headers,
        ContentEncoding,
        Type,
        UserID,
        State,
        CreateDateTime,
        OwnerID
    )
    VALUES
    (
        gen_random_uuid(),
        p_QueueName,
        ConvertTextToBinaryZip(v_Result, ''),
        NULL,
        NULL,
        v_ContentEncoding,
        v_Type,
        p_User,
        0,
        p_dateTime,
        null
    );
END;
$$
LANGUAGE plpgsql;