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

CREATE procedure [dbo].[mps_execute_job_link](
@cmpid int ,
@useruid int ,
@bizid int ,
@ordbcid int,
@ordid bigint,
@ordtype nvarchar(50),
@ordno varchar(50),
@ordtitle nvarchar(100),
@orddate datetime ,
@ordcreator nvarchar(50),
@ftype varchar(10),
@fbizid int,
@fordid bigint,
@ford2tag int ,
@ford2id bigint,
@rtype varchar(10),
@rbizid int,
@rordid bigint,
@rord2tag int ,
@rord2id bigint,
@wrkid int ,
@jbid bigint,
@wsid int,
@gotojbid bigint -- 用于任意步骤中转
)
as 
begin
/*
这段程序是负责执行LINK 
考虑到有自由流程与流程转发，在处理LINK将有以下功能:
如果是转发的工作，将不处理任何事项，做完后，重新生成初始步骤的那项工作
如果是自由流程，也不能处理LINK上的某些事项
*/
set nocount on
declare @ws_swrkidint 
declare @ws_twrkidint 
declare @ws_captionnvarchar(50) 
declare @ws_statusnvarchar(20) 
declare @ws_status_fnvarchar(20) 
declare @ws_status_ffnvarchar(20) 
declare @ws_status_rnvarchar(20) 
declare @ws_keynvarchar(20) 
declare @ws_key_fnvarchar(20) 
declare @ws_key_ffnvarchar(20) 
declare @ws_key_rnvarchar(20) 
declare @ws_prevwrkids varchar(1000) 
declare @wrk_prevwrkids varchar(1000) 
declare @ws_fjbstatus nvarchar(50) 
declare @ws_bizrelation nvarchar(4000) 
declare @wrk_bizrelation nvarchar(4000) 
declare @shapevarchar(30) 
declare @mysql nvarchar(1000) 
declare @li bigint 
declare @fjbid bigint 

-- 判断一下，当前流程是不是自由流程或是不是被其它流程转发过来的。
declare @isfreewrk int = 1 
declare @undofreewrk int = 1
declare @isfwd int =1
declare @undofwdwrk int =1
declare @wrktype int 
 
select top 1 
  @wrktype=wrk_type,
      @isfreewrk=case when wrk_type=10 then 0 else 1 end  , 
  @isfwd = case when wrk_keyparams like '%allowfwd%' then 0 else 1 end 
 from mps_flowwork with(nolock) where wrk_id=@wrkid 
if (@isfreewrk=0)  -- 如果是自由流程，则需要判断自由流程还有没有全部做完
begin
-- 看看还有没有没有做完的步骤
if (exists (select 1 from mps_job with(nolock) where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and  jb_wrkid=@wrkid and jb_wsid=0)) 
begin
set @undofreewrk = 0 
end else 
begin
set @isfreewrk=1 
if (exists (select 1 from mps_job_available with(nolock) 
where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_wrkid=@wrkid and jbn_created=1 and jbn_ordid=@ordid   
)) set @isfreewrk = 0 
end 
end 
-- 当前步骤是可以转发的
if (@isfwd=0) 
begin
-- 看看还有没有没有做完的步骤 
select top 1 @fjbid=jb_fjbid  from  mps_job with(nolock) where jb_id=@jbid 
if (exists (select 1 from mps_job with(nolock)
where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and jb_wrkid=@wrkid and jb_wsid=0 and jb_fjbid=@fjbid and jb_id<>@jbid and jb_isfwd=0)) 
begin
set @undofwdwrk = 0 
end
if (@undofwdwrk=1) -- 没有发现未做的转发的步骤，但要看看，当前步骤是不是转过来的，如果不是，说明没有发起转发
begin
select @isfwd=jb_isfwd  from  mps_job with(nolock) where jb_id=@jbid  
end 
end 

-- 先删除预警
if (@isfreewrk=1 and @isfwd=1) 
delete mps_job_task 
where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_jbid=@jbid and wt_type in ('job','wrkalarm') and (wt_task not like '%mps_new_work%')

select @ws_swrkid=ws_swrkid,@ws_twrkid=ws_twrkid,@ws_caption=ws_caption,@shape=ws_tshape,@ws_fjbstatus=ws_fjbstatus,
@ws_status=ws_status,@ws_status_f=ws_status_f,@ws_status_ff=ws_status_ff,@ws_status_r=ws_status_r,
@ws_key=ws_key,@ws_key_f=ws_key_f,@ws_key_ff=ws_key_ff,@ws_key_r=ws_key_r ,@ws_prevwrkids=ws_prevwrkids ,
@ws_bizrelation=ws_bizrelation  
from  mps_flowworkstatus with(nolock) where ws_id=@wsid 
set @ws_fjbstatus = isnull(@ws_fjbstatus , '') 
set @ws_caption = replace(@ws_caption,'　','')
  set @ws_caption = ltrim(rtrim(@ws_caption)) 
  
  select @wrk_prevwrkids=wrk_prevwrkids ,@wrk_bizrelation =wrk_bizrelation 
  from  mps_flowwork with(nolock) where wrk_id=@ws_twrkid 
  set @wrk_prevwrkids=isnull(@wrk_prevwrkids,'') 
  
  
  declare @thistbname varchar(50) 
  declare @thistbnamenodb varchar(50) 
  declare @thistbid int
  select @thistbname=biz_database , @thistbid=biz_table from   mps_biztype with(nolock)where biz_id=@bizid 
  if (@thistbid>0) select @thistbname=biz_database , @thistbid=biz_id from   mps_biztype with(nolock)where biz_id=@thistbid 

if (@thistbid=0) set @thistbnamenodb='order_'+ltrim(rtrim(str(@bizid))) 
else set @thistbnamenodb='order_'+ltrim(rtrim(str(@thistbid))) 

if (@thistbname>'') set @thistbname=@thistbname+'.dbo.'  
set @thistbname=@thistbname+@thistbnamenodb   
   
