
            SELECT distinct
                ED.EvnClass_id as "EvnClass_id",
                d.Diag_FullName as "Diag_FullName",
                ED.EvnDirection_id as "EvnDirection_id",
                ED.EvnDirection_IsConcilium as "EvnDirection_IsConcilium",
                ED.EvnDirection_pid as "EvnDirection_pid",
                ED.EvnDirection_rid as "EvnDirection_rid",
                r.EvnClass_SysNick as "RootEvnClass_SysNick",
                EUT.EvnUslugaTelemed_id as "EvnUslugaTelemed_id",
                CF.ConsultingForm_Name as "ConsultingForm_Name",
                ED.Diag_id as "Diag_id",
                ED.Person_id as "Person_id",
                ED.PersonEvn_id as "PersonEvn_id",
                ED.Server_id as "Server_id",
                ED.EvnDirection_Num as "EvnDirection_Num",
                ED.PayType_id as "PayType_id",
                to_char(ED.EvnDirection_setDT, 'dd.mm.yyyy') as "EvnDirection_setDT",
                to_char(ED.EvnDirection_insDT, 'dd.mm.yyyy') as "EvnDirection_insDate",
                to_char(ED.EvnDirection_insDT, 'hh24:MI') as "EvnDirection_insTime",
                l.Lpu_Nick as "Lpu_Nick",
                l.Lpu_id as "Lpu_id",
                pp.RiskType_id as "RiskType_id",
                mp.Person_Fin as "Person_Fin",
                 case
                    when ED.RecMethodType_id = 20 and ED.MedStaffFact_id is null and TED.TelemedEvnDirection_MedPersonalFIO is not null
                    then TED.TelemedEvnDirection_MedPersonalFIO
                    else mp.Person_Fin
                end as "Person_Fin",
                mp2.Person_Fin as "MedPersonalFIO_whom_sent",
                (PS.Person_SurName || ' ' || Coalesce(PS.Person_FirName, '') || ' ' || Coalesce(PS.Person_SecName,'')) as "Person_FIO",
                case when 2 = Coalesce(ED.EvnDirection_IsCito,1) then 'true' else 'false' end as "EvnDirection_IsCito",
                RCC.RemoteConsultCause_Name as "RemoteConsultCause_Name",
                Coalesce(LSPT.LpuSectionProfile_name,LSP.LpuSectionProfile_Name) as "LpuSectionProfile_Name",
                UTRT.UslugaTelemedResultType_Name as "UslugaTelemedResultType_Name",
                consForm.ConsultationForm_Code as "ConsultationForm_Code",
                consForm.ConsultationForm_Name as "ConsultationForm_Name",
                Coalesce(isDel.isDel, 0) as "isDel"
                
                    , ED.EvnDirection_failDT as "evndirection_faildt_fororder",
                    EUT.EvnUslugaTelemed_id as "evnuslugatelemed_id_fororder",
                    Coalesce(ED.EvnDirection_isCito,1) as "evndirection_iscito_fororder",
                    ED.EvnDirection_setDT as "evndirection_setdt_fororder"
                ,
                case
                    when consForm.ConsultationForm_Code is null
                        then ''
                    when consForm.ConsultationForm_Code = '1'
                        then to_char(DATEADD('day',14,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '2'
                        then to_char(DATEADD('day',1,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '3'
                        then to_char(DATEADD('hour',2,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(dateadd('hour',2,ED.EvnDirection_insDT), 'hh24:MI')
                end as "deadLine",
                case
                    when consForm.ConsultationForm_Code is null
                        then 99999999
                    when consForm.ConsultationForm_Code = '1'
                        then abs(DATE_PART('day', DATEADD('day',14,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',14,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '2'
                        then abs(DATE_PART('day', DATEADD('day',1,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',1,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '3'
                        then abs(DATE_PART('day', DATEADD('day',2,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',2,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                end as "deadDiff",
                DT.DirType_Code as "DirType_Code",
                ED.EvnQueue_id as "EvnQueue_id",
                ED.EvnQueue_id as "EvnQueue_id",
                ED.TimeTableStac_id as "TimeTableStac_id",
                ED.TimeTableGraf_id as "TimeTableGraf_id",
                ED.TimeTableMedService_id as "TimeTableMedService_id",
                ESC.EvnStatusCause_Name as "EvnStatusCause_Name",
                case
                    when ED.EvnDirection_failDT is not null
                        then DFT.DirFailType_Name
                    when EC.EvnConcilium_id is not null and ES2.EvnStatus_SysNick in ('Confirmed', 'Posted')
                        then NCR.ConciliumResult_Name
                    else
                        UTRT.UslugaTelemedResultType_Name
                end as "evndirection_result",
                ex.EvnXml_id as "EvnXml_id",
                ex.EvnXml_IsSigned as "EvnXml_IsSigned",
                to_char(ex.EvnXml_signDT, 'dd.mm.yyyy') as "EvnXml_signDT",
                coalesce(pu2.pmUser_Name, pu.pmUser_Name) as "pmUser_signName",
                ED.RequestConsult_id as "RequestConsult_id",
                EC.EvnConcilium_id as "EvnConcilium_id",
                EC.ConciliumType_id as "ConciliumType_id",
                ED.MedService_id as "MedService_id",
                LpuD.Org_id as "Org_tid",
                ED.RemoteConsultCause_id as "RemoteConsultCause_id",
                ED.RecMethodType_id as "RecMethodType_id",
                ED.EvnDirection_isAuto as "EvnDirection_isAuto",
                coalesce(EC.EvnConcilium_IsSigned, ex.EvnXml_IsSigned) as "Evn_IsSigned",
                to_char(coalesce(EC.EvnConcilium_signDT, ex.EvnXml_signDT), 'dd.mm.yyyy') as "Evn_signDT",
                ES2.EvnStatus_SysNick as "EvnConciliumStatus_SysNick",
                ES.EvnStatus_SysNick as "EvnDirectionStatus_SysNick",
                case when csmsf.ConciliumStaff_id is not null then 2 else 1 end as "isConciliumMember",
                CR.ConciliumResults_id as "ConciliumResults_id",
                ED.pmUser_insID as "pmUser_insID",
                ED.UslugaComplex_did as "UslugaComplex_did",
                
                case
                    -- Если услуга отмененная
                    when ED.EvnDirection_failDT is not null
                        then 5
                    -- Если услуга оказана или статус направления Обслужено (15)
                    when EUT.EvnUslugaTelemed_id is not null or ED.EvnStatus_id = 15
                        then 4
                    when EUT.EvnUslugaTelemed_id is null and (ED.EvnStatus_id is null or ED.EvnStatus_id <> 15)
 						then case
 								-- Если направление на службу
                                when ED.MedPersonal_did is null
                                    then 2
                                -- Если направление на врача
                                when ED.MedPersonal_did is not null
                                    then 3
              				end
                end as "evndirection_group"
            
                
                ,case
                	when ED.TimeTableGraf_id is not null then
                		to_char(ttgl.TimeTableGraf_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttgl.TimeTableGraf_begTime, 'hh24:MI')
                	else
                		to_char(ttmsl.TimeTableMedService_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttmsl.TimeTableMedService_begTime, 'hh24:MI')
                end as "TimeTable_begTime"
            , ED.ConsultingForm_id as "ConsultingForm_id"
            FROM
                v_EvnDirection_all ED
                INNER JOIN v_Evn r on r.Evn_id = ED.EvnDirection_rid
                INNER JOIN v_PersonState PS on PS.Person_id = ED.Person_id
                left join v_TelemedEvnDirection TED on TED.TelemedEvnDirection_id = ED.EvnDirection_id
                LEFT JOIN v_EvnConcilium EC on EC.EvnConcilium_pid = ED.EvnDirection_id
                left join v_PersonPregnancy pp  on pp.Person_id = ed.Person_id
                LEFT JOIN ConsultingForm CF  on CF.ConsultingForm_id = ED.ConsultingForm_id
                LEFT JOIN LATERAL(
                    select
                    1 as isDel
                    from v_pmUserCache pm
                    where pm.pmuser_id = ed.pmuser_insid
                    --AND lpu_id IS NOT NULL – если очень надо проверять по лпу можно добавить такое условие
                    limit 1
                ) isDel on true
                 LEFT JOIN LATERAL (
                    select
                        EUT.EvnUslugaTelemed_id,
                        EUT.EvnUslugaTelemed_setDT,
                        EUT.UslugaTelemedResultType_id,
                        EUT.UslugaComplex_id,
                        EUT.LpuSectionProfile_id
                    from v_EvnUslugaTelemed EUT
                    where EUT.EvnDirection_id = ED.EvnDirection_id
                    limit 1
                ) EUT on true 
                left join v_EvnXml ex on ex.Evn_id = EUT.EvnUslugaTelemed_id
                left join v_pmUser pu on pu.pmUser_id = ex.pmUser_signID
                left join v_pmUser pu2 on pu2.pmUser_id = EC.pmUser_signID
                left join v_UslugaTelemedResultType UTRT  on UTRT.UslugaTelemedResultType_id = EUT.UslugaTelemedResultType_id
                LEFT JOIN v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
                left join lateral (
                    select LSPT.LpuSectionProfile_id,LSPT.LpuSectionProfile_name
                    from v_LpuSectionProfile LSPT
                    where LSPT.LpuSectionProfile_id = EUT.LpuSectionProfile_id limit 1
                ) LSPT on true
                LEFT JOIN v_RemoteConsultCause RCC  on RCC.RemoteConsultCause_id = ED.RemoteConsultCause_id
                LEFT JOIN v_Lpu l on l.Lpu_id = ED.Lpu_sid
                LEFT JOIN v_Lpu LpuD on LpuD.Lpu_id = ED.Lpu_did
                LEFT JOIN v_MedPersonal mp  on mp.MedPersonal_id = ED.MedPersonal_id and mp.Lpu_id = ED.Lpu_sid
                LEFT JOIN v_MedPersonal mp2 on mp2.MedPersonal_id = ED.MedPersonal_did and mp2.Lpu_id = ED.Lpu_did
                LEFT JOIN lateral
                (
                    select d.Diag_fullName from v_Diag d where ED.Diag_id = d.Diag_id limit 1
                ) d on true
                LEFT JOIN v_ConsultationForm consForm on consForm.ConsultationForm_id = ED.ConsultationForm_id
                LEFT JOIN v_DirType DT  on DT.DirType_id = ED.DirType_id
                LEFT JOIN v_EvnStatus ES  on ES.EvnStatus_id = ED.EvnStatus_id
                LEFT JOIN v_EvnStatus ES2 on ES2.EvnStatus_id = EC.EvnStatus_id
                LEFT JOIN v_ConciliumResult CR on CR.EvnConcilium_id = EC.EvnConcilium_id
                left join nsi.v_ConciliumResult NCR on NCR.ConciliumResult_id = CR.ConciliumResults_id
                left join v_DirFailType DFT on DFT.DirFailType_id = ED.DirFailType_id
                LEFT JOIN LATERAL (
                    select
                        esh.EvnStatusCause_id,
                        esh.pmUser_updID
                    from
                        v_EvnStatusHistory esh
                    where
                        esh.Evn_id = ed.EvnDirection_id
                        and esh.EvnStatus_id = ed.EvnStatus_id
                        and esh.EvnStatusCause_id is not null
                    order by
                        esh.EvnStatusHistory_id desc
                    limit 1
                ) esh on true
                left join v_EvnStatusCause esc  on esc.EvnStatusCause_id = esh.EvnStatusCause_id
                LEFT JOIN LATERAL (
                    select
                        cs.ConciliumStaff_id
                    from v_ConciliumStaff cs
                    where cs.MedPersonal_id = '81428' and cs.EvnConcilium_id = ec.EvnConcilium_id
                    limit 1
                ) as csmsf on true
                 left join v_TimeTableGraf_lite ttgl on ttgl.TimeTableGraf_id = ED.TimeTableGraf_id left join v_TimeTableMedService_lite ttmsl on ttmsl.TimeTableMedService_id = ED.TimeTableMedService_id
            WHERE
                1=1 AND 
                (
                    (EUT.EvnUslugaTelemed_setDT is null AND ED.EvnDirection_failDT is null AND coalesce(ED.EvnStatus_id, 1) != 15)
                    OR
                    (EUT.EvnUslugaTelemed_setDT is not null AND cast(EUT.EvnUslugaTelemed_setDT as date) between '2023-07-22' and '2023-07-22')
                    OR
                    (ED.EvnDirection_failDT is not null AND cast(ED.EvnDirection_failDT as date) between '2023-07-22' and '2023-07-22')
                    OR
                    ED.EvnDirection_setDT between '2023-07-22' and '2023-07-22'
                )
            
                 AND coalesce(ED.EvnDirection_IsConcilium, 1) = 1 AND
                ED.MedService_id = '11380'
            
        
                union
            
            SELECT distinct
                ED.EvnClass_id as "EvnClass_id",
                d.Diag_FullName as "Diag_FullName",
                ED.EvnDirection_id as "EvnDirection_id",
                ED.EvnDirection_IsConcilium as "EvnDirection_IsConcilium",
                ED.EvnDirection_pid as "EvnDirection_pid",
                ED.EvnDirection_rid as "EvnDirection_rid",
                r.EvnClass_SysNick as "RootEvnClass_SysNick",
                EUT.EvnUslugaTelemed_id as "EvnUslugaTelemed_id",
                CF.ConsultingForm_Name as "ConsultingForm_Name",
                ED.Diag_id as "Diag_id",
                ED.Person_id as "Person_id",
                ED.PersonEvn_id as "PersonEvn_id",
                ED.Server_id as "Server_id",
                ED.EvnDirection_Num as "EvnDirection_Num",
                ED.PayType_id as "PayType_id",
                to_char(ED.EvnDirection_setDT, 'dd.mm.yyyy') as "EvnDirection_setDT",
                to_char(ED.EvnDirection_insDT, 'dd.mm.yyyy') as "EvnDirection_insDate",
                to_char(ED.EvnDirection_insDT, 'hh24:MI') as "EvnDirection_insTime",
                l.Lpu_Nick as "Lpu_Nick",
                l.Lpu_id as "Lpu_id",
                pp.RiskType_id as "RiskType_id",
                mp.Person_Fin as "Person_Fin",
                 case
                    when ED.RecMethodType_id = 20 and ED.MedStaffFact_id is null and TED.TelemedEvnDirection_MedPersonalFIO is not null
                    then TED.TelemedEvnDirection_MedPersonalFIO
                    else mp.Person_Fin
                end as "Person_Fin",
                mp2.Person_Fin as "MedPersonalFIO_whom_sent",
                (PS.Person_SurName || ' ' || Coalesce(PS.Person_FirName, '') || ' ' || Coalesce(PS.Person_SecName,'')) as "Person_FIO",
                case when 2 = Coalesce(ED.EvnDirection_IsCito,1) then 'true' else 'false' end as "EvnDirection_IsCito",
                RCC.RemoteConsultCause_Name as "RemoteConsultCause_Name",
                Coalesce(LSPT.LpuSectionProfile_name,LSP.LpuSectionProfile_Name) as "LpuSectionProfile_Name",
                UTRT.UslugaTelemedResultType_Name as "UslugaTelemedResultType_Name",
                consForm.ConsultationForm_Code as "ConsultationForm_Code",
                consForm.ConsultationForm_Name as "ConsultationForm_Name",
                Coalesce(isDel.isDel, 0) as "isDel"
                
                    , ED.EvnDirection_failDT as "evndirection_faildt_fororder",
                    EUT.EvnUslugaTelemed_id as "evnuslugatelemed_id_fororder",
                    Coalesce(ED.EvnDirection_isCito,1) as "evndirection_iscito_fororder",
                    ED.EvnDirection_setDT as "evndirection_setdt_fororder"
                ,
                case
                    when consForm.ConsultationForm_Code is null
                        then ''
                    when consForm.ConsultationForm_Code = '1'
                        then to_char(DATEADD('day',14,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '2'
                        then to_char(DATEADD('day',1,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '3'
                        then to_char(DATEADD('hour',2,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(dateadd('hour',2,ED.EvnDirection_insDT), 'hh24:MI')
                end as "deadLine",
                case
                    when consForm.ConsultationForm_Code is null
                        then 99999999
                    when consForm.ConsultationForm_Code = '1'
                        then abs(DATE_PART('day', DATEADD('day',14,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',14,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '2'
                        then abs(DATE_PART('day', DATEADD('day',1,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',1,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '3'
                        then abs(DATE_PART('day', DATEADD('day',2,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',2,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                end as "deadDiff",
                DT.DirType_Code as "DirType_Code",
                ED.EvnQueue_id as "EvnQueue_id",
                ED.EvnQueue_id as "EvnQueue_id",
                ED.TimeTableStac_id as "TimeTableStac_id",
                ED.TimeTableGraf_id as "TimeTableGraf_id",
                ED.TimeTableMedService_id as "TimeTableMedService_id",
                ESC.EvnStatusCause_Name as "EvnStatusCause_Name",
                case
                    when ED.EvnDirection_failDT is not null
                        then DFT.DirFailType_Name
                    when EC.EvnConcilium_id is not null and ES2.EvnStatus_SysNick in ('Confirmed', 'Posted')
                        then NCR.ConciliumResult_Name
                    else
                        UTRT.UslugaTelemedResultType_Name
                end as "evndirection_result",
                ex.EvnXml_id as "EvnXml_id",
                ex.EvnXml_IsSigned as "EvnXml_IsSigned",
                to_char(ex.EvnXml_signDT, 'dd.mm.yyyy') as "EvnXml_signDT",
                coalesce(pu2.pmUser_Name, pu.pmUser_Name) as "pmUser_signName",
                ED.RequestConsult_id as "RequestConsult_id",
                EC.EvnConcilium_id as "EvnConcilium_id",
                EC.ConciliumType_id as "ConciliumType_id",
                ED.MedService_id as "MedService_id",
                LpuD.Org_id as "Org_tid",
                ED.RemoteConsultCause_id as "RemoteConsultCause_id",
                ED.RecMethodType_id as "RecMethodType_id",
                ED.EvnDirection_isAuto as "EvnDirection_isAuto",
                coalesce(EC.EvnConcilium_IsSigned, ex.EvnXml_IsSigned) as "Evn_IsSigned",
                to_char(coalesce(EC.EvnConcilium_signDT, ex.EvnXml_signDT), 'dd.mm.yyyy') as "Evn_signDT",
                ES2.EvnStatus_SysNick as "EvnConciliumStatus_SysNick",
                ES.EvnStatus_SysNick as "EvnDirectionStatus_SysNick",
                case when csmsf.ConciliumStaff_id is not null then 2 else 1 end as "isConciliumMember",
                CR.ConciliumResults_id as "ConciliumResults_id",
                ED.pmUser_insID as "pmUser_insID",
                ED.UslugaComplex_did as "UslugaComplex_did",
                
                case
                    -- Если направление на консилиум отменено
                    when ED.EvnDirection_failDT is not null
                        then 5
                    -- Если консилиум проведен и (заполнен результат или есть ТМК услуга)
                    when ES2.EvnStatus_SysNick in ('Confirmed', 'Posted') and (CR.ConciliumResults_id is not null or
                        EUT.EvnUslugaTelemed_id is not null)
                        then 4
                    else 1
                end as "evndirection_group"
            
                
                ,case
                	when ED.TimeTableGraf_id is not null then
                		to_char(ttgl.TimeTableGraf_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttgl.TimeTableGraf_begTime, 'hh24:MI')
                	else
                		to_char(ttmsl.TimeTableMedService_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttmsl.TimeTableMedService_begTime, 'hh24:MI')
                end as "TimeTable_begTime"
            , ED.ConsultingForm_id as "ConsultingForm_id"
            FROM
                v_EvnDirection_all ED
                INNER JOIN v_Evn r on r.Evn_id = ED.EvnDirection_rid
                INNER JOIN v_PersonState PS on PS.Person_id = ED.Person_id
                left join v_TelemedEvnDirection TED on TED.TelemedEvnDirection_id = ED.EvnDirection_id
                INNER JOIN v_EvnConcilium EC on EC.EvnConcilium_pid = ED.EvnDirection_id
                left join v_PersonPregnancy pp  on pp.Person_id = ed.Person_id
                LEFT JOIN ConsultingForm CF  on CF.ConsultingForm_id = ED.ConsultingForm_id
                LEFT JOIN LATERAL(
                    select
                    1 as isDel
                    from v_pmUserCache pm
                    where pm.pmuser_id = ed.pmuser_insid
                    --AND lpu_id IS NOT NULL – если очень надо проверять по лпу можно добавить такое условие
                    limit 1
                ) isDel on true
                 LEFT JOIN v_EvnUslugaTelemed EUT on EUT.EvnDirection_id = ED.EvnDirection_id 
                left join v_EvnXml ex on ex.Evn_id = EUT.EvnUslugaTelemed_id
                left join v_pmUser pu on pu.pmUser_id = ex.pmUser_signID
                left join v_pmUser pu2 on pu2.pmUser_id = EC.pmUser_signID
                left join v_UslugaTelemedResultType UTRT  on UTRT.UslugaTelemedResultType_id = EUT.UslugaTelemedResultType_id
                LEFT JOIN v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
                left join lateral (
                    select LSPT.LpuSectionProfile_id,LSPT.LpuSectionProfile_name
                    from v_LpuSectionProfile LSPT
                    where LSPT.LpuSectionProfile_id = EUT.LpuSectionProfile_id limit 1
                ) LSPT on true
                LEFT JOIN v_RemoteConsultCause RCC  on RCC.RemoteConsultCause_id = ED.RemoteConsultCause_id
                LEFT JOIN v_Lpu l on l.Lpu_id = ED.Lpu_sid
                LEFT JOIN v_Lpu LpuD on LpuD.Lpu_id = ED.Lpu_did
                LEFT JOIN v_MedPersonal mp  on mp.MedPersonal_id = ED.MedPersonal_id and mp.Lpu_id = ED.Lpu_sid
                LEFT JOIN v_MedPersonal mp2 on mp2.MedPersonal_id = ED.MedPersonal_did and mp2.Lpu_id = ED.Lpu_did
                LEFT JOIN lateral
                (
                    select d.Diag_fullName from v_Diag d where ED.Diag_id = d.Diag_id limit 1
                ) d on true
                LEFT JOIN v_ConsultationForm consForm on consForm.ConsultationForm_id = ED.ConsultationForm_id
                LEFT JOIN v_DirType DT  on DT.DirType_id = ED.DirType_id
                LEFT JOIN v_EvnStatus ES  on ES.EvnStatus_id = ED.EvnStatus_id
                LEFT JOIN v_EvnStatus ES2 on ES2.EvnStatus_id = EC.EvnStatus_id
                LEFT JOIN v_ConciliumResult CR on CR.EvnConcilium_id = EC.EvnConcilium_id
                left join nsi.v_ConciliumResult NCR on NCR.ConciliumResult_id = CR.ConciliumResults_id
                left join v_DirFailType DFT on DFT.DirFailType_id = ED.DirFailType_id
                LEFT JOIN LATERAL (
                    select
                        esh.EvnStatusCause_id,
                        esh.pmUser_updID
                    from
                        v_EvnStatusHistory esh
                    where
                        esh.Evn_id = ed.EvnDirection_id
                        and esh.EvnStatus_id = ed.EvnStatus_id
                        and esh.EvnStatusCause_id is not null
                    order by
                        esh.EvnStatusHistory_id desc
                    limit 1
                ) esh on true
                left join v_EvnStatusCause esc  on esc.EvnStatusCause_id = esh.EvnStatusCause_id
                LEFT JOIN LATERAL (
                    select
                        cs.ConciliumStaff_id
                    from v_ConciliumStaff cs
                    where cs.MedPersonal_id = '81428' and cs.EvnConcilium_id = ec.EvnConcilium_id
                    limit 1
                ) as csmsf on true
                 left join v_TimeTableGraf_lite ttgl on ttgl.TimeTableGraf_id = ED.TimeTableGraf_id left join v_TimeTableMedService_lite ttmsl on ttmsl.TimeTableMedService_id = ED.TimeTableMedService_id
            WHERE
                1=1 AND 
                (
                    (EUT.EvnUslugaTelemed_setDT is null AND ED.EvnDirection_failDT is null AND coalesce(ED.EvnStatus_id, 1) != 15)
                    OR
                    (EUT.EvnUslugaTelemed_setDT is not null AND cast(EUT.EvnUslugaTelemed_setDT as date) between '2023-07-22' and '2023-07-22')
                    OR
                    (ED.EvnDirection_failDT is not null AND cast(ED.EvnDirection_failDT as date) between '2023-07-22' and '2023-07-22')
                    OR
                    ED.EvnDirection_setDT between '2023-07-22' and '2023-07-22'
                )
            
                 AND ED.EvnDirection_IsConcilium = 2 AND
                ED.MedService_id = '11380'
            
        
                union
            
            SELECT distinct
                ED.EvnClass_id as "EvnClass_id",
                d.Diag_FullName as "Diag_FullName",
                ED.EvnDirection_id as "EvnDirection_id",
                ED.EvnDirection_IsConcilium as "EvnDirection_IsConcilium",
                ED.EvnDirection_pid as "EvnDirection_pid",
                ED.EvnDirection_rid as "EvnDirection_rid",
                r.EvnClass_SysNick as "RootEvnClass_SysNick",
                EUT.EvnUslugaTelemed_id as "EvnUslugaTelemed_id",
                CF.ConsultingForm_Name as "ConsultingForm_Name",
                ED.Diag_id as "Diag_id",
                ED.Person_id as "Person_id",
                ED.PersonEvn_id as "PersonEvn_id",
                ED.Server_id as "Server_id",
                ED.EvnDirection_Num as "EvnDirection_Num",
                ED.PayType_id as "PayType_id",
                to_char(ED.EvnDirection_setDT, 'dd.mm.yyyy') as "EvnDirection_setDT",
                to_char(ED.EvnDirection_insDT, 'dd.mm.yyyy') as "EvnDirection_insDate",
                to_char(ED.EvnDirection_insDT, 'hh24:MI') as "EvnDirection_insTime",
                l.Lpu_Nick as "Lpu_Nick",
                l.Lpu_id as "Lpu_id",
                pp.RiskType_id as "RiskType_id",
                mp.Person_Fin as "Person_Fin",
                 case
                    when ED.RecMethodType_id = 20 and ED.MedStaffFact_id is null and TED.TelemedEvnDirection_MedPersonalFIO is not null
                    then TED.TelemedEvnDirection_MedPersonalFIO
                    else mp.Person_Fin
                end as "Person_Fin",
                mp2.Person_Fin as "MedPersonalFIO_whom_sent",
                (PS.Person_SurName || ' ' || Coalesce(PS.Person_FirName, '') || ' ' || Coalesce(PS.Person_SecName,'')) as "Person_FIO",
                case when 2 = Coalesce(ED.EvnDirection_IsCito,1) then 'true' else 'false' end as "EvnDirection_IsCito",
                RCC.RemoteConsultCause_Name as "RemoteConsultCause_Name",
                Coalesce(LSPT.LpuSectionProfile_name,LSP.LpuSectionProfile_Name) as "LpuSectionProfile_Name",
                UTRT.UslugaTelemedResultType_Name as "UslugaTelemedResultType_Name",
                consForm.ConsultationForm_Code as "ConsultationForm_Code",
                consForm.ConsultationForm_Name as "ConsultationForm_Name",
                Coalesce(isDel.isDel, 0) as "isDel"
                
                    , ED.EvnDirection_failDT as "evndirection_faildt_fororder",
                    EUT.EvnUslugaTelemed_id as "evnuslugatelemed_id_fororder",
                    Coalesce(ED.EvnDirection_isCito,1) as "evndirection_iscito_fororder",
                    ED.EvnDirection_setDT as "evndirection_setdt_fororder"
                ,
                case
                    when consForm.ConsultationForm_Code is null
                        then ''
                    when consForm.ConsultationForm_Code = '1'
                        then to_char(DATEADD('day',14,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '2'
                        then to_char(DATEADD('day',1,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '3'
                        then to_char(DATEADD('hour',2,ED.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(dateadd('hour',2,ED.EvnDirection_insDT), 'hh24:MI')
                end as "deadLine",
                case
                    when consForm.ConsultationForm_Code is null
                        then 99999999
                    when consForm.ConsultationForm_Code = '1'
                        then abs(DATE_PART('day', DATEADD('day',14,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',14,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '2'
                        then abs(DATE_PART('day', DATEADD('day',1,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',1,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '3'
                        then abs(DATE_PART('day', DATEADD('day',2,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',2,ED.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                end as "deadDiff",
                DT.DirType_Code as "DirType_Code",
                ED.EvnQueue_id as "EvnQueue_id",
                ED.EvnQueue_id as "EvnQueue_id",
                ED.TimeTableStac_id as "TimeTableStac_id",
                ED.TimeTableGraf_id as "TimeTableGraf_id",
                ED.TimeTableMedService_id as "TimeTableMedService_id",
                ESC.EvnStatusCause_Name as "EvnStatusCause_Name",
                case
                    when ED.EvnDirection_failDT is not null
                        then DFT.DirFailType_Name
                    when EC.EvnConcilium_id is not null and ES2.EvnStatus_SysNick in ('Confirmed', 'Posted')
                        then NCR.ConciliumResult_Name
                    else
                        UTRT.UslugaTelemedResultType_Name
                end as "evndirection_result",
                ex.EvnXml_id as "EvnXml_id",
                ex.EvnXml_IsSigned as "EvnXml_IsSigned",
                to_char(ex.EvnXml_signDT, 'dd.mm.yyyy') as "EvnXml_signDT",
                coalesce(pu2.pmUser_Name, pu.pmUser_Name) as "pmUser_signName",
                ED.RequestConsult_id as "RequestConsult_id",
                EC.EvnConcilium_id as "EvnConcilium_id",
                EC.ConciliumType_id as "ConciliumType_id",
                ED.MedService_id as "MedService_id",
                LpuD.Org_id as "Org_tid",
                ED.RemoteConsultCause_id as "RemoteConsultCause_id",
                ED.RecMethodType_id as "RecMethodType_id",
                ED.EvnDirection_isAuto as "EvnDirection_isAuto",
                coalesce(EC.EvnConcilium_IsSigned, ex.EvnXml_IsSigned) as "Evn_IsSigned",
                to_char(coalesce(EC.EvnConcilium_signDT, ex.EvnXml_signDT), 'dd.mm.yyyy') as "Evn_signDT",
                ES2.EvnStatus_SysNick as "EvnConciliumStatus_SysNick",
                ES.EvnStatus_SysNick as "EvnDirectionStatus_SysNick",
                case when csmsf.ConciliumStaff_id is not null then 2 else 1 end as "isConciliumMember",
                CR.ConciliumResults_id as "ConciliumResults_id",
                ED.pmUser_insID as "pmUser_insID",
                ED.UslugaComplex_did as "UslugaComplex_did",
                
                case
                    -- Если направление на консилиум отменено
                    when ED.EvnDirection_failDT is not null
                        then 5
                    -- Если консилиум проведен и (заполнен результат или есть ТМК услуга)
                    when ES2.EvnStatus_SysNick in ('Confirmed', 'Posted') and (CR.ConciliumResults_id is not null or
                        EUT.EvnUslugaTelemed_id is not null)
                        then 4
                    else 1
                end as "evndirection_group"
            
                
                ,case
                	when ED.TimeTableGraf_id is not null then
                		to_char(ttgl.TimeTableGraf_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttgl.TimeTableGraf_begTime, 'hh24:MI')
                	else
                		to_char(ttmsl.TimeTableMedService_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttmsl.TimeTableMedService_begTime, 'hh24:MI')
                end as "TimeTable_begTime"
            , ED.ConsultingForm_id as "ConsultingForm_id"
            FROM
                v_EvnDirection_all ED
                INNER JOIN v_Evn r on r.Evn_id = ED.EvnDirection_rid
                INNER JOIN v_PersonState PS on PS.Person_id = ED.Person_id
                left join v_TelemedEvnDirection TED on TED.TelemedEvnDirection_id = ED.EvnDirection_id
                INNER JOIN v_EvnConcilium EC on EC.EvnConcilium_pid = ED.EvnDirection_id
                    INNER JOIN v_ConciliumStaff CS on CS.MedPersonal_id = '81428' AND
                        CS.EvnConcilium_id = EC.EvnConcilium_id
                left join v_PersonPregnancy pp  on pp.Person_id = ed.Person_id
                LEFT JOIN ConsultingForm CF  on CF.ConsultingForm_id = ED.ConsultingForm_id
                LEFT JOIN LATERAL(
                    select
                    1 as isDel
                    from v_pmUserCache pm
                    where pm.pmuser_id = ed.pmuser_insid
                    --AND lpu_id IS NOT NULL – если очень надо проверять по лпу можно добавить такое условие
                    limit 1
                ) isDel on true
                 LEFT JOIN v_EvnUslugaTelemed EUT on EUT.EvnDirection_id = ED.EvnDirection_id 
                left join v_EvnXml ex on ex.Evn_id = EUT.EvnUslugaTelemed_id
                left join v_pmUser pu on pu.pmUser_id = ex.pmUser_signID
                left join v_pmUser pu2 on pu2.pmUser_id = EC.pmUser_signID
                left join v_UslugaTelemedResultType UTRT  on UTRT.UslugaTelemedResultType_id = EUT.UslugaTelemedResultType_id
                LEFT JOIN v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
                left join lateral (
                    select LSPT.LpuSectionProfile_id,LSPT.LpuSectionProfile_name
                    from v_LpuSectionProfile LSPT
                    where LSPT.LpuSectionProfile_id = EUT.LpuSectionProfile_id limit 1
                ) LSPT on true
                LEFT JOIN v_RemoteConsultCause RCC  on RCC.RemoteConsultCause_id = ED.RemoteConsultCause_id
                LEFT JOIN v_Lpu l on l.Lpu_id = ED.Lpu_sid
                LEFT JOIN v_Lpu LpuD on LpuD.Lpu_id = ED.Lpu_did
                LEFT JOIN v_MedPersonal mp  on mp.MedPersonal_id = ED.MedPersonal_id and mp.Lpu_id = ED.Lpu_sid
                LEFT JOIN v_MedPersonal mp2 on mp2.MedPersonal_id = ED.MedPersonal_did and mp2.Lpu_id = ED.Lpu_did
                LEFT JOIN lateral
                (
                    select d.Diag_fullName from v_Diag d where ED.Diag_id = d.Diag_id limit 1
                ) d on true
                LEFT JOIN v_ConsultationForm consForm on consForm.ConsultationForm_id = ED.ConsultationForm_id
                LEFT JOIN v_DirType DT  on DT.DirType_id = ED.DirType_id
                LEFT JOIN v_EvnStatus ES  on ES.EvnStatus_id = ED.EvnStatus_id
                LEFT JOIN v_EvnStatus ES2 on ES2.EvnStatus_id = EC.EvnStatus_id
                LEFT JOIN v_ConciliumResult CR on CR.EvnConcilium_id = EC.EvnConcilium_id
                left join nsi.v_ConciliumResult NCR on NCR.ConciliumResult_id = CR.ConciliumResults_id
                left join v_DirFailType DFT on DFT.DirFailType_id = ED.DirFailType_id
                LEFT JOIN LATERAL (
                    select
                        esh.EvnStatusCause_id,
                        esh.pmUser_updID
                    from
                        v_EvnStatusHistory esh
                    where
                        esh.Evn_id = ed.EvnDirection_id
                        and esh.EvnStatus_id = ed.EvnStatus_id
                        and esh.EvnStatusCause_id is not null
                    order by
                        esh.EvnStatusHistory_id desc
                    limit 1
                ) esh on true
                left join v_EvnStatusCause esc  on esc.EvnStatusCause_id = esh.EvnStatusCause_id
                LEFT JOIN LATERAL (
                    select
                        cs.ConciliumStaff_id
                    from v_ConciliumStaff cs
                    where cs.MedPersonal_id = '81428' and cs.EvnConcilium_id = ec.EvnConcilium_id
                    limit 1
                ) as csmsf on true
                 left join v_TimeTableGraf_lite ttgl on ttgl.TimeTableGraf_id = ED.TimeTableGraf_id left join v_TimeTableMedService_lite ttmsl on ttmsl.TimeTableMedService_id = ED.TimeTableMedService_id
            WHERE
                1=1 AND 
                (
                    (EUT.EvnUslugaTelemed_setDT is null AND ED.EvnDirection_failDT is null AND coalesce(ED.EvnStatus_id, 1) != 15)
                    OR
                    (EUT.EvnUslugaTelemed_setDT is not null AND cast(EUT.EvnUslugaTelemed_setDT as date) between '2023-07-22' and '2023-07-22')
                    OR
                    (ED.EvnDirection_failDT is not null AND cast(ED.EvnDirection_failDT as date) between '2023-07-22' and '2023-07-22')
                    OR
                    ED.EvnDirection_setDT between '2023-07-22' and '2023-07-22'
                )
            
                 AND ED.EvnDirection_IsConcilium = 2 
        
