DROP PROCEDURE sp_GetSetV002Profile;
-- =============================================
-- Author:      Vitacore (с)
-- Create date: 2016-06-24
-- Description: Выгрузка V002Profile
-- =============================================
CREATE PROCEDURE sp_GetSetV002Profile
(
     "Xml" text DEFAULT NULL
    ,"IsNew" boolean DEFAULT '1'
    ,"ID" uuid DEFAULT NULL::uuid
    ,"QueueName" character varying DEFAULT NULL::character varying
    ,"dateTime" timestamp without time zone DEFAULT NULL::timestamp without time zone
    ,"User" uuid DEFAULT NULL::uuid
)
    language plpgsql
as
$$
Begin
    declare 
        _Result text;
        ,_ContentEncoding varchar(255) := 'utf-8';
        ,_Type varchar(255) := 'V002_PROFILE';
        ,_OperationType varchar(6);
        ,_OwnerID uuid := "ID";
    Begin
        "dateTime" := Coalesce("dateTime", Current_TimeStamp::TIMESTAMP);
        if ("Xml" is null)
        then
            select 
            xmlelement
            (
                name "V002_PROFILE",
                xmlelement
                (
                    name header,
                    xmlforest (_OperationType as "TYPE")
                ),
                xmlelement
                (
                    name body,
                    xmlelement
                    (
                        name "V002_PROFILE_ITEMS",
                        xmlelement
                        (
                            name "V002_PROFILE_ITEM",
                            xmlforest
                            (
                                 vp.Id                                          as "ID"
                                ,vp.Name                                        as "NAME"
                                ,to_char(vp.DateBegin, 'YYYY-MM-DD')			as "DATE_BEGIN"
                                ,to_char(vp.DateEnd, 'YYYY-MM-DD')			    as "DATE_END"
                            )
                        )
                    )
                )
            )
            from RDS.T_V002_PROFILE vp
            into _Result;

            insert into RDS.T_RABBIT_SEND_OBJECT
                (RabbitSendObjectID, QueueName, Content, MessageID, Headers, ContentEncoding, Type, UserID, State, CreateDateTime, OwnerID)
        select uuid_generate_v4()--(<RabbitSendObjectID, uniqueidentifier,>
              ,s.ServerName--,<QueueName, varchar(255),>
              ,ConvertTextToBinaryZip(_Result, '')--,<Content, text,>
              ,null--,<MessageID, varchar(255),>
              ,null--,<Headers, varchar(255),>
              ,_ContentEncoding--,<ContentEncoding, varchar(255),>
              ,_Type--,<Type, varchar(255),>
              ,"User"--,<UserID, varchar(255),>
              ,0--,<State, int,>)
              ,"dateTime"--,<CreateDateTime, datetime,>)
              ,_OwnerID--,<OwnerID, varchar(255),>
        from RDS.T_SYNC_SERVERS s
        where ("QueueName" is not null and s.ServerName = "QueueName") or ("QueueName" is null and s.ServerType in (0, 1)); -- Рассылаем пакет всем подписанным серверам AKUZ-а и APS, если не указана очередь

        end if;
    End;
End;
$$;