SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

--if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_DirectDispancerISZLWorkOnModify]') and xtype = N'P')
--DROP PROCEDURE  [dbo].[sp_DirectDispancerISZLWorkOnModify]
--GO

---- =============================================
---- Author:      Vitacore (с)
---- Create date: 2017-09-21
---- Description: Хранимка рулит какую хранимку звать по действию при изменении объекта
---- =============================================

--хранимка изменена в регионах, как минимум в Севасе, поэтому вместо Drop/Create - Create if not exists
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_DirectDispancerISZLWorkOnModify]') and xtype = N'P')
	EXEC('CREATE PROCEDURE [dbo].[sp_DirectDispancerISZLWorkOnModify]
	@GuidPackage uniqueidentifier,
	@QueueName varchar(100)
	AS
	BEGIN
		SET NOCOUNT ON;
	 
		if ((select [Status] from AKUZ.T_RDS_DISP_PACKAGE where RdsdispPackageID = @GuidPackage) = ''' + 'ident' + ''')
		begin
			exec sp_DirectDispancerISZLXML @GuidPackage, @QueueName
		end
		else 
		begin
			declare @id varchar(36);
			declare @entName varchar(255);
			(select @id = [Status], @entName = ent.EntName from AKUZ.T_RDS_DISP_PACKAGE pack
			 join AKUZ.T_RDS_DISP_PROGRAM program on program.RdsDispProgramID = pack.RdsDispProgram
			 join AKUZ.T_RDS_DISP_PROGRAM_GROUP programgroup on program.RdsDispProgramGroup = programgroup.RdsDispProgramGroupID
			 join VCLib.T_ENTITIES ent on ent.EntityID = programgroup.Entities
			 where pack.RdsdispPackageID = @GuidPackage)
			if (@id is not null)
			begin
				if(@entName = ''' + 'DISPANSERISATION_INIT' + ''' or @entName = ''' + 'ONK_REGISTRY_CARD_TALON' + '''or @entName = ''' + 'AMBULANCE_CARD' + ''') 
				begin
				exec sp_DispanserisationInitToISZL @id, @QueueName
				end
				else 
				begin
				exec sp_DirectDispancerISZLXMLDir @id, @QueueName
				end
			end
		end
	END')
GO







--Для Севаса этот вариант хранимки

--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO

--if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_DirectDispancerISZLWorkOnModify]') and xtype = N'P')
--DROP PROCEDURE  [dbo].[sp_DirectDispancerISZLWorkOnModify]
--GO

------ =============================================
------ Author:      Vitacore (с)
------ Create date: 2017-09-21
------ Description: Хранимка рулит какую хранимку звать по действию при изменении объекта
------ =============================================

--CREATE PROCEDURE [dbo].[sp_DirectDispancerISZLWorkOnModify]
--@GuidPackage uniqueidentifier,
--@QueueName varchar(100)
--AS

--BEGIN
--	SET NOCOUNT ON;
	 
--	if ((select [Status] from AKUZ.T_RDS_DISP_PACKAGE where RdsdispPackageID = @GuidPackage) = 'ident')
--	begin
--		exec sp_DirectDispancerISZLXML @GuidPackage, @QueueName
--	end
--	else 
--	begin
--		declare @id varchar(36);
--		declare @entName varchar(255);
--		(select @id = [Status], @entName = ent.EntName from AKUZ.T_RDS_DISP_PACKAGE pack
--		 join AKUZ.T_RDS_DISP_PROGRAM program on program.RdsDispProgramID = pack.RdsDispProgram
--		 join AKUZ.T_RDS_DISP_PROGRAM_GROUP programgroup on program.RdsDispProgramGroup = programgroup.RdsDispProgramGroupID
--		 join VCLib.T_ENTITIES ent on ent.EntityID = programgroup.Entities
--		 where pack.RdsdispPackageID = @GuidPackage)
--		if (@id is not null)
--		begin
--			if(@entName = 'DISPANSERISATION_INIT' or @entName = 'AMBULANCE_CARD') 
--			begin
--			exec sp_DispanserisationInitToISZL @id, @QueueName
--			end
--			else if (@entName = 'ONK_REGISTRY_CARD_TALON')
--			begin
--			exec sp_DispanserisationOnkoToISZL @id, @QueueName
--			end
--			else
--			begin
--			exec sp_DirectDispancerISZLXMLDir @id, @QueueName
--			end
--		end
--	end
--END