declare @sql nvarchar(4000) 
declare @db varchar(50) 
declare @tbid int 
declare @ord2str varchar(10) 
declare @s1 varchar(300) 
declare @s2 varchar(300) 
declare @s11 varchar(300) 
declare @s0 nvarchar(500) 
declare @s00 nvarchar(500) 

declare @i int 
declare @ic int 
declare @ic2 int 
declare @auto varchar(500)
declare @manual nvarchar(2000)

set @auto=','
set @manual='|'
-- 先处理 bizrelation 
-- na202,r0:供货单,n0,am202,n0,r1:供货单|n0,r4:出库单,n0,n0,n0,r0:出库单
set @sql = @ws_bizrelation 
while (@sql>'')
begin
set @i = charindex('|',@sql) 
if (@i>0) 
begin
set @s0 = substring(@sql,1,@i-1) 
set @sql = substring(@sql,@i+1,len(@sql)-@i) 
end else 
begin
set @s0 = @sql 
set @sql = '' 
end

set @i = charindex('aa',@s0) 
set @s00 = @s0 
if (@i>0) set @s00 = SUBSTRING(@s0,@i,100)  
set @s1 = substring(@s00,1,2) 
set @s00 = substring(@s00,3,len(@s00)-2) 
if (@s1='aa'  ) 
begin
set @i = charindex(',',@s00) 
if (@i>0) 
begin
set @s2=substring(@s00,@i+1,len(@s00)-@i) 
set @s00=substring(@s00,1,@i-1) 
set @i = charindex(':',@s2) 
if (@i>0) set @s2 = substring(@s2,1,@i-1) else set @s2=''
end else set @s2 = '' 
if (charindex(','+@s00+',',@auto)<=0) 
begin
set @auto=@auto+@s00+','+@s2+':'  
end 
end  

set @i = charindex('am',@s0) 
set @s00 = @s0 
if (@i>0) set @s00 = SUBSTRING(@s0,@i,100)  
set @s1 = substring(@s00,1,2) 
set @s00 = substring(@s00,3,len(@s00)-2) 
if (@s1='am'  ) 
begin
if (charindex('|'+@s00+'|',@manual)<=0) set @manual=@manual+@s00+'|' 
end
end
-- na2,r1:差旅费报销单 
set @sql =  @wrk_bizrelation 
while (@sql>'')
begin
set @i = charindex('|',@sql) 
if (@i>0) 
begin
set @s0 = substring(@sql,1,@i-1) 
set @sql = substring(@sql,@i+1,len(@sql)-@i) 
end else 
begin
set @s0 = @sql 
set @sql = '' 
end

set @i = charindex('am',@s0) 
set @s00 = @s0 
if (@i>0) set @s00 = SUBSTRING(@s0,@i,100)  
set @s1 = substring(@s00,1,2) 
set @s00 = substring(@s00,3,len(@s00)-2) 
if (@s1='am') 
begin
if (charindex('|'+@s00+'|',@manual)<=0) set @manual=@manual+@s00+'|' 
end

set @i = charindex('dm',@s0) 
set @s00 = @s0 
if (@i>0) set @s00 = SUBSTRING(@s0,@i,100)  
set @s1 = substring(@s00,1,2) 
set @s00 = substring(@s00,3,len(@s00)-2) 
if (@s1='dm') 
begin
if (charindex('|'+@s00+'|',@manual)<=0) set @manual=@manual+@s00+'|' 
end
end
if (@manual='|') set @manual=''
if (@auto=',') set @auto=''
if (len(@manual)>2) set @manual = substring(@manual , 2, len(@manual) - 2) 
if (len(@auto)>2) set @auto = substring(@auto , 2, len(@auto) - 1) 
if (len(@manual)>200) set @manual=substring(@manual,1,200) 

set @ic=0 
declare @tprevwrkids table(pwrkid int , rt int , grp int , jbid bigint ) 
while (@wrk_prevwrkids>'') 
begin
set @i = charindex('|',@wrk_prevwrkids)
if (@i>0)
begin
set @s1 = substring(@wrk_prevwrkids,1,@i-1)
set @wrk_prevwrkids=substring(@wrk_prevwrkids,@i+1,len(@wrk_prevwrkids)-@i) 
end else 
begin
set @s1=@wrk_prevwrkids
set @wrk_prevwrkids=''
end
if (@s1>'') set @ic = @ic + 1 
while (@s1>'') 
begin
set @i = charindex(',',@s1)
if (@i>0)
begin
set @s11 = substring(@s1,1,@i-1)
set @s1=substring(@s1,@i+1,len(@s1)-@i) 
end else 
begin
set @s11=@s1
set @s1=''
end
if (@s11>'') 
insert into @tprevwrkids(pwrkid,rt,grp) values(convert(int,@s11),0 , @ic ) 
end 
end

if (not exists (select * from @tprevwrkids)) 
begin
-- 如果一个也没有设置，则默认是所有
insert into @tprevwrkids(pwrkid,rt,grp) 
select distinct ws_swrkid , 0 , 1 
from mps_flowworkstatus  with(nolock) 
where ws_twrkid=@ws_twrkid 
end


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

set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wsid)+'_wrklink_before' ;
if (dbo.checkprocedure(@mysql)=0)
begin
set @mysql = 'exec '+@mysql+' @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid,@wsstatus ,@operation,@errorinfo '
exec sp_executesql @mysql, N'@cmpid int, @useruid int ,@bizid int ,@ordid bigint ,@wrkid int ,@jbid bigint ,@wsid int ,@wsstatus nvarchar(30),@operation varchar(50) output,@errorinfo nvarchar(4000) output', 
@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid,@ws_status  ,@operation output ,@errorinfo output 
end

--exec mps_link_beforeexecute @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid,@operation,@errorinfo 

if (@operation='abort')
begin
if (@errorinfo='') set @errorinfo='error' 
select @errorinfo errorinfo
return 
end

