IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_work_old]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_work_old]
GO
 
 

create procedure [dbo].[mps_new_work_old](
@cmpid int ,
@useruid int ,
@bizid int ,
@ordbcid int,
@ordid bigint,
@ordtype nvarchar(50),
@ordno varchar(50),
@ordtitle nvarchar(100),
@orddate datetime ,
@ordcreator nvarchar(50), 
@backinfo nvarchar(200),
@ftype varchar(10),
@fbizid int,
@fordid bigint,
@ford2tag int ,
@ford2id bigint,
@rtype varchar(10),
@rbizid int,
@rordid bigint,
@rord2tag int ,
@rord2id bigint,
@wrkid int ,
@fjbid bigint ,
@taskid int 
)
as
begin
set nocount on
declare @otlevel int
declare @managelevel int
declare @professionlevel int 
declare @bcid int 
declare @dpid int 
declare @otid int 
declare @roleid int
declare @branch nvarchar(50)
declare @depart nvarchar(50)
declare @callname nvarchar(50)
declare @mysql nvarchar(4000) 
declare @newcuid int 
set @newcuid = @useruid 
select top 1 @bcid=lgu_bcid,@dpid=lgu_dpid,@otid=lgu_otid,@roleid=lgu_function 
  , @branch=lgu_branch , @depart=lgu_depart , @callname=lgu_name  , @otlevel=lgu_otlevel ,
  @managelevel=lgu_managelevel , @professionlevel=lgu_professionlevel 
 from mps_login_role with(nolock) where lgu_cuid=@useruid  and   lgu_bcid=@ordbcid 
set @bcid = isnull(@bcid,0)
set @dpid = isnull(@dpid,0)
set @otid = isnull(@otid,0)
if (@dpid=0)   -- 如果找不到人，则从员工表中获取
begin
select top 1 @bcid=cu_bcid,@dpid=cu_dpid,@otid=cu_otid,@roleid=cu_function 
  , @branch=cu_branch , @depart=cu_depart , @callname=cu_name ,@otlevel=cu_otlevel ,
  @managelevel=cu_managelevel , @professionlevel=cu_professionlevel 
 from mps_employee with(nolock) where cu_id=@useruid   
end
declare @enabled int 
set @enabled = 0 
set @taskid = ISNULL(@taskid,0) 
if (@taskid=0) 
begin
declare @wrk_start int 
select @wrk_start=wrk_start from mps_flowwork with(nolock)  where wrk_id=@wrkid 
set @wrk_start = ISNULL(@wrk_start,0) 
if (@wrk_start>0) 
begin
if (not exists (select 1 from mps_job_task with(nolock)  where wt_wrkid=@wrkid and  wt_bizid=@bizid and wt_ordid=@ordid and wt_type='job')) 
begin
insert into mps_job_task(wt_begin,wt_wrkid,wt_cmpid,wt_bizid,wt_ordid,wt_useruid,wt_jbid,wt_task,wt_interval,wt_mbid,wt_type)
values(GETDATE()+@wrk_start/(60.00*24.00) , @wrkid , @cmpid,@bizid,@ordid,@useruid,0,
'exec mps_new_work '+convert(varchar(10),@cmpid)+','+
convert(varchar(10),@useruid )+','+
convert(varchar(10),@bizid )+','+
convert(varchar(10),@ordbcid)+','+
convert(varchar(10),@ordid)+','+
''''+@ordtype+''','+
''''+@ordno+''','+
''''+@ordtitle +''','+
''''+convert(varchar(19),@orddate,120)+''','+
''''+@ordcreator+''','+ 
''''+@backinfo+''','+
''''+@ftype+''','+
convert(varchar(10),@fbizid)+','+
convert(varchar(10),@fordid)+','+
convert(varchar(10),@ford2tag)+','+
convert(varchar(10),@ford2id)+','+
''''+@rtype+''','+
convert(varchar(10),@rbizid)+','+
convert(varchar(10),@rordid)+','+
convert(varchar(10),@rord2tag)+','+
convert(varchar(10),@rord2id)+','+
convert(varchar(10),@wrkid)+','+
convert(varchar(10),@fjbid)+',@taskid',5,0 ,'job'
)
return 
end else 
begin
return 
end 
end 
end 

declare @operation nvarchar(50) 
declare @errorinfo nvarchar(4000) 
set @operation = 'continue' 
set @errorinfo = '' 

