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"
                        )
                     ))
                )
        FROM RDS.T_DLO_ACCESSED_POSTS p)
    )::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;