
            with curDT as (select localtimestamp as curDateTime)

            select
                 
                case
                    when 'stac' in ('stac', 'dstac', 'hstac', 'pstac') and 
            case
                when ES.Lpu_id = '13100875' and ES.LpuSection_id in (select LpuSection_id from v_MedStaffFact where MedPersonal_id = '58355') then 1
                when
                    exists (
                        select WG.WorkGraph_id
                        from v_WorkGraph WG
                            inner join v_MedStaffFact MSF on MSF.MedStaffFact_id = WG.MedStaffFact_id
                        where
                        MSF.MedPersonal_id = '58355'
                                                and WG.MedStaffFact_id = '12328'
                                                and WG.WorkGraph_begDT <= localtimestamp
                                                and WG.WorkGraph_endDT >= localtimestamp
                        limit 1
                    )
                    then 1
                when (FALSE::boolean = TRUE or TRUE::boolean = TRUE or TRUE::boolean = TRUE) and ES.Lpu_id = '13100875' then 1
                else 0
            end = 1
         and not exists (
                select RD.Registry_id
                from r50.v_RegistryData RD
                    inner join r50.v_Registry R on R.Registry_id = RD.Registry_id
                    inner join v_RegistryStatus RS on RS.RegistryStatus_id = R.RegistryStatus_id
                where
                    RD.Evn_id = ES.EvnSection_id
                    and RS.RegistryStatus_SysNick = 'paid'
                limit 1
            ) then 'edit'
                    else 'view'
                end as "accessTypeUsluga"
            ,
                case
                    when 'stac' in ('stac', 'dstac', 'hstac', 'pstac') and 
            case
                when ES.Lpu_id = '13100875' and ES.LpuSection_id in (select LpuSection_id from v_MedStaffFact where MedPersonal_id = '58355') then 1
                when
                    exists (
                        select WG.WorkGraph_id
                        from v_WorkGraph WG
                            inner join v_MedStaffFact MSF on MSF.MedStaffFact_id = WG.MedStaffFact_id
                        where
                        MSF.MedPersonal_id = '58355'
                                                and WG.MedStaffFact_id = '12328'
                                                and WG.WorkGraph_begDT <= localtimestamp
                                                and WG.WorkGraph_endDT >= localtimestamp
                        limit 1
                    )
                    then 1
                when (FALSE::boolean = TRUE or TRUE::boolean = TRUE or TRUE::boolean = TRUE) and ES.Lpu_id = '13100875' then 1
                else 0
            end = 1
         then 'edit'
                    else 'view'
                end as "accessType"
            
                ,ES.EvnSection_id as "EvnSection_id"
                ,ES.EvnSection_pid as "EvnSection_pid"
                ,ES.Lpu_id as "Lpu_id"
                ,ES.Person_id as "Person_id"
                ,dbo.Age2(PS.Person_BirthDay, localtimestamp) as "Person_Age"
                ,ES.PersonEvn_id as "PersonEvn_id"
                ,ES.Server_id as "Server_id"
                ,case when ES.EvnSection_id = EPS.EvnSection_lastid then 1 else 0 end as "EvnSection_IsLast"
                -- Данные движения
                ,ES.CureResult_id as "CureResult_id"
                ,ES.DeseaseBegTimeType_id as "DeseaseBegTimeType_id"
                ,ES.DeseaseType_id as "DeseaseType_id"
                ,ES.Diag_eid as "Diag_eid"
                ,ES.Diag_id as "Diag_id"
                ,ES.Diag_spid as "Diag_spid"
                ,isRP.YesNo_id as "EvnSection_IsRecoveryPotential"
                ,ES.DiagSetPhase_id as "DiagSetPhase_id"
                ,ES.DiagSetPhase_aid as "DiagSetPhase_aid"
                ,ES.EvnSection_Absence as "EvnSection_Absence"
                ,ES.EvnSection_BarthelIdx as "EvnSection_BarthelIdx"
                ,cast(ES.EvnSection_CoeffCTP as float) as "EvnSection_CoeffCTP"
                ,to_char(ES.EvnSection_disDT, 'dd.mm.yyyy') as "EvnSection_disDate"
                ,ES.EvnSection_disDT as "EvnSection_disDT"
                ,to_char(ES.EvnSection_disDT, 'hh24:mi') as "EvnSection_disTime"
                ,ES.EvnSection_GraceScalePoints as "EvnSection_GraceScalePoints"
                ,ESIndex.EvnSection_Index as "EvnSection_Index"
                ,COALESCE(ES.EvnSection_IndexRep, 0) as "EvnSection_IndexRep"
                ,COALESCE(ES.EvnSection_IndexRepInReg, 1) as "EvnSection_IndexRepInReg"
                ,ES.EvnSection_insideNumCard as "EvnSection_insideNumCard"
                ,ES.EvnSection_InsultScale as "EvnSection_InsultScale"
                ,case when ES.EvnSection_IsAdultEscort = 2 then 1 else null end as "EvnSection_IsAdultEscort"
                ,ES.EvnSection_IsCardShock as "EvnSection_IsCardShock"
                ,ES.EvnSection_IsMeal as "EvnSection_IsMeal"
                ,case when ES.EvnSection_IsMedReason = 2 then 1 else null end as "EvnSection_IsMedReason"
                ,COALESCE(ES.EvnSection_IsPaid, 1) as "EvnSection_IsPaid"
                ,case when ES.EvnSection_isPartialPay = 2 then 1 else null end as "EvnSection_isPartialPay"
                ,ES.EvnSection_IsRehab as "EvnSection_IsRehab"
                ,ES.EvnSection_IsST as "EvnSection_IsST"
                ,case when ES.EvnSection_IsTerm = 2 then 1 else null end as "EvnSection_IsTerm"
                ,case when ES.EvnSection_IsZNO = 2 then 1 else null end as "EvnSection_IsZNO"
                ,ES.EvnSection_NIHSSAfterTLT as "EvnSection_NIHSSAfterTLT"
                ,ES.EvnSection_NIHSSLeave as "EvnSection_NIHSSLeave"
                ,ES.EvnSection_PhaseDescr as "EvnSection_PhaseDescr"
                ,to_char(ES.EvnSection_PlanDisDT, 'dd.mm.yyyy') as "EvnSection_PlanDisDT"
                ,to_char(ES.EvnSection_setDT, 'dd.mm.yyyy') as "EvnSection_setDate"
                ,to_char(ES.EvnSection_setDT, 'hh24:mi') as "EvnSection_setTime"
                ,ES.EvnSection_SofaScalePoints as "EvnSection_SofaScalePoints"
                ,ES.EvnSection_StartPainHour as "EvnSection_StartPainHour"
                ,ES.EvnSection_StartPainMin as "EvnSection_StartPainMin"
                ,htmcc.HTMedicalCareClass_id as "HTMedicalCareClass_id"
                ,htmcc.HTMedicalCareClass_Code as "HTMedicalCareClass_Code"
                ,htmcc.HTMedicalCareClass_Name as "HTMedicalCareClass_Name"
                ,htmct.HTMedicalCareType_id as "HTMedicalCareType_id"
                ,htmct.HTMedicalCareType_Code as "HTMedicalCareType_Code"
                ,htmct.HTMedicalCareType_Name as "HTMedicalCareType_Name"
                ,case when thtm.TalonHtm_id is not null and thtm.EvnSection_id != ES.EvnSection_id then 2 else 1 end as "HTMedicalCare_isAdjacentES"
                ,ES.HTMedicalCareClass_id as "ESHTMedicalCareClass_id"
                ,ES.LeaveType_fedid as "LeaveType_fedid"
                ,LT.LeaveType_fedid as "LeaveTypeFed_id"
                ,ES.LeaveType_id as "LeaveType_id"
                ,LT.LeaveType_SysNick as "LeaveType_SysNick"
                ,ES.LpuSection_id as "LpuSection_id"
                ,ES.LpuSectionBedProfile_id as "LpuSectionBedProfile_id"
                ,ES.LpuSectionBedProfileLink_fedid as "LpuSectionBedProfileLink_fedid"
                ,ES.LpuSectionProfile_id as "LpuSectionProfile_id"
                ,ES.LpuSectionTransType_id as "LpuSectionTransType_id"
                ,ES.LpuSectionWard_id as "LpuSectionWard_id"
                ,ES.MedicalCareBudgType_id as "MedicalCareBudgType_id"
                ,ES.MedPersonal_id as "MedPersonal_id"
                ,MP.Person_Fio as "MedPersonal_Fio"
                ,ES.MedStaffFact_id as "MedStaffFact_id"
                ,ES.Mes_id as "Mes_id"
                ,ES.Mes_kid as "Mes_kid"
                ,ES.Mes_sid as "Mes_sid"
                ,ES.Mes_tid as "Mes_tid"
                ,ES.Mes2_id as "Mes2_id"
                ,ES.MesTariff_id as "MesTariff_id"
                ,ES.MesTariff_sid as "MesTariff_sid"
                ,ES.Morbus_id as "Morbus_id"
                ,ES.PainIntensity_id as "PainIntensity_id"
                ,ES.PayType_id as "PayType_id"
                ,ES.PayTypeERSB_id as "PayTypeERSB_id"
                ,ES.PrivilegeType_id as "PrivilegeType_id"
                ,ES.RankinScale_id as "RankinScale_id"
                ,ES.RankinScale_sid as "RankinScale_sid"
                ,ES.RehabScale_id as "RehabScale_id"
                ,ES.RehabScale_vid as "RehabScale_vid"
                ,ES.ResultDeseaseType_fedid as "ResultDeseaseType_fedid"
                ,ES.TariffClass_id as "TariffClass_id"
                ,ES.TumorStage_id as "TumorStage_id"
                ,ES.UslugaComplex_id as "UslugaComplex_id"
                ,ES.EvnClass_id as "EvnClass_id"
                ,ES.EvnSection_IsSigned as "EvnSection_IsSigned"
                ,CR.CureResult_Code as "CureResult_Code"
                ,coalesce(ES.EvnSection_IsPriem,1) as "EvnSection_IsPriem"
                ,ES.ACSType_id as "ACSType_id"
                ,D.Diag_Code as "Diag_Code"
                ,D.Diag_IsPairedOrgans as "Diag_IsPairedOrgans"
                ,DF.DiagFinance_IsOms as "DiagFinance_IsOms"
                ,null as "DiagFinance_IsRankin"
                ,EDPS.EvnDiagPS_id as "EvnDiagPS_id"
                ,DHD.HSNFuncClass_id as "HSNFuncClass_id"
                ,DHD.HSNStage_id as "HSNStage_id"
                ,LS.LpuSection_Code as "LpuSection_Code"
                ,LS.LpuSection_Name as "LpuSection_Name"
                ,LS.LpuSection_IsHTMedicalCare as "LpuSection_IsHTMedicalCare"
                ,LS.LpuSectionProfile_id as "LpuSectionProfile_eid"
                ,LUT.LpuUnitType_SysNick as "LpuUnitType_SysNick"
                ,OLSL.OnkoLesionSide_id as "OnkoLesionSide_id"
                ,PT.PayType_SysNick as "PayType_SysNick"
                ,PEPS.PregnancyEvnPS_Period as "PregnancyEvnPS_Period"
                ,0 as "hasDrugTherapySchemeLinks"
                ,0 as "hasRehabScaleLinks"
                ,0 as "hasSofaLinks"
                -- КСГ
                ,ksgkpg.MesType_id as "MesType_id"
                ,ksgkpg.Mes_id as "Mes_ksgid"
                ,ksgkpg.Mes_Code as "Mes_Code"
                ,ksgkpg.Mes_Name as "Mes_Name"
                ,ksgkpg.MesOld_Num as "MesOld_Num"
                ,to_char(spmt.MesTariff_Value, '9999999999999999999D'||99) as "MesTariff_Value"
                ,null as "Mes_IsRehab"
                -- Данные из КВС
                ,EPS.Diag_eid as "DiagPriem_eid"
                ,EPS.Diag_pid as "DiagPriem_id"
                ,to_char(EPS.Evn_setDT, 'dd.mm.yyyy') as "EvnPS_setDate"
                ,to_char(cast(EPS.Evn_setDT as time), 'HH24:MI') as "EvnPS_setTime"
                ,EPS.EvnPS_IsWaif as "EvnPS_IsWaif"
                ,EPS.PrehospTrauma_id as "PrehospTrauma_id"
                -- ,EPS.EvnPS_HTMTicketNum as "EvnPS_HTMTicketNum"
                -- ,to_char(EPS.EvnPS_HTMBegDate, 'dd.mm.yyyy') as "EvnPS_HTMBegDate"
                -- ,to_char(EPS.EvnPS_HTMHospDate, 'dd.mm.yyyy') as "EvnPS_HTMHospDate"
                ,EDH.EvnDirectionHTM_id as "EvnDirectionHTM_id"
                ,coalesce(EDH.EvnDirectionHTM_id, ledhtm.EvnDirectionHTM_id) as "EvnDirectionHTMAll_id"
                -- Данные пациента
                ,to_char(PS.Person_Birthday, 'dd.mm.yyyy') as "Person_Birthday"
                ,PS.Sex_id as "Sex_id"
                -- Данные по исходу
                ,ELB.EvnLeaveBase_id as "EvnLeaveBase_id"
                ,ELBC.EvnClass_SysNick as "EvnLeaveClass_SysNick"
                ,ELB.PrehospWaifRetired_id as "PrehospWaifRetired_id"
                ,cast(ELB.EvnLeaveBase_UKL as numeric(10, 2)) as "EvnLeave_UKL"
                ,ELB.ResultDesease_id as "ResultDesease_id"
                ,ELB.LeaveCause_id as "LeaveCause_id"
                ,null as "EvnDie_id"
                ,null as "EvnLeave_id"
                ,null as "EvnOtherLpu_id"
                ,null as "EvnOtherSection_id"
                ,null as "EvnOtherSectionBedProfile_id"
                ,null as "EvnOtherStac_id"
                ,null as "EvnLeave_IsAmbul"
                ,null as "Org_oid"
                ,null as "LpuUnitType_oid"
                ,null as "LpuSection_oid"
                ,null as "LpuSectionBedProfile_oid"
                ,null as "LpuSectionBedProfileLink_fedoid"
                ,null as "DeathPlace_id"
                ,null as "EvnDie_IsWait"
                ,null as "EvnDie_IsAnatom"
                ,null as "AnatomWhere_id"
                ,null as "Diag_aid"
                ,null as "Org_aid"
                ,null as "LpuSection_aid"
                ,null as "MedPersonal_aid"
                ,null as "MedPersonal_did"
                ,null as "MedStaffFact_did"
                ,null as "EvnDie_expDate"
                ,null as "EvnDie_expTime"
                ,RoutingLevel.RoutingLevel_id as "RoutingLevel_id"
                ,PDE.PayContract_id as "PayContract_id"
                ,PDE.PolisDMS_id as "PolisDMS_id"
                ,ERP2.EvnReanimatPeriod_id as "EvnReanimatPeriod_id"
                ,thtm.TalonHTM_id as "TalonHTM_id"
                ,thtm.TalonHTM_IsSigned as "TalonHTM_IsSigned"
                ,ENK.EvnNotifyKas_id as "EvnNotifyKas_id",
                case
                    when evndir.EvnDirection_id is not null and dt.DirType_Code = 15 then to_char(evndir.EvnDirection_setDT, 'yyyy-mm-dd')
                    when evndir.EvnDirection_id is not null and dt.DirType_Code in (1, 5) and evnl.Evn_lid is not null then to_char(edlid.EvnDirection_setDT, 'yyyy-mm-dd')
                end as "begDate"
                
                ,COALESCE(to_char(EPS.EvnPS_CmpTltDT, 'DD.MM.YYYY'),'') as "EvnPS_CmpTltDate"
                ,COALESCE(to_char(EPS.EvnPS_CmpTltDT, 'HH24:MI'),'') as "EvnPS_CmpTltTime"
                ,DED.DataEvnDop_ValueInt AS "ThrombolysisBSMP_58"
            from v_EvnSection ES
                left join lateral (
                    select
                        t1.EvnSection_id,
                        ROW_NUMBER() OVER(ORDER BY t1.EvnSection_setDT asc, t1.EvnSection_IsPriem desc nulls last) - 1
                            as EvnSection_Index
                    from v_EvnSection t1
                    where t1.EvnSection_pid = ES.EvnSection_pid
                ) as ESIndex on ESIndex.EvnSection_id = ES.EvnSection_id
                left join EvnPS EPS on EPS.Evn_id = ES.EvnSection_pid
                left join v_EvnLeaveBase ELB on ELB.EvnLeaveBase_pid = ES.EvnSection_id
                left join v_EvnClass ELBC on ELBC.EvnClass_id = ELB.EvnClass_id
                left join v_LpuSection LS on LS.LpuSection_id = ES.LpuSection_id
                left join v_LpuUnit LU on LU.LpuUnit_id = LS.LpuUnit_id
                left join v_LpuUnitType LUT on LUT.LpuUnitType_id = LU.LpuUnitType_id
                left join v_Diag D on D.Diag_id = ES.Diag_id
                left join v_MedPersonal MP on MP.MedPersonal_id = ES.MedPersonal_id
                left join v_YesNo isRP on isRP.YesNo_id = 1
                left join lateral (
                    select
                        DiagFinance_IsOms,
                        DiagFinance_IsRankin
                    from v_DiagFinance
                    where Diag_id = ES.Diag_id
                    limit 1
                ) as DF on true
                left join v_EvnDiagPS EDPS on EDPS.Diag_id = ES.Diag_id
                    and EDPS.DiagSetClass_id = 1
                    and EDPS.EvnDiagPS_pid = ES.EvnSection_id
                left join v_PayType PT on PT.PayType_id = ES.PayType_id
                left join v_LeaveType LT on LT.LeaveType_id = ES.LeaveType_id
                left join v_CureResult CR on CR.CureResult_id = ES.CureResult_id
                left join v_MesTariff spmt on ES.MesTariff_id = spmt.MesTariff_id
                left join v_MesOld as ksgkpg on spmt.Mes_id = ksgkpg.Mes_id
                left join v_PregnancyEvnPS PEPS on PEPS.EvnPS_id = ES.EvnSection_pid
                left join v_DiagHSNDetails as DHD on DHD.Evn_id = ES.EvnSection_id
                left join v_PersonState as PS on PS.Person_id = ES.Person_id
                left join OnkoLesionSideLink as OLSL on OLSL.Evn_id = ES.EvnSection_id
                left join dbo.v_EvnDirection evndir on evndir.EvnDirection_id = eps.EvnDirection_id
                left join dbo.v_DirType dt on dt.DirType_id = evndir.DirType_id
                left join dbo.v_EvnLink evnl on evnl.Evn_id = evndir.EvnDirection_id
                left join dbo.v_EvnDirection_all edlid on edlid.EvnDirection_id = evnl.Evn_lid
                left join lateral (
                    select
                        RM.RoutingLevel_id
                    from
                        v_RoutingMap RM
                        inner join v_RoutingProfile RP on RP.RoutingProfile_id = RM.RoutingProfile_id
                        and RoutingProfile_sysnick = 'oks'
                    where
                        RM.Lpu_id = ES.Lpu_id
                        and RM.RoutingMap_endDate is null
                        and RP.RoutingProfile_endDate is null
                    order by
                        RM.RoutingLevel_id
                    limit 1
                ) RoutingLevel on true
                left join lateral (
                    select
                        ERP.EvnReanimatPeriod_id
                    from
                        dbo.v_EvnReanimatPeriod ERP
                    where EvnReanimatPeriod_pid = ES.EvnSection_id
                        and ERP.EvnReanimatPeriod_disDT is null
                    order by EvnReanimatPeriod_setDT desc
                ) ERP2 on true
                left join v_EvnDirectionHTM EDH on EDH.EvnDirectionHTM_id = EPS.EvnDirection_id
                left join v_EvnDirection edr on edr.EvnDirection_id = EPS.EvnDirection_id and edr.DirType_id in (1, 5)
                left join v_EvnLink elk on elk.Evn_id = edr.EvnDirection_id
                left join v_EvnDirectionHTM ledhtm on ledhtm.EvnDirectionHTM_id = elk.Evn_lid
                left join v_MedStaffFact MSF on MSF.MedStaffFact_id = ES.MedStaffFact_id
                left join dbo.v_DataEvnDop DED ON DED.Evn_id = EPS.Evn_id AND DataEvnDopElement_id = 58
                left join lateral (
                    select
                        PDE.PayContract_id,
                        PDE.PolisDMS_id
                    from v_PayDocEvn PDE
                    where PDE.Evn_id = ES.EvnSection_id
                    limit 1
                ) PDE on true
                left join lateral (
                    select
                        TalonHTM_id,
                        TalonHTM_IsSigned,
                        EvnSection_id
                    from v_TalonHTM
                    where EvnPS_id = ES.EvnSection_pid
                    limit 1
                ) thtm on true
                left join lateral (
                    select
                        HTMedicalCareClass_id,
                        HTMedicalCareType_id
                    from v_HTMTalonStage
                    where TalonHTM_id = thtm.TalonHTM_id
                        and HTMedicalCareClass_id is not null
                    order by HTMTalonStage_Stage desc
                    limit 1
                ) htmts on true
                left join v_HTMedicalCareClass htmcc on htmcc.HTMedicalCareClass_id = htmts.HTMedicalCareClass_id
                left join v_HTMedicalCareType htmct on htmct.HTMedicalCareType_id = htmts.HTMedicalCareType_id
                left join lateral (
                     select
                        ENK.EvnNotifyKas_id
                     from v_EvnNotifyKas ENK
                     left join v_MorbusKas MK on MK.EvnNotifyKas_id = ENK.EvnNotifyKas_id
                     where ES.EvnSection_id = ENK.EvnNotifyKas_pid and ENK.EvnStatus_id = 59
                     and ( (MK.MorbusKas_insDT <= (select curDateTime from curDT) )
                     and (MK.MorbusKas_disDT >= (select curDateTime from curDT) or MK.MorbusKas_disDT IS NULL) )
                     order by EvnNotifyKas_setDt desc
                     limit 1
                 ) ENK on true
                
            where
                ES.EvnSection_id = '500910021682238' and (ES.Lpu_id = '13100875' or 1 = 1)
            limit 1
        
