IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_calc_bizpower_data_finished1]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sys_calc_bizpower_data_finished1]
GO
CREATE procedure [dbo].[mps_sys_calc_bizpower_data_finished1](
@cmpid int ,
@bizid int 
) as
begin
-- 计算新建人有哪些
create table #user(pptype varchar(20),cuid int , ordid bigint) 
  
exec mps_gen_powerusers_data @cmpid,'biz',@bizid,@bizid,'bizaudit,bizview,bizabolish,bizdelete','ord_updated>=getdate()-0.003'

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 @user table(ptype varchar(20),uid int,ord bigint) 
insert into @user(ptype,uid,ord) 
select distinct pptype,cuid,ord_id from #user,'+@dbname+@tbname+' with(nolock) where ordid=0 and ord_updated>=getdate()-0.003 

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+' with(nolock) 
where ord_updated>=getdate()-0.003 and ord_id=ordp_ordid and  ordp_type=''bizview'' and ordp_otid>-1 and
 not exists (select 1 from @user where ordp_cuid=uid and ordp_ordid=ord) 
insert into '+@dbname+@tbname+'_users(ordp_type,ordp_cuid,ordp_ordid) 
select distinct ''bizview'',uid,ord from @user
where  (not exists (select 1 from '+@dbname+@tbname+'_users  with(nolock) 
where ordp_type=''bizview'' and ordp_cuid=uid and ordp_ordid=ord)) 

-- 处理非bizview
delete '+@dbname+@tbname+'_users 
from '+@dbname+@tbname+' with(nolock)
where ord_updated>=getdate()-0.003 and ord_id=ordp_ordid and ordp_type<>''bizview'' and (ordp_type not like ''my%'') and 
 not exists (select 1 from @user where ordp_type=ptype and ordp_cuid=uid and ordp_ordid=ord) 
insert into '+@dbname+@tbname+'_users(ordp_type,ordp_cuid,ordp_ordid) 
select ptype,uid,ord from @user
where  ptype>'''' and ptype<>''bizview'' and  (ptype not like ''my%'') and 
(not exists (select 1 from '+@dbname+@tbname+'_users  with(nolock)
where ordp_type=ptype and ordp_cuid=uid and ordp_ordid=ord)) 

-- 处理biztype_users 
--insert into mps_biztype_users(bp_type , bp_bizid,bp_cuid) 
--select distinct ''bizview'','+CONVERT(varchar(10),@bizid)+',ordp_cuid 
--from '+@dbname+@tbname+'_users where ordp_type=''bizview'' and 
--not exists (select 1 from mps_biztype_users where bp_type=''bizview'' and 
--bp_bizid='+CONVERT(varchar(10),@bizid)+' and bp_cuid=ordp_cuid)

-- 如果是项目类，则将正在处理的步骤中的人，加入到mps_order_users中去
declare @style int 
select @style=biz_formstyle  from mps_biztype where biz_id='+CONVERT(varchar(10),@bizid)+' 
if (@style=0) 
begin
delete @user
insert into @user(uid,ord) 
select distinct jp_cuid,ord_id  from mps_job ,mps_job_users, '+@dbname+@tbname+' 
where ord_style=0 and ord_id=jb_ordid and jb_crmid=ord_bizid and jb_wsid=0 and jb_back=1 and jb_isauto=1 and jp_jbid=jb_id 

delete mps_order_users 
from @user a
where  ordp_bizid='+CONVERT(varchar(10),@bizid)+' and ordp_ordid=a.ord and 
  (not exists (select 1 from @user b where ordp_cuid=b.uid and ordp_ordid=b.ord))  
insert into mps_order_users(ordp_bizid,ordp_ordid,ordp_cuid) 
select '+CONVERT(varchar(10),@bizid)+',ord,uid 
from @user 
where not exists (select 1 from mps_order_users where ordp_bizid='+CONVERT(varchar(10),@bizid)+' and ordp_ordid=ord and ordp_cuid=uid)
end 
'
  
print @sql 
execute(@sql)
drop table #user 
end