IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_calc_bizpower_authority]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sys_calc_bizpower_authority]
GO
CREATE  procedure [dbo].[mps_sys_calc_bizpower_authority](
@cmpid int ,
@bizid int ,
@ordids nvarchar(max)  -- ordid:fiid,ordid:fiid
) as
begin
-- 计算新建人有哪些
 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
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 @cnds nvarchar(4000) 
declare @s varchar(1000) 
declare @s1 varchar(1000) 
declare @i int 
declare @bi bigint
declare @ai int
set @i = CHARINDEX(',',@ordids) 
if (@i<=0) 
begin
set @i = CHARINDEX(':',@ordids) 
set @s = SUBSTRING(@ordids,1,@i-1) 
set @ordids = SUBSTRING(@ordids,@i+1,len(@ordids)) 
set @cnds = 'ord_id='+CONVERT(varchar(10),@s) 
set @bi = CONVERT(bigint,@s) 
set @ai = CONVERT(int,@ordids) 
insert into #srcobjs(objid,aid) values(@bi,@ai)
end else
begin
while (@ordids>'') 
begin
set @i = CHARINDEX(',',@ordids) 
if (@i>0) 
begin
set @s = SUBSTRING(@ordids,1,@i-1) 
set @ordids = SUBSTRING(@ordids,@i+1,len(@ordids)) 
end else
begin
set @s = @ordids 
set @ordids = '' 
end 
if (@s>'') 
begin
set @i = CHARINDEX(':',@s) 
set @s1 = SUBSTRING(@s,1,@i-1) 
set @s = SUBSTRING(@s,@i+1,100) 
set @bi = CONVERT(bigint,@s1) 
set @ai = CONVERT(int,@s) 
if (not exists(select 1 from #srcobjs where objid=@bi)) 
begin
insert into #srcobjs(objid,aid) values(@bi,@ai)
end 
end 
end 
set @cnds = 'objid=ord_id' 
end 
exec mps_gen_powerusers_data @cmpid,'biz',@bizid,@bizid,'bizaudit,bizview,bizabolish,bizdelete',@cnds 


 
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 
declare @sql nvarchar(4000) 
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