IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_RDS_NewbornDevelopmentHistoryXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_RDS_NewbornDevelopmentHistoryXml]
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-08-20
---- Description:	Выгрузка Истории развития новорожденного
---- =============================================
--CREATE PROCEDURE [dbo].[sp_RDS_NewbornDevelopmentHistoryXml]
--	@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_NEWBORN_DEVELOPMENT_HISTORY ndh where ndh.MedicalRecordId = @ID))
--begin
--select @OperationType = 'Update'
--end
--else if (@IsNew = 0 and not exists (select * from [AKUZ].T_NEWBORN_DEVELOPMENT_HISTORY ndh where ndh.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('NEWBORN_DEVELOPMENT_HISTORY')
--	) 
--end

--select @ContentEncoding = 'utf-8'
--	   ,@Type = 'NEWBORN_DEVELOPMENT_HISTORY'
--	   ,@OwnerID = @ID

--SET NOCOUNT ON;
--if(@OperationType <> 'Delete')
--BEGIN
--	select @Result = (
--		select 
--		@LpuCode					as 'header/LPU'
--		,@OperationType				as 'header/TYPE'
--		,@ID						as 'header/ID'		
		
--		,ndh.LastName				as  'body/LAST_NAME'
--		,ndh.FirstName				as	'body/FIRST_NAME'
--		,ndh.FatherName				as	'body/FATHER_NAME'
--		,ndh.Malformations			as  'body/MALFORMATIONS'
--		,ndh.BirthTrauma			as  'body/BIRTH_TRAUMA'
--		,ndh.PreventionGonoblenorei	as  'body/PREVENTION_GONOBLENOREI'
--		,ndh.MotherHeredity			as  'body/MOTHER_HIREDITY'
--		,ndh.FatherHeredity			as  'body/FATHER_HIREDYTY'
--		,convert(varchar(10), ndh.TransferDate, 20)	as  'body/TRANSFER_DATE'
--		,ndh.DutyMidwife			as  'body/DUTY_MIDWIFE'
--		,convert(varchar(20), ndh.TransferTime, 20)	as  'body/TRANSFER_TIME'
--		,ndh.[Weight]				as  'body/WEIGHT'
--		,ndh.Height					as  'body/HEIGHT'
--		,ndh.HeaCircumference		as  'body/HEA_CIRCUMFERENCE'
--		,ndh.ChestCircumference		as  'body/CHEST_CIRCUMFERENCE'
--		,convert(varchar(10), ndh.DateBurn, 20)	as  'body/DATE_BURN'
--		,convert(varchar(20), ndh.TimeBurn, 20)	as  'body/TIME_BURN'
--		,ndh.Chamber				as  'body/CHAMBER'
--		,ndh.Bed					as  'body/BED'
--		,ndh.MotherLastName			as  'body/MOTHER_LAST_NAME'
--		,ndh.MotherFirstName		as  'body/MOTHER_FIRST_NAME'
--		,ndh.MotherFatherName		as  'body/MOTHER_FATHER_NAME'
--		,convert(varchar(10), ndh.MotherBirthdate, 20)	as  'body/MOTHER_BIRTHDATE'
--		,ndh.[Address]				as  'body/ADDRESS'
--		,ndh.Nationality			as  'body/NATIONALITY'
--		,ndh.Profession				as  'body/PROFESSION'
--		,ndh.MotherBed				as  'body/MOTHER_BED'
--		,ndh.MotherChamber			as  'body/MOTHER_CHAMBER'
--		,ndh.MidwifeHandedBaby		as  'body/MIDWIFE_HANDED_BABY'
--		,ndh.ReceivingNurse			as  'body/RECEIVING_NURSE'
--		,ndh.PreliminaryMkb			as  'body/PRELIMINARY_MKB'
--		,ndh.PreliminaryDiagnosis	as  'body/PRELIMINARY_DIAGNOSIS'
--		,hr.MKBCode					as  'body/FINAL_MKB'
--		,hr.Diagnosis				as  'body/FINAL_DIAGNOSIS'
--		,ndh.Epicrisis				as  'body/EPICRISIS'
--		,ndh.DischargeChildCondition	as  'body/DISCHARGE_CHILD_CONDITION'
--		,ndh.RecommendationsGP		as  'body/RECOMMENDATIONS_GP'
--		,convert(varchar(10), ndh.ClinicNotificationDate, 20)	as  'body/CLINIC_NOTIFICATION_DATE'
--		,Cast(ndh.Year as varchar) + ndh.PregnancyCardNumber	as  'body/PREGNANCY_CARD_NUMBER'
--		,ndh.PregnancyNumber		as  'body/PREGNANCY_NUMBER'
--		,ndh.ChildbirthNumber		as  'body/CHILDBIRTH_NUMBER'
--		,ndh.DiseaseDuringChildbirth	as  'body/DISEASE_DURING_CHILDBIRTH'
--		,ndh.FirstStage				as  'body/FIRST_STAGE'
--		,ndh.SecondStage			as  'body/SECOND_STAGE'
--		,ndh.ChildbirthCourseFeatures	as  'body/CHILDBIRTH_COURSE_FEATURES'
--		,ndh.AnhydrousInterval		as  'body/ANHYDROUS_INTERVAL'
--		,ndh.AmnioticFluidCharacterization	as  'body/AMNIOTIC_FLUID_CHARACTERIZATION'
--		,ndh.MotherWorkPlace		as  'body/MOTHER_WORk_PLACE'
--		,ndh.MotherPhone		as  'body/MOTHER_PHONE'
--		,convert(varchar(20), ndh.DischargeDateTime, 20) as  'body/DISCHARGE_DATE_TIME'
--		,convert(varchar(10), ndh.DeathDate, 20) as  'body/DEATH_DATE'
--		,convert(varchar(20), ndh.DeathTime, 20)	as  'body/DEATH_TIME'
--		,ndh.HistoryNumber			as  'body/HISTORY_NUMBER'
--		,convert(varchar(10), ndh.BCGDate, 20)	as  'body/BCG_DATE'
--		,convert(varchar(10), ndh.GHGDate, 20)	as  'body/GHG_DATE'
--		,p.Code + ' ' + w.LastName + ' ' + w.FirstName + ' ' + w.FatherName as 'body/POST'
--		,(select Top 1 aan.Heartbeat + aan.Breathing + aan.ColorSkin + aan.MuscleTone + aan.Reflexes from [AKUZ].T_APGAR_ASSESSMENT_OF_NEWBORN aan where aan.TimeAfterBirth = 0)  as 'body/APGAR_1'
--		,(select Top 1 aan.Heartbeat + aan.Breathing + aan.ColorSkin + aan.MuscleTone + aan.Reflexes from [AKUZ].T_APGAR_ASSESSMENT_OF_NEWBORN aan where aan.TimeAfterBirth = 1)  as 'body/APGAR_2'
--		,ndh.FatherBloodGroup		as 'body/FATHER_BLOOD_GROUP'
--		,ndh.FatherRhesus_factor	as 'body/FATHER_RHESUS'
--		,ndh.BloodGroup				as 'body/BLOOD_GROUP'
--		,ndh.Rhesus_factor			as 'body/RHESUS_FACTOR'
--		,ndh.Sex					as 'body/SEX'
--		,ndh.MotherBloodGroup		as 'body/MOTHER_BLOOD_GROUP'
--		,ndh.MotherRhesus_factor	as 'body/MOTHER_RHESUS'
--		,cc.LpuCode					as 'body/CHILDREN_CLINIC'
--		,wc.LpuCode					as 'body/WOMEN_CONSULTATION'
--		,ndh.RodzalTransferCondition as 'body/RODZAL_TRANSFER_CONDITION'
--		,ndh.SkinColor				as 'body/SKIN_COLOR'
--		,ndh.CharacterShout			as 'body/CHARACTER_SHOUT'
--		,ndh.BurnState				as 'body/BURN_STATE'
--		,ndh.FullTermState			as 'body/FULL_TERM_STATE'
--		,ndh.Education				as 'body/EDUCATION'
--		,ndh.Maritalstatus			as 'body/MARITAL_STATUS'
--		,ndh.DischargeWeight		as 'body/DISCHARGE_WEIGHT'
--		,ndh.ChildNumber			as 'body/CHILD_NUMBER'
--		,convert(varchar(10), ndh.UmbilicalResidueDropped, 20) as 'body/UMBILICAL_RESIDUE'
--		,convert(varchar(10), m.MovementDate, 20) as 'body/PREGNANT_HOSPITALISATION_DATE'
--		,convert(varchar(20), m.MovementTime, 20) as 'body/PREGNANT_HOSPITALISATION_TIME'
--		,m.MovementID				as 'body/PREGNANT_ID'
--		from [AKUZ].T_NEWBORN_DEVELOPMENT_HISTORY ndh
--		join [AKUZ].T_HOSPITAL_RECORD hr on hr.MedicalRecordID = ndh.MedicalRecordId
--		join [AKUZ].T_MEDICAL_RECORD mr on mr.MedicalRecordId = ndh.MedicalRecordId
--		join [AKUZ].T_AMBULANCE_CARD_RECORD acr on acr.MedicalRecordId = hr.MedicalRecordId
--		join [AKUZ].T_POST p on p.ResourceID = mr.Post
--		join [AKUZ].T_WORKER w on w.WorkerID = p.Worker
--		left join [AKUZ].T_LPU_MAIN cc on cc.LpuID = ndh.ChildrenClinic
--		left join [AKUZ].T_LPU_MAIN wc on wc.LpuID = ndh.WomenConsultation
--		join [AKUZ].T_CHILD_REGISTRATION cr on cr.MovementID = acr.Movement
--		join [AKUZ].T_MEDICALCARD_REGISTRATION mcr on mcr.MovementID = cr.MedicalcardRegistration
--		join [AKUZ].T_MOVEMENT m on mcr.MovementID = m.MovementID
--		where ndh.MedicalRecordId = @ID
--		FOR XML PATH(''),
--		ROOT('NEWBORN_DEVELOPMENT_HISTORY')
--	) 
--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