IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_calc_bizpower_authority_data]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sys_calc_bizpower_authority_data]
GO
create proc mps_sys_calc_bizpower_authority_data
@cmpid int ,
@bizid int
as
begin
-- 计算新建人有哪些
create table #user(pptype varchar(20),cuid int , ordid bigint) 
create table  #user2(ptype varchar(20),uid int) 
create table #srcobjs(objid bigint,aid int) 

declare @sql nvarchar(4000) 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
set @sql = ' insert into #srcobjs(objid,aid) select ordaf_ordid,ordaf_id from '+@dbname+@tbname+'_authority_factor'
exec(@sql)

declare @cnds nvarchar(4000) 
set @cnds = 'objid=ord_id' 
exec mps_gen_powerusers_data @cmpid,'biz',@bizid,@bizid,'bizaudit,bizview,bizabolish,bizdelete',@cnds 


  
set @sql = '
declare @bizview int= 1
declare @bizaudit int= 2
declare @bizabolish int= 3
declare @bizdelete int= 4
declare @bizlog int= 5

insert into '+@dbname+@tbname+'_authority_control(orda_type,orda_cuid,orda_ordafid,orda_auto) 
select distinct 
case when pptype=''bizaudit'' then @bizaudit 
 when pptype=''bizview'' then @bizview 
 when pptype=''bizabolish'' then @bizabolish 
 when pptype=''bizdelete'' then @bizdelete 
 when pptype=''bizlog'' then @bizlog 
 else 100 
end 
,cuid,aid,0 from #user,#srcobjs where pptype<>''bizview'' and (objid=ordid or ordid=0) and 
(not exists (select 1 from  '+@dbname+@tbname+'_authority_control with(nolock) where 
 orda_type=case when pptype=''bizaudit'' then @bizaudit 
 when pptype=''bizview'' then @bizview 
 when pptype=''bizabolish'' then @bizabolish 
 when pptype=''bizdelete'' then @bizdelete 
 when pptype=''bizlog'' then @bizlog 
 else 100 
end and orda_cuid=cuid and orda_ordafid=aid))

delete #user where pptype<>''bizview''

insert into '+@dbname+@tbname+'_authority(orda_cuid,orda_ordafid,orda_auto) 
select distinct cuid,aid,0 from #user,#srcobjs where pptype=''bizview'' and (objid=ordid or ordid=0) and 
(not exists (select 1 from  '+@dbname+@tbname+'_authority with(nolock) where 
  orda_cuid=cuid and orda_ordafid=aid))

'
  
print @sql 
exec sp_executesql @sql 

drop table #user 
drop table #user2 
drop table #srcobjs
end