-- 这里要加一些代码，如果是任签，此时就应该删除其它工作

if (@wrktype=1) 
begin
declare @jbi table(jbid bigint)
insert into @jbi(jbid) select jb_id from mps_job with(nolock)  where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and jb_wrkid=@wrkid and jb_wsid=0 and jb_id<>@jbid 
delete mps_job from @jbi where jb_id=jbid 
delete mps_job_users from @jbi where jp_jbid=jbid 
delete mps_task from @jbi where tsk_cmpid=@cmpid and tsk_objtype='biz' and tsk_jbid=jbid 
delete mps_job_task where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_type='job' and wt_wrkid=@wrkid and   (wt_task not like '%mps_new_work%')
delete mps_job_task from @jbi where wt_jbid=jbid and wt_type in ('job','wrkalarm') and (wt_task not like '%mps_new_work%') 
delete mps_alarm_message from @jbi  where  almmsg_jbid=jbid 
end 

set @sql=''  
if ((@ws_status<>'' or @ws_key<>'') and (@isfreewrk=1 and @isfwd=1))
begin
if (@ws_status='auto' or @ws_status='系统自动' or @ws_status='(系统自动)') set @ws_status=@ws_caption 
if (@ws_key='auto') set @ws_key=@ws_key 

set @sql=N'update '+@thistbname+' with(rowlock)  set ord_createbizids=@manual ' 

if (@ws_status>'') 
begin
set @sql = @sql +N' , ord_status=@ws_status  '  
end
if (@ws_key>'') 
begin
set @sql = @sql +' , ord_key=@ws_key '  
end
set @sql = @sql + ' where ord_id=@ordid '
exec sp_executesql @sql, N'@ws_status nvarchar(50),@ws_key varchar(50) , @ordid bigint,@manual nvarchar(200)', @ws_status  ,@ws_key, @ordid ,@manual 
end

set @sql=''  
set @ord2str = '' 
if ((@ws_status_f<>'' or @ws_key_f<>'') and (@isfreewrk=1 and @isfwd=1))
begin
select @db=biz_database , @tbid=biz_table from   mps_biztype with(nolock) where biz_id=@fbizid 
if (@tbid>0) select @db=biz_database , @tbid=biz_id from   mps_biztype with(nolock) where biz_id=@tbid 
if (@db>'') set @db=@db+'.dbo.' ;
if (@tbid=0) set @db=@db+'order_'+ltrim(rtrim(str(@fbizid))) else set @db=@db+'order_'+ltrim(rtrim(str(@tbid))) 
if (@ford2tag>0 and @ford2id>0) 
begin
set @db = @db+'_'+@ford2tag 
set @ord2str = '2' 
end
if (@ws_status_f='auto'  or @ws_status_f='系统自动' or @ws_status_f='(系统自动)') set @ws_status_f=@ws_caption 
if (@ws_key_f='auto') set @ws_key_f=@ws_key_f 

if (@ws_status_f>'') set @sql = @sql +' set ord'+@ord2str+'_status=@ws_status_f '  
if (@ws_key_f>'') 
begin
if (@sql>'') set @sql = @sql+' , ' 
   set @sql = @sql +' set ord'+@ord2str+'_key=@ws_key_f ' ;
end
if (@sql>'') set @sql = 'update '+@db+@sql+' with(rowlock)  where ord'+@ord2str+'_id=' ;
   if (@ford2tag>0 and @ford2id>0)  set @sql=@sql+'@ford2id'  else set @sql=@sql+'@fordid'
if (@sql>'') exec sp_executesql @sql, N'@ws_status_f nvarchar(50),@ws_key_f varchar(50) , @ford2id bigint , @fordid bigint', @ws_status_f  ,@ws_key_f, @ford2id ,@fordid 
end

set @sql=''  
set @ord2str = '' 
if ((@ws_status_r<>'' or @ws_key_r<>'') and (@isfreewrk=1 and @isfwd=1))
begin
select @db=biz_database , @tbid=biz_table from   mps_biztype with(nolock) where biz_id=@rbizid 
if (@tbid>0) select @db=biz_database , @tbid=biz_id from   mps_biztype with(nolock) where biz_id=@tbid 
if (@db>'') set @db=@db+'.dbo.' ;
if (@tbid=0) set @db=@db+'order_'+ltrim(rtrim(str(@rbizid))) else set @db=@db+'order_'+ltrim(rtrim(str(@tbid))) 
if (@rord2tag>0 and @rord2id>0) 
begin
set @db = @db+'_'+@rord2tag 
set @ord2str = '2' 
end
if (@ws_status_r='auto'  or @ws_status_r='系统自动' or @ws_status_r='(系统自动)') set @ws_status_r=@ws_caption 
if (@ws_key_r='auto') set @ws_key_r=@ws_key_r 

if (@ws_status_r>'') set @sql = @sql +' set ord'+@ord2str+'_status=@ws_status_r  '  
if (@ws_key_r>'') 
begin
if (@sql>'') set @sql = @sql+' , ' 
   set @sql = @sql +' set ord'+@ord2str+'_key=@ws_key_r  ' ;
end
if (@sql>'') set @sql = 'update '+@db+@sql+' with(rowlock)  where ord'+@ord2str+'_id=' ;
  if (@rord2tag>0 and @rord2id>0)  set @sql=@sql+'@rord2id'  else set @sql=@sql+'@rordid'
if (@sql>'') exec sp_executesql @sql, N'@ws_status_r nvarchar(50),@ws_key_r varchar(50) , @rord2id bigint , @rordid bigint', @ws_status_r  ,@ws_key_r, @rord2id ,@rordid 
end

