
/****** Object:  StoredProcedure [dbo].[sp_TFOMS_OutcomeHospitalisationXml]    Script Date: 7/29/2021 9:27:25 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_TFOMS_OutcomeHospitalisationXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_TFOMS_OutcomeHospitalisationXml]
GO
-- =============================================
-- Author:	Ilya Zhuykov	Vitacore (с)
-- Create date: 2020-10-02
-- Description:	Выгрузка информации о завершении госпитализации в ТФОМС
-- =============================================
CREATE PROCEDURE [dbo].[sp_TFOMS_OutcomeHospitalisationXml]
	@ID uniqueidentifier = null,
	@QueueName varchar(255) = null,	
	@Sender varchar(255) = null
AS
BEGIN
declare @Result varchar(max)
		,@refNumber varchar(225)
		,@hospType int
		,@mainID uniqueidentifier = null
SET NOCOUNT ON;
	select @hospType = mdc.HospitalizationType
	,@mainID = mov.MovementID
	from [AKUZ].T_MOVEMENT_RESULT mov_res 
		join [AKUZ].T_MOVEMENT mov on mov_res.MedicalcardRegistration = mov.MovementID
		join [AKUZ].T_MEDICALCARD_REGISTRATION  mdc on mov_res.MedicalcardRegistration = mdc.MovementID
		join [AKUZ].T_EVENT_BASE eb on eb.EventID = mov.EmergencyEvent
		left join [AKUZ].T_DIRECTION dir on dir.DirectionID = eb.Direction
		left join [AKUZ].T_DIRECTION_BASE db on db.DirectionID = dir.DirectionID
		left join [AKUZ].T_LPU_MAIN lpDir on lpDir.LpuID = db.DirectorLpu
	where mov_res.MedicalRecordID = @ID
	
	select @refNumber = Concat(lpDir.LpuCode,YEAR(GETDATE()),case 
	when db.DirectionNumber<10 then concat('0000',db.DirectionNumber)
	when db.DirectionNumber<100 then concat('000',db.DirectionNumber)
	when db.DirectionNumber<1000 then concat('00',db.DirectionNumber)
	when db.DirectionNumber<10000 then concat('0',db.DirectionNumber)
	else db.DirectionNumber
	end)
	,@hospType = mdc.HospitalizationType
	,@mainID = mov.MovementID
	from [AKUZ].T_MOVEMENT_RESULT mov_res 
		join [AKUZ].T_MOVEMENT mov on mov_res.MedicalcardRegistration = mov.MovementID
		join [AKUZ].T_MEDICALCARD_REGISTRATION  mdc on mov_res.MedicalcardRegistration = mdc.MovementID
		join [AKUZ].T_EVENT_BASE eb on eb.EventID = mov.EmergencyEvent
		left join [AKUZ].T_DIRECTION dir on dir.DirectionID = eb.Direction
		left join [AKUZ].T_DIRECTION_BASE db on db.DirectionID = dir.DirectionID
		left join [AKUZ].T_LPU_MAIN lpDir on lpDir.LpuID = db.DirectorLpu
	where mov_res.MedicalRecordID = @ID and db.DirectionNumber is not null
--__________xml______________________________________________________________________________________________________________________________--
 	
if @hospType =  13001
begin
	;WITH xmlnamespaces ('https://services.rosminzdrav.ru/' as ser)
		SELECT
		@Result = 
		(
				SELECT 
				 '1.0'				                                   		    as 'ser:HEAD/ser:VERSION'				 --версия
				,@Sender	                                             		as 'ser:HEAD/ser:SENDER'			     --МО-отправитель сведений
				,convert(varchar(10),CURRENT_TIMESTAMP, 20)            		    as 'ser:HEAD/ser:D_EVENT'				 --Дата отправления сведений
				--,''												   		    as 'ser:HEAD/ser:GUID_QUERY'			 --Идентификатор для №4

			
				,
				(
					select 
					lm.LpuCode                                                		as 'ser:CODE_MO'				 --Принимающая направление МО
					,
					(
						select 
						IsNull(tt.MedicalRecordID,mov.MovementID )                      as 'ser:GUID'			 --Уникальный код записи 
						,lpDir.LpuCode											   		as 'ser:NPR_MO'		 --Реестровый номер МО, направившей пациента на госпитализацию
						,CASE 
							WHEN brDir.TFomsCode like '%.1' THEN null 
							ELSE brDir.TFomsCode
						 END 													   		as 'ser:NPR_LPU'		 --Региональный код подразделения МО, направившего пациента на госпитализацию			                                              		    
						,lm.LpuCode 										   			as 'ser:DEST_MO'		 --Реестровый номер МО, в которую направлен/госпитализирован пациент
						,CASE 
							WHEN br.TFomsCode like '%.1' THEN null 
							ELSE br.TFomsCode
						 END												   		    as 'ser:DEST_LPU'		 --Региональный код подразделения МО, в которое направлен/госпитализирован пациент
						,CASE mdc.HospitalizationType
							WHEN 13001 THEN 3
							WHEN 13002 THEN 1
							WHEN 13003 THEN 2
						 END															as 'ser:FOR_POM'	         --Форма оказания медицинской помощи F003
						 ,
						(
							select
								 pat.LastName													as 'ser:FAM'	 --Фамилия пациента
								,pat.FirstName													as 'ser:IM'	 --Имя пациента
								,pat.FatherName													as 'ser:OT'	 --Отчество пациента
								,(case when pat.Sex = 10301 then 1 else 2 end)					as 'ser:W'	     --Пол
								,convert(varchar(10), pat.Birthday, 20)							as 'ser:DR'	 --День рождения пациента

								--,''															as 'ser:FAM_P'	 --Фамилия представителя пациента
								--,''															as 'ser:IM_P'	 --Имя представителя пациента
								--,''															as 'ser:OT_P'	 --Отчество представителя пациента
								--,''															as 'ser:DR_P'	 --День рождения представителя пациента

								,CASE pol.PolicyForm
										WHEN 4 THEN 2
										WHEN 1 THEN 1
										else 3
									END															as 'ser:VPOLIS'	 --Тип ДПФС
								,pol.Serial     												as 'ser:SPOLIS'	 --Серия ДПФС
								,pol.Number    									    			as 'ser:NPOLIS'	 --Номер ДПФС
								,ISNULL(i1.SmoCod, i2.SmoCod)					    			as 'ser:SMO'		 --Реестровый номер СМО
								--,''															as 'ser:SMO_NAM'	 --Наименование СМО - не заполнять если номер есть
								,substring (ISNULL(ISNULL(r1.Okato, r2.Okato),''),1,5)			as 'ser:ST_OKATO'	 --Регион страхования
								--,''															as 'ser:NOVOR_NUM'	 --Номер ДПФС
								,isnull(tt.Phone ,
								(select top 1 Number  
								from akuz.t_phone_number ph_n
								where ph_n.Patient = pat.PatientID))							as 'ser:CONTACT'	 --Контактный номер
							FOR XML PATH('ser:PACIENT'),
							TYPE
						)
						,
						(
							select
							IsNull(convert(varchar(10), tt.ReferralDate, 20),
							convert(varchar(10), db.DirectionDate, 20))						as 'ser:NPR_DATE'	 --Номер ДПФС
							,@refNumber														as 'ser:NPR_NUM'	 --Номер ДПФС
							,IsNull(tt.MkbCode, db.Mkb)										as 'ser:DS'
							,CASE 
							-- исходного направления нет, но номер направления установлен
								when tt.DivisionType is null and @refnumber is not null then
									CASE d.DivisionType
										WHEN 12808 THEN 2
										WHEN 12809 THEN 2
										WHEN 12810 THEN 2
									ELSE 1
									end
								when tt.DivisionType is null then null
								WHEN tt.DivisionType  = 12808 THEN 2
								WHEN tt.DivisionType  =  12809 THEN 2
								WHEN tt.DivisionType  = 12810 THEN 2				
								ELSE 1
								END															as 'ser:USL_OK'     --Условия оказания МП V006 
							,IsNull(tt.sbCode,sb.CodeMz)									as 'ser:PROFK'
							,isnull(tt.divCode,d.Code )										as 'ser:PODR'
							,tt.Snils														as 'ser:CODE_MD'  --СНИЛС медработника, направившего больного
							,isNull(convert(varchar(10), tt.HospitalisationDate, 20)
							,convert(varchar(10), db.PreHospitalDate, 20))					as 'ser:DATE_P'
							FOR XML PATH('ser:NPR'),
							TYPE
						)
						,
						(
							select isnull(isnull(rdrd.Mkb ,mov.Mkb), db.Mkb) 				as 'ser:DS_P'		--Диагноз приемного отделения/диагноз при поступлении в отделение
							,isnull(isNull(convert(varchar(10), tt.HospitalisationDate, 20)
								,convert(varchar(10), db.PreHospitalDate, 20)),
							convert(varchar(10),  mov.MovementTime, 20))					as 'ser:DATE_P'		--Плановая дата госпитализации
							,convert(varchar(10), mov.ExpectedDateOfDischarge, 20)			as 'ser:DATE_V'		--Плановая дата выбытия
							,CASE d.DivisionType
								WHEN 12808 THEN 2
								WHEN 12809 THEN 2
								WHEN 12810 THEN 2
							ELSE 1
							END																as 'ser:USL_OK'	--Условия оказания МП
							,sb.CodeMz														as 'ser:PROFK'	--Код профиля койки
							,d.Code															as 'ser:PODR'	--Код отделения
							,mov.MedCardNumber												as 'ser:N_MKSB'	--Номер карты стационарного больного
							,replace(convert(varchar(20), mov.MovementTime, 20),' ','T')	as 'ser:DATE_1'	--Дата и время фактической госпитализации пациента
							,convert(varchar(10), mov_res.DatOut, 20)						as 'ser:DATE_2'	--Дата выбытия
							,ooi.MKBCode													as 'ser:DS'		--Диагноз при выбытии
							FOR XML PATH('ser:HOSP'),
							TYPE
						)
	--_____________________________________________________________________________________________________________________________________________________________
				
						FOR XML PATH('ser:REC'),
						TYPE
					)
					FOR XML PATH('ser:DATA'),
					TYPE
				)
			
			from [AKUZ].T_MOVEMENT_RESULT mov_res 
			join [AKUZ].T_OUTCOME_OF_INSPECTION ooi on mov_res.MedicalRecordID = ooi.MedicalRecordID
			join [AKUZ].T_MOVEMENT mov on mov_res.MedicalcardRegistration = mov.MovementID
			join [AKUZ].T_STRUCTURE_BED sb on sb.StructureBedID = mov.StructureBed
			join [AKUZ].T_V002_PROFILE v0 on v0.V002ProfileID = sb.V002Profile
			join [AKUZ].T_MEDICALCARD_REGISTRATION mdc on mdc.MovementID = mov.MovementID
			--LPU
			join [AKUZ].T_DIVISION d on d.DivisionID = mov.Division
			join [AKUZ].T_BRANCH br on br.LpuID = d.Branch
			join [AKUZ].T_LPU_MAIN lm on br.LpuMain = lm.LpuID
			--patient
			join [AKUZ].T_EVENT_BASE eb on eb.EventID = mov.EmergencyEvent
			join [AKUZ].T_EMERGENCY_EVENT ee on ee.EventID = eb.EventID
			join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
			join [AKUZ].T_PATIENT pat on pat.PatientID = ac.Patient
			--diagnosis
			left join [AKUZ].T_RECEPTION_DIAGNOSIS rd on rd.EmergencyEvent = eb.EventID 
			left join [AKUZ].T_DIAGNOSIS rdrd on rdrd.MedicalRecordID =  rd.MedicalRecordID 
			--polis
			left join [AKUZ].T_POLICY pol on pol.PolicyID = [dbo].[fn_GetOmsPolicy](pat.PatientID,mov.MovementDate)
			left join [AKUZ].T_INSURANCE_COMPANY ic on ic.OrganizationID = pol.InsuranceCompany
			left join [AKUZ].T_INSURER i1 on i1.InsurerID = ic.Insurer
			left join [AKUZ].T_INSURER i2 on i2.InsurerID = pol.Insurer
			left join [AKUZ].V_INSURER_REGION ir1 on ir1.InsurerRegionID = i1.OKATO		
			left join [AKUZ].T_REGION r1 on r1.RegionID = ir1.Region
			left join [AKUZ].V_INSURER_REGION ir2 on ir2.InsurerRegionID = i2.OKATO		
			left join [AKUZ].T_REGION r2 on r2.RegionID = ir2.Region
			--referral
			left join [AKUZ].T_DIRECTION dir on dir.DirectionID = eb.Direction
			left join [AKUZ].T_DIRECTION_BASE db on db.DirectionID = dir.DirectionID
			left join [AKUZ].T_LPU_MAIN lpDir on lpDir.LpuID = db.DirectorLpu
			left join [AKUZ].T_DIVISION dDir on  dDir.DivisionID= dir.DirectorDivision
			left join [AKUZ].T_BRANCH brDir on brDir.LpuID = dDir.Adress
		
			--original referral
			outer apply
			(
				select hr.MedicalRecordID, hr.Phone,hrr.ReferralDate,hrr.MkbCode, dT.DivisionType, hrsb.CodeMz as sbCode,hr.HospitalisationDate,dT.Code as divCode,hrw.Snils
				from [AKUZ].T_HOSPITALIZATION_REFERRAL hr
				join [AKUZ].T_REFERRAL hrr on hrr.MedicalRecordID = hr.MedicalRecordID
				join [AKUZ].T_MEDICAL_RECORD hrmr on hrmr.MedicalRecordID = hrr.MedicalRecordID
				join [AKUZ].T_EVENT_BASE hrae on  hrae.EventID = hrmr.AmbulanceEvent
				join [AKUZ].T_AMBULANCE_CARD hrac on  hrac.AmbulanceCardID = hrae.AmbulanceCard
				join [AKUZ].T_PATIENT hrp on  hrp.PatientID = hrac.Patient
				join [AKUZ].T_STRUCTURE_BED hrsb on hrsb.StructureBedID = hr.StructureBed
				join [AKUZ].T_DIVISION dT on dT.DivisionID = hrr.Division			
				join [AKUZ].T_POST hrpst on hrpst.ResourceID = hrmr.Post
				join [AKUZ].T_RESOURCE hrrs on hrrs.ResourceID = hrpst.ResourceID
				join [AKUZ].T_WORKER hrw on hrpst.Worker = hrw.WorkerID
				where hr.ReferralNumber = db.DirectionNumber
				and hrr.Lpu = db.Lpu
				and hrp.PatientID = pat.PatientID
				and hrr.ReferralDate <= eb.EventDate
				and DATEADD(M,2,hrr.ReferralDate)>= eb.EventDate
			) tt
			where mov_res.MedicalRecordID = @ID

			FOR XML PATH(''),
			ROOT('ser:SendRouteRqst')
		) 	
		
		select @mainID = IsNull(tt.MedicalRecordID,mov.MovementID )
		from [AKUZ].T_MOVEMENT_RESULT mov_res 
			join [AKUZ].T_MOVEMENT mov on mov_res.MedicalcardRegistration = mov.MovementID
			join [AKUZ].T_EVENT_BASE eb on eb.EventID = mov.EmergencyEvent
			join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
			join [AKUZ].T_PATIENT pat on pat.PatientID = ac.Patient
			left join [AKUZ].T_DIRECTION_BASE db on db.DirectionID = eb.Direction
			outer apply
			(
				select hr.MedicalRecordID
				from [AKUZ].T_HOSPITALIZATION_REFERRAL hr
				join [AKUZ].T_REFERRAL hrr on hrr.MedicalRecordID = hr.MedicalRecordID
				join [AKUZ].T_MEDICAL_RECORD hrar on  hrar.MedicalRecordID = hr.MedicalRecordID
				join [AKUZ].T_EVENT_BASE hrae on  hrae.EventID = hrar.AmbulanceEvent
				join [AKUZ].T_AMBULANCE_CARD hrac on  hrac.AmbulanceCardID = hrae.AmbulanceCard
				join [AKUZ].T_PATIENT hrp on  hrp.PatientID = hrac.Patient

				where hr.ReferralNumber = db.DirectionNumber
				and hrr.Lpu = db.Lpu
				and hrp.PatientID = pat.PatientID
				and hrr.ReferralDate <= eb.EventDate
				and DATEADD(M,2,hrr.ReferralDate)>= eb.EventDate
			) tt
			where mov_res.MedicalRecordID = @ID
end
else
begin
	;WITH xmlnamespaces ('https://services.rosminzdrav.ru/' as ser)
		SELECT @Result = 
		(
				SELECT 
				 '1.0'				                                   		    as 'ser:HEAD/ser:VERSION'				 --версия
				,@Sender	                                             		as 'ser:HEAD/ser:SENDER'			     --МО-отправитель сведений
				,convert(varchar(10),CURRENT_TIMESTAMP, 20)            		    as 'ser:HEAD/ser:D_EVENT'				 --Дата отправления сведений
				--,''												   		    as 'ser:HEAD/ser:GUID_QUERY'			 --Идентификатор для №4

			
				,
				(
					select 
					lm.LpuCode                                                		as 'ser:CODE_MO'				 --Принимающая направление МО
					,
					(
						select 
						mov.MovementID							                        as 'ser:GUID'			 --Уникальный код записи 
						,lm.LpuCode 										   			as 'ser:DEST_MO'		 --Реестровый номер МО, в которую направлен/госпитализирован пациент
						,CASE 
							WHEN br.TFomsCode like '%.1' THEN null 
							ELSE br.TFomsCode
						 END												   		    as 'ser:DEST_LPU'		 --Региональный код подразделения МО, в которое направлен/госпитализирован пациент
						,CASE mdc.HospitalizationType
							WHEN 13001 THEN 3
							WHEN 13002 THEN 1
							WHEN 13003 THEN 2
						 END															as 'ser:FOR_POM'	         --Форма оказания медицинской помощи F003
						 ,
						(
							select
								 pat.LastName													as 'ser:FAM'	 --Фамилия пациента
								,pat.FirstName													as 'ser:IM'	 --Имя пациента
								,pat.FatherName													as 'ser:OT'	 --Отчество пациента
								,(case when pat.Sex = 10301 then 1 else 2 end)					as 'ser:W'	     --Пол
								,convert(varchar(10), pat.Birthday, 20)							as 'ser:DR'	 --День рождения пациента

								--,''															as 'ser:FAM_P'	 --Фамилия представителя пациента
								--,''															as 'ser:IM_P'	 --Имя представителя пациента
								--,''															as 'ser:OT_P'	 --Отчество представителя пациента
								--,''															as 'ser:DR_P'	 --День рождения представителя пациента

								,CASE pol.PolicyForm
										WHEN 4 THEN 2
										WHEN 1 THEN 1
										else 3
									END													as 'ser:VPOLIS'	 --Тип ДПФС
								,pol.Serial     												as 'ser:SPOLIS'	 --Серия ДПФС
								,pol.Number    									    			as 'ser:NPOLIS'	 --Номер ДПФС
								,ISNULL(i1.SmoCod, i2.SmoCod)					    			as 'ser:SMO'		 --Реестровый номер СМО
								--,''															as 'ser:SMO_NAM'	 --Наименование СМО - не заполнять если номер есть
								,substring (ISNULL(ISNULL(r1.Okato, r2.Okato),''),1,5)			as 'ser:ST_OKATO'	 --Регион страхования
								--,''															as 'ser:NOVOR_NUM'	 --Номер ДПФС
								,
								(select top 1 Number  
								from akuz.t_phone_number ph_n
								where ph_n.Patient = pat.PatientID)								as 'ser:CONTACT'	 --Контактный номер
							FOR XML PATH('ser:PACIENT'),
							TYPE
						)
						,
						(
							select isnull(isnull(rdrd.Mkb ,mov.Mkb), db.Mkb)				as 'ser:DS_P'		--Диагноз приемного отделения/диагноз при поступлении в отделение
							,convert(varchar(10),db.PreHospitalDate	, 20)					as 'ser:DATE_P'		--Плановая дата госпитализации
							,convert(varchar(10), mov.ExpectedDateOfDischarge, 20)			as 'ser:DATE_V'		--Плановая дата выбытия
							,CASE d.DivisionType
								WHEN 12808 THEN 2
								WHEN 12809 THEN 2
								WHEN 12810 THEN 2
							ELSE 1
							END																as 'ser:USL_OK'	--Условия оказания МП
							,sb.CodeMz														as 'ser:PROFK'	--Код профиля койки
							,d.Code															as 'ser:PODR'	--Код отделения
							,mov.MedCardNumber												as 'ser:N_MKSB'	--Номер карты стационарного больного
							,replace(convert(varchar(20), mov.MovementTime, 20),' ','T')	as 'ser:DATE_1'	--Дата и время фактической госпитализации пациента
							,convert(varchar(10), mov_res.DatOut, 20)						as 'ser:DATE_2'	--Дата выбытия
							,ooi.MKBCode													as 'ser:DS'		--Диагноз при выбытии
							FOR XML PATH('ser:HOSP'),
							TYPE
						)
	--_____________________________________________________________________________________________________________________________________________________________

						FOR XML PATH('ser:REC'),
						TYPE
					)
					FOR XML PATH('ser:DATA'),
					TYPE
				)
			
			from [AKUZ].T_MOVEMENT_RESULT mov_res 
			join [AKUZ].T_OUTCOME_OF_INSPECTION ooi on mov_res.MedicalRecordID = ooi.MedicalRecordID
			join [AKUZ].T_MOVEMENT mov on mov_res.MedicalcardRegistration = mov.MovementID
			join [AKUZ].T_STRUCTURE_BED sb on sb.StructureBedID = mov.StructureBed
			join [AKUZ].T_V002_PROFILE v0 on v0.V002ProfileID = sb.V002Profile
			join [AKUZ].T_MEDICALCARD_REGISTRATION mdc on mdc.MovementID = mov.MovementID
			--LPU
			join [AKUZ].T_DIVISION d on d.DivisionID = mov.Division
			join [AKUZ].T_BRANCH br on br.LpuID = d.Branch
			join [AKUZ].T_LPU_MAIN lm on br.LpuMain = lm.LpuID
			--patient
			join [AKUZ].T_EVENT_BASE eb on eb.EventID = mov.EmergencyEvent
			join [AKUZ].T_EMERGENCY_EVENT ee on ee.EventID = eb.EventID
			join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
			join [AKUZ].T_PATIENT pat on pat.PatientID = ac.Patient
			--diagnosis
			left join [AKUZ].T_RECEPTION_DIAGNOSIS rd on rd.EmergencyEvent = eb.EventID 
			left join [AKUZ].T_DIAGNOSIS rdrd on rdrd.MedicalRecordID =  rd.MedicalRecordID 
			--polis
			left join [AKUZ].T_POLICY pol on pol.PolicyID = [dbo].[fn_GetOmsPolicy](pat.PatientID,mov.MovementDate)
			left join [AKUZ].T_INSURANCE_COMPANY ic on ic.OrganizationID = pol.InsuranceCompany
			left join [AKUZ].T_INSURER i1 on i1.InsurerID = ic.Insurer
			left join [AKUZ].T_INSURER i2 on i2.InsurerID = pol.Insurer
			left join [AKUZ].V_INSURER_REGION ir1 on ir1.InsurerRegionID = i1.OKATO		
			left join [AKUZ].T_REGION r1 on r1.RegionID = ir1.Region
			left join [AKUZ].V_INSURER_REGION ir2 on ir2.InsurerRegionID = i2.OKATO		
			left join [AKUZ].T_REGION r2 on r2.RegionID = ir2.Region
			--referral
			left join [AKUZ].T_DIRECTION dir on dir.DirectionID = eb.Direction
			left join [AKUZ].T_DIRECTION_BASE db on db.DirectionID = dir.DirectionID
			left join [AKUZ].T_LPU_MAIN lpDir on lpDir.LpuID = db.DirectorLpu
			left join [AKUZ].T_DIVISION dDir on  dDir.DivisionID= dir.DirectorDivision
			left join [AKUZ].T_BRANCH brDir on brDir.LpuID = dDir.Adress
		
			where mov_res.MedicalRecordID = @ID

			FOR XML PATH(''),
			ROOT('ser:SendRouteRqst')
		) 	
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),>
		   ,'utf-8'--,<ContentEncoding, varchar(255),>
		   ,'SendRouteRqst'--,<Type, varchar(255),>
		   ,null--,<UserID, varchar(255),>
		   ,0--,<State, int,>)
		   ,GETDATE()
		   ,@mainID	

end
