select t1.system_appsid, t1.fappname, t1.fnote
from system_apps t1
where t1.fisused=1 and (t1.siteid =$siteid$ or t1.siteid is null)
  and not exists(select *from tauthgroup_apps where siteid =$siteid$
  and tauthgroupid=$tauthgroupid$
  and system_appsid=t1.system_appsid
)
and
exists
(
select *
from tappmodelauth
where siteid=$siteid$ and fisdefault=1 and t1.fappmodels like '%/'+tappmodelauth.fappmodel+'/%'
)
