
            with curDT as (select localtimestamp as curDate)
            SELECT
                MS.MedService_id as "MedService_id"
                ,MS.MedService_Nick as "MedService_Nick"
                ,MS.MedService_Name as "MedService_Name"
                ,MS.MedServiceType_id as "MedServiceType_id"
                ,MS.Org_id as "Org_id"
                ,MS.Lpu_id as "Lpu_id"
                ,MS.LpuBuilding_id as "LpuBuilding_id"
                ,MS.LpuUnitType_id as "LpuUnitType_id"
                ,MS.LpuUnit_id as "LpuUnit_id"
                ,MS.LpuSection_id as "LpuSection_id"
                ,l.Lpu_Nick as "Lpu_Name"
                ,to_char(MS.MedService_begDT,'dd.mm.yyyy') as "MedService_begDT"
                ,to_char(MS.MedService_endDT,'dd.mm.yyyy') as "MedService_endDT"
                ,mst.MedServiceType_SysNick as "MedServiceType_SysNick"
                ,lut.LpuUnitType_SysNick as "LpuUnitType_SysNick"
                ,lut.LpuUnitType_Code as "LpuUnitType_Code"
                ,ls.LpuSectionProfile_id as "LpuSectionProfile_id"
                ,ls.LpuSection_Name as "LpuSection_Name"
                ,ms.MedService_IsExternal as "MedService_IsExternal"
                ,ms.AuthorizationIS_id as "AuthorizationIS_id"
                ,ms.MedService_IsShowDiag as "MedService_IsShowDiag"
                ,ms.MedService_IsDisplayByFilter as "MedService_IsDisplayByFilter"
                ,ms.MedService_IsCytologic as "MedService_IsCytologic"
                ,ms.MedService_IsFileIntegration as "MedService_IsFileIntegration"
                ,ms.MedService_IsStorageZoneBio as "MedService_IsStorageZoneBio"
                ,ms.MedService_IsShowServiceConsultative as "MedService_IsShowServiceConsultative"
                ,case
                    when MS.MedService_endDT is null or MS.MedService_endDT > (select curDate from curDT) then 0 else 1
                end MedService_IsClosed
                ,MS.recordqueue_id as "RecordQueue_id"
                ,MS.AuthorizationIS_id as "AuthorizationIS_id"
                
                ,MS2.MedService_Nick as "MedService_Nick_lab",MS2.MedService_id as "MedService_id_lab"
            FROM
                v_MedService MS
                inner join v_Lpu l on l.Lpu_id = MS.Lpu_id
                left join v_MedServiceType mst on ms.MedServiceType_id = mst.MedServiceType_id
                left join v_LpuUnit lu on ms.LpuUnit_id = lu.LpuUnit_id
                left join v_LpuUnitType lut on COALESCE(ms.LpuUnitType_id,lu.LpuUnitType_id) = lut.LpuUnitType_id
                left join v_LpuSection ls on ms.LpuSection_id = ls.LpuSection_id
                
                left join lateral (
                    select EP.MedService_id, EP.MedService_pzmid
                    from v_EvnPrescrLabDiag EP
                    where EP.UslugaComplex_id = '224910'
                        and EP.MedService_pzmid = MS.MedService_id
                        and (1=1)
                    order by EP.EvnPrescrLabDiag_id DESC
                    limit 1
                ) EPLD on true
                left join v_MedService MS2 ON MS2.MedService_id = EPLD.MedService_id
            
            where MS.Lpu_id = '13102076' and 
                    EXISTS (
                        SELECT 1
                        FROM v_MedServiceLinkUslugaComplexLink MSLUCL
                            LEFT JOIN v_MedServiceLink MSL ON MSL.MedServiceLink_id = MSLUCL.MedServiceLink_id
                        WHERE MSL.MedService_id = MS.MedService_id AND MSLUCL.UslugaComplex_id = '224910'
                        LIMIT 1
                    )
                 and 
                    EPLD.MedService_id is not null
                 and MS.MedService_id = '550101000004158' and MS.MedService_begDT <= (select curDate from curDT) and (MS.MedService_endDT >= (select curDate from curDT) or MS.MedService_endDT is null) and MS.MedService_Nick iLIKE '%ПЗ Приемного %' or MS.MedService_Name iLIKE '%ПЗ Приемного %'
            order by
                MS.MedService_Name
        
