SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_DirectDispancerRDSXML03_Bur]') and xtype = N'P')
DROP PROCEDURE  [dbo].[sp_DirectDispancerRDSXML03_Bur]
GO


---- =============================================
---- Author:      Vitacore (с)
---- Create date: 2017-11-29
---- Description: Список пациентов, направленных на идентификацию формате XML (DIRECT_IDENT)
---- =============================================

--CREATE PROCEDURE [dbo].[sp_DirectDispancerRDSXML03_Bur]
--	@GuidPackage uniqueidentifier,
--    @QueueName varchar(100) = null,
--    @Step bit = 0
--AS
--BEGIN
--	SET NOCOUNT ON;
--	declare @i int
--	select @i= case  when count(*)  > 0  then 1 else 0 end  
--	from  AKUZ.T_RDS_DISP_PATIENT
--	where  AKUZ.T_RDS_DISP_PATIENT.RdsDispPackage = @GuidPackage

-----------------------  Удаляем результаты загрузки предыдущего акта -----------
--	update AKUZ.T_RDS_DISP_PATIENT 
--    set  AKUZ.T_RDS_DISP_PATIENT.SMO = null,  AKUZ.T_RDS_DISP_PATIENT.RdsDispPackage = @GuidPackage
--	from AKUZ.T_RDS_DISP_PATIENT pt
--	where pt.RdsDispPatientID in 
--	(
--	   select pt.RdsDispPatientID from  AKUZ.T_RDS_DISP_PATIENT pt
--		   left join AKUZ.T_RDS_DISP_PACKAGE pc on  pt.RdsDispPackage = pc.RdsdispPackageID
--	   where pc.RdsDispPackage = @GuidPackage
--	)

--   delete from AKUZ.T_RDS_DISP_PACKAGE    where RdsDispPackage = @GuidPackage

-------------- XML список пациентов -------------------
--	declare @filename varchar(50)
--	set @filename = (select dp1.FILENAME from AKUZ.T_RDS_DISP_PACKAGE dp1  where dp1.RdsdispPackageID= @GuidPackage)

--	declare @xml varchar(max)
--	set @xml = 
--	'<?xml version="1.0" encoding="windows-1251"?>' + 
--	(
--		SELECT 
--		-- Заголовок файла   ZGLV
--		(
--			SELECT  
--			'1.0' AS 'VERSION'
--			,convert(varchar(10),GETDATE(),120) AS 'DATA'    
--			,dp.FILENAME AS 'FILENAME'
--			,dp.CODE_MO AS 'MCOD'
--			FROM AKUZ.T_RDS_DISP_PACKAGE dp
--			WHERE dp.RdsdispPackageID =  @GuidPackage
--			FOR XML PATH ('ZGLV'),TYPE 
--		)
--		,( 
--		-- Данные ZL
--			SELECT 
--			dpt.ID_PAC as 'ZL_ID'
--			,dpt.FAM   as 'FAM' 
--			,dpt.IM   as 'IM'     
--			,dpt.OT  as 'OT' 
--			,convert(varchar(10),dpt.DR,120) AS 'DR'
--			,dpt.ENP as 'ENP'
--			,dpt.VPOLIS  as 'TPOLIS'
-- 			,case when REPLACE(dpt.SPOLIS, ' ', '' ) <> '' then REPLACE(dpt.SPOLIS, ' ', '' ) else null end as 'SPOLIS'
--			,case when dpt.NPOLIS  <> '' then dpt.NPOLIS else null end as 'NPOLIS'
--			,(	
--				SELECT 
--				(
--					select
--					pn.PhoneNumberID as 'CONTACT_ID'
--					,case when pn.PhoneType = 3 then 1
--						when pn.PhoneType in (1,2) then 2 end as 'TYPE'
--					,pn.Number as 'PHONE'
--					from AKUZ.T_PHONE_NUMBER pn 
--					where pn.Patient = dpt.ID_PAC and pn.PhoneType in (1,2,3)
--					FOR XML PATH ('CONTACT'), TYPE
--				)
--				,(
--					select 
--					 pn.PhoneNumberID as 'CONTACT_ID'
--					,3 as 'TYPE'
--					,pn.Number as 'EMAIL' 
--					from AKUZ.T_PHONE_NUMBER pn 
--					where pn.Patient = dpt.ID_PAC and pn.PhoneType = 4
--					FOR XML PATH ('CONTACT'), TYPE
--				) 
--				,(
--					select
--					--dpt.CONTACT_ID as 'CONTACT_ID'
--					4 as 'TYPE'
--					,(
--						select 
--						 dpt.ADDRESS_P_ID as 'ADDRES_P_ID'
--						,dpt.FULL_MAME as 'FULL_NAME'
--						,dpt.DOM as 'DOM'
--						,dpt.KV as 'KV'
--						FOR XML PATH ('ADDRES_P'), TYPE
--					)
--					FOR XML PATH ('CONTACT'), TYPE
--				) 
--				for xml path ('CONTACTS'), type
--			)
--			,(
--				select 
--				dpt.DN_ID as 'DN_ID'
--				,dpt.DS1 as 'DS'
--				,convert (varchar(10), dpt.DS1_PR_DATE,120) as 'DS1_PR_DATE'
--				,dpt.DS1_PR_SOURCE as 'DS1_PR_SOURCE'
--				,convert (varchar(10), dpt.DATE_OUT,120) as 'DATE_OUT'
--				,(
--					select 
--					dpt.APPOINTMENT_ID as 'APPOINTMENT_ID'
--					,convert (varchar(10), dpt.DATE_NEXT,120) as 'DATE_PLAN'
--					,dpt.CODE_MD as 'CODE_MD'
--					,dpt.[LOCATION] as 'PLACE'
--					for xml path ('APPOINTMENT'), type
--				)
--				for xml path ('DN'), type
--			)
--		FROM AKUZ.T_RDS_DISP_PATIENT dpt
--		WHERE dpt.RdsDispPackage = @GuidPackage 
--		FOR XML PATH('ZL'),TYPE
--	)
--	FOR XML PATH (''), ROOT('DN_LIST')
--  )

--  if (@QueueName is null)
--  begin
		
--		SELECT @filename,@xml
--  end
--  else
--  begin
--          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(@xml, '')--,<Content, text,>
--              ,@GuidPackage--,<MessageID, varchar(255),>
--              ,null--,<Headers, varchar(255),>
--              ,'utf-8'--@ContentEncoding--,<ContentEncoding, varchar(255),>
--              ,case when @Step = 0 then 'DISP_DIRECT_IDENT' else 'DISP_DIRECT' end--@Type--,<Type, varchar(255),>
--              ,null--,<UserID, varchar(255),>
--              ,0--,<State, int,>)
--              ,getdate()--@dateTime--,<CreateDateTime, datetime,>)
--              ,null--@OwnerID--,<OwnerID, varchar(255),>
--  end

--  END
