-- =============================================
-- Author:      Vitacore (с)
-- Create date: 2017-09-21
-- Description: Хранимка рулит какую хранимку звать по действию при изменении объекта
-- =============================================
drop procedure if exists sp_DirectDispancerISZLWorkOnModify;
CREATE PROCEDURE sp_DirectDispancerISZLWorkOnModify("GuidPackage" uuid, "QueueName" varchar(100))
As 
$$
declare _id varchar(36);
	_entName varchar(255);
Begin
	if exists(select* from AKUZ.T_RDS_DISP_PACKAGE r where r.RdsdispPackageID = "GuidPackage" and r.Status = 'ident')
	then
		select sp_DirectDispancerISZLXML("GuidPackage", "QueueName");
	else 		
		select into 
			_id, _entName 
			pack.Status, ent.EntName 
		from AKUZ.T_RDS_DISP_PACKAGE pack
		inner join 
			AKUZ.T_RDS_DISP_PROGRAM pr 
			on pr.RdsDispProgramID = pack.RdsDispProgram
		inner join 
			AKUZ.T_RDS_DISP_PROGRAM_GROUP programgroup 
			on pr.RdsDispProgramGroup = programgroup.RdsDispProgramGroupID
		inner join 
			VCLib.T_ENTITIES ent 
			on ent.EntityID = programgroup.Entities
		where pack.RdsdispPackageID = "GuidPackage";
		if _id is not null
		then
			if _entName in ('DISPANSERISATION_INIT', 'ONK_REGISTRY_CARD_TALON', 'AMBULANCE_CARD') 
			then
				select sp_DispanserisationInitToISZL(_id, "QueueName");
			else
				select sp_DirectDispancerISZLXMLDir(_id, "QueueName");
			end if;
		end if;
	end if;
End;
$$ Language plpgsql;