---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-02-05
---- Description:	Выгрузка/загрузка связей профилей и отделений в(из) RDS 
---- =============================================
DROP FUNCTION if exists sp_GetSetDLSBXml(text, xml, boolean, uuid);
--CREATE OR REPLACE FUNCTION sp_GetSetDLSBXml
--(
--	"LpuCode" text DEFAULT NULL::text
--,	"Xml" xml DEFAULT NULL::xml
--,	"IsNew" integer = 1::boolean
--,	"ID" uuid DEFAULT NULL::uuid
--)
--RETURNS void AS
--$BODY$
--Declare	_Result text;
--	_ContentEncoding text;
--	_Type text;
--	_OperationType text;
--	_OwnerID uuid;
--begin
--	if "Xml" is null
--	then	
--		--выясняем тип операции
--		if "IsNew" = '1'
--		then
--			_OperationType := 'Insert';
--		elsif "IsNew" = '0' and exists(select * from RDS.T_DIVISION_LINK_STRUCTURE_BED d where d.DivisionLinkStructureBedID = "ID")
--		then
--			_OperationType := 'Update';
--		elsif "IsNew" = '0' and not exists(select * from RDS.T_DIVISION_LINK_STRUCTURE_BED d where d.DivisionLinkStructureBedID = "ID")
--		then
--			_OperationType := 'Delete';
--			_Result := XMLELEMENT
--				  (
--					name "DIVISION_LINK_STRUCTURE_BED"
--				  ,	XMLELEMENT
--					(
--						name header
--					,	XMLFOREST
--						(
--							"LpuCode" 	as "LPU"
--						,	_OperationType	as "TYPE"
--						,	"ID"		as "ID"
--						)
--					)
--				  ); 
--		END if;

--		_ContentEncoding := 'utf-8';
--		_Type := 'DIVISION_LINK_STRUCTURE_BED';
--		_OwnerID := "ID";
	
--		if _OperationType <> 'Delete'
--		then
--			select 	
--			XMLELEMENT
--			(
--				name "DIVISION_LINK_STRUCTURE_BED"
--			,	XMLELEMENT
--				(
--					name header
--				,	XMLFOREST
--					(
--						"LpuCode"	as "LPU"
--					,	_OperationType	as "TYPE"
--					,	"ID"		as "ID"
--					)
--				)				
--			,	XMLELEMENT
--				(
--					name body
--				,	XMLFOREST
--					(		    				
--						dlsb.Division		as "DIVISION_ID"
--					,	dlsb.StructureBed	as "STRUCTURE_BED"
--					,	dlsb.V020StructureBed	as "V020_STRUCTURE_BED"
--					)
--				)
--			)
--			from RDS.T_DIVISION_LINK_STRUCTURE_BED dlsb			
--			into _Result
--			where dlsb.DivisionLinkStructureBedID = "ID";
--		end if;

--		insert into RDS.T_RABBIT_SEND_OBJECT(RabbitSendObjectID, QueueName, Content, MessageID, Headers, ContentEncoding, Type, UserID, State, CreateDateTime, OwnerID)
--		select uuid_generate_v4(), s.ServerName, _Result, null, null, _ContentEncoding, _Type, null, 0, current_timestamp, _OwnerID
--		from RDS.T_SYNC_SERVERS s
--		where s.ServerType <> 1 
--		and not exists 
--		(
--			select * 
--			from RDS.T_SYNC_SERVERS s1
--			inner join 
--				RDS.T_SERVERS_LINK sl 
--				on sl.SyncServers = s1.SyncServersID
--			inner join 
--				RDS.T_SERVERS_LINK_LPU ll 
--				on ll.ServersLinkID = sl.ServersLinkID
--			inner join 
--				RDS.T_LPU_MAIN lp 
--				on lp.MCOD = ll.LpuMain
--			where lp.MCOD = "LpuCode" 
--			and s1.SyncServersID = s.SyncServersID
--		);
--	elsif "Xml" is not null
--	then
--		-- Generate table #DIVISION_LINK_STRUCTURE_BED
--		Drop Table If Exists TmpDIVISION_LINK_STRUCTURE_BED;
--		Create Temp Table TmpDIVISION_LINK_STRUCTURE_BED
--		As
--		select  (xpath('/DIVISION_LINK_STRUCTURE_BED/header[1]/TYPE[1]/text()', x))[1]::text			as OperationType
--		,	(xpath('/DIVISION_LINK_STRUCTURE_BED/header[1]/LPU[1]/text()', x))[1]::text			as LpuMain
--		,	(xpath('/DIVISION_LINK_STRUCTURE_BED/header[1]/ID[1]/text()', x))[1]::text::uuid		as ID
--		,	(xpath('/DIVISION_LINK_STRUCTURE_BED/body[1]/DIVISION_ID[1]/text()', x))[1]::text::uuid		as Div
--		,	(xpath('/DIVISION_LINK_STRUCTURE_BED/body[1]/STRUCTURE_BED[1]/text()', x))[1]::text::int	as CodeSB
--		,	(xpath('/DIVISION_LINK_STRUCTURE_BED/body[1]/V020_STRUCTURE_BED[1]/text()', x))[1]::text::int	as V020StructureBed
--		from
--		(
--			select "Xml" as x
--		) t;

--		_OperationType := (select OperationType from TmpDIVISION_LINK_STRUCTURE_BED limit 1);
		
--		if _OperationType <> 'Delete'
--		then
--			insert into RDS.T_DIVISION_LINK_STRUCTURE_BED(DivisionLinkStructureBedID, Division, StructureBed, ID, V020StructureBed)
--			select uuid_generate_v4(), (select d.DivisionID from RDS.T_DIVISION d where d.ID = dsl.Div or d.DivisionID = dsl.Div limit 1)
--			, dsl.CodeSB, dsl.ID, dsl.V020StructureBed
--			from TmpDIVISION_LINK_STRUCTURE_BED dsl;
--		else
--			delete from RDS.T_DIVISION_LINK_STRUCTURE_BED dsl
--			where exists
--			(
--				select* from TmpDIVISION_LINK_STRUCTURE_BED b where b.ID = dsl.ID or b.ID = dsl.DivisionLinkStructureBedID
--			);
--		end if;
		
--		-- Drop table
--		drop TABLE if exists TmpDIVISION_LINK_STRUCTURE_BED;
--	end if;
--end;
--$BODY$
--  LANGUAGE plpgsql VOLATILE
--  COST 100;
--ALTER FUNCTION sp_GetSetDLSBXml(text, xml, boolean, uuid)
--  OWNER TO postgres;