IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_RDS_DeliveryAndPostpartumPeriodXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_RDS_DeliveryAndPostpartumPeriodXml]
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-09-08
---- Description:	Выгрузка информации о родах
---- =============================================
--CREATE PROCEDURE [dbo].[sp_RDS_DeliveryAndPostpartumPeriodXml]
--	@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_DELIVERY_AND_POSTPARTUM_PERIOD dpp where dpp.MedicalRecordId = @ID))
--begin
--select @OperationType = 'Update'
--end
--else if (@IsNew = 0 and not exists (select * from [AKUZ].T_DELIVERY_AND_POSTPARTUM_PERIOD dpp where dpp.MedicalRecordId = @ID))
--begin
--select @OperationType = 'Delete'
--	select @Result = (
--		select 
--		@LpuCode				as 'header/LPU'
--		,@OperationType			as 'header/TYPE'
--		,@ID					as 'header/ID'
--		FOR XML PATH(''),
--		ROOT('DELIVERY_AND_POSTPARTUM_PERIOD')
--	) 
--end
--select @ContentEncoding = 'utf-8'
--	   ,@Type = 'DELIVERY_AND_POSTPARTUM_PERIOD'
--	   ,@OwnerID = @ID

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

--		,Cast(dpp.Year as varchar) + dpp.ObmennayKarta as 'body/PASSPORT'
--		,wc.LpuCode				as 'body/WOMEN_CONSULTATION'
--		,dpp.FirstStage			as 'body/FIRST_STAGE'
--		,dpp.SecondStage		as 'body/SECOND_STAGE'
--		,dpp.ChildbirthCourseFeatures as 'body/CHILDBIRTH_COURSE_FEATURES'
--		,dpp.AnhydrousInterval	as 'body/ANHYDROUS_INTERVAL'
--		,dpp.AmnioticFluidCharacterization as 'body/AMNIOTIC_FLUID_CHARECTERIZATION'
--		,dpp.Complications		as 'body/COMPLICATIONS'
--		,dpp.Recommendations	as 'body/RECOMMENDATIONS'
--		,dpp.AidedDelivery		as 'body/AIDED_DELIVERY'
--		,dpp.Treatment			as 'body/TREATMENT'
--		,p.Code + ' ' + w.LastName + ' ' + w.FirstName + ' ' + w.FatherName as 'body/POST'
--		,hr.MKBCode				as 'body/MKB'
--		,hr.Diagnosis			as 'body/DIAGNOSIS'
--		,convert(varchar(10), acr.PostingDate, 20) as 'body/DATE_OF_BIRTH'
--		,convert(varchar(10), m.MovementDate, 20) as 'body/HOSPITALISATION_DATE'
--		,convert(varchar(20), m.MovementTime, 20) as 'body/HOSPITALISATION_TIME'
--		,m.MovementID			as 'body/PREGNANT_ID'
--		,pat.LastName			as 'body/LAST_NAME'
--		,pat.FirstName			as 'body/FIRST_NAME'
--		,pat.FatherName			as 'body/FATHER_NAME'
--		,convert(varchar(10), pat.Birthday, 20) as 'body/BIRTHDAY'
--		from [AKUZ].T_DELIVERY_AND_POSTPARTUM_PERIOD dpp
--		join [AKUZ].T_HOSPITAL_RECORD hr on hr.MedicalRecordID = dpp.MedicalRecordId
--		join [AKUZ].T_MEDICAL_RECORD mr on mr.MedicalRecordId = dpp.MedicalRecordId
--		join [AKUZ].T_AMBULANCE_CARD_RECORD acr on acr.MedicalRecordId = hr.MedicalRecordId
--		left join [AKUZ].T_LPU_MAIN wc on wc.LpuID = dpp.PregnantLpu		
--		join [AKUZ].T_POST p on p.ResourceID = mr.Post
--		join [AKUZ].T_WORKER w on w.WorkerID = p.Worker		
--		join [AKUZ].T_MOVEMENT m on m.MovementID = acr.Movement
--		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 pat on pat.PatientID = ac.Patient
--		where dpp.MedicalRecordId = @ID
--		FOR XML PATH(''),
--		ROOT('DELIVERY_AND_POSTPARTUM_PERIOD')
--	) 
--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