---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2015-06-01
---- Description:	Выгрузка/загрузка информации о количестве свободных мест в отделении
---- =============================================
--CREATE OR REPLACE FUNCTION sp_rds_freebedsxml
--(
--	"LpuCode" text DEFAULT NULL::text
--,	"Xml" xml DEFAULT NULL::xml
--,	"IsNew" boolean DEFAULT '1'::boolean
--,	"ID" uuid DEFAULT NULL::uuid
--,	"QueueName" varchar(255) DEFAULT NULL::varchar(255)
--)
--RETURNS void AS
--$BODY$
--Declare	_Result text;
--	_ContentEncoding varchar(255);
--	TypeVar varchar(255);
--	_OperationType varchar(255);
--	_OwnerID UUID;
--Begin
--	if "Xml" is null
--	then
--		--выясняем тип операции
--		IF "IsNew" = '1'
--		then
--			_OperationType := 'Insert';
--		elsif "IsNew" = '0' and exists(select* from akuz.T_FREE_BEDS_INFORMATION fb where fb.FreeBedsInformationID = "ID")
--		then
--			_OperationType := 'Update';
--		elsif "IsNew" = '0' and not exists(select* from akuz.T_FREE_BEDS_INFORMATION fb where fb.FreeBedsInformationID = "ID")
--		then
--			_OperationType := 'Delete';
--			_Result := xmlelement
--			   	   (
--					name "FREE_BEDS_INFORMATION"
--				   ,	xmlforest
--					(
--						xmlforest
--						(
--							"LpuCode" as "LPU"
--						,	_OperationType as "TYPE"
--						,	"ID"	as "ID"
--						) as header
--					)
--				    );
--		end if;

--		_ContentEncoding := 'utf-8';
--		TypeVar := 'FREE_BEDS_INFORMATION';
--		_OwnerID := "ID";

--		IF _OperationType <> 'Delete'
--		then
--			_Result := 
--			xmlelement 
--			(
--				name "FREE_BEDS_INFORMATION"
--			,	(
--					select
--					xmlagg
--					( 
--						xmlforest
--						(
--							xmlforest
--							(
--								"LpuCode" as "LPU"
--							,	_OperationType as "TYPE"
--							,	"ID" as "ID"
--							) as header
--						,	xmlforest
--							(
--								fb.DivisionLinkStructureBed 		as "DLSB"
--							,	strb.Code				as "STRUCTURE_BED"
--							,	to_char(fb.ActualDate, 'YYYY-MM-DD')	as "ACTUAL_DATE"
--							,	fb.Amount				as "AMOUNT"
--							) as body
--						)
--					)
--					from akuz.T_FREE_BEDS_INFORMATION fb
--					inner join 
--						AKUZ.T_DIVISION_LINK_STRUCTURE_BED dlsb 
--						on dlsb.DivisionLinkStructureBedID = fb.DivisionLinkStructureBed
--					inner join 
--						AKUZ.T_DIVISION div 
--						on div.DivisionID = dlsb.Division
--					inner join 
--						AKUZ.T_STRUCTURE_BED strb 
--						on strb.StructureBedID = dlsb.StructureBed
--					where fb.FreeBedsInformationID = "ID"
--				)
--			);
--		end if;

--		INSERT INTO akuz.T_RABBIT_SEND_OBJECT(RabbitSendObjectID, QueueName, Content, MessageID, Headers, ContentEncoding, Type, UserID, State, CreateDateTime, OwnerID)
--		select uuid_generate_v4(), "QueueName", _Result, null, null, _ContentEncoding, TypeVar, null, 0, Current_TimeStamp::TIMESTAMP, _OwnerID;
--	END if;
--End;
--$BODY$
--  LANGUAGE plpgsql VOLATILE
--  COST 100;
--ALTER FUNCTION sp_rds_freebedsxml(text, xml, boolean, uuid, varchar(255))
--  OWNER TO postgres;