-- =============================================
-- Author:      Vitacore (с)
-- Create date: 2018-07-02
-- Description: Отправка XML рецепта
-- =============================================
DROP PROCEDURE IF EXISTS stor.sp_RDS_RecipeSignature;
DROP PROCEDURE IF EXISTS public.sp_RDS_RecipeSignature;

CREATE PROCEDURE public.sp_RDS_RecipeSignature
(
	"ID" uuid DEFAULT NULL::uuid
,	"User" uuid DEFAULT NULL::uuid
,	"QueueName" varchar(100) DEFAULT 'RDSSync'::varchar(100)
)
    language plpgsql
as
$$
Begin
		-- Отправка
		INSERT INTO akuz.T_RABBIT_SEND_OBJECT(RabbitSendObjectID, QueueName, Content, MessageID, Headers, ContentEncoding, Type, UserID
											  , State, CreateDateTime, OwnerID, CorrelationId)
		select uuid_generate_v4()
		,	"QueueName"
		,	ConvertTextToBinaryZip(coalesce(rs.XmlData, ''), '')
		,	null
		,	null
		,	'utf-8'
		,	case when rs.DataState = 1 then 'SIGNED_RECIPE' else 'RECIPE' end
		,	coalesce("User", rs.CreatedUser)
		,	0
		,	rs.SignatureDateTime
		,	rs.RecipeSignatureID
		,	rs.RecipeSignatureID
		from AKUZ.T_RECIPE_SIGNATURE rs
		where rs.RecipeSignatureID = "ID";	
End;
$$;