
                    with vars as (
                        select 45131 as begDay_id, 45131 as endDay_id
                    )

                    SELECT
                        
            ttg.TimetableGraf_id as "TimetableGraf_id",
            ttg.TimetableType_id as "TimetableType_id",
            ttg.TimeTableGraf_countRec as "TimeTableGraf_countRec",
            ttg.TimeTableGraf_PersRecLim as "TimeTableGraf_PersRecLim",
            case
                when ttg.Person_id is not null then coalesce(ttt.TimetableType_SysNick,'busy')
                else coalesce(ttt.TimetableType_SysNick,'free') end as "TimetableType_SysNick",
            coalesce(ttt.TimetableType_Name,'') as "TimetableType_Name",
            MSF.LpuSection_id as "LpuSection_id",
            ttg.Person_id as "Person_id",
            case
                when TimetableGraf_begTime is not null then to_char(TimetableGraf_begTime,'dd.mm.yyyy')
                when TimetableGraf_factTime is not null then to_char(TimetableGraf_factTime,'dd.mm.yyyy')
                else to_char(TimetableGraf_insDT,'dd.mm.yyyy')
            end as "TimetableGraf_Date",
            coalesce(to_char (TimetableGraf_begTime, 'hh24:mi'),'б/з') as "TimetableGraf_begTime",
            coalesce(to_char (eviz.EvnVizit_setTime, 'hh24:mi'), to_char (ttg.TimetableGraf_factTime, 'hh24:mi')) as "TimetableGraf_factTime",
            coalesce(eviz.EvnVizit_id, 0) as "EvnVizit_id",
            case when IP.IndicationPerson_IsNeedConciliumOnko = 2 then 'true' else 'false' end as "IndicationPerson_IsNeedConciliumOnko",
            case when IP.IndicationPerson_IsNotConciliumOnko = 2 then 'true' else 'false' end as "IndicationPerson_IsNotConciliumOnko",
            TimetableGraf_begTime as "timetableDatetime",
            to_char (ed.EvnDirection_setDate, 'dd.mm.yyyy') as "EvnDirection_setDate",
            case when ttg.Person_id is not null then
                case
                    when pu.pmUser_id is not null then rtrim(pu.pmUser_Name)
                    else 'Запись через интернет'
                end
            end as "pmUser_Name",
            ed.EvnDirection_Num as "EvnDirection_Num",
            case when exists(select ttg2.TimetableGraf_id as TimetableGraf_id from v_TimetableGraf_lite ttg2 where ttg2.MedStaffFact_id = ttg.MedStaffFact_id and ttg2.Person_id = ttg.Person_id and ttg2.TimetableGraf_Day > ttg.TimetableGraf_Day - 21 and ttg2.TimetableGraf_Day < ttg.TimetableGraf_Day and ttg.Evn_id is not null limit 1) then 2 else 1 end as "VizitType"
        
                , ttg.MedStaffFact_id as "MedStaffFact_id",
                ttg.TimetableGraf_Day as "TimetableGraf_Day",
                case when ttg.Person_id is not null then to_char (TimetableGraf_updDT, 'dd.mm.yyyy') || ' ' || to_char (TimetableGraf_updDT, 'hh24:mi') end as "TimetableGraf_updDT",
                ttg.pmUser_updId as "pmUser_updId",
                ttg.pmUser_insId as "pmUser_insId",
                case when ed.EvnDirection_isAuto != 2 then 'true' else 'false' end as "IsEvnDirection",
                ed.EvnQueue_id as "EvnQueue_id",
                ed.EvnStatus_id as "EvnStatus_id",
                MSF.Person_Fin as "MSF_Person_Fin",
                RTRIM(LSP.LpuSectionProfile_Name) as "LpuSectionProfile_Name",
                ed.EvnDirection_id as "EvnDirection_id",
                EPLDD13.EvnPLDispDop13_id as "EPLDD13_id",
                EPLDD13.DispClass_id as "EPLDD13_DispClass_id",
                ed.DirType_id as "DirType_id",
                ed.ConsultingForm_id as "ConsultingForm_id",
                to_char (ed.EvnDirection_setDT, 'dd.mm.yyyy') as "EvnDirection_setDT",
                ed.MedPersonal_did as "MedPersonal_did",
                ed.ARMType_id as "ARMType_id",
                eviz.EvnVizit_rid as "Evn_id"
            
                    FROM
                        v_TimetableGraf_lite ttg
                        
            join v_MedStaffFact MSF on MSF.MedStaffFact_id = ttg.MedStaffFact_id
            left join lateral(
                SELECT
                    ElectronicTalonStatus_id,
                    ElectronicTalon_id,
                    ElectronicTreatment_id,
                    EvnDirection_uid
                FROM v_ElectronicTalon et where et.EvnDirection_uid = ttg.EvnDirection_id
                UNION
                SELECT
                    ElectronicTalonStatus_id,
                    ElectronicTalon_id,
                    ElectronicTreatment_id,
                    EvnDirection_uid
                FROM v_ElectronicTalon et where et.EvnDirection_id = ttg.EvnDirection_id
            ) et on true
            left join lateral (select * from v_pmUser pu where pu.pmUser_id = ttg.pmUser_updId limit 1) pu on true
            left join TimetableType ttt on ttt.TimetableType_id = ttg.TimetableType_id
            left join v_EvnVizit eviz on eviz.TimetableGraf_id = ttg.TimetableGraf_id /*eviz.EvnDirection_id = ttg.EvnDirection_id*/
            left join v_IndicationPerson IP on IP.EvnPL_id = eviz.EvnVizit_pid and IP.person_id=eviz.person_id
            left join v_EvnDirection_all ed on ed.EvnDirection_id = ttg.EvnDirection_id and ed.DirFailType_id is null and ED.EvnStatus_id not in (12,13)
            left join v_EvnPLDispDop13 EPLDD13 on EPLDD13.EvnPLDispDop13_id = ed.EvnDirection_pid
            left join LpuSectionProfile LSP on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
            left join lateral (
                select
                    pp.PersonPhone_Phone as "PersonPhone_Phone"
                from v_PersonPhone pp
                where pp.Person_id = ed.Person_id
                order by pp.PersonPhone_insDT desc
                limit 1
            ) PersonPhone on true
        
                    WHERE
                        (1 = 1) and TimetableGraf_Day between (select begDay_id from vars) and (select endDay_id from vars) and ed.DirType_id = 17 and coalesce(ed.RequestConsult_id, 1) = 1
                        and MSF.MedStaffFact_id != '330101000001974'
                        and ttg.TimetableType_id = 12
                        and ttg.Person_id is not null
                        and MSF.MedStaffFact_id in (
                            select
                                mseq.MedStaffFact_id as MedStaffFact_id
                            from v_MedServiceElectronicQueue mseq
                            inner join v_ElectronicService es on es.ElectronicService_id = mseq.ElectronicService_id
                            inner join v_ElectronicQueueInfo eqi on eqi.ElectronicQueueInfo_id = es.ElectronicQueueInfo_id
                            inner join v_ElectronicTreatmentLink etl on etl.ElectronicQueueInfo_id = eqi.ElectronicQueueInfo_id

                            where etl.ElectronicTreatment_id in (
                                select
                                    etlIn.ElectronicTreatment_id as ElectronicTreatment_id
                                from v_ElectronicTreatmentLink etlIn
                                inner join v_ElectronicQueueInfo eqiIn on eqiIn.ElectronicQueueInfo_id = etlIn.ElectronicQueueInfo_id
                                inner join v_ElectronicService esIn2 on esIn2.ElectronicQueueInfo_id = eqiIn.ElectronicQueueInfo_id
                                where esIn2.ElectronicService_id = '330101000000019'
                            )
                        )
                    ORDER BY
                        "TimetableGraf_Day"
                        ,"TimetableGraf_Date"
                
