--do $$
--begin
--	if exists 
--	(
--		select * from pg_proc p
--		where p.proname = 'sp_rds_dlsbxml'
--	)
--	then
--		drop function public.sp_rds_dlsbxml(text, xml, bit, uuid, varchar(255));
--	end if;
--end;
--$$ language plpgsql;
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-02-05
---- Description:	Выгрузка/загрузка связки профилей и отделений в(из) RDS 
---- =============================================
--CREATE or Replace Function public.sp_RDS_DLSBXml
--(
--	"LpuCode" text = null,
--	"Xml" xml = null,
--	"IsNew" bit = '1',
--	"ID" UUID = null,
--	"QueueName" varchar(255) = null
--)
--RETURNS Void As 
--$$
--Declare	Result text;
--			ContentEncodingVar varchar(255);
--			TypeVar varchar(255);
--			OperationTypeVar varchar(255);
--			OwnerIDVar UUID;
--Begin
--	IF "Xml" is null
--	then
--	--выясняем тип операции
--		IF "IsNew" = '1'
--		then
--			OperationTypeVar := 'Insert';
--		ELSIF "IsNew" = '0' and exists (select * from akuz.T_DIVISION_LINK_STRUCTURE_BED d  where d.DivisionLinkStructureBedID = "ID")
--		then
--			OperationTypeVar := 'Update';
--		ELSIF "IsNew" = '0' and not exists (select * from akuz.T_DIVISION_LINK_STRUCTURE_BED d where d.DivisionLinkStructureBedID = "ID")
--		then
--			OperationTypeVar := 'Delete';
--			Result = XMLELEMENT
--				 (
--					name "DIVISION_LINK_STRUCTURE_BED"
--				 ,	XMLFOREST
--					(
--						XMLFOREST
--						(
--							"LpuCode" as "LPU"
--						,	OperationTypeVar as "TYPE"
--						,	"ID" as "ID"
--						) as header
--					)
--				 );
--		END if;

--		ContentEncodingVar := 'utf-8';
--		TypeVar := 'DIVISION_LINK_STRUCTURE_BED';
--		OwnerIDVar := "ID";

--		IF OperationTypeVar <> 'Delete'
--		then
--			select 
--			XMLELEMENT
--			(
--				name "DIVISION_LINK_STRUCTURE_BED"
--			,	XMLFOREST
--				(
--					XMLFOREST
--					(
--						"LpuCode" as "LPU"
--					,	OperationTypeVar as "TYPE"
--					,	"ID" as "ID"
--					) as header
--				)
--			,	XMLFOREST
--				(
--					XMLFOREST
--					(
--						dlsb.Division as "DIVISION_ID"
--					,	v0.Id as "STRUCTURE_BED"
--					,	sb.Code as "V020_STRUCTURE_BED"
--					) as body
--				)
--			)
--			from akuz.T_DIVISION_LINK_STRUCTURE_BED dlsb
--			join akuz.T_STRUCTURE_BED sb on sb.StructureBedID = dlsb.StructureBed
--			join akuz.T_V002_PROFILE v0 on v0.Id = sb.V002Profile
--			into Result
--			where dlsb.DivisionLinkStructureBedID = "ID";			
--		END IF;

--		INSERT INTO akuz.T_RABBIT_SEND_OBJECT(RabbitSendObjectID,QueueName,Content,MessageID,Headers,ContentEncoding,Type,UserID,State,CreateDateTime,OwnerID)
--		select uuid_generate_v1(), "QueueName", Result, null, null, ContentEncodingVar, TypeVar, null, 0, Current_TimeStamp::TIMESTAMP, OwnerIDVar;
--	ELSE 
--		IF "Xml" is not null
--		then
--			-- Generate table #DIVISION_LINK_STRUCTURE_BED
--			Drop Table If Exists Tmp_DIVISION_LINK_STRUCTURE_BED;
--			Create Temp Table Tmp_DIVISION_LINK_STRUCTURE_BED(OperationType text, LpuMain varchar(6), ID UUID, Div UUID, CodeSB int);
--			insert into Tmp_DIVISION_LINK_STRUCTURE_BED(OperationType, LpuMain, ID, Div, CodeSB, V020StructureBed)
--			SELECT	nullif(trim(xpath('/DIVISION_LINK_STRUCTURE_BED/header[1]/TYPE[1]/text()', "Xml")::text,'{}"'),'')
--			,	nullif(trim(xpath('/DIVISION_LINK_STRUCTURE_BED/header[1]/LPU[1]/text()', "Xml")::text,'{}"'),'')::varchar(6)
--			,	nullif(trim(xpath('/DIVISION_LINK_STRUCTURE_BED/header[1]/ID[1]/text()', "Xml")::text,'{}"'),'')::UUID			
--			,	nullif(trim(xpath('/DIVISION_LINK_STRUCTURE_BED/body[1]/DIVISION_ID[1]/text()', "Xml")::text,'{}"'),'')::UUID
--			,	nullif(trim(xpath('/DIVISION_LINK_STRUCTURE_BED/body[1]/STRUCTURE_BED[1]/text()', "Xml")::text,'{}"'),'')::int
--			,	nullif(trim(xpath('/DIVISION_LINK_STRUCTURE_BED/body[1]/V020_STRUCTURE_BED[1]/text()', "Xml")::text,'{}"'),'')::int;

--			select OperationType 
--			from Tmp_DIVISION_LINK_STRUCTURE_BED
--			into OperationTypeVar
--			Limit 1;

--			if OperationTypeVar <> 'Delete'
--			then
--				-- INSERT akuz.T_DIVISION_LINK_STRUCTURE_BED
--				INSERT INTO akuz.T_DIVISION_LINK_STRUCTURE_BED(DivisionLinkStructureBedID, Division, StructureBed, OtherID)
--				SELECT uuid_generate_v1()
--				, (
--					select d.DivisionID from akuz.T_DIVISION d where d.OtherID = dsl.Div
--				  )
--				,case when V020StructureBed = null then (select sb.StructureBedID from AKUZ.T_STRUCTURE_BED sb
--														join AKUZ.T_V002_PROFILE v0 on v0.Id = sb.V002Profile
--															where v0.Id = dsl.CodeSB limit 1)
--				  else (select sb.StructureBedID from AKUZ.T_STRUCTURE_BED sb
--														join AKUZ.T_V002_PROFILE v0 on v0.Id = sb.V002Profile
--															where v0.Id = dsl.CodeSB
--															and sb.Code = dsl.V020StructureBed 
--															and sb.DateBegin <= current_timestamp
--															and (sb.DateEnd is null or
--																	sb.DateEnd >= current_timestamp) limit 1) end 
--				, dsl.ID
--				from Tmp_DIVISION_LINK_STRUCTURE_BED dsl
--				where not exists 
--				(
--					select * from akuz.T_DIVISION_LINK_STRUCTURE_BED dl where dl.OtherID = dsl.ID
--				);		
--			ELSE
--				delete from akuz.T_DIVISION_LINK_STRUCTURE_BED dsl
--				where dsl.OtherID in 
--				(
--					select b.ID from Tmp_DIVISION_LINK_STRUCTURE_BED b
--				);
--			END if;

--		-- Drop table
--		DROP TABLE if exists Tmp_DIVISION_LINK_STRUCTURE_BED;

--		END if;
--	end if;
--End;
--$$ Language plpgsql;