---- =============================================
---- Author:      Vitacore (с)
---- Create date: 2017-11-29
---- Description: Список пациентов, направленных на диспансеризацию
---- =============================================
--drop procedure if exists sp_DirectDispancerISZLXMLDir;
--CREATE PROCEDURE sp_DirectDispancerISZLXMLDir("GuidPackage" uuid, "QueueName" varchar(100) default null::varchar(100))
--As 
--$$
--declare _filename varchar(50);
--	_xml text;
--Begin
--	_filename := (select dp1.FILENAME from AKUZ.T_RDS_DISP_PACKAGE dp1 where dp1.RdsdispPackageID = "GuidPackage");

--	_xml := '<?xml version="1.0" encoding="windows-1251"?>' ||
--		--ZGLV
--		(
--			SELECT
--			xmlelement
--			(
--				name "PERS_LIST"
--			,	(
--					-- Заголовок файла   ZGLV
--					select
--					xmlelement
--					(
--						name "ZGLV"
--					,	xmlforest
--						(
--							'2.1' AS "VERSION"
--		 				,	to_char(now(), 'YYYY-MM-DD') AS "DATA"
--		 				,	dp.YearDisp  AS "YEAR"
--     					,	dp.MONTH AS "MONTH"
--		 				,	dp.CODE_MO AS "MCOD"
--		 				,	dp.FILENAME AS "FILENAME"
--						)
--					)
--					FROM AKUZ.T_RDS_DISP_PACKAGE dp
--					WHERE dp.RdsdispPackageID = "GuidPackage"
--				)
--			,	(
--					SELECT
--					xmlelement
--					(
--						name "PERS"
--					,	xmlforest
--						(
--							-- Данные   PERS
--							dpt.YEAR as "YEAR"
--						,	DATE_PART('Month', dpt.DR) as "MONTH"
--						,	dpt.TYPE as "TYPE"
--		 				,	dpt.V016DispType as  "IDDT"
--						,	dpt.PODR as "PODR"
--						,	dpt.UCH as "UCH"
--						,	dpt.SMO as "SMO"
--						,	dpt.FAM as "FAM"
--						,	dpt.IM as "IM"
--						,	dpt.OT as "OT"
--						,	dpt.W as "W"
--						,	to_char(dpt.DR, 'YYYY-MM-DD') AS "DR"
--						,	dpt.ENP as "ENP"
--						)
--					,	(
--							select
--							xmlagg
--							(
--								xmlelement
--								(
--									name "CONTACTS"
--								,	xmlforest
--									(
--										pn.Number as "CONTACT"
--									,	case when pn.PhoneType = 3 then 3 when pn.PhoneType in (1,2,5) then 4 when pn.PhoneType = 4 then 2 else 3 end as "TYPE"
--									)
--								)
--							)
--							from AKUZ.T_PHONE_NUMBER pn 
--							where pn.Patient = dpt.ID_PAC
--						)
--					,	xmlforest
--						(
--							dpt.MR as "MR"
--						,	dpt.DOCTYPE as "DOCTYPE"
--						,	case when REPLACE(dpt.DOCSER, ' ', '') <> '' then REPLACE(dpt.DOCSER, ' ', '') end as "DOCSER"
--						,	case when dpt.DOCNUM <> '' then dpt.DOCNUM end as "DOCNUM"
--						,	dpt.SNILS  as "SNILS"
--						,	case when dpt.VPOLIS in (2, 3) then 3 when dpt.VPOLIS = 4 then 2 when dpt.VPOLIS = 1 then 1 end as "VPOLIS"
--						,	case when REPLACE(dpt.SPOLIS, ' ', '') <> '' then REPLACE(dpt.SPOLIS, ' ', '') end as "SPOLIS"
--						,	case when dpt.NPOLIS <> '' then dpt.NPOLIS end as "NPOLIS"
--						)
--					)
--					FROM AKUZ.T_RDS_DISP_PATIENT dpt
--    				WHERE dpt.RdsDispPackage = "GuidPackage" 
--					and dpt.MonthDISP <> 0   --  Если месяц установлен в 0 - диспансеризация не проводится.
--				)
--			)
--		)::text;
		
--	if "QueueName" is null
--	then
--		select _filename, _xml;
--  	end if;
	
--	INSERT INTO AKUZ.T_RABBIT_SEND_OBJECT(RabbitSendObjectID, QueueName, Content, MessageID, Headers, ContentEncoding, Type, UserID, State, CreateDateTime, OwnerID)
--	select uuid_generate_v4(), "QueueName", ConvertTextToBinaryZip(_xml, ''), "GuidPackage", null, 'utf-8', 'DISP_DIRECT', null, 0, now()::timestamp, null;
	
--End;
--$$ Language plpgsql;