select
	L.Lpu_Name as lpu_name,
	L.UAddress_Address as lpu_adr,
	epld.EvnPLDisp_setDT as set_dt,
	concat_ws(' ',
		ps.Person_SurName,
		ps.Person_FirName,
		ps.Person_SecName) as fio,
	ps.Sex_id as sex_id,
	ps.Person_BirthDay as birthday,
	year(epld.EvnPLDisp_setDT) - year(ps.Person_BirthDay) as age,
	greatest(epl_cvd.cvd_date, eps_cvd.cvd_date) as cvd_date,
	adr.KLAreaType_id as area_type,
	rgn.KLRgn_FullName as rgn_name,
	srgn.KLSubRgn_Name as srgn_name,
	coalesce(ct.KLCity_Name, tn.KLTown_Name) as city_name,
	strt.KLStreet_Name as strt_name,
	adr.Address_House as house_num,
	adr.Address_Corpus as corp_num,
	adr.Address_Flat as flat,
	case
		when ps.SocStatus_id in (330,332,333,334,340) then 3
		when ps.SocStatus_id in (329,331,337,338,339) then 2
		else 1
		end as soc_st,
	epld.EvnPLDisp_IsMobile as is_mob,
	dips_13.EvnPLDispDop13_IsTwoStage as two_stage,
	case
		when dips_13.EvnPLDispDop13_IsTwoStage = 2
			then dips_13_2.EvnPLDispDop13_disDate
		else dips_13.EvnPLDispDop13_disDate
		end as dis_date,
	case
		when sub.endless_1st or (sub.endless_2st and dips_13.EvnPLDispDop13_IsTwoStage = 2)
			then 2
		else 1 end as endless,
	diags.diag_list,
	case
		when dips_13.EvnPLDispDop13_IsTwoStage = 2
			then coalesce(dips_13_2.EvnPLDispDop13_isDisp, 1)
		else coalesce(dips_13.EvnPLDispDop13_isDisp, 1)
		end as has_disp,
	hk.HealthKind_Code as hk_code
from v_EvnPLDisp as epld
	left join v_EvnPLDispDop13 as dips_13 on dips_13.EvnPLDispDop13_id = epld.EvnPLDisp_id
	left join v_EvnPLDispDop13 as dips_13_2 on dips_13_2.EvnPLDispDop13_fid = epld.EvnPLDisp_id
	inner join v_Lpu_all as L on L.Lpu_id = epld.Lpu_id
	inner join v_PersonState as ps on ps.Person_id = epld.Person_id
	left join v_Address as adr on adr.Address_id = coalesce(ps.PAddress_id, ps.UAddress_id)
	left join v_KLRgn as rgn on rgn.KLRgn_id = adr.KLRgn_id
	left join v_KLSubRgn as srgn on srgn.KLSubRgn_id = adr.KLSubRgn_id
	left join v_KLCity as ct on ct.KLCity_id = adr.KLCity_id
	left join v_KLTown as tn on tn.KLTown_id = adr.KLTown_id
	left join v_KLStreet as strt on strt.KLStreet_id = adr.KLStreet_id
	left join dbo.HealthKind as hk on hk.HealthKind_id = coalesce(dips_13_2.HealthKind_id, dips_13.HealthKind_id)
	left join lateral(
		select max(epl.EvnPL_setDate) as cvd_date
		from v_EvnPL as epl
			inner join Diag as d on d.Diag_id = epl.Diag_id
		where epl.Person_id = epld.Person_id
			and epl.EvnPL_setDate < epld.EvnPLDisp_setDT
			and d.Diag_Code in ('U07.1', 'U07.2')
	)epl_cvd on true
	left join lateral(
		select max(eps.EvnPS_setDate) as cvd_date
		from v_EvnPS as eps
			inner join Diag as d on d.Diag_id = eps.Diag_id
		where eps.Person_id = epld.Person_id
			and eps.EvnPS_setDate < epld.EvnPLDisp_setDT
			and d.Diag_Code in ('U07.1', 'U07.2')
	)eps_cvd on true
	left join lateral(
		select 
			exists(
				select 1
				from v_DopDispInfoConsent as ddic
				where ddic.EvnPLDisp_id = epld.EvnPLDisp_id
					and ddic.EvnUsluga_id is null
					and not exists(
						select 1
						from v_EvnUslugaDispDop as eudd
							left join v_SurveyTypeLink as stl on stl.UslugaComplex_id = eudd.UslugaComplex_id
						where eudd.DopDispInfoConsent_id = ddic.DopDispInfoConsent_id
							or(eudd.EvnUslugaDispDop_rid = dips_13.EvnPLDispDop13_id
								and stl.SurveyTypeLink_id = ddic.SurveyTypeLink_id
								and dips_13.DispClass_id in (31,32)
								and coalesce(ddic.DopDispInfoConsent_IsEarlier,1) != 2)
						)
				) as endless_1st,
			exists(
				select 1
				from v_DopDispInfoConsent as ddic
				where ddic.EvnPLDisp_id = dips_13_2.EvnPLDispDop13_id
					and ddic.EvnUsluga_id is null
					and not exists(
						select 1
						from v_EvnUslugaDispDop as eudd
							left join v_SurveyTypeLink as stl on stl.UslugaComplex_id = eudd.UslugaComplex_id
						where eudd.DopDispInfoConsent_id = ddic.DopDispInfoConsent_id
							or(eudd.EvnUslugaDispDop_rid = dips_13_2.EvnPLDispDop13_id
								and stl.SurveyTypeLink_id = ddic.SurveyTypeLink_id
								and dips_13_2.DispClass_id in (31,32)
								and coalesce(ddic.DopDispInfoConsent_IsEarlier,1) != 2)
						)
				) as endless_2st
	)sub on true
	left join lateral(
		select string_agg(distinct d.Diag_Code, ', ') as diag_list
		from v_EvnDiagDopDisp as eddd
			inner join Diag as d on d.Diag_id = eddd.Diag_id
		where eddd.EvnDiagDopDisp_rid in(dips_13.EvnPLDispDop13_id, dips_13_2.EvnPLDispDop13_id)
	)diags on true
where epld.EvnPLDisp_id = cast($1 as bigint)
