-- =============================================
-- Author:      Vitacore (с)
-- Create date: 2017-11-29
-- Description: Список пациентов, направленных на идентификацию формате XML (DIRECT_IDENT, DN_IDENT)
-- =============================================
drop procedure if exists sp_DirectDispancerISZLXML;
CREATE PROCEDURE public.sp_DirectDispancerISZLXML("GuidPackage" uuid, "QueueName" varchar(100) default null::varchar(100))
As 
$$
declare _filename varchar(50);
	_entityName varchar(100);
	_xml text;
Begin
	---------------------  Удаляем результаты загрузки предыдущего акта -----------
 	update AKUZ.T_RDS_DISP_PATIENT pt
	set	SMO = null
	,	RdsDispPackage = "GuidPackage"
	where pt.RdsDispPackage in (select pc.RdsdispPackageID from AKUZ.T_RDS_DISP_PACKAGE pc where pc.RdsDispPackage = "GuidPackage");
	
	delete from AKUZ.T_RDS_DISP_PACKAGE where RdsDispPackage = "GuidPackage";
   
	select into
		_filename, _entityName 
		dp1.FILENAME, ent.EntName 
	from AKUZ.T_RDS_DISP_PACKAGE dp1 
	inner join 
		AKUZ.T_RDS_DISP_PROGRAM pr 
		on pr.RdsDispProgramID = dp1.RdsDispProgram
	inner join 
		AKUZ.T_RDS_DISP_PROGRAM_GROUP gr 
		on gr.RdsDispProgramGroupID = pr.RdsDispProgramGroup
	inner join 
		VCLib.T_ENTITIES ent 
		on ent.EntityID = gr.Entities
	where dp1.RdsdispPackageID = "GuidPackage";

	if _entityName in ('DISPANSERISATION_INIT', 'ONK_REGISTRY_CARD_TALON', 'AMBULANCE_CARD')
	then
		--здесь формировать distinct #ZL
		create temp table TmpZLSDIST
		as
		select distinct dpt.ID_PAC, dpt.FAM, dpt.IM, dpt.OT, dpt.W
		, case when dpt.DOCSER = '' then null else dpt.DOCSER end as DOCSER
		, case when dpt.DOCNUM = '' then null else dpt.DOCNUM end as DOCNUM
		, case when dpt.DOCTYPE = '' then null else dpt.DOCTYPE end as DOCTYPE
		, to_char(dpt.DR, 'YYYY-MM-DD') AS DR
		, case when dpt.NPOLIS = '' then null else dpt.NPOLIS end as NPOLIS
		, case when dpt.ENP = '' then null else dpt.ENP end as ENP
		, case when dpt.VPOLIS = '2' or dpt.VPOLIS = '3' then '3'
				when dpt.VPOLIS = '4' then '2' 
				when dpt.VPOLIS = '1' then '1' 
			end as VPOLIS
		, case when dpt.SPOLIS = '' then null else dpt.SPOLIS end as SPOLIS
		, case when dpt.SMO = '' then null else dpt.SMO end as SMO
		, case when dpt.SNILS = '' then null else dpt.SNILS end as SNILS
		from AKUZ.T_RDS_DISP_PATIENT dpt
		where dpt.RdsDispPackage = "GuidPackage";

		_xml := '<?xml version="1.0" encoding="windows-1251"?>' ||
			--ZGLV
			(
				SELECT
				xmlelement
				(
					name "IDENTIFICATION_DN"
				,	(
						select
						xmlelement
						(
							name "ZGLV"
						,	xmlforest
							(
								dp.VERSION AS "VERSION"
							,	to_char(now(), 'YYYY-MM-DD') AS "DATA"
							,	dp.FILENAME AS "FILENAME"
							,	dp.CODE_MO AS "MCOD"
							)
						)
						FROM AKUZ.T_RDS_DISP_PACKAGE dp
						WHERE dp.RdsdispPackageID = "GuidPackage"
					)
				,	(
						SELECT
						xmlelement
						(
							name "ZL"
						,	xmlforest
							(
								zldist.ID_PAC as "ID_PAC"
							,	zldist.FAM as "FAM"
							,	zldist.IM as "IM"
							,	zldist.OT as "OT"
							,	zldist.W as "W"
							,	zldist.DR as "DR"
							,	zldist.ENP as "ENP"
							,	zldist.DOCTYPE as "DOCTYPE"
							,	zldist.DOCSER as "DOCSER"
							,	zldist.DOCNUM as "DOCNUM"
							,	zldist.VPOLIS as "VPOLIS"
							,	zldist.SPOLIS as "SPOLIS"
							,	zldist.NPOLIS as "NPOLIS"
							,	zldist.SMO as "SMO"
							,	zldist.SNILS as "SNILS"
							)
						)
						FROM TmpZLSDIST zldist
					)
				)
			)::text;
			
		DROP TABLE if exists TmpZLSDIST;
	end if;

	--TODO: ANOTHER TYPES

	--файловая загрузка
	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'
    , case when _entityName in ('DISPANSERISATION_INIT', 'ONK_REGISTRY_CARD_TALON', 'AMBULANCE_CARD') then 'IDENTIFICATION_DN' end
    , null, 0, now()::timestamp, null;
End;
$$ Language plpgsql;