
            with recursive list as (
                
                select distinct
                    
                    XTC.XmlTemplateCat_id,
                    XTC.XmlTemplateCat_pid,
                    LS.LpuSection_id,
                    MP.MedPersonal_id,
                    1 as level
                
                from
                    v_XmlTemplate XT
                    inner join v_Lpu L on L.Lpu_id = XT.Lpu_id
                    left join v_XmlTemplateCat XTC1 on XTC1.XmlTemplateCat_id = XT.XmlTemplateCat_id
                    left join v_XmlTemplateCat XTC2 on XTC2.XmlTemplateCat_id = XTC1.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC3 on XTC3.XmlTemplateCat_id = XTC2.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC4 on XTC4.XmlTemplateCat_id = XTC3.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC5 on XTC5.XmlTemplateCat_id = XTC4.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC6 on XTC6.XmlTemplateCat_id = XTC5.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC on (   XTC.XmlTemplateCat_id = XTC1.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC2.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC3.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC4.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC5.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC6.XmlTemplateCat_id)
                    left join v_LpuSection LS on LS.Lpu_id = L.Lpu_id and LS.LpuSection_id = XT.LpuSection_id
                    left join v_pmUserCache Author on Author.pmUser_id = XT.pmUser_insID
                    left join v_XmlType XType on XType.XmlType_id = XT.XmlType_id
                    left join v_EvnClass EC on EC.EvnClass_id = XT.EvnClass_id
                    left join v_XmlTemplateScope XTS on XTS.XmlTemplateScope_id = XT.XmlTemplateScope_id
                    left join v_MedPersonal MP on MP.MedPersonal_id = XT.MedPersonal_id
                    left join lateral (
                        select case when exists(
                            select * from v_XmlTemplateSelected
                            where XmlTemplate_id = XT.XmlTemplate_id
                            and MedStaffFact_id = '140101000042866'
                            and MedPersonal_id = '140101000004935'
                            and pmUser_id = '406637177849'
                        ) then 2 else 1 end as value
                        limit 1
                    ) IsFavorite on true
                where
                    coalesce(XT.XmlTemplate_IsDeleted, 1) = 1
                    and XT.Lpu_id = '140101000000007'
                    and XT.XmlTemplateScope_id in (2,3,4)
                    and (XT.XmlTemplateScope_id <> 4 or XT.LpuSection_id = '140101000000567')
                    and 1=1
and XT.XmlType_id = '9'
and XT.EvnClass_id = '32'
and XT.pmUser_insID <> '406637177849'
and LS.LpuSection_id = '140101000000567'
and MP.MedPersonal_id is null
                    and not exists(
                        select * from v_XmlTemplateBase XTB
                        where XTB.XmlTemplate_id = XT.XmlTemplate_id
                    )
            
                union all
                select
                    XTC.XmlTemplateCat_id,
                    XTC.XmlTemplateCat_pid,
                    list.LpuSection_id,
                    list.MedPersonal_id,
                    level + 1 as level
                from v_XmlTemplateCat XTC
                inner join list on list.XmlTemplateCat_pid = XTC.XmlTemplateCat_id and list.XmlTemplateCat_id <> list.XmlTemplateCat_pid
                where XTC.LpuSection_id is not null
            )
            select distinct
                XTC.XmlTemplateCat_id as "XmlTemplateCat_id",
                XTC.XmlTemplateCat_pid as "XmlTemplateCat_pid",
                XTC.XmlTemplateCat_Name as "XmlTemplateCat_Name",
                list.LpuSection_id as "LpuSection_id",
                list.MedPersonal_id as "MedPersonal_id",
                list.level as "level",
                ChildrenFolders.Count as "childrenFoldersCount",
                ChildrenTemplates.Count as "childrenTemplatesCount"
            from list
            inner join v_XmlTemplateCat XTC on XTC.XmlTemplateCat_id = list.XmlTemplateCat_id
            left join lateral (
                select
                    count(*) as Count
                from (
                    
                select distinct
                    XTC.XmlTemplateCat_id
                from
                    v_XmlTemplate XT
                    inner join v_Lpu L on L.Lpu_id = XT.Lpu_id
                    left join v_XmlTemplateCat XTC1 on XTC1.XmlTemplateCat_id = XT.XmlTemplateCat_id
                    left join v_XmlTemplateCat XTC2 on XTC2.XmlTemplateCat_id = XTC1.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC3 on XTC3.XmlTemplateCat_id = XTC2.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC4 on XTC4.XmlTemplateCat_id = XTC3.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC5 on XTC5.XmlTemplateCat_id = XTC4.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC6 on XTC6.XmlTemplateCat_id = XTC5.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC on (   XTC.XmlTemplateCat_id = XTC1.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC2.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC3.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC4.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC5.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC6.XmlTemplateCat_id)
                    left join v_LpuSection LS on LS.Lpu_id = L.Lpu_id and LS.LpuSection_id = XT.LpuSection_id
                    left join v_pmUserCache Author on Author.pmUser_id = XT.pmUser_insID
                    left join v_XmlType XType on XType.XmlType_id = XT.XmlType_id
                    left join v_EvnClass EC on EC.EvnClass_id = XT.EvnClass_id
                    left join v_XmlTemplateScope XTS on XTS.XmlTemplateScope_id = XT.XmlTemplateScope_id
                    left join v_MedPersonal MP on MP.MedPersonal_id = XT.MedPersonal_id
                    left join lateral (
                        select case when exists(
                            select * from v_XmlTemplateSelected
                            where XmlTemplate_id = XT.XmlTemplate_id
                            and MedStaffFact_id = '140101000042866'
                            and MedPersonal_id = '140101000004935'
                            and pmUser_id = '406637177849'
                        ) then 2 else 1 end as value
                        limit 1
                    ) IsFavorite on true
                where
                    coalesce(XT.XmlTemplate_IsDeleted, 1) = 1
                    and XT.Lpu_id = '140101000000007'
                    and XT.XmlTemplateScope_id in (2,3,4)
                    and (XT.XmlTemplateScope_id <> 4 or XT.LpuSection_id = '140101000000567')
                    and 1=1
and XT.XmlType_id = '9'
and XT.EvnClass_id = '32'
and XT.pmUser_insID <> '406637177849'
and LS.LpuSection_id = '140101000000567'
and MP.MedPersonal_id is null
                    and not exists(
                        select * from v_XmlTemplateBase XTB
                        where XTB.XmlTemplate_id = XT.XmlTemplate_id
                    )
            
                    and XTC.XmlTemplateCat_pid = list.XmlTemplateCat_id
                ) T
                limit 1
            ) as ChildrenFolders on true
            left join lateral (
                select
                    count(*) as Count
                from (
                    
                select distinct
                    XT.XmlTemplate_id
                from
                    v_XmlTemplate XT
                    inner join v_Lpu L on L.Lpu_id = XT.Lpu_id
                    left join v_XmlTemplateCat XTC1 on XTC1.XmlTemplateCat_id = XT.XmlTemplateCat_id
                    left join v_XmlTemplateCat XTC2 on XTC2.XmlTemplateCat_id = XTC1.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC3 on XTC3.XmlTemplateCat_id = XTC2.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC4 on XTC4.XmlTemplateCat_id = XTC3.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC5 on XTC5.XmlTemplateCat_id = XTC4.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC6 on XTC6.XmlTemplateCat_id = XTC5.XmlTemplateCat_pid
                    left join v_XmlTemplateCat XTC on (   XTC.XmlTemplateCat_id = XTC1.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC2.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC3.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC4.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC5.XmlTemplateCat_id
                        											or XTC.XmlTemplateCat_id = XTC6.XmlTemplateCat_id)
                    left join v_LpuSection LS on LS.Lpu_id = L.Lpu_id and LS.LpuSection_id = XT.LpuSection_id
                    left join v_pmUserCache Author on Author.pmUser_id = XT.pmUser_insID
                    left join v_XmlType XType on XType.XmlType_id = XT.XmlType_id
                    left join v_EvnClass EC on EC.EvnClass_id = XT.EvnClass_id
                    left join v_XmlTemplateScope XTS on XTS.XmlTemplateScope_id = XT.XmlTemplateScope_id
                    left join v_MedPersonal MP on MP.MedPersonal_id = XT.MedPersonal_id
                    left join lateral (
                        select case when exists(
                            select * from v_XmlTemplateSelected
                            where XmlTemplate_id = XT.XmlTemplate_id
                            and MedStaffFact_id = '140101000042866'
                            and MedPersonal_id = '140101000004935'
                            and pmUser_id = '406637177849'
                        ) then 2 else 1 end as value
                        limit 1
                    ) IsFavorite on true
                where
                    coalesce(XT.XmlTemplate_IsDeleted, 1) = 1
                    and XT.Lpu_id = '140101000000007'
                    and XT.XmlTemplateScope_id in (2,3,4)
                    and (XT.XmlTemplateScope_id <> 4 or XT.LpuSection_id = '140101000000567')
                    and 1=1
and XT.XmlType_id = '9'
and XT.EvnClass_id = '32'
and XT.pmUser_insID <> '406637177849'
and LS.LpuSection_id = '140101000000567'
and MP.MedPersonal_id is null
                    and not exists(
                        select * from v_XmlTemplateBase XTB
                        where XTB.XmlTemplate_id = XT.XmlTemplate_id
                    )
            
                    and XT.XmlTemplateCat_id = list.XmlTemplateCat_id
                ) T
                limit 1
            ) as ChildrenTemplates on true
            where XTC.XmlTemplateCat_id is not null
and XTC.XmlTemplateCat_pid is null
            order by level, XTC.XmlTemplateCat_pid
        
