
            with fav as (
                select distinct
                    XTSe.XmlTemplate_id
                from
                    v_XmlTemplateSelected XTSe
                where
                    XTSe.MedStaffFact_id = '380101000009748'
                    and XTSe.MedPersonal_id = '380101000004286'
                    and XTSe.pmUser_insID = '411474205274'
            )

            select
                -- select
                XT.XmlTemplate_id as "XmlTemplate_id",
                XT.XmlTemplate_Caption as "XmlTemplate_Caption",
                XT.XmlTemplate_Descr as "XmlTemplate_Descr",
                XTH.XmlTemplateHtml_HtmlTemplate as "XmlTemplate_HtmlTemplate",
                
                XT.XmlTemplateCat_id as "XmlTemplateCat_id",
                XT.XmlTypeKind_id as "XmlTypeKind_id",
                coalesce(XTK.XmlTypeKind_Name, null) as "XmlTypeKind_Name",
                case when fav.XmlTemplate_id is not null then 2 else 1 end as "XmlTemplate_IsFavorite",
                Author.pmUser_id as "Author_id",
                (
                    rtrim(Author.PMUser_SurName)||
                    coalesce(' '||left(Author.PMUser_FirName, 1)||'.', '')||
                    coalesce(' '||left(Author.PMUser_SecName, 1)||'.', '')
                ) as "Author_Fin",
                XType.XmlType_id as "XmlType_id",
                XType.XmlType_Name as "XmlType_Name",
                EC.EvnClass_id as "EvnClass_id",
                EC.EvnClass_SysNick as "EvnClass_SysNick",
                EC.EvnClass_Name as "EvnClass_Name",
                XTS.XmlTemplateScope_id as "XmlTemplateScope_id",
                XTS.XmlTemplateScope_Name as "XmlTemplateScope_Name",
                XTSh.XmlTemplateShared_id as "XmlTemplateShared_id",
                XTSh.XmlTemplateShared_IsReaded as "XmlTemplateShared_IsReaded"
                -- end select
            from
                -- from
                v_XmlTemplate XT
                left join v_XmlTemplateHtml XTH on XTH.XmlTemplateHtml_id = XT.XmlTemplateHtml_id
                
                
                inner join v_EvnClass EC on EC.EvnClass_id = XT.EvnClass_id
                inner join v_XmlType XType on XType.XmlType_id = XT.XmlType_id
                left join fav on fav.XmlTemplate_id = XT.XmlTemplate_id
                left join v_PMUserCache Author on Author.pmUser_id = XT.pmUser_insID
                left join v_XmlTemplateScope XTS on XTS.XmlTemplateScope_id = XT.XmlTemplateScope_id
                left join v_XmlTemplateShared XTSh on XTSh.XmlTemplate_id = XT.XmlTemplate_id and XTSh.pmUser_getID = '411474205274' and XTSh.Lpu_gid = '357'
                left join v_XmlTemplateCat XTC on XTC.XmlTemplateCat_id = XT.XmlTemplateCat_id
                left join v_XmlTypeKind XTK on XTK.XmlTypeKind_id = XT.XmlTypeKind_id
                -- end from
            where
                -- where
                1=1
and coalesce(XT.XmlTemplate_IsDeleted, 1) = 1
and XT.XmlType_id = '3'
and XT.EvnClass_id = '11'
and XT.Lpu_id = '357'
and XT.pmUser_insID = '411474205274'
and not exists(
            select * from v_XmlTemplateBase XTB
            where XTB.XmlTemplate_id = XT.XmlTemplate_id
        )
                -- end where
         limit 1000
