CREATE OR REPLACE PROCEDURE AKUZ.sp_AKUZ_FTLOAD_DloAccessPost(
    p_Xml xml DEFAULT NULL::XML,
    p_IsNew boolean DEFAULT TRUE,
    p_ID uuid DEFAULT NULL::uuid,
    p_QueueName character varying DEFAULT NULL::character varying,
    p_dateTime timestamp without time zone DEFAULT NULL::timestamp without time zone,
    p_User uuid DEFAULT NULL::uuid
)
AS $$
DECLARE
    v_ContentEncoding varchar(255);
    v_Type varchar(255);
BEGIN
    p_dateTime := COALESCE(p_dateTime, CURRENT_TIMESTAMP(0));
    v_ContentEncoding := 'utf-8';
    v_Type := 'DLO_ACCESSED_POSTS_FTLOAD';

    IF (p_Xml IS NOT NULL) THEN
        CREATE TEMPORARY TABLE temp_DLO_ACCESSED_POSTS AS
        SELECT
            (xpath('/DLO_ACCESSED_POST/ID/text()', x))[1]::text::uuid AS ID,
            (xpath('/DLO_ACCESSED_POST/POST_CODE/text()', x))[1]::text AS PostCode,
            (xpath('/DLO_ACCESSED_POST/LAST_NAME/text()', x))[1]::text AS LastName,
            (xpath('/DLO_ACCESSED_POST/FIRST_NAME/text()', x))[1]::text AS FirstName,
            (xpath('/DLO_ACCESSED_POST/FATHER_NAME/text()', x))[1]::text AS FatherName,
            (xpath('/DLO_ACCESSED_POST/CATEGORY/text()', x))[1]::text::int AS Category,
            to_timestamp((xpath('/DLO_ACCESSED_POST/BEGIN_ACCESS_DATE/text()', x))[1]::text, 'YYYY-MM-DD') AS BeginAccessDate,
            to_timestamp((xpath('/DLO_ACCESSED_POST/END_ACCESS_DATE/text()', x))[1]::text, 'YYYY-MM-DD') AS EndAccessDate,
            to_timestamp((xpath('/DLO_ACCESSED_POST/BEGIN_WORK_DATE/text()', x))[1]::text, 'YYYY-MM-DD') AS BeginWorkDate,
            to_timestamp((xpath('/DLO_ACCESSED_POST/CERTIFICATE_DATE/text()', x))[1]::text, 'YYYY-MM-DD') AS CertificateDate,
            (xpath('/DLO_ACCESSED_POST/MEDICAL_SPECIALITY_CODE/text()', x))[1]::text AS MedicalSpecialityCode,
            to_timestamp((xpath('/DLO_ACCESSED_POST/EXPULSION_DATE/text()', x))[1]::text, 'YYYY-MM-DD') AS ExpulsionDate,
            (xpath('/DLO_ACCESSED_POST/SNILS/text()', x))[1]::text AS Snils,
            (xpath('/DLO_ACCESSED_POST/POST_TYPE/text()', x))[1]::text AS PostType,
            (xpath('/DLO_ACCESSED_POST/SPECIALITY/text()', x))[1]::text AS Speciality,
            (xpath('/DLO_ACCESSED_POST/LPU_MAIN/text()', x))[1]::text AS LpuMain
        FROM
            unnest(xpath('//DLO_ACCESSED_POST', p_Xml)) as x;

        DELETE FROM AKUZ.T_DLO_ACCESSED_POSTS;

        INSERT INTO AKUZ.T_DLO_ACCESSED_POSTS (
            DloAccessedPostsID,
            PostCode,
            LastName,
            FirstName,
            FatherName,
            Category,
            BeginAccessDate,
            EndAccessDate,
            BeginWorkDate,
            CertificateDate,
            MedicalSpecialityCode,
            ExpulsionDate,
            Snils,
            PostType,
            Speciality,
            LpuMain
        )
        SELECT
            ID,
            PostCode,
            LastName,
            FirstName,
            FatherName,
            Category,
            BeginAccessDate,
            EndAccessDate,
            BeginWorkDate,
            CertificateDate,
            MedicalSpecialityCode,
            ExpulsionDate,
            Snils,
            (SELECT pt.PostTypeID FROM AKUZ.T_POST_TYPE pt WHERE pt.Code = p.PostType LIMIT 1),
            (SELECT s.SpecialityID FROM AKUZ.T_SPECIALITY s WHERE s.Code = p.Speciality LIMIT 1),
            (SELECT lm.LpuId FROM AKUZ.T_LPU_MAIN lm WHERE lm.LpuCode = p.LpuMain LIMIT 1)
        FROM temp_DLO_ACCESSED_POSTS p;

        DROP TABLE temp_DLO_ACCESSED_POSTS;
    END IF;
END;
$$
LANGUAGE plpgsql;
