DROP PROCEDURE IF EXISTS DBO.sp_rr_patientbenefits;
-- =============================================
-- Author:      Vitacore (с)
-- Create date: 2024-06-21
-- Description: Загрузка регистра региональных льготников
-- =============================================
CREATE OR REPLACE PROCEDURE APS.sp_rr_patientbenefits
(
	"Xml" xml DEFAULT NULL::xml
,	"ID" uuid DEFAULT NULL::uuid
,	"QueueName" character varying DEFAULT 'RDSSync'::character varying
,	"dateTime" timestamp without time zone DEFAULT NULL::timestamp without time zone
,	"User" uuid DEFAULT NULL::uuid
)
    language plpgsql
as
$$
Begin
	Declare	
		_OperationType varchar(255);
	BEGIN
		"dateTime" := Coalesce("dateTime", Current_TimeStamp::TIMESTAMP);
	
		IF "Xml" is not null
		then 
			_OperationType := nullif(trim(xpath('/RR_PATIENT_BENEFIT/header[1]/TYPE[1]/text()', "Xml")::text,'{}'),'');

			drop table if exists Tmp_RR_PATIENT_BENEFITS;
			create temp table Tmp_RR_PATIENT_BENEFITS
			as
			SELECT	
				nullif(trim(xpath('/RR_PATIENT_BENEFIT/RrPatient[1]/text()', x)::text,'{}'),'')::int							as RrPatient
				,to_timestamp(nullif(trim(xpath('/RR_PATIENT_BENEFIT/DiagDate[1]/text()', x)::text,'{}'),''),'DD.MM.YYYY')		as DiagDate
				,nullif(trim(xpath('/RR_PATIENT_BENEFIT/Document[1]/text()', x)::text,'{}'),'')::varchar(16)					as Document
				,to_timestamp(nullif(trim(xpath('/RR_PATIENT_BENEFIT/DocumentDate[1]/text()', x)::text,'{}'),''),'DD.MM.YYYY')	as DocumentDate
				,nullif(trim(xpath('/RR_PATIENT_BENEFIT/Dop[1]/text()', x)::text,'{}'),'')::varchar(1)							as Dop
				,nullif(trim(xpath('/RR_PATIENT_BENEFIT/Mkb[1]/text()', x)::text,'{}'),'')::varchar(8)							as Mkb
				,to_timestamp(nullif(trim(xpath('/RR_PATIENT_BENEFIT/BeginDate[1]/text()', x)::text,'{}'),''),'DD.MM.YYYY')		as BeginDate
				,to_timestamp(nullif(trim(xpath('/RR_PATIENT_BENEFIT/EndDate[1]/text()', x)::text,'{}'),''),'DD.MM.YYYY')		as EndDate
				,nullif(trim(xpath('/RR_PATIENT_BENEFIT/BenefitCode[1]/text()', x)::text,'{}'),'')::varchar(255)				as BenefitCode
				,nullif(trim(xpath('/RR_PATIENT_BENEFIT/BenefitViews[1]/text()', x)::text,'{}'),'')::int						as BenefitViews
				,nullif(trim(xpath('/RR_PATIENT_BENEFIT/Lpu[1]/text()', x)::text,'{}'),'')::varchar(6)							as Lpu
				,to_timestamp(nullif(trim(xpath('/RR_PATIENT_BENEFIT/LastEditDate[1]/text()', x)::text,'{}'),''),'DD.MM.YYYY')	as LastEditDate
				,nullif(trim(xpath('/RR_PATIENT_BENEFIT/Post[1]/text()', x)::text,'{}'),'')::varchar(10)						as Post
				,nullif(trim(xpath('/RR_PATIENT_BENEFIT/DivisionManager[1]/text()', x)::text,'{}'),'')::varchar(10)				as DivisionManager
			from
			(
				select	unnest(xpath('/RR_PATIENT_BENEFIT/body/RR_PATIENT_BENEFITS/RR_PATIENT_BENEFIT', "Xml")) as x
			) RR_PATIENT_BENEFIT
			where _OperationType = 'FTLoad';

			-- Обработка операции
			if _OperationType = 'FTLoad'
			then		
			    delete from APS.T_RR_PATIENT_BENEFIT pb;

			    INSERT INTO APS.T_RR_PATIENT_BENEFIT (RrPatientBenefitID, DiagDate, Document, DocumentDate, Dop, Mkb, Benefits, RrPatient, BeginDate, EndDate, LastEditDate, LastEditLpu, Post, DivisionManager)
			    SELECT
					gen_random_uuid()
					,npb.DiagDate
					,npb.Document
					,npb.DocumentDate
					,npb.Dop
					,npb.Mkb
					,b.BenefitsID
					,npb.RrPatient
					,npb.BeginDate
					,npb.EndDate
					,npb.LastEditDate
					,npb.Lpu
					,npb.Post
					,npb.DivisionManager
			    from Tmp_RR_PATIENT_BENEFITS npb
			        left join VCLib.T_ENUMS eBv on eBv.Code = npb.BenefitViews and eBv.EnumTypeID = 190
			        left join APS.T_BENEFITS b on b.BenefitCode = npb.BenefitCode and b.BenefitViews = eBv.EnumID;
			end if;

			-- Drop tables
				DROP TABLE if exists Tmp_RR_PATIENT_BENEFITS;
		end if;
    End;
End;
$$;