IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_biztype_query_authority]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_biztype_query_authority]
GO
create proc [dbo].[mps_biztype_query_authority]
@cmpid int,
@branchid int,
@bizid int,
@useradmin int,
@userdpstid int,
@userdpid int,
@userotid int
AS
BEGIN
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
--存取所有具有权限的条件
create table #t (mpid int,ctgid int, ppid int,objid1 int,objid2 int )

--存取满足权限的条件
create table #pobj(ctgid int)

--未设置权限的默认显示
insert into #pobj select bql_id from mps_biztype_query
where bql_bizid=@bizid and bql_id not in
(select mp_ctgid from mps_permission where mp_type ='biz'+convert(varchar,@bizid)  ) 

--筛选出所有具有权限的数据
insert into #t select mp_id,mp_ctgid,mp_ppid,mp_objid1,mp_objid2 
from mps_permission   with(nolock)
where mp_type ='biz'+convert(varchar,@bizid) 

--如果当前公司为总公司
if @branchid=0
begin

insert into #pobj  select distinct(ctgid)  from #t where ppid=1 and objid1=0 and objid2=0 

insert into #pobj  select distinct(ctgid)  from #t where ppid=3 and objid1=0 and objid2=0 

insert into #pobj  select distinct(ctgid)  from #t where ppid=4 and objid1=0 and objid2=4 

insert into #pobj  select distinct(ctgid)  from #t where ppid=12 and objid1=@userdpid 

insert into #pobj  select distinct(ctgid)  from #t where ppid=9 and objid1=@userotid 

insert into #pobj  select distinct(ctgid)  from #t where ppid=6 and objid1=@useradmin 

insert into #pobj  select distinct(ctgid)  from #t where ppid=10 and objid1=@userdpid and objid2=@userotid 

end

--如果当前公司为子公司 
if @branchid <> 0
begin

insert into #pobj  select distinct(ctgid) from #t where ppid=1 and objid1=0 and objid2=0

insert into #pobj  select distinct(ctgid) from #t where ppid=2 and objid1=0 and objid2=0 

insert into #pobj  select distinct(ctgid) from #t where ppid=13 and objid1=@userdpstid 

insert into #pobj  select distinct(ctgid) from #t where ppid=8 and objid1=@userotid 

insert into #pobj  select distinct(ctgid) from #t where ppid=7 and objid1=@useradmin 

insert into #pobj  select distinct(ctgid) from #t where ppid=11 and objid1=@userdpstid and objid2=@userotid 

end
select distinct * from #pobj 

drop table #t
drop table #pobj

set nocount off
END