set @sql=''  
-- 下面这段程序，主要目的是修改上一触发本业务的那个工作步骤，同时执行之
if (@ws_fjbstatus<>'' and (@isfreewrk=1 and @isfwd=1)) 
begin
declare @jbwrkid int 
declare @jbwsid int 
declare @nbizid int 
declare @nordbcid int 
declare @nordid bigint 
declare @nordtype nvarchar(50) 
declare @nordno varchar(50) 
declare @nordtitle nvarchar(100)  
declare @norddate datetime  
declare @nordcreator nvarchar(50) 
declare @nftype varchar(10) 
declare @nfbizid int 
declare @nfordid bigint 
declare @nford2tag int 
declare @nford2id bigint 
declare @nrtype varchar(10) 
declare @nrbizid int 
declare @nrordid bigint 
declare @nrord2tag int  
declare @nrord2id bigint 
declare @njbid bigint 
declare @ordfjbid bigint 

set @ordfjbid = 0  
set @jbwsid =0 
set @sql = N'select top 1 @ordfjbid=ord_fjbid  from  '+@thistbname+N' with(nolock) where ord_id=@ordid ' 
exec sp_executesql @sql, N'@ordfjbid bigint output , @ordid bigint  ', @ordfjbid output,@ordid  
select top 1 @jbwsid = jb_wsid , @jbwrkid=jb_wrkid from mps_job with(nolock) where jb_id=@ordfjbid 
declare @tbwrks table(jbid bigint,wrkid int, wscaption nvarchar(50),wsid int) 
if (@jbwsid>0) 
begin
set @nordid = 0 
insert into @tbwrks(jbid,wrkid,wscaption,wsid) 
select distinct jb_id,jb_wrkid,'',0 from mps_job with(nolock)  where jb_fjbid=@ordfjbid and jb_wsid=0   
update @tbwrks set wscaption=ws_caption, wsid=ws_id 
from mps_flowworkstatus with(nolock) 
where ws_swrkid=wrkid and ws_caption=@ws_fjbstatus 
end else 
begin
insert into @tbwrks(jbid,wrkid,wscaption,wsid) 
select @ordfjbid,@jbwrkid , ws_caption, ws_id 
from mps_flowworkstatus with(nolock) 
where ws_swrkid=@jbwrkid and ws_caption=@ws_fjbstatus 
if ((select COUNT(1) from @tbwrks)=0) 
begin
insert into @tbwrks(jbid,wrkid,wscaption,wsid) 
select @ordfjbid,@jbwrkid,@ws_fjbstatus,0 
end 
end 
set @njbid=0 
select  @njbid=jbid,@jbwrkid=wrkid,@jbwsid=wsid from @tbwrks order by wscaption desc 
set @jbwsid = ISNULL(@jbwsid,0) 

set @db = '' 
select @nbizid=jb_crmid , @nordid=jb_ordid from mps_job with(nolock) where jb_id=@ordfjbid 
select @db=biz_database from   mps_biztype with(nolock)where biz_id=@nbizid 
if (@db>'')   set @db=@db+'.dbo.' 
set @db=@db+'order_'+ltrim(rtrim(str(@nbizid))) 

if ( @jbwrkid>0 )  -- 说明是有效的状态 如果无效状态 ，不操作
begin
 set @sql = N'select top 1 @nordtype=ord_type ,@nordbcid=ord_bcid, @nordno=ord_no,@nordtitle=ord_title,@norddate=ord_date,@nordcreator=ord_creator ,
 @nftype=ord_ftype,@nfbizid=ord_fbizid,@nfordid=ord_fordid,@nford2tag=ord_ford2tag,@nford2id=ord_ford2id,
 @nrtype=ord_rtype,@nrbizid=ord_rbizid,@nrordid=ord_rordid,@nrord2tag=ord_rord2tag,@nrord2id=ord_rord2id 
 from '+@db+N'  with(nolock) where ord_id=@nordid ' 
exec sp_executesql @sql, 
N'@nordid bigint,@nordbcid int output,
@nordtype nvarchar(50) output,
@nordno varchar(50)  output,
@nordtitle nvarchar(100)   output,
@norddate datetime   output,
@nordcreator nvarchar(50)  output,
@nftype varchar(10)  output,
@nfbizid int  output,
@nfordid bigint  output,
@nford2tag int  output,
@nford2id bigint  output,
@nrtype varchar(10)  output,
@nrbizid int  output,
@nrordid bigint  output,
@nrord2tag int   output,
@nrord2id bigint output   ',
@nordid ,
@nordbcid output,
@nordtype output,
@nordno output,
@nordtitle  output,
@norddate  output,
@nordcreator  output,
@nftype  output,
@nfbizid  output,
@nfordid  output,
@nford2tag  output,
@nford2id  output,
@nrtype  output,
@nrbizid  output,
@nrordid  output,
@nrord2tag  output,
@nrord2id  output 


set @sql = '由'+@ordtype+'['+@ordno+']触发'  
/*
exec mps_execute_job 
@cmpid , 
@useruid  , 
@nbizid ,
@nordbcid,
@nordid ,
@nordtype,
@nordno ,
@nordtitle ,
@norddate ,
@nordcreator ,
@nftype ,
@nfbizid ,
@nfordid ,
@nford2tag,
@nford2id ,
@nrtype ,
@nrbizid ,
@nrordid ,
@nrord2tag,
@nrord2id ,
@jbwrkid ,
@njbid ,
@jbwsid ,
@ws_fjbstatus ,
@sql ,
0  
*/
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行，执行触发本业务的工作步骤','','exec mps_execute_job '+
convert(varchar(10),@cmpid)+' ,'+
convert(varchar(10), @useruid )+' ,'+
convert(varchar(10), @nbizid )+' ,'+
convert(varchar(10), @nordbcid)+' ,'+
convert(varchar(10), @nordid )+' ,'+
''''+@nordtype+''','+
''''+@nordno +''','+
''''+@nordtitle +''','+
''''+convert(varchar(18), @norddate ,120)+''','+
''''+@nordcreator +''','+
''''+@nftype +''','+
convert(varchar(10), @nfbizid )+' ,'+
convert(varchar(10), @nfordid )+' ,'+
convert(varchar(10), @nford2tag)+' ,'+
convert(varchar(10), @nford2id )+' ,'+
''''+@nrtype +''','+
convert(varchar(10), @nrbizid )+' ,'+
convert(varchar(10), @nrordid )+' ,'+
convert(varchar(10), @nrord2tag)+' ,'+
convert(varchar(10), @nrord2id )+' ,'+
convert(varchar(10), @jbwrkid )+' ,'+
convert(varchar(10), @njbid )+' ,'+
convert(varchar(10), @jbwsid )+' ,'+
''''+@ws_fjbstatus +''','+
''''+@sql +''',0 ') 
end
end

