---- DROP FUNCTION RecalculateDIAGNOSTICS_DICTIONARYTreeParams(uuid, integer, integer);
--CREATE OR REPLACE FUNCTION RecalculateDIAGNOSTICS_DICTIONARYTreeParams
--(
--	"ParentID" uuid DEFAULT NULL::uuid
--,	"Begin" integer DEFAULT 1
--,	OUT "End" integer
--,	"Level" integer DEFAULT 0
--)
--RETURNS integer
--LANGUAGE 'plpgsql'
--COST 100
--VOLATILE 
--AS $BODY$
--Declare
--	_ChCount int;
--	_ID UUID;
--	_NestedLevel INT;
--Begin
--	if "ParentID" IS NULL 
--	then
--		select count(*) from AKUZ.T_DIAGNOSTICS_DICTIONARY m into _ChCount where m.ParentID is null;
--	else 
--		select count(*) from AKUZ.T_DIAGNOSTICS_DICTIONARY m into _ChCount where m.ParentID = "ParentID";
--	end if;
	
--	if _ChCount = 0 or _ChCount is null
--	then
--		"End" := "Begin" + 1;
--	else
--		_NestedLevel := "Level" + 1;
		
--		if "ParentID" is null
--		then
--			"Begin" := 1;
			
--			for _ID in select m.DiagnosticsDictionaryID from AKUZ.T_DIAGNOSTICS_DICTIONARY m where m.ParentID is null order by m.DiagnosticsDictionaryID
--			loop
--				select (select * from RecalculateDIAGNOSTICS_DICTIONARYTreeParams(_ID, "Begin", _NestedLevel)) into "End";
				
--				update AKUZ.T_DIAGNOSTICS_DICTIONARY
--                set	LeftIndex = "Begin"
--				,	RightIndex = "End"
--				,	TreeLevel = "Level"
--                where DiagnosticsDictionaryID = _ID;
				
--				"Begin" := "End" + 1;
--			end loop;
--		else
--			"Begin" := "Begin" + 1;
			
--			for _ID in select m.DiagnosticsDictionaryID from AKUZ.T_DIAGNOSTICS_DICTIONARY m where m.ParentID = "ParentID" order by m.DiagnosticsDictionaryID
--			loop
--				select (select * from RecalculateDIAGNOSTICS_DICTIONARYTreeParams(_ID, "Begin", _NestedLevel)) into "End";
				
--				update AKUZ.T_DIAGNOSTICS_DICTIONARY
--                set LeftIndex = "Begin"
--                ,	RightIndex = "End"
--                ,	TreeLevel = "Level"
--                where DiagnosticsDictionaryID = _ID;

--				"Begin" := "End" + 1;
--			end loop;
--		end if;	
--	"End" := "End" + 1;
--	end if;
--End;
--$BODY$;
--ALTER FUNCTION RecalculateDIAGNOSTICS_DICTIONARYTreeParams(uuid, integer, integer) OWNER TO postgres;
