IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_unconfirm_job]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_unconfirm_job]
GO
CREATE procedure [dbo].[mps_g_unconfirm_job]( 
@cmpid int , 
@useruid int , 
@ordbcid int ,
@bizid int,
@ordid bigint 
) as
begin 
set nocount on 

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 @tbname =@dbname+@tbname
--- 这里加一段代码，如果有confirm=1 并且，并且是没有找到的，则重新计算一下
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select jb_id,jb_wrkid  from mps_job 
where jb_cmpid=@cmpid and jb_ordid=@ordid  and jb_cuid=@useruid  and jb_crmid=@bizid 
and jb_back=1 and jb_isauto=1 and jb_confirm=1 and jb_wsid=0 and 
(select count(1) from mps_job_users where jp_jbid=jb_id and jp_type='wrkexecutor')<=1 
  open status1
  declare @wrkid int
  declare @newjbid bigint 
  declare @newcuid int 
  declare @fjbid bigint
  declare @ts nvarchar(100) 
  declare @execsql nvarchar(4000) 
declare @auditsql nvarchar(4000) 
declare @coopsql nvarchar(4000) 
declare @viewsql nvarchar(4000) 
declare @logsql nvarchar(4000) 
declare @freesql nvarchar(4000) 
declare @newsql nvarchar(4000) 
declare @newsql2 nvarchar(4000) 
declare @myi int 
declare @extsql nvarchar(4000) 

exec drop_temp_table '#permittion' 
create table #permittion(ptype varchar(30), ctgid int , pid int , pobjid1 int , pobjid2 int) 
exec drop_temp_table '#permit' 
create table #permit(type varchar(30),cuid int) 
create table #myuser(pptype varchar(30),cuid int, grp int) 
declare @users table(pptype varchar(30), ppgroup int , useruid int) 
  FETCH NEXT FROM status1 INTO @newjbid,@wrkid  
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
select top 1 @execsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkexecutor' 
select top 1 @auditsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkaudit' 
select top 1 @coopsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkcoop' 
select top 1 @viewsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkview' 
select top 1 @logsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrklog' 
select top 1 @freesql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkfree' 
set @execsql = isnull(@execsql,'') 
set @auditsql = isnull(@auditsql,'') 
set @coopsql = isnull(@coopsql,'') 
set @viewsql = isnull(@viewsql,'') 
set @logsql = isnull(@logsql,'') 
set @freesql = isnull(@freesql,'') 

set @execsql = replace(@execsql,'#user','#permit') 
set @auditsql = replace(@auditsql,'#user','#permit') 
set @coopsql = replace(@coopsql,'#user','#permit') 
set @viewsql = replace(@viewsql,'#user','#permit') 
set @logsql = replace(@logsql,'#user','#permit') 
set @freesql = replace(@freesql,'#user','#permit') 

delete #permittion 
delete #permit
set @newsql = @execsql+@auditsql+@coopsql+@viewsql+@logsql+@freesql 

if (len(@newsql)>20) 
begin
set @extsql = '' 
if (CHARINDEX('#master',@newsql)>0) 
begin
set @extsql = 'exec drop_temp_table ''#master'' '+char(13)+char(10) 
set @extsql = @extsql +' select * into #master from '+@tbname+' where ord_id='+convert(varchar(10),@ordid) +char(13)+char(10) 
end 
set @myi = 1 
while (@myi<300) 
begin
 if (charindex('#detail'+convert(varchar(10),@myi),@newsql)>0) 
 begin