-- 执行扩展程序 

if (@isfreewrk=1 and @isfwd=1)
begin
set @sql=N'
exec order_'+ltrim(rtrim(str(@bizid)))+N'_dolinkdata @cmpid,@useruid,@bizid,@wsid,@ordbcid,@ordid,@jbid,
@ftype,@fbizid,@fordid,@ford2tag,@ford2id,
@rtype,@rbizid,@rordid,@rord2tag,@rord2id'
exec sp_executesql @sql,N'@cmpid int,@useruid int,@bizid int,@wsid int,@ordbcid int,@ordid bigint,@jbid bigint,
@ftype varchar(10),@fbizid int,@fordid bigint,@ford2tag int,@ford2id bigint,
@rtype varchar(10),@rbizid int,@rordid bigint,@rord2tag int,@rord2id bigint',
@cmpid,@useruid,@bizid,@wsid,@ordbcid,@ordid,@jbid , 
@ftype,@fbizid,@fordid,@ford2tag,@ford2id,
@rtype,@rbizid,@rordid,@rord2tag,@rord2id 
end 
-- 执行扩展SQL

if (@isfreewrk=1 and @isfwd=1)
begin
set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wsid)+'_wrklink_after' ;
if (dbo.checkprocedure(@mysql)=0)
begin
set @mysql = 'exec '+@mysql+' @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid,@wsstatus '
exec sp_executesql @mysql, N'@cmpid int, @useruid int ,@bizid int ,@ordid bigint ,@wrkid int ,@jbid bigint ,@wsid int ,@wsstatus nvarchar(30)', 
@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid,@ws_status   
end
end 
--- exec mps_link_afterexecute @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid

-- 提交后应新建的业务
-- 调整哪些业务可以新建
if ((@ws_status='' and @ws_key='') and (@isfreewrk=1 and @isfwd=1)) 
begin
set @sql=N'update '+@thistbname+' with(rowlock)  set ord_createbizids=@manual where ord_id=@ordid and ord_createbizids<>@manual' 
exec sp_executesql @sql,N'@ordid bigint,@manual nvarchar(200)',@ordid,@manual 
end 
-- 以下开始新建
-- 11,r1:
declare @createnewcuid int 
declare @jbfatherid bigint
while (@auto>'' and (@isfreewrk=1 and @isfwd=1)) 
begin
set @i = charindex(',',@auto) 
if (@i>0) 
begin
set @s1 = substring(@auto,1,@i-1) 
set @auto = substring(@auto,@i+1,len(@auto)-@i) 
end else 
begin
set @s1 = @auto 
set @auto=''
end
set @i = charindex(':',@auto) 
if (@i>0) 
begin
set @s2 = substring(@auto,1,@i-1) 
set @auto = substring(@auto,@i+1,len(@auto)-@i) 
end else 
begin
set @s2 = '' 
end
-- @s2 = 自动r0|当前工作执行人r1|上一步工作执行人r2|新单人r3|责任人r4｜字段名
-- begin new1biz 
if (@s1>'') 
begin
set @createnewcuid = @useruid 
if (@s2='r1' or @s2='r0') 
begin
select top 1 @createnewcuid = jb_cuid from mps_job with(nolock)  where jb_id=@jbid 
end else 
if (@s2='r2') 
begin
select top 1 @jbfatherid = jb_fjbid from mps_job  with(nolock) where jb_id=@jbid 
set @jbfatherid = isnull(@jbfatherid,@jbid) 
select top 1 @createnewcuid = jb_cuid from mps_job  with(nolock) where jb_id=@jbfatherid 
end else 
if (@s2='r3')
begin
set @sql='select top 1 @createnewcuid=ord_createcuid from '+@thistbname+' with(nolock)  where ord_id=@ordid '
exec sp_executesql @sql,N'@createnewcuid int output ,@ordid bigint',@createnewcuid output,@ordid 
end else 
if (@s2='r4')
begin
set @sql='select top 1 @createnewcuid=ord_cuid from '+@thistbname+' with(nolock)  where ord_id=@ordid '
exec sp_executesql @sql,N'@createnewcuid int output ,@ordid bigint',@createnewcuid output,@ordid 
end else 
if (@s2>'')
begin
set @sql='select top 1 @createnewcuid='+@s2+' from '+@thistbname+' with(nolock)  where ord_id=@ordid '
exec sp_executesql @sql,N'@createnewcuid int output ,@ordid bigint',@createnewcuid output,@ordid 
end
set @createnewcuid = isnull(@createnewcuid,@useruid) 
set @i = CONVERT(int,@s1) 
set @db = CONVERT(varchar(50),@ordid) 
/*
set @sql = 'order_'+@s1+N'_newbiz @cmpid,0,0,0,@useruid,@jbid,@bizid,''biz'',@sbizid,@sordids,0,0,2,'''','''','''',@newordid output '
exec sp_executesql @sql,N'@cmpid int,@useruid int, @jbid bigint, 
     @bizid int,@sbizid int,@sordids varchar(max) ,@newordid bigint output',
     @cmpid,@createnewcuid,@jbid,@i,@bizid,@db,@li output 
     */
     set @mysql = 'exec order_'+@s1+N'_newbiz '+
     convert(varchar(10),@cmpid)+',0,0,0,'+
     convert(varchar(10),@createnewcuid)+
     ','+convert(varchar(10),@jbid)+
     ','+convert(varchar(10),@i)+',''biz'''+
     ','+convert(varchar(100),@bizid)+','''+@db+''',0,0,2,'''','''','''',0 '

insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行完毕，新建业务','',@mysql)  

end 
end

-- 发送消息 
if (@isfreewrk=1 and @isfwd=1)
begin
set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_endmessage_'+CONVERT(varchar(10),@wrkid)+'_'+CONVERT(varchar(10),@wsid) ;
if (dbo.checkprocedure(@mysql)=0)
begin
/*
set @mysql = 'exec '+@mysql+' @cmpid,@ordid,@jbid '
exec sp_executesql @mysql, N'@cmpid int, @ordid bigint ,@jbid bigint ',@cmpid,@ordid,@jbid  
*/
set @mysql = 'exec '+@mysql+' '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@ordid)+
','+convert(varchar(10),@jbid) 
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行完毕，发送消息','',@mysql)  
end
end 

-- 因为转发只能一个工作步骤，而且只能是串或汇，所以，不存在还要建新的工作步骤的可能。
if (@isfwd=0) 
begin
-- 如果当前步骤是转发，并且步骤都已经执行完毕，则需要恢复到最开始的那个发起人的工作里
if (@undofwdwrk=1) 
begin
declare @fjbid1 bigint , @newuseruid1 int , @newwrkid1 int 
select @fjbid=jb_fjbid from mps_job with(nolock) where jb_id=@jbid 
select @newuseruid1=jb_cuid ,@newwrkid1=jb_wrkid , @fjbid1=jb_fjbid  from mps_job with(nolock) where jb_id=@fjbid 
/*
exec mps_new_work @cmpid ,@newuseruid1 ,@bizid,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,'转发的流程已经处理完毕，请重新处理本项工作',@ftype,@fbizid,@fordid,@ford2tag,@ford2id,
@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@newwrkid1,@fjbid1,0
*/
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行，有转发，恢复到发起者','','exec mps_new_work '+
convert(varchar(10),@cmpid )+','+
convert(varchar(10),@newuseruid1 )+','+
convert(varchar(10),@bizid)+','+
convert(varchar(10),@ordbcid)+','+
convert(varchar(10),@ordid)+','+
''''+@ordtype+''','+
''''+@ordno+''','+
''''+@ordtitle+''','+
''''+convert(varchar(19),@orddate,120)+''','+
''''+@ordcreator+''','+
''''+'转发的流程已经处理完毕，请重新处理本项工作'+''','+
''''+@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),@newwrkid1)+','+
convert(varchar(10),@fjbid1)+',0')  
end 
return 
end 

-- 如果还有自由流程没生成，则
if (@isfreewrk=0) 
begin
if (@undofreewrk=0)  -- 是自由流程，并且还有没有做的
begin
-- 什么都不要做
set @undofreewrk=@undofreewrk 
end else -- 是自由流程，并且是需要重新建单的
begin
declare @newuseruid11 int , @newwrkid11 int , @mess2 nvarchar(300) 
-- 这里要判断一下，如果当前步骤有转发，则要先建转发
if (exists (select 1 from mps_job_available with(nolock) 
where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_created=1 and jbn_jbid=@jbid and jbn_level>1)) 
begin
select @newuseruid11=jb_cuid,@fjbid=jb_fjbid , @newwrkid11=jb_wrkid , @mess2='由'+jb_executor+'转发，请协助处理' from mps_job with(nolock) where jb_id=@jbid 
/*
exec mps_new_work @cmpid ,@newuseruid11 ,@bizid,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator, @mess2,@ftype,@fbizid,@fordid,@ford2tag,@ford2id,
@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@newwrkid11,@jbid,0
*/
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行，生成转发工作1','','exec mps_new_work '+
convert(varchar(10),@cmpid )+','+
convert(varchar(10),@newuseruid11 )+','+
convert(varchar(10),@bizid)+','+
convert(varchar(10),@ordbcid)+','+
convert(varchar(10),@ordid)+','+
''''+@ordtype+''','+
''''+@ordno+''','+
''''+@ordtitle+''','+
''''+convert(varchar(19),@orddate,120)+''','+
''''+@ordcreator+''','+
''''+@mess2+''','+
''''+@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),@newwrkid11)+','+
convert(varchar(10),@jbid)+',0')  
end else
begin
select @newuseruid11=jb_cuid,@fjbid=jb_fjbid , @newwrkid11=jb_wrkid from mps_job with(nolock) where jb_id=@jbid 
/*
exec mps_new_work @cmpid ,@newuseruid11 ,@bizid,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,'',@ftype,@fbizid,@fordid,@ford2tag,@ford2id,
@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@newwrkid11,@fjbid,0
*/
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行，生成转发工作2','','exec mps_new_work '+
convert(varchar(10),@cmpid )+','+
convert(varchar(10),@newuseruid11 )+','+
convert(varchar(10),@bizid)+','+
convert(varchar(10),@ordbcid)+','+
convert(varchar(10),@ordid)+','+
''''+@ordtype+''','+
''''+@ordno+''','+
''''+@ordtitle+''','+
''''+convert(varchar(19),@orddate,120)+''','+
''''+@ordcreator+''','+
''''','+
''''+@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),@newwrkid11)+','+
convert(varchar(10),@fjbid)+',0')  
end 
end 
end 

if (@isfreewrk=0 or @isfwd=0) return 

