IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_RDS_PregnantHospitalizationXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_RDS_PregnantHospitalizationXml]
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-08-11
---- Description:	Выгрузка информации о госпитализации беременной
---- =============================================
--CREATE PROCEDURE [dbo].[sp_RDS_PregnantHospitalizationXml]
--	@LpuCode varchar(max) = null
--	,@ID uniqueidentifier = null
--	,@IsNew bit = 0,
--	@QueueName varchar(255) = 'RDSMotherhood'
--AS
--BEGIN

--declare @Result varchar(max)
--		,@ContentEncoding varchar(255)
--		,@Type varchar(255)
--	    ,@OperationType varchar(255)
--		,@OwnerID uniqueidentifier
--if(@IsNew = 1)
--begin
--select @OperationType = 'Insert'
--end
--else if (@IsNew = 0 and exists (select * from [AKUZ].T_MEDICALCARD_REGISTRATION m where m.MovementID = @ID))
--begin
--select @OperationType = 'Update'
--end
--else if (@IsNew = 0 and not exists (select * from [AKUZ].T_MEDICALCARD_REGISTRATION m where m.MovementID = @ID))
--begin
--select @OperationType = 'Delete'
--select @Result = (
--		select 
--		@LpuCode				as 'header/LPU'
--		,@OperationType			as 'header/TYPE'
--		,@ID					as 'header/ID'
--		FOR XML PATH(''),
--		ROOT('PREGNANT_HOSPITALIZATION')
--	) 
--end
--select @ContentEncoding = 'utf-8'
--	   ,@Type = 'PREGNANT_HOSPITALIZATION'
--	   ,@OwnerID = @ID

--SET NOCOUNT ON;
--if(@OperationType <> 'Delete')
--BEGIN
--	select @Result = (
--		select 
--		@LpuCode				as 'header/LPU'
--		,@OperationType			as 'header/TYPE'
--		,@ID					as 'header/ID'

--		,p.LastName				as 'body/LAST_NAME'
--		,p.FirstName			as 'body/FIRST_NAME'
--		,p.FatherName			as 'body/FATHER_NAME'
--		,convert(varchar(10), p.Birthday, 20) as 'body/BIRTHDAY'
--		,convert(varchar(10), m.MovementDate, 20) as 'body/HOSPITALISATION_DATE'
--		,convert(varchar(20), m.MovementTime, 20) as 'body/HOSPITALISATION_TIME'

--		from [AKUZ].T_MEDICALCARD_REGISTRATION mcr
--		join [AKUZ].T_MOVEMENT m  on m.MovementID = mcr.MovementID
--		join [AKUZ].T_EVENT_BASE eb on eb.EventID = m.EmergencyEvent
--		join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
--		join [AKUZ].T_PATIENT p on p.PatientID = ac.Patient
--		where m.MovementID = @ID
--		FOR XML PATH(''),
--		ROOT('PREGNANT_HOSPITALIZATION')
--	) 
--END

--INSERT INTO [AKUZ].[T_RABBIT_SEND_OBJECT]
--		   ([RabbitSendObjectID]
--		   ,[QueueName]
--		   ,[Content]
--		   ,[MessageID]
--		   ,[Headers]
--		   ,[ContentEncoding]
--		   ,[Type]
--		   ,[UserID]
--		   ,[State]
--		   ,[CreateDateTime]
--		   ,[OwnerID])
--select
--		   NewID()--(<RabbitSendObjectID, uniqueidentifier,>
--		   ,@QueueName--,<QueueName, varchar(255),>
--		   ,dbo.ConvertTextToBinaryZip(@Result, '')--,<Content, text,>
--		   ,null--,<MessageID, varchar(255),>
--		   ,null--,<Headers, varchar(255),>
--		   ,@ContentEncoding--,<ContentEncoding, varchar(255),>
--		   ,@Type--,<Type, varchar(255),>
--		   ,null--,<UserID, varchar(255),>
--		   ,0--,<State, int,>)
--		   ,GETDATE()
--		   ,@OwnerID
--END
--GO