IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_calc_prdpower_data]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sys_calc_prdpower_data]
GO
create procedure [dbo].[mps_sys_calc_prdpower_data](
@cmpid int ,
@bizid int 
) as
begin
-- 计算新建人有哪些
 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
create table #user(pptype varchar(20),cuid int , ordid bigint) 

exec mps_gen_powerusers_data @cmpid,'prd',@bizid,@bizid,'prdaudit,prdview,prdabolish,prddelete,prdedit',''


declare @mctid int
set @mctid = @bizid * (-1) 

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'prd',@mctid,0,@tbname output,@fkey output ,@dbname output 
declare @sql nvarchar(4000) 
set @sql = '
declare @user table(ptype varchar(20),uid int,ord bigint) 
insert into @user(ptype,uid,ord) 
select distinct pptype,cuid,prd_id from #user,'+@dbname+@tbname+' where ordid=0 

insert into @user(ptype,uid,ord) 
select distinct pptype,cuid,ordid from #user where ordid>0 and 
not exists (select 1 from @user where ptype=pptype and cuid=uid and ordid=ord)  

-- 处理bizview 
delete '+@dbname+@tbname+'_users 
from '+@dbname+@tbname+' 
where prdp_prdid=prd_id and prd_pcid='+LTRIM(rtrim(str(@bizid)))+' and prdp_type=''prdview'' and prdp_otid>-1 and
 not exists (select 1 from @user where prdp_cuid=uid and prdp_prdid=ord) 
insert into '+@dbname+@tbname+'_users(prdp_type,prdp_cuid,prdp_prdid) 
select distinct ''prdview'',uid,ord from @user
where  (not exists (select 1 from '+@dbname+@tbname+'_users 
where prdp_type=''prdview'' and prdp_cuid=uid and prdp_prdid=ord)) 

-- 处理非bizview
delete '+@dbname+@tbname+'_users 
from '+@dbname+@tbname+' 
where prdp_prdid=prd_id and prd_pcid='+LTRIM(rtrim(str(@bizid)))+' and prdp_type<>''prdview'' and (prdp_type not like ''my%'') and 
 not exists (select 1 from @user where prdp_type=ptype and prdp_cuid=uid and prdp_prdid=ord) 
insert into '+@dbname+@tbname+'_users(prdp_type,prdp_cuid,prdp_prdid) 
select ptype,uid,ord from @user
where  ptype>'''' and ptype<>''cstview'' and  (ptype not like ''my%'') and 
(not exists (select 1 from '+@dbname+@tbname+'_users 
where prdp_type=ptype and prdp_cuid=uid and prdp_prdid=ord)) 

 
'
  
print @sql 
execute(@sql)
drop table #user 
end