-- 生成下一步工作 
declare @jid bigint
if (@shape='subflow') -- 转子流程
begin
declare @newflwid int 
set @newflwid = 0 
if (dbo.checkprocedure(@thistbnamenodb+'_set_subflow')=0)   -- 判断有没有子流程
begin
set @sql = 'exec '+@thistbnamenodb+'_set_subflow @ordid,@wrkid,@newflwid output ' 
exec sp_executesql @sql, N'@ordid  bigint,@wrkid int,@newflwid int output', @ordid,@ws_swrkid ,@newflwid output  
end
set @newflwid = ISNULL(@newflwid,0) 
if (@newflwid>0) -- 找到了新的子流程
begin
-- 从子流程中去寻找可以开始的第一步。
set @ws_twrkid=0
select top 1 @ws_twrkid = wrk_id from mps_flowwork  with(nolock) where wrk_flwid=@newflwid and wrk_bizid=@bizid and  wrk_shape like 'begin%'  
set @ws_twrkid=isnull(@ws_twrkid,0)   
if (@ws_twrkid=0)   
select top 1 @ws_twrkid = wrk_id from mps_flowwork  with(nolock) where wrk_flwid=@newflwid and wrk_bizid=@bizid  order by wrk_id   
set @ws_twrkid=isnull(@ws_twrkid,0)   
if (@ws_twrkid>0) -- 可以新建
begin
/*exec mps_new_work @cmpid ,@useruid,@bizid,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,'',@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@ws_twrkid,@jbid ,0*/
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行，shape=subflow,转子流程','','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+''','''','+
''''+@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),@ws_twrkid)+','+
convert(varchar(10),@jbid)+',0')
end 
end 
end else 
if (@shape='end' or @shape='job')  -- 结束
begin
-- 什么都不要做，结束即可
set @jid=0 
end else 
if (@shape='top' or @shape='back' or (@shape='backany' and @gotojbid>0) or @shape='abolish') 
begin
declare @backjb table(id int identity(1,1),jbid bigint , first int) 
if (@shape='top')  -- 回到发起人
begin
-- 在mps_job表中，找到第一条记录
select top 1 @jid=jb_id from mps_job with(nolock)  where jb_cmpid=@cmpid and jb_ordid=@ordid and jb_crmid=@bizid and jb_back=1 order by jb_id 
set @jid=isnull(@jid,0) 
insert into @backjb(jbid,first) values(@jid,0)
end else 
if (@shape='back')  -- 回到到上一步
begin
-- 有多种情况 ，即有可能有多个上一步
insert into @backjb(jbid,first) 
select jr_fjbid,0 from mps_job_relation with(nolock) ,mps_job  with(nolock) where jr_jbid=@jbid and jr_fjbid=jb_id and jb_back=1 
set @jid=isnull(@jid,0) 
end else 
if (@shape='backany' and @gotojbid>0)  -- 回到任意步
begin
set @jid=@gotojbid 
insert into @backjb(jbid,first) values(@gotojbid,0)
end else 
if (@shape='abolish')  -- 结束
begin
set @jid=@jbid  
insert into @backjb(jbid,first) values(@jbid,1)
end
-- 从jid，包括(@jid) 开始，所以后面的工作都要都搜索出来，如果没有做，就删除，如果做了，就设置回退。

select  @ic=0 
set @ic2=0 
begin
while (@ic=@ic)
begin
insert into @backjb(jbid,first) select jr_jbid,1 from mps_job_relation with(nolock)  , @backjb,mps_job  with(nolock) 
where jr_fjbid=jbid and jr_jbid=jb_id and jb_back=1 
select @ic2=count(distinct jbid) from @backjb 
if (@ic2=@ic) 
begin
break 
end else 
begin
set @ic=@ic2 
end
end
end 
-- 删除没有做的
delete mps_job from @backjb where jb_id=jbid and jb_wsid=0 and jb_wsstatus='' and jb_id<>@jbid 
-- 删除task 
delete mps_task from @backjb where tsk_cmpid=@cmpid and tsk_objtype='biz' and tsk_jbid=jbid  
-- 删除任务
delete mps_job_task from @backjb where wt_jbid=jbid and wt_type in ('job','wrkalarm') and (wt_task not like '%mps_new_work%')
delete mps_alarm_message from @backjb  where  almmsg_jbid=jbid 
-- 按先进后出原则，回退步骤及数据
declare status1 cursor LOCAL  FORWARD_ONLY READ_ONLY 
for select  jb_wrkid,jb_id from mps_job with(nolock)  , @backjb where jb_id=jbid order by jb_enddate  desc , jb_id desc 
declare @jbid3 bigint
declare @wrkid3 int 
set @operation='continue'
set @errorinfo=''
open status1
FETCH NEXT FROM status1 INTO   @wrkid3 , @jbid3 
WHILE (@@FETCH_STATUS = 0)
 BEGIN
 set @operation='continue'
 -- 执行回退sQL
 exec mps_job_afterback @cmpid,@useruid,@bizid,@ordid,@wrkid3,@jbid3,@operation output 
 if (@operation='continue') 
 begin
 -- 删除日志，同时回滚数据
 set @wrkid3=@wrkid3
 end 
 FETCH NEXT FROM status1 INTO   @wrkid3 , @jbid3 
 END
 CLOSE status1
DEALLOCATE status1 

-- 将所有的
declare @orderstatus nvarchar(30)
declare @orderkey nvarchar(30)
declare @mess nvarchar(1000) 
select @mess=case when jb_text>'' then jb_text+',' else '' end + '被'+jb_executor+'('+jb_title+')回退，时间是'+convert(varchar(16),getdate(),120) from mps_job with(nolock)  where jb_id=@jbid 
set @mess = CONVERT(nvarchar(200),@mess) 

update mps_job with(rowlock)  set jb_back=0 , jb_text = case when jb_text='' then @mess else jb_text+'。'+@mess end from @backjb where jb_id=jbid and jb_back=1 

update mps_job_available with(rowlock)  set jbn_created=1 from @backjb, mps_job with(nolock) where jbn_id=jb_jbnid and jbid=jb_id 

update mps_account_operation with(rowlock)  set accop_back=0 
from @backjb where accop_jbid=jbid  and accop_back=1 

update mps_warehouse_operation with(rowlock)  set whop_back=0 
from @backjb where whop_jbid=jbid  and whop_back=1  


delete mps_job_task from @backjb 
where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_jbid=jbid and wt_type in ('job','wrkalarm')  and (wt_task not like '%mps_new_work%') 

-- 如果是回退 就需要重新新建一步工作
if (@shape<>'abolish') 
begin
set @sql=''  
select @db=biz_database , @tbid=biz_table from   mps_biztype with(nolock) where biz_id=@bizid 
if (@tbid>0) select @db=biz_database , @tbid=biz_id from   mps_biztype with(nolock) where biz_id=@tbid 
if (@db>'') set @db=@db+'.dbo.' ;
if (@tbid=0) set @db=@db+'order_'+ltrim(rtrim(str(@bizid))) else set @db=@db+'order_'+ltrim(rtrim(str(@tbid))) 
set @sql = 'update '+@db+' with(rowlock)  set ord_status=@ws_status ,  ord_key=@ws_key  where ord_id=@ordid ' 

declare status12 cursor LOCAL  FORWARD_ONLY READ_ONLY 
for select  distinct jb_cuid,jb_wrkid,jb_id , jb_ordstatus,jb_ordkey , jb_fjbid from mps_job  with(nolock) , @backjb where jb_id=jbid  and first=0 
declare @newuseruid int 
declare @newwrkid int 
open status12
FETCH NEXT FROM status12 INTO   @newuseruid , @newwrkid , @jid,@orderstatus,@orderkey , @fjbid
WHILE (@@FETCH_STATUS = 0)
 BEGIN
 -- 这里是需要重新还原业务状态
 exec sp_executesql @sql, N'@ws_status nvarchar(30) ,@ws_key varchar(30),@ordid bigint', @orderstatus,@orderkey,@ordid 
 set @mess = REPLACE(@mess,'''','"')  
 /*
exec mps_new_work @cmpid ,@newuseruid ,@bizid,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,@mess,@ftype,@fbizid,@fordid,@ford2tag,@ford2id,
@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@newwrkid,@fjbid,0 */
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行，回退，新建要回退的工作','','exec mps_new_work '+
convert(varchar(10),@cmpid)+','+
convert(varchar(10),@newuseruid)+','+
convert(varchar(10),@bizid)+','+
convert(varchar(10),@ordbcid)+','+
convert(varchar(10),@ordid)+','+
''''+@ordtype+''','+
''''+@ordno+''','+
''''+@ordtitle+''','+
''''+convert(varchar(19),@orddate,120)+''','+
''''+@ordcreator+''','+
''''+@mess+''','+
''''+@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),@newwrkid)+','+
convert(varchar(10),@fjbid)+',0')  
FETCH NEXT FROM status12 INTO   @newuseruid , @newwrkid , @jid,@orderstatus,@orderkey , @fjbid
 END
 CLOSE status12
DEALLOCATE status12
end
end else 
begin
-- 这里要判别，能不能新建    新建下一步工作
declare @create2 int 
set @create2 = 0 
-- 首先判断，相同的工作，有没有还没有结束的，主要用于汇签
if (exists (select 1 from mps_job with(nolock)  where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and jb_wrkid=@wrkid and jb_wsid=0)) set @create2=1 
if (@create2=0)
begin
-- delete @tprevwrkids where pwrkid=@wrkid 
select @create2=count(*) from @tprevwrkids
if (@create2>=1)
begin
declare @tpw table(pwrkid2 int , jbid2 bigint) 
insert into @tpw(pwrkid2,jbid2) 
select distinct pwrkid ,0 from @tprevwrkids 
--- 看看有没有新建好
update @tpw set jbid2=jb_id 
from mps_job  with(nolock) 
where jb_cmpid=@cmpid and jb_ordid=@ordid and jb_wrkid=pwrkid2 and jb_back=1 and jb_wsid>0 
update @tpw set jbid2=0 where jbid2 is null 

update @tprevwrkids set jbid=jbid2 from @tpw where pwrkid=pwrkid2
update @tprevwrkids set jbid=0 where jbid is null 

declare @tg table(gp1 int,wrk1 int , jb1 int)
insert into @tg(gp1,wrk1,jb1)
select grp,count(1),0 from @tprevwrkids group by grp 

declare @tg2 table(gp2 int,wrk2 int , jb2 int)
insert into @tg2(gp2,wrk2,jb2)
select grp,0,count(1) from @tprevwrkids where jbid>0  group by grp 

update @tg set jb1=jb2 from @tg2 where gp1=gp2 

-- 只在存在 wrk1=jb1 则可以新建

if (exists (select * from @tg where wrk1=jb1)) set @create2=0 

end else set @create2=0 
end 

if (@create2=0) 
begin
-- 在创建之前，看看是不是已经创建了，如何已经创建了，则不需要再创建
declare @newjbid bigint   -- 只要下一步有新建了，则不再新建 
select top 1 @newjbid=jb_id from mps_job with(nolock)  where jb_cmpid=@cmpid and jb_ordid=@ordid and jb_wrkid=@ws_twrkid and jb_back=1 and jb_wsid=0   
set @newjbid=isnull(@newjbid,0) 
if (@newjbid=0) 
begin
 
exec mps_new_work @cmpid ,@useruid,@bizid,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,'',@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@ws_twrkid,@jbid ,0
/*
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('linkexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'LINK执行，创建下一步工作','','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+''','''','+
''''+@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),@ws_twrkid)+','+
convert(varchar(10),@jbid)+',0')
*/
end else
begin
if (not exists (select * from mps_job_relation with(nolock)  where jr_jbid=@newjbid and jr_fjbid=@jbid)) 
begin
if (isnull(@jbid,0)>0) insert into mps_job_relation(jr_jbid,jr_fjbid) values(@newjbid,@jbid) 
end
end
end 
end 
set nocount off
end