set @extsql = 'exec drop_temp_table ''#detail'+convert(varchar(10),@myi)+''' '+char(13)+char(10) 
set @extsql = @extsql +' select * into #detail'+convert(varchar(10),@myi)+' from '+@tbname+'_'+convert(varchar(10),@myi)+' where ord2_ordid='+convert(varchar(10),@ordid) +char(13)+char(10) 
 end
 set @myi = @myi + 1
end

if (len(@execsql)>10) 
begin
set @execsql = @extsql + @execsql
exec sp_executesql  @execsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @newjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkexecutor' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@auditsql)>10) 
begin
set @auditsql = @extsql + @auditsql
exec sp_executesql @auditsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @newjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkaudit' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@coopsql)>10) 
begin
set @coopsql = @extsql + @coopsql
exec sp_executesql @coopsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @newjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkcoops' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@viewsql)>10) 
begin
set @viewsql = @extsql + @viewsql
exec sp_executesql @viewsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @newjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkview' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@logsql)>10) 
begin
set @logsql = @extsql + @logsql
exec sp_executesql @logsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @newjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrklog' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@freesql)>10) 
begin
set @freesql = @extsql + @freesql
exec sp_executesql @freesql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @newjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkfree' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit
end

delete #myuser 
exec mps_gen_powerusers @cmpid,@ordbcid,0,@useruid,0,0,0,0,0,
'biz',@bizid,@ordid,@newjbid,@wrkid,'wrkexecutor,wrkview,wrkaudit,wrkcoops,wrklog,wrkfree' 

delete @users  
insert into @users( pptype ,ppgroup,useruid) 
select distinct pptype ,grp , cuid from #myuser 

-- 正好找到一个人
if ((select count(*) from @users where pptype='wrkexecutor')=1) 
begin
set @fjbid=0 
select top 1 @fjbid=jp_sjbid from mps_job_users  where jp_jbid=@newjbid and jp_sjbid>0
set @fjbid = ISNULL(@fjbid,0) 
delete mps_job_users where jp_jbid=@newjbid 
insert into mps_job_users(jp_type,jp_group,jp_jbid,jp_cuid,jp_sjbid) 
select pptype,ppgroup,@newjbid,useruid,@fjbid from @users  
select @newcuid=useruid from @users -- 显然有问题 where pptype='wrkexecutor' 
set @ts = CONVERT(varchar(10),@newjbid)+','+CONVERT(varchar(10),@newcuid)+',0' 
exec mps_do_unconfirm_job @cmpid, @useruid,@ordbcid ,@bizid, @ordid ,@ts,'' 
end else
if ((select count(*) from @users where pptype='wrkexecutor')>1)  -- 找到了很多人
begin
set @fjbid=0 
select top 1 @fjbid=jp_sjbid from mps_job_users  where jp_jbid=@newjbid and jp_sjbid>0
set @fjbid = ISNULL(@fjbid,0) 
delete mps_job_users where jp_jbid=@newjbid 
insert into mps_job_users(jp_type,jp_group,jp_jbid,jp_cuid,jp_sjbid) 
select pptype,ppgroup,@newjbid,useruid,@fjbid from @users  where pptype='wrkexecutor'  
end 
   FETCH NEXT FROM status1 INTO @newjbid,@wrkid   
  END
  CLOSE status1
DEALLOCATE status1 
drop table #myuser
exec drop_temp_table '#permit' 
exec drop_temp_table '#permittion' 

-- 检查有没有需要我确认的内容
declare @t table(jbid bigint , jbtitle nvarchar(100) , jbwrktype int , execnum int) 
insert into @t(jbid,jbtitle,jbwrktype,execnum) 
select jb_id,jb_title,jb_wrktype,isnull(jb_execnum,0)  from mps_job 
where jb_cmpid=@cmpid and jb_ordid=@ordid  and jb_cuid=@useruid  and jb_crmid=@bizid 
and jb_back=1 and jb_isauto=1 and jb_confirm=1 and jb_wsid=0 

 
declare @r table(jb_title nvarchar(100),jb_id bigint , cu_id int , cu_bcid int ,cu_dpid int ,
cu_otid int , cu_name nvarchar(50), cu_depart nvarchar(200), cu_branch nvarchar(100), cu_title nvarchar(50) ,
jp_group int , jb_wrktype int , jb_execnum int , flag int) 

 
-- 返回执行人  t1 

insert into @r(jb_title  ,jb_id , cu_id , cu_bcid ,cu_dpid ,
cu_otid , cu_name , cu_depart , cu_branch , cu_title,jp_group , jb_wrktype,jb_execnum,flag)
select jbtitle ,jbid , jp_cuid,0,0,0,'','','','' ,jp_group,jbwrktype ,execnum,1 
from @t,mps_job_users 
where jbid=jp_jbid and  jp_type='wrkexecutor'   
  
insert into @r(jb_title  ,jb_id , cu_id , cu_bcid ,cu_dpid ,
cu_otid , cu_name , cu_depart , cu_branch , cu_title,jp_group , jb_wrktype,jb_execnum,flag)
select jbtitle ,jbid , jp_cuid,0,0,0,'','','','' ,jp_group,jbwrktype ,execnum,2
from @t,mps_job_users 
where jbid=jp_jbid and   jp_type='wrkcoops'   


update @r set 
cu_bcid = lgu_bcid ,
cu_dpid = lgu_dpid ,
cu_otid = lgu_otid ,
cu_branch = lgu_branch ,
cu_depart = lgu_depart ,
cu_title = lgu_title , 
cu_name = lgu_name 
from mps_login_role 
where cu_id=lgu_cuid and lgu_bcid=@ordbcid 

update a set 
a.cu_bcid = b.cu_bcid ,
a.cu_dpid = b.cu_dpid ,
a.cu_otid = b.cu_otid ,
a.cu_branch = b.cu_branch ,
a.cu_depart = b.cu_depart ,
a.cu_title = b.cu_title , 
a.cu_name = b.cu_name 
from @r a ,mps_employee  b 
where a.cu_id=b.cu_id and  a.cu_dpid<=0 

-- 返回执行人 t1
select * from @r where flag=1 and cu_name>''
order by jb_id , jp_group ,  cu_name 

-- 返回协作人 t2
select * from @r where flag=2  and cu_name>'' 
order by jb_id , jp_group ,  cu_name 

set nocount off 
end