IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_systemprogram_md]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_systemprogram_md]
GO
create procedure [dbo].[mps_g_systemprogram_md]
(
@cmpid int,
@useradmin int,
@terminal int, --0:pc 1:mobile
@useruid int ,
@programname varchar(50) 
)
as
begin
set nocount on

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @prgid int 
select  top 1 @prgid=pt_id from mps_programtype where pt_cmpid=@cmpid and pt_program=0 and pt_filename=@programname
set @prgid = ISNULL(@prgid,0) 

-- t1 模板内容 default项
select * from mps_template where tmp_cmpid=@cmpid and tmp_type='prg' and tmp_objid=@prgid and tmp_objid2=0 
order by case when tmp_caption='body' then 0 else 1 end , tmp_caption 
-- t2 模板内容 当前项
select * from mps_template where tmp_cmpid=@cmpid and tmp_type='prg' and tmp_objid=@prgid and tmp_objid2=@useradmin 
order by case when tmp_caption='body' then 0 else 1 end , tmp_caption 


-- t3 返回菜单
 

declare @mdtag varchar(100) 
  
set @mdtag='prg'+CONVERT(varchar(10),@prgid) 
if (@terminal=0) 
begin
if (exists (select 1 from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=0 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=0 and mn_roleid=@useradmin order by mn_order 
end  else 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=0 and mn_roleid=0 order by mn_order 
end 
end else 
begin
if (exists (select 1 from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=1 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=1 and mn_roleid=@useradmin order by mn_order 
end  else 
begin
if (exists (select 1 from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=1 and mn_roleid=0)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=1 and mn_roleid=0 order by mn_order 
end else 
if (exists (select 1 from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=0 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=0 and mn_roleid=@useradmin order by mn_order 
end else 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag and mn_terminal=0 and mn_roleid=0 order by mn_order 
end 
end 
end
 
set nocount off
end