
                with curDT as (select cast(dbo.tzGetDate() as date) as curDate)
                SELECT
                    'order'|| cast(EUP.EvnUslugaPar_id as varchar) as "item_key",
                    EUP.EvnUslugaPar_id as "EvnUslugaPar_id",
                    ED.EvnDirection_id as "EvnDirection_id",
                    ED.Diag_id as "Diag_id",
                    ED.EvnStatus_id as "EvnStatus_id",
                    ED.EvnDirection_Num as "EvnDirection_Num",
                    EQ.EvnQueue_id as "EvnQueue_id",
                    ED.TimetableMedService_id as "TimetableMedService_id",
                    null as "TimetableMedService_begDate",
                    EUP.Person_id as "Person_id",
                    EUP.PersonEvn_id as "PersonEvn_id",
                    EUP.Server_id as "Server_id",
                    EUP.UslugaComplex_id as "UslugaComplex_id",
                    EPCU.EvnPrescrConsUsluga_id as "EvnPrescr_id",
                    coalesce(EPCU.EvnPrescrConsUsluga_IsExec, 1) as "EvnPrescr_IsExec",
                    EPCU.PrescriptionStatusType_id as "PrescriptionStatusType_id",
                    13 as "PrescriptionType_id",
                    EPP.Evn_id as "EvnPrescrParentEvn_id",
                    EPP.Evn_rid as "EvnPrescrParentEvn_rid",
                    EPP.EvnClass_SysNick as "EvnPrescrParentEvnClass_SysNick",
                    LpuFrom.Lpu_Nick as "Lpu_Name",
                    LpuSectionFrom.LpuSection_Name as "LpuSection_Name",
                    case when 2 = coalesce(EUP.EvnUslugaPar_IsCito, ED.EvnDirection_IsCito, 1) then 'true' else 'false' end as "EvnUslugaPar_isCito",
                    case when 2 = COALESCE(EPCU.EvnPrescrConsUsluga_IsExec, 1) OR 15 = ED.EvnStatus_id then 'true' else 'false' end as "Usluga_IsHasReception",
                    to_char (ED.EvnDirection_setDT, 'dd.mm.yyyy') as "EvnDirection_setDT",
                    'б/з' as "TimetableMedService_begTime",
                    PS.Person_Fio as "Person_FIO",
                    UC.UslugaComplex_Name as "UslugaComplex_Name",
                    MP.Person_Fin as "MedPersonal_FIO",
                    D.Diag_FullName as "Diag_Name",
                    'Общее' as "TimetableMedServiceType",
                    null as "UslugaComplexRecord_id",
                    MPF.EvnDirection_From as "EvnDirection_From",
                    ttmsuc.UslugaComplexMedService_id as "UslugaComplexMedService_id" -- услуга для записи
                FROM v_EvnUslugaPar EUP
                    inner join v_EvnDirection_all ED on EUP.EvnDirection_id = ED.EvnDirection_id
                        and ED.EvnDirection_failDT is null
                        and ED.EvnStatus_id not in (12,13)
                    inner join v_UslugaComplex UC on UC.UslugaComplex_id = EUP.UslugaComplex_id
                    left join lateral (select * from v_Person_all PS where ED.PersonEvn_id = PS.PersonEvn_id AND ED.Server_id = PS.Server_id limit 1) PS on true
                    left join v_Lpu LpuFrom on LpuFrom.Lpu_id = ED.Lpu_id
                    left join v_LpuSection LpuSectionFrom on LpuSectionFrom.LpuSection_id = ED.LpuSection_id
                    left join v_Diag D on D.Diag_id = ED.Diag_id
                    left join v_EvnPrescrDirection EPD on EPD.EvnDirection_id = ED.EvnDirection_id
                    left join v_EvnPrescrConsUsluga EPCU on EPCU.EvnPrescrConsUsluga_id = EPD.EvnPrescr_id
                    left join v_EvnUslugaCommon EUC on EUC.EvnPrescr_id = EUP.EvnPrescr_id
                    left join lateral (select * from v_Evn EPP where EPP.Evn_id = EPCU.EvnPrescrConsUsluga_pid limit 1) EPP on true
                    left join lateral (select * from v_EvnPL EPL where EPL.EvnDirection_id = ED.EvnDirection_id limit 1) EPL on true
                    left join v_EvnVizitPL EVPL on EVPL.EvnDirection_id = ED.EvnDirection_id
                    left join lateral (select * from v_MedPersonal MP where coalesce(EPL.MedPersonal_id,EVPL.MedPersonal_id,EUC.MedPersonal_id) = MP.MedPersonal_id limit 1) MP on true
                    /* только из очереди */
                    INNER JOIN LATERAL (
                        SELECT EvnQueue_id as EvnQueue_id
                        from v_EvnQueue
                        where EvnDirection_id = ED.EvnDirection_id
                            --and EvnQueue_id = ED.EvnQueue_id
                            AND EvnQueue_recDT is null
                            AND EvnQueue_failDT is NULL
                        limit 1
                    ) EQ ON TRUE
                    LEFT JOIN LATERAL (
                        select
                            UCMS.UslugaComplexMedService_id as UslugaComplexMedService_id
                        from
                            v_UslugaComplexMedService UCMS
                            inner join v_TimetableMedService_lite TTMS2 on TTMS2.UslugaComplexMedService_id = ucms.UslugaComplexMedService_id
                        where
                            (TTMS2.MedService_id = '890101000000594' OR UCMS.MedService_id = '890101000000594')
                            AND cast(TTMS2.TimetableMedService_begTime as date) > (select curDate from curDT)
                            and UCMS.UslugaComplex_id = EUP.UslugaComplex_id
                        limit 1
                    ) ttmsuc ON TRUE
                    LEFT JOIN LATERAL (
                        select
                            (l.Lpu_Nick || COALESCE(' / ' || LS.LpuSection_Name, '') || COALESCE(' / ' || MP.Person_Fio, '')) as EvnDirection_From
                        from
                            v_MedPersonal MP
                            left join v_LpuSection LS on ED.LpuSection_id = LS.LpuSection_id
                            left join v_Lpu l on l.Lpu_id = ED.Lpu_sid
                        where
                            MP.MedPersonal_id = ED.MedPersonal_id
                        limit 1
                    ) MPF ON TRUE
                WHERE
                    ED.Lpu_did = '13101856' AND ED.MedService_id = '890101000000594' AND 
                (
                    cast(coalesce(EUC.EvnUslugaCommon_setDate, EPL.EvnPL_setDate, EVPL.EvnVizitPL_setDate, ED.EvnDirection_setDT) as date)
                    between cast('2022-02-18' as date) and cast('2022-02-18' as date)
                    or (
                        2 != COALESCE(EPCU.EvnPrescrConsUsluga_IsExec, 1) and
                        15 != ED.EvnStatus_id and
                        ED.TimetableMedService_id is null
                    )
                )
         AND EUP.UslugaComplex_id = '206712'
                LIMIT 10000
            
