
				with doc(
					EMDDocumentTypeLocal_id,
					EMDDocumentTypeLocal_Name,
					Evn_id,
					LpuSection_id,
					LpuBuilding_id,
					MedStaffFact_id,
					Doc_Num,
					IsSigned,
					EvnXml_id
				) as ( 
		-- Протокол консультации
			(SELECT
				100047 as EMDDocumentTypeLocal_id,
				'Протокол оказания услуги (Оказание общей услуги)' as EMDDocumentTypeLocal_Name,
			
				evn.Evn_id,
				ls.LpuSection_id,
				ls.LpuBuilding_id,
				e.MedStaffFact_id,
				num.Doc_Num,
				x.EvnXml_IsSigned as IsSigned,
				x.EvnXml_id
			FROM v_EvnXml x
			INNER JOIN v_Evn evn on evn.Evn_id = x.Evn_id
			INNER JOIN v_EvnUslugaCommon e on e.EvnUslugaCommon_id = evn.Evn_id
			LEFT JOIN v_PersonState ps on ps.Person_id = evn.Person_id
			LEFT JOIN v_LpuSection ls on ls.LpuSection_id = e.LpuSection_uid
			
				LEFT JOIN v_pmUserCache pmuIns on pmuIns.PMUser_id = evn.pmUser_insID
				LEFT JOIN v_pmUserCache pmuUpd on pmuUpd.PMUser_id = evn.pmUser_updID
			
			left join lateral (
				SELECT cast(EvnXml_id as varchar) as Doc_Num FROM v_EvnXml WHERE EvnXml_id = x.EvnXml_id limit 1
			) num on true
			WHERE (1=1)
				and e.EvnClass_id = 22
				and x.XmlType_id = 4
				 and evn.Person_id IS NOT NULL  and evn.Lpu_id = '13101856'  and ls.LpuBuilding_id = '890101000000078'  and ls.LpuSection_id = '890101000000828'  and (pmuIns.MedPersonal_id = '890101000001148'
							or pmuUpd.MedPersonal_id = '890101000001148') 
				and cast(evn.Evn_insDT as date) >= '2022-01-03' and cast(evn.Evn_insDT as date) <= '2022-01-17'
			
			limit 100)
		 UNION ALL 
		-- Справка студенту / учащемуся / ребенку о болезни/карантине и прочих причинах отсутствия
			(SELECT
				100048 as EMDDocumentTypeLocal_id,
				'Документ в свободной форме (Посещение поликлиники)' as EMDDocumentTypeLocal_Name,
			
				evn.Evn_id,
				ls.LpuSection_id,
				ls.LpuBuilding_id,
				e.MedStaffFact_id,
				num.Doc_Num,
				x.EvnXml_IsSigned as IsSigned,
				x.EvnXml_id
			FROM v_EvnXml x
			INNER JOIN v_Evn evn on evn.Evn_id = x.Evn_id
			INNER JOIN v_EvnVizitPL e on e.EvnVizitPL_id = evn.Evn_id
			LEFT JOIN v_PersonState ps on ps.Person_id = evn.Person_id
			LEFT JOIN v_LpuSection ls on ls.LpuSection_id = e.LpuSection_id
			LEFT JOIN v_XmlTemplate tpl on tpl.XmlTemplate_id = x.XmlTemplate_id
			
				LEFT JOIN v_pmUserCache pmuIns on pmuIns.PMUser_id = evn.pmUser_insID
				LEFT JOIN v_pmUserCache pmuUpd on pmuUpd.PMUser_id = evn.pmUser_updID
			
			left join lateral (
				SELECT cast(EvnXml_id as varchar) as Doc_Num FROM v_EvnXml WHERE EvnXml_id = x.EvnXml_id limit 1
			) num on true
			WHERE (1=1)
				and e.EvnClass_id = 11
				and x.XmlType_id = 2
				and tpl.XmlTypeKind_id = 7
				 and evn.Person_id IS NOT NULL  and evn.Lpu_id = '13101856'  and ls.LpuBuilding_id = '890101000000078'  and ls.LpuSection_id = '890101000000828'  and (pmuIns.MedPersonal_id = '890101000001148'
							or pmuUpd.MedPersonal_id = '890101000001148') 
				and cast(evn.Evn_insDT as date) >= '2022-01-03' and cast(evn.Evn_insDT as date) <= '2022-01-17'
			
			limit 100)
		 UNION ALL 
		-- ТАП
			(SELECT
				1 as EMDDocumentTypeLocal_id,
				'Талон амбулаторного пациента' as EMDDocumentTypeLocal_Name,
			
				evn.Evn_id,
				ls.LpuSection_id,
				ls.LpuBuilding_id,
				e.MedStaffFact_did as MedStaffFact_id,
				num.Doc_Num,
				e.EvnPL_IsSigned as IsSigned,
				CAST(null as bigint) as EvnXml_id
			FROM v_EvnPL e
			INNER JOIN v_Evn evn on evn.Evn_id = e.EvnPL_id
			LEFT JOIN v_PersonState ps on ps.Person_id = evn.Person_id
			LEFT JOIN v_MedStaffFact msf on msf.MedStaffFact_id = e.MedStaffFact_did
			LEFT JOIN v_LpuSection ls on ls.LpuSection_id = msf.LpuSection_id
			
				LEFT JOIN v_pmUserCache pmuIns on pmuIns.PMUser_id = evn.pmUser_insID
				LEFT JOIN v_pmUserCache pmuUpd on pmuUpd.PMUser_id = evn.pmUser_updID
			
			left join lateral (
				SELECT cast(EvnPL_NumCard as varchar) as Doc_Num FROM v_EvnPL WHERE EvnPL_id = evn.Evn_id limit 1
			) num on true
			WHERE (1=1)
				and e.EvnClass_id = 3
				 and evn.Person_id IS NOT NULL  and evn.Lpu_id = '13101856'  and ls.LpuBuilding_id = '890101000000078'  and ls.LpuSection_id = '890101000000828'  and (pmuIns.MedPersonal_id = '890101000001148'
							or pmuUpd.MedPersonal_id = '890101000001148') 
				and cast(evn.Evn_insDT as date) >= '2022-01-03' and cast(evn.Evn_insDT as date) <= '2022-01-17'
			
			limit 100)
		 )
				SELECT
					coalesce(cast(d.EvnXml_id as varchar), cast(d.Evn_id as varchar)) as "EMDRegistry_ObjectID",
					case when d.EvnXml_id is not null then 'EvnXml' else cls.EvnClass_SysNick end as "EMDRegistry_ObjectName",
					d.LpuSection_id as "LpuSection_id",
					d.LpuBuilding_id as "LpuBuilding_id",
					d.MedStaffFact_id as "MedStaffFact_id",
					d.Doc_Num as "Doc_Num",
					d.EMDDocumentTypeLocal_id as "EMDDocumentTypeLocal_id",
					d.EMDDocumentTypeLocal_Name as "EMDDocumentTypeLocal_Name",
					msf.Person_Fio as "MedPersonal_Fio",
					evn.Lpu_id as "Lpu_id",
					evn.pmUser_insID as "pmUser_insID",
					evn.pmUser_updID as "pmUser_updID",
					(to_char(evn.Evn_insDT, 'dd.mm.yyyy') || ' ' || to_char(evn.Evn_insDT, 'HH24:MI:SS')) as "Evn_insDT",
					(to_char(evn.Evn_updDT, 'dd.mm.yyyy') || ' ' || to_char(evn.Evn_updDT, 'HH24:MI:SS')) as "Evn_updDT",
					d.IsSigned as "IsSigned",
					evn.Person_id as "Person_id",
					ps.Person_id as "Person_id",
					ps.Person_SurName || coalesce(' ' || ps.Person_FirName,'') || coalesce(' ' || ps.Person_SecName,'') as "Person_FIO",
					to_char(ps.Person_BirthDay, 'dd.mm.yyyy') as "Person_BirthDay",
					evn.Evn_pid as "Evn_pid",
					parent.EvnClass_Name as "parent_EvnClass_Name"
				FROM doc d
					INNER JOIN v_Evn evn on evn.Evn_id = d.Evn_id
					LEFT JOIN v_EvnClass cls on cls.EvnClass_id = evn.EvnClass_id
					LEFT JOIN v_PersonState ps on ps.Person_id = evn.Person_id
					LEFT JOIN v_Evn parent on parent.Evn_id = evn.Evn_pid
					LEFT JOIN v_MedStaffFact msf on msf.MedStaffFact_id = d.MedStaffFact_id
				where (1=1)
					 and d.IsSigned IS NULL 
				limit 100
			
