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

IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'AKUZ.T_EVENT_BASE_Update'))
DROP TRIGGER AKUZ.T_EVENT_BASE_Update
GO
---- =============================================
---- Author:		ЗАО Витакор
---- Create date: 17.11.2022
---- Description:	Взаимодействия по профосмотрам с ТФОМС, при закрытии случая кидаем SENDING_EXB_ITEM
---- =============================================
--CREATE TRIGGER AKUZ.T_EVENT_BASE_Update ON AKUZ.T_EVENT_BASE 
--FOR UPDATE
--AS 
--BEGIN
--	-- SET NOCOUNT ON added to prevent extra result sets from
--	-- interfering with SELECT statements.
--	SET NOCOUNT ON;

--    -- Insert statements for trigger here
--	IF (UPDATE(CloseDate))
--	BEGIN
--		INSERT INTO akuz.t_sending_exb_item(SendingEXBItemID,ExaminationBase,ErrorStr,SendingExbPackage,InstanceType)
--		select newid(), i.EventID, null, null, 1 
--		from inserted i 
--			join akuz.t_examination_base exb on i.EventID = exb.EventID
--			join akuz.t_examination_type et on exb.ExaminationType = et.ExaminationTypeID
--		where CloseDate is not null and et.ExaminationType in (1,2,4,9)
--	END
--END
--GO

--IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'AKUZ.T_EXAMINATION_BASE_Insert'))
--DROP TRIGGER AKUZ.T_EXAMINATION_BASE_Insert
--GO
---- =============================================
---- Author:		ЗАО Витакор
---- Create date: 17.11.2022
---- Description:	Взаимодействия по профосмотрам с ТФОМС, при новом случае профосмотра кидаем SENDING_EXB_ITEM
---- =============================================
--CREATE TRIGGER AKUZ.T_EXAMINATION_BASE_Insert ON AKUZ.T_EXAMINATION_BASE 
--FOR INSERT
--AS 
--BEGIN
--	-- SET NOCOUNT ON added to prevent extra result sets from
--	-- interfering with SELECT statements.
--	SET NOCOUNT ON;

--    -- Insert statements for trigger here
--	INSERT INTO akuz.t_sending_exb_item(SendingEXBItemID,ExaminationBase,ErrorStr,SendingExbPackage,InstanceType)
--	select newid(), i.EventID, null, null, (case when ev.CloseDate is not null then 1 else 0 end) 
--	from inserted i 
--		join akuz.t_event_base ev on i.EventID = ev.EventID
--		join akuz.t_examination_type et on i.ExaminationType = et.ExaminationTypeID
--	where et.ExaminationType in (1,2,4,9)
--END
--GO

--IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'AKUZ.T_EXAMINATION_BASE_Delete'))
--DROP TRIGGER AKUZ.T_EXAMINATION_BASE_Delete
--GO
---- =============================================
---- Author:		ЗАО Витакор
---- Create date: 17.11.2022
---- Description:	Взаимодействия по профосмотрам с ТФОМС, при удалении случая профосмотра удаляем SENDING_EXB_ITEM с пустым пакетом
---- =============================================
--CREATE TRIGGER AKUZ.T_EXAMINATION_BASE_Delete ON AKUZ.T_EXAMINATION_BASE 
--INSTEAD OF DELETE
--AS 
--BEGIN
--	-- SET NOCOUNT ON added to prevent extra result sets from
--	-- interfering with SELECT statements.
--	SET NOCOUNT ON;

--    -- Insert statements for trigger here
--	delete sei
--	from akuz.t_sending_exb_item sei
--	join deleted d on sei.ExaminationBase = d.EventID
--	where sei.SendingExbPackage is null

--	delete eb
--	from akuz.t_examination_base eb
--	join deleted d on eb.EventID = d.EventID
--END
--GO