if (dbo.checkprocedure('order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@wrkid)+'_wrknew_start')=0) 
begin
set @mysql='exec order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@wrkid)+'_wrknew_start 
@cmpid, @useruid, @bizid, @ordid, @wrkid, 0, @operation output, @errorinfo output ' 
exec sp_executesql @mysql, N'@cmpid int,@useruid int,@bizid int,@ordid bigint,@wrkid int ,@operation varchar(50) out,@errorinfo nvarchar(4000) out', 
@cmpid,@useruid,@bizid,@ordid,@wrkid,@operation output,@errorinfo output

end 

if (@operation<>'continue') 
begin
if (@taskid<=0)   -- 不成功，则送至任务  并且5分钟后再试
begin
if (not exists (select 1 from mps_job_task with(nolock) where wt_wrkid=@wrkid and  wt_bizid=@bizid and wt_ordid=@ordid and wt_type='job')) 
begin
insert into mps_job_task(wt_begin,wt_wrkid,wt_cmpid,wt_bizid,wt_ordid,wt_useruid,wt_jbid,wt_task,wt_interval,wt_mbid ,wt_type)
values(GETDATE()+5.00/(60.00*24.00) , @wrkid , @cmpid,@bizid,@ordid,@useruid,0,
'exec mps_new_work '+convert(varchar(10),@cmpid)+','+
convert(varchar(10),@useruid )+','+
convert(varchar(10),@bizid )+','+
convert(varchar(10),@ordbcid)+','+
convert(varchar(10),@ordid)+','+
''''+@ordtype+''','+
''''+@ordno+''','+
''''+@ordtitle +''','+
''''+convert(varchar(19),@orddate,120)+''','+
''''+@ordcreator+''','+ 
''''+@backinfo+''','+
''''+@ftype+''','+
convert(varchar(10),@fbizid)+','+
convert(varchar(10),@fordid)+','+
convert(varchar(10),@ford2tag)+','+
convert(varchar(10),@ford2id)+','+
''''+@rtype+''','+
convert(varchar(10),@rbizid)+','+
convert(varchar(10),@rordid)+','+
convert(varchar(10),@rord2tag)+','+
convert(varchar(10),@rord2id)+','+
convert(varchar(10),@wrkid)+','+
convert(varchar(10),@fjbid)+',@taskid' , 5,0 , 'job' 
)
end
end 
return 
end 

if (@taskid>0) 
begin
delete mps_job_task where wt_id=@taskid 
end 

-- 本工作有没有存在模板
declare @tmpid int 
select @tmpid=tmp_id from mps_template with(nolock) 
where tmp_cmpid=@cmpid and tmp_type='wrkform' and tmp_objid=@wrkid 
and len(convert(varchar(1000),tmp_form))>50 

set @tmpid=isnull(@tmpid,0) 

declare @orderstatus nvarchar(30) 
declare @orderkey varchar(30) 
exec mps_get_order_status @bizid,@ordid , @orderstatus output , @orderkey output 

declare @oldjbid bigint 
select @oldjbid = max(jb_id) from mps_job with(nolock) 

declare @jbnid bigint 
declare @jbnwrkcreated int  -- 这个变量表示：有预步骤，并且已经创建过wrkid的步骤的。那么有很事件就不需要再处理了。
set @jbnwrkcreated = 1 
-- 首先要决定下一步到底该建哪一个步
-- 1 先根据 @fjbid 去找，有没有与之相关的，需要建的步骤，如果有，就建之，若没有，则找一个没有建的来建
select top 1 @jbnid=jbn_id from mps_job_available with(nolock) 
where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_wrkid=@wrkid and jbn_created=1 and jbn_jbid=@fjbid 
order by jbn_id 
set @jbnid = isnull(@jbnid,0) 
if (@jbnid=0) 
begin
select top 1 @jbnid=jbn_id from mps_job_available with(nolock) 
where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_wrkid=@wrkid and jbn_created=1 and jbn_jbid<@fjbid 
order by jbn_id 
end 
if (exists (select 1 from mps_job_available with(nolock) where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_wrkid=@wrkid and jbn_created=0)) 
begin
set @jbnwrkcreated = 0
end 
declare @newjbs table(newjbid bigint) 
if (@jbnid>0) -- 发现了预定义的流程
begin
insert into mps_job(jb_cmpid,jb_bcid,jb_dpid,jb_cuid,jb_executor,jb_crmid,jb_ordtype,jb_ordid,jb_ordno,jb_fjbid , 
jb_ordtitle,jb_orddate,jb_ordcreator,jb_wrkid,jb_title,jb_date,jb_enddate,jb_backinfo,jb_ordstatus,jb_ordkey,
jb_isauto,jb_sign, jb_branch,jb_depart ,jb_wrktype,jb_execnum,
jb_templateid,jb_enabled,jb_createcuid,jb_creator,jb_confirm,jb_limitedtime,jb_showinundo,jb_jbnid) 
select @cmpid,cu_bcid,cu_dpid,cu_id,cu_callname,@bizid,@ordtype,@ordid,@ordno,
case when @fjbid=-999999999 then 0 else @fjbid end,
@ordtitle,@orddate,@ordcreator,@wrkid,wrk_title,getdate(),'1900-1-1',@backinfo,@orderstatus,@orderkey,
   case when charindex('auto',wrk_keyparams)>0 then 0 else 1 end ,
case when charindex('sign',wrk_keyparams)>0 then 0 else 1 end ,@branch,@depart  , wrk_type ,wrk_executor_num , 
@tmpid,@enabled,@useruid,@callname   , 
0 ,ISNULL(wrk_limited,0) ,
case when charindex('hideinundo',wrk_keyparams)>0 then 1 else 0 end  , @jbnid 
from mps_flowwork  with(nolock) , mps_login with(nolock) , mps_job_available_users with(nolock)  
where wrk_id=@wrkid  and jbnu_jbnid=@jbnid and jbnu_cuid=cu_id 
insert into @newjbs(newjbid) select jb_id from mps_job with(nolock) where jb_id>@oldjbid and jb_wrkid=@wrkid and jb_wsid=0 
end else
begin
insert into mps_job(jb_cmpid,jb_bcid,jb_dpid,jb_cuid,jb_executor,jb_crmid,jb_ordtype,jb_ordid,jb_ordno,jb_fjbid , 
jb_ordtitle,jb_orddate,jb_ordcreator,jb_wrkid,jb_title,jb_date,jb_enddate,jb_backinfo,jb_ordstatus,jb_ordkey,
jb_isauto,jb_sign, jb_branch,jb_depart ,jb_wrktype,jb_execnum,
jb_templateid,jb_enabled,jb_createcuid,jb_creator,jb_confirm,jb_limitedtime,jb_showinundo) 
select @cmpid,@bcid,@dpid,@useruid,@callname,@bizid,@ordtype,@ordid,@ordno,
case when @fjbid=-999999999 then 0 else @fjbid end,
@ordtitle,@orddate,@ordcreator,@wrkid,wrk_title,getdate(),'1900-1-1',@backinfo,@orderstatus,@orderkey,
   case when charindex('auto',wrk_keyparams)>0 then 0 else 1 end ,
case when charindex('sign',wrk_keyparams)>0 then 0 else 1 end ,@branch,@depart  , wrk_type ,wrk_executor_num , 
@tmpid,@enabled,@useruid,@callname   , 
case when charindex('auto',wrk_keyparams)>0 then 0 else 1 end ,
ISNULL(wrk_limited,0) ,
case when charindex('hideinundo',wrk_keyparams)>0 then 1 else 0 end  
from mps_flowwork  with(nolock) 
where wrk_id=@wrkid  
insert into @newjbs(newjbid) select jb_id from mps_job with(nolock) where jb_id>@oldjbid and jb_wrkid=@wrkid and jb_wsid=0 
end 

declare @newjbid bigint 
select top 1 @newjbid=newjbid from @newjbs 
set @newjbid = isnull(@newjbid,0) 

-- 需要纠正前面的关系
declare @tr table(wrkid int , jbid bigint) 
if (@jbnid=0) 
begin
insert into @tr(wrkid,jbid ) 
select distinct ws_swrkid ,0 from mps_flowworkstatus with(nolock) where ws_twrkid=@wrkid 
update @tr set jbid = jb_id from mps_job with(rowlock) where jb_cmpid=@cmpid and jb_ordid=@ordid and jb_back=1 and jb_wrkid=wrkid 
delete @tr where jbid=0 

declare @confirmed int 
set @confirmed = 0  -- 是否已经确认工作人员了
-- 写入job关系表
insert into mps_job_relation(jr_jbid,jr_fjbid) 
select @newjbid , jbid from @tr 
end else 
begin
insert into mps_job_relation(jr_jbid,jr_fjbid) 
select newjbid , @fjbid from @newjbs 
end 

-- 根据配置，重新生成该工作的人员清单
-- 以下处理用户自定义的权限
declare @jbisauto int 
declare @jbwrktype int   -- 0 串  1=任 2=会
declare @wrkshape varchar(40) 
declare @wrkexecutornum int
select top 1 @jbisauto = jb_isauto , @jbwrktype=jb_wrktype,@wrkshape=wrk_shape , @wrkexecutornum=wrk_executor_num 
from mps_job with(nolock) ,mps_flowwork with(nolock)  where jb_id=@newjbid and wrk_id=jb_wrkid 

-- 如果是自动 ，就不需要计算人员
-- if (@jbisauto>0)   改进一下，虽然是自动执行，也是要处理人员关系，因为有可能会发送消息  在处理的时候，会对executeor 进行特殊处理

begin

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 

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 @myi int 
declare @extsql nvarchar(4000) 

declare @users table(pptype varchar(30), ppgroup int , useruid int) 

if (@jbnwrkcreated=1) 
begin
if (@jbnid=0)  -- 如果有预处理步骤，才会做下面的，以下还有很多类似的判断
select top 1 @execsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkexecutor' 

select top 1 @freesql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkfree' 

select top 1 @auditsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkaudit' 
select top 1 @coopsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkcoop' 
select top 1 @viewsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkview' 
select top 1 @logsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrklog' 
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') 

declare @newsql nvarchar(4000) 
declare @newsql2 nvarchar(4000) 
set @newsql = @execsql+@auditsql+@coopsql+@viewsql+@logsql+@freesql 
if (len(@newsql)>20) 
begin

exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
set @tbname =@dbname+@tbname

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+' with(nolock) 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)+' with(nolock) where ord2_ordid='+convert(varchar(10),@ordid) +char(13)+char(10) 
 end
 set @myi = @myi + 1
end

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) 
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 v
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

exec drop_temp_table '#permit' 
end

create table #myuser(pptype varchar(30),cuid int, grp int) 
exec mps_gen_powerusers @cmpid,@bcid,@dpid,@useruid,@otid,@roleid,@otlevel,@managelevel,@professionlevel,
'biz',@bizid,@ordid,@newjbid,@wrkid,'wrkexecutor,wrkview,wrkaudit,wrkcoops,wrklog,wrkfree' 


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

drop table #myuser

exec drop_temp_table '#permittion' 
exec drop_temp_table '#permit' 
end 
   
-- 下面这段程序，目的是分析当前工作的人员情况
-- 如果发现，所有的人员都已经确定了，则将jb_confirm改为0
-- 只发现了一个人，则认为是正确的
  
-- 是第一步，并且没有人
if (@jbnid=0 and charindex('begin',@wrkshape)=1 and (select count(*) from @users where pptype='wrkexecutor')=0) 
begin
insert into @users(pptype , ppgroup   , useruid  ) 
  values('wrkexecutor',0,@useruid) 
end
-- 如果是自动执行
if (@jbisauto<=0 and @jbnid=0) 
begin
-- 如果一条都没有
if ((select count(*) from @users where pptype='wrkexecutor')=0)
begin
insert into @users(pptype , ppgroup   , useruid  ) 
  values('wrkexecutor',0,@useruid) 
end 
-- 删除多余的 
-- 要加逻辑，如果是任和会签并且 wrk_executor_num=0 则不能删除
declare @mycuid1 int 
if (@jbwrktype>0 and @wrkexecutornum=0) 
begin
set @mycuid1=0 -- 相当于什么都不要做
end else 
begin
select top 1  @mycuid1 = useruid from @users where pptype='wrkexecutor' 
delete @users where pptype='wrkexecutor'  and useruid<>@mycuid1 
end 
end 
if (@jbnid=0) 
begin
if ((select count(*) from @users where pptype='wrkexecutor')=0)
begin
  insert into @users(pptype , ppgroup   , useruid  ) 
  values('wrkexecutor',0,@useruid) 
  delete from @users where pptype<>'wrkexecutor'
  set @confirmed = 1   -- 没有确定人员
  -- 没有找到人，在前端，根据这种条件，提示选择下一步人员
end else 
if ((select count(*) from @users where pptype='wrkexecutor')=1)
begin
--  工作执行正好只有一个的情况 
select top 1 @newcuid=useruid from @users where pptype='wrkexecutor' 
set @newcuid = ISNULL(@newcuid,0) 
if (@newcuid>0 and @useruid<>@newcuid) 
begin
if (exists (select 1 from mps_login_role with(nolock) where  lgu_bcid=@ordbcid and lgu_cuid=@newcuid)) 
begin 
update mps_job set jb_confirm=0 ,
jb_cuid = @newcuid ,
jb_executor = lgu_name ,
jb_bcid = lgu_bcid ,
jb_branch = lgu_branch ,
jb_dpid = lgu_dpid ,
jb_depart = lgu_depart 
from  mps_login_role   with(nolock) 
where jb_id=@newjbid and lgu_bcid=@ordbcid and lgu_cuid=@newcuid  
end else
begin
update mps_job set jb_confirm=0 ,
jb_cuid = @newcuid ,
jb_executor = cu_name ,
jb_bcid = cu_bcid ,
jb_branch = cu_branch ,
jb_dpid = cu_dpid ,
jb_depart = cu_depart 
from mps_employee  with(nolock) 
where jb_id=@newjbid and cu_id=@newcuid 
end
end else 
begin
update mps_job set jb_confirm=0 where jb_id=@newjbid 
end
end else
if ((select count(*) from @users where pptype='wrkexecutor')>1 and @jbwrktype>0 and 
(@wrkexecutornum=0 or (select count(*) from @users where pptype='wrkexecutor')=@wrkexecutornum) )
begin
update mps_job set jb_confirm=0  where jb_id=@newjbid 
-- 处理任签和会签的情况 
-- 如果是汇签或任签，则生成新的job记录
delete @users where pptype='wrkexecutor' and useruid=@newcuid 
insert into mps_job(jb_bcid,jb_branch,jb_dpid,jb_depart,jb_cuid,jb_executor,
jb_cmpid,jb_crmid,jb_ordtype,jb_ordid,jb_ordno, jb_ordtitle,jb_orddate,jb_ordcreator,
jb_wrkid,jb_title,jb_date,jb_enddate,jb_isauto,jb_templateid,jb_sign,jb_back,
jb_enabled,jb_createcuid,jb_creator,jb_fjbid,jb_confirm,jb_limitedtime,jb_elapsedtime,
jb_ordstatus,jb_ordkey,jb_wrktype,jb_execnum)
select distinct cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,
jb_cmpid,jb_crmid,jb_ordtype,jb_ordid,jb_ordno, jb_ordtitle,jb_orddate,jb_ordcreator,
jb_wrkid,jb_title,jb_date,jb_enddate,jb_isauto,jb_templateid,jb_sign,jb_back,
jb_enabled,jb_createcuid,jb_creator,jb_fjbid,jb_confirm,jb_limitedtime,jb_elapsedtime,
jb_ordstatus,case when @jbwrktype=1 then 'hidden' else '' end,jb_wrktype,jb_execnum
from @users , mps_job  with(nolock) , mps_login with(nolock) 
where pptype='wrkexecutor' and  jb_id=@newjbid and useruid=cu_id and useruid<>@newcuid 
set @confirmed = 0  
declare @mjbid bigint 
select @mjbid=max(newjbid) from @newjbs 
insert into @newjbs(newjbid) 
select jb_id from mps_job with(nolock)
where jb_id>@mjbid and jb_cmpid=@cmpid and jb_crmid=@bizid and jb_wrkid=@wrkid and jb_wsid=0 
end else 
begin
set @confirmed = 1   -- 没有确定人员
end 
end else
begin
set @confirmed = 0    
end 
-- 将所有的人员插入到jobusers 
insert into mps_job_users(jp_type,jp_group,jp_jbid,jp_cuid,jp_sjbid) 
select pptype,ppgroup,@newjbid,useruid,case when @fjbid=-999999999 then 0 else @fjbid end from @users  
end 

if (@confirmed=1)  -- 需要再次确认工作执行人
begin
-- 下面的程序，将在[mps_do_unconfirm_job] 中继续 
return 
end 
-- 执行工作新建前
set @operation = 'continue' 
set @errorinfo = '' 
set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wrkid)+'_wrknew_before' ;
if (dbo.checkprocedure(@mysql)=0)
begin
set @mysql = 'exec '+@mysql+' @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@operation output,@errorinfo output '
exec sp_executesql @mysql, N'@cmpid int, @useruid int ,@bizid int ,@ordid bigint ,@wrkid int ,@jbid bigint ,@operation varchar(50) output,@errorinfo nvarchar(4000) output', 
@cmpid,@useruid,@bizid,@ordid,@wrkid,@newjbid,@operation output ,@errorinfo  output 
end
--exec mps_wrk_beforenew @cmpid,@useruid,@bizid,@ordid ,@wrkid,@newjbid ,@operation output , @errorinfo output 
if (@operation='abort') 
begin
delete mps_job from @newjbs where jb_id=newjbid 
if (@errorinfo='') set @errorinfo = 'error' 
select @errorinfo 
return 
end

declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select jb_id,jb_cuid  from  mps_job   with(nolock) , @newjbs where jb_id=newjbid 
  open status1
  declare @jbid bigint
  declare @jbcuid int 
  FETCH NEXT FROM status1 INTO @jbid  , @jbcuid 
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
exec mps_job_enabled @cmpid ,@jbcuid ,@bizid ,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,
@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@wrkid,@jbid  
FETCH NEXT FROM status1 INTO @jbid  , @jbcuid 
  END
  CLOSE status1
DEALLOCATE status1 
set nocount off
end