IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_prdbrowse_md]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_prdbrowse_md]
GO
CREATE procedure [dbo].[mps_g_prdbrowse_md](
@cmpid int,
@useradmin int,
@terminal int,
@useruid int,
@pctag int,
@pcid int  
)
as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @tmpid int 
set @tmpid = 0 
if (@pcid=0) -- 返回大类
begin
select top 1 * from mps_prdctg where pc_cmpid=@cmpid and pc_tag=@pctag
select top 1 @tmpid=tmp_id from  mps_template where tmp_cmpid=@cmpid  and tmp_type='prdtagbrow' and tmp_objid=@pctag 
if (@tmpid=0) 
select top 1 @tmpid=tmp_id from  mps_template,mps_prdctg where tmp_cmpid=@cmpid  and tmp_type='prdbrow' and tmp_objid=pc_id and pc_tag=@pctag and pc_cmpid=@cmpid 
select * from mps_template where tmp_id=@tmpid 
select a.* from mps_prdctg_users a , mps_prdctg 
where pp_pcid=pc_id  and  pp_cuid=@useruid and pc_tag=@pctag 
end else 
begin
select top 1 * from mps_prdctg where pc_cmpid=@cmpid and pc_id=@pcid 
select top 1 @tmpid=tmp_id from mps_template where tmp_cmpid=@cmpid  and tmp_type='prdbrow' and tmp_objid=@pcid  
declare @pcid2 int 
declare @pcidf int 
set @pcid2 = @pcid 
set @pcidf = 0 
while (@tmpid=0 and @pcid2>0) 
begin
set @pcidf = 0 
select top 1 @pcidf=pc_father from mps_prdctg where pc_id=@pcid2 
if (@pcidf>0) 
begin
select top 1 @tmpid=tmp_id from mps_template where tmp_cmpid=@cmpid  and tmp_type='prdbrow' and tmp_objid=@pcidf 
set @pcid2 = @pcidf   
end else 
begin
set @pcid2 = 0 
end 
end 
if (@tmpid<=0) 
begin
select top 1 @tmpid=tmp_id from  mps_template where tmp_cmpid=@cmpid  and tmp_type='prdtagbrow' and tmp_objid=@pctag 
end 
select * from mps_template where tmp_id=@tmpid 
select a.* from mps_prdctg_users a , mps_prdctg 
where pp_pcid=pc_id  and  pp_cuid=@useruid and pc_id=@pcid 
end 

declare @pv int 
set @pv =  @pcid 
if (@pcid=0) set @pv=@pctag 
declare @mdtag varchar(100) 
-- 扩展功能功能 t5 
set @mdtag='prdtagbrow' 
if (@terminal=0) 
begin
if (exists (select 1 from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) and mn_terminal=0 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) and mn_terminal=1 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) and mn_terminal=1 and mn_roleid=0)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) and mn_terminal=0 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) and mn_terminal=0 and mn_roleid=0 order by mn_order 
end 
end 
end
-- 选中后功能 t5 
set @mdtag='prdtagbrowslt' 
if (@terminal=0) 
begin
if (exists (select 1 from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) and mn_terminal=0 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) and mn_terminal=1 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) and mn_terminal=1 and mn_roleid=0)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) and mn_terminal=0 and mn_roleid=@useradmin)) 
begin
select * from mps_menu where mn_cmpid=@cmpid and mn_flag=@mdtag+convert(varchar(10),@pv) 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+convert(varchar(10),@pv) and mn_terminal=0 and mn_roleid=0 order by mn_order 
end 
end 
end
set nocount off
end