IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_job_enabled]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_job_enabled]
GO
CREATE procedure [dbo].[mps_job_enabled](
@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 
)
as
begin
set nocount on
 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
-- 修改业务单状态 
declare @wrk_statusnvarchar(20) 
declare @wrk_status_fnvarchar(20) 
declare @wrk_status_ffnvarchar(20) 
declare @wrk_status_rnvarchar(20) 
declare @wrk_keynvarchar(20) 
declare @wrk_key_fnvarchar(20) 
declare @wrk_key_ffnvarchar(20) 
declare @wrk_key_rnvarchar(20)
declare @wrk_bizrelationnvarchar(4000)
declare @wrk_keyparams varchar(300) 
declare @mysql nvarchar(1000) 
select @wrk_status=wrk_status,@wrk_status_f=wrk_status_f,@wrk_status_ff=wrk_status_ff,@wrk_status_r=wrk_status_r,
@wrk_key=wrk_key,@wrk_key_f=wrk_key_f,@wrk_key_ff=wrk_key_ff,@wrk_key_r=wrk_key_r ,@wrk_bizrelation=wrk_bizrelation ,
@wrk_keyparams=wrk_keyparams 
from  mps_flowwork with(nolock) where wrk_id=@wrkid 


declare @thistbname 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 (@thistbname>'') set @thistbname=@thistbname+'.dbo.' ;
if (@thistbid=0) set @thistbname=@thistbname+'order_'+ltrim(rtrim(str(@bizid))) 
else set @thistbname=@thistbname+'order_'+ltrim(rtrim(str(@thistbid))) 

declare @thissql nvarchar(4000)=''
   
declare @sql nvarchar(4000) 
declare @db varchar(50) 
declare @tbid int 
declare @ord2str varchar(10) 
declare @s1 varchar(100) 
declare @s2 varchar(100) 
declare @s0 nvarchar(500) 
declare @s00 nvarchar(500) 
declare @i int 
declare @li bigint 

declare @auto varchar(500)
declare @manual nvarchar(2000)

-- 先处理 bizrelation 
set @auto=','
set @manual='|'
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('na',@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='na') 
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('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) 


 

set @sql=''  
set @ord2str = '' 
if (@wrk_status_f<>'' or @wrk_key_f<>'') 
begin
set @wrk_status_f = ISNULL(@wrk_status_f,'')
set @wrk_key_f = ISNULL(@wrk_key_f,'')
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 (@wrk_status_f>'') set @sql = @sql +' set ord'+@ord2str+'_status=@wrk_status_f  '  
if (@wrk_key_f>'') 
begin
if (@sql>'') set @sql = @sql+' , ' 
   set @sql = @sql +' set ord'+@ord2str+'_key=@wrk_key_f ' ;
end
if (@sql>'') set @sql = 'update '+@db+' with(rowlock) '+@sql+' where ord'+@ord2str+'_id=' ;
   if (@ford2tag>0 and @ford2id>0)  set @sql=@sql+'@ford2id'  else set @sql=@sql+'@fordid'
exec sp_executesql @sql, N'@wrk_status_f nvarchar(50),@wrk_key_f varchar(50),@fordid bigint,@ford2id bigint', @wrk_status_f  ,@wrk_key_f, @fordid ,@ford2id
end

set @sql=''  
set @ord2str = '' 
if (@wrk_status_r<>'' or @wrk_key_r<>'') 
begin
set @wrk_status_r = ISNULL(@wrk_status_r,'')
set @wrk_key_r = ISNULL(@wrk_key_r,'')
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 (@wrk_status_r>'') set @sql = @sql +' set ord'+@ord2str+'_status=@wrk_status_r '  
if (@wrk_key_r>'') 
begin
if (@sql>'') set @sql = @sql+' , ' 
   set @sql = @sql +' set ord'+@ord2str+'_key=@wrk_key_r ' ;
end
if (@sql>'') set @sql = 'update '+@db+' with(rowlock) '+@sql+' where ord'+@ord2str+'_id=' ;
   if (@rord2tag>0 and @rord2id>0)  set @sql=@sql+'@rord2id'  else set @sql=@sql+'@rordid'
exec sp_executesql @sql, N'@wrk_status_r nvarchar(50),@wrk_key_r varchar(50),@rordid bigint,@rord2id bigint', @wrk_status_r  ,@wrk_key_r, @rordid ,@rord2id
end


-- 工作创建后 

set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wrkid)+'_wrknew_after' ;
if (dbo.checkprocedure(@mysql)=0)
begin
set @mysql = 'exec '+@mysql+' @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid '
exec sp_executesql @mysql, N'@cmpid int, @useruid int ,@bizid int ,@ordid bigint ,@wrkid int ,@jbid bigint ', 
@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid
end

-- exec mps_wrk_afternew @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid 

-- 新建之后自动新建业务 
if (@wrk_status<>'' or @wrk_key<>'') 
begin
SET @wrk_status = ISNULL(@wrk_status,'')
SET @wrk_key = ISNULL(@wrk_key,'')
set @sql = 'update '+@thistbname+' with(rowlock) set ord_createbizids=@manual  ' 
if (@auto>'') set @sql = @sql+', ord_finished=1  '
if (@wrk_status>'') set @sql = @sql +' , ord_status=@wrk_status ' ;
if (@wrk_key>'') set @sql = @sql +' , ord_key=@wrk_key  ' ;

set @sql = @sql + ' where ord_id=@ordid '
exec sp_executesql @sql, N'@wrk_status nvarchar(50),@wrk_key varchar(50),@ordid bigint, @manual nvarchar(200)', @wrk_status  ,@wrk_key, @ordid,@manual  
end else 
begin
set @sql=N'update '+@thistbname+' with(rowlock)  set ord_createbizids=@manual'
if (@auto>'') set @sql = @sql+', ord_finished=1  '
set @sql = @sql+'  where ord_id=@ordid  ' 
exec sp_executesql @sql,N'@ordid bigint,@manual nvarchar(200)',@ordid,@manual 
end 
-- 以下开始新建
-- 11,r1:
declare @createnewcuid int 
declare @jbfatherid bigint
while (@auto>'') 
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(100) , @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 @sql = '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(10),@bizid)+
     ','+@db+',0,0,2,''autocreate_sys'','''','''',@newordid output ' 
     insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('jobenabled',@bizid,@wrkid,0,@ordid,@jbid,'工作步骤启动后，创建新业务','newbiz',@sql) 
end 
end

   -- 如果是自动执行
   declare @auto1 int 
   declare @jbcuname nvarchar(100) 
   declare @jbcuid1 int 
   select @auto1=jb_isauto , @jbcuname=jb_executor , @jbcuid1=jb_cuid  from mps_job with(nolock)  where jb_id=@jbid 
   set @jbcuname = ISNULL(@jbcuname,'')
   update mps_job  with(rowlock) set jb_enabled=0 where jb_id=@jbid and jb_enabled=1 
   
   declare @newwsid int 
declare @newstatus nvarchar(50) 
declare @jbtext nvarchar(100) 
set @jbtext = '' 
set @newstatus = '' 
set @newwsid = 0

declare @sendmsg int 
set @sendmsg = 0
   if (@auto1=0) 
   begin  
-- 如果有SQL
if (exists (select 1 from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and LEN(ws_condition)>10)) 
begin
set @newstatus = '(系统自动)'
set @newwsid = 0
set @jbtext = '系统自动执行' 
end else -- 如果只有一个
if ((select COUNT(1) from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid)=1)
begin
select @newwsid=ws_id,@newstatus=ws_status from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid 
set @newwsid = ISNULL(@newwsid,0) 
set @newstatus = ISNULL(@newstatus,'') 
set @jbtext = '由系统自动执行，' +@newstatus 
end else
begin
if ((select COUNT(1) from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and (ws_tshape<>'abolish' and ws_tshape<>'back'
and ws_tshape<>'backany' and ws_tshape<>'top' and ws_tshape<>'job'))=1)
begin
select top 1 @newwsid = ws_id , @newstatus=ws_caption from mps_flowworkstatus  with(nolock) 
where ws_swrkid=@wrkid and (ws_tshape<>'abolish' and ws_tshape<>'back'
and ws_tshape<>'backany' and ws_tshape<>'top' and ws_tshape<>'job') 
set @newwsid = ISNULL(@newwsid,0) 
set @newstatus = ISNULL(@newstatus,'') 
if (@newstatus='') set @newstatus = '(系统自动)'
if (@newwsid=0) set @newwsid = 0
set @jbtext = '系统自动执行1' 
end else
begin
set @newstatus = '(系统自动)'
set @newwsid = 0
set @jbtext = '系统自动执行2' 
end 
end 
   end else -- 不是自动
   begin   -- 将人员关系写入 order_1_users 中
-- 增加判断 ， 是否nodup 
set @sql = ' insert into '+@thistbname+'_users(ordp_type,ordp_cuid,ordp_ordid,ordp_otid) '+
'select distinct ''bizview'', jp_cuid, jb_ordid , -1 from mps_job_users with(nolock) ,mps_job with(nolock)  '+
'where jb_id=@jbid and jp_jbid=jb_id and '+
'(not exists ( select 1 from '+@thistbname+'_users with(nolock)  where ordp_type=''bizview'' and ordp_cuid=jp_cuid and ordp_ordid=jb_ordid)) '
exec sp_executesql @sql,N'@jbid bigint',@jbid 

--- 判断可否自动执行
if (CHARINDEX('nodup',@wrk_keyparams)=0 and 
 (exists 
  (select 1 from mps_job  with(nolock) where 
jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and 
jb_cuid=@jbcuid1 and jb_isauto=1 and jb_back=1 and jb_wsid>0 and 
jb_wrkid<>@wrkid 
  )
 )
) 
begin
-- 
-- 找到，下一步执行的状态，对于这种情况 ，如何有SQL，则自动执行，如果没有SQL，则找一个不是回退的执行
if (exists (select 1 from mps_flowworkstatus with(nolock)  where ws_swrkid=@wrkid and LEN(ws_condition)>10  and ws_tshape<>'job')) 
begin
set @newstatus = '(系统自动)'
set @newwsid = 0
set @jbtext = '在此之前，'+@jbcuname+'已经在本业务处理过相关事项了，本次工作已忽略(auto)' 
set @sendmsg = 1
end else 
begin
-- 找到一个可以执行的 
--- 要优化一下寻找的逻辑
-- 如果只有一个，则必须执行
if ((select COUNT(1) from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_tshape<>'job')=1) 
begin
select top 1 @newwsid = ws_id , @newstatus=ws_caption from mps_flowworkstatus with(nolock)  where ws_swrkid=@wrkid 
set @jbtext = '在此之前，'+@jbcuname+'已经在本业务处理过相关事项了，本次工作已忽略' 
set @sendmsg = 1
end else 
begin

select top 1 @newwsid = ws_id , @newstatus=ws_caption from mps_flowworkstatus  with(nolock) 
where ws_swrkid=@wrkid and (ws_tshape<>'abolish' and ws_tshape<>'back'
and ws_tshape<>'backany' and ws_tshape<>'top' and ws_tshape<>'job') 
set @newwsid = ISNULL(@newwsid,0) 
set @newstatus = ISNULL(@newstatus,'') 
set @jbtext = '在此之前，'+@jbcuname+'已经在本业务处理过相关事项了，本次工作已忽略' 
set @sendmsg = 1

end 
end 
end 
if (@newwsid=0 and @newstatus='' and dbo.checkprocedure('order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@wrkid)+'_alarm_task')=0) 
begin
/*
set @mysql = 'exec order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@wrkid)+'_alarm_task @cmpid,@bizid,@wrkid,@ordid,@jbid ' 
exec sp_executesql @mysql, N'@cmpid int, @bizid int , @wrkid int,@ordid bigint,@jbid bigint',@cmpid,@bizid,@wrkid,@ordid,@jbid 
*/
set @mysql = 'exec order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@wrkid)+
'_alarm_task '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@bizid)+
','+convert(varchar(10),@wrkid)+','+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('jobenabled',@bizid,@wrkid,0,@ordid,@jbid,'工作步骤启动后，生成预警数据','',@mysql) 
end 
   end  
   
   -- 在这里发消息 
   declare @jbback int 
   if (@sendmsg = 0) 
   begin
-- 根据配置程序，发送消息
select @jbback=0 from mps_job with(nolock)  where jb_id=@jbid and jb_backinfo like '%被%' and jb_backinfo like '%回退%' 
set @jbback = ISNULL(@jbback,1) 
if (@jbback=1) 
begin
if (dbo.checkprocedure('order_'+CONVERT(varchar(10),@bizid)+'_startmessage_'+CONVERT(varchar(10),@wrkid))=0)
begin
/*
set @mysql='exec order_'+CONVERT(varchar(10),@bizid)+'_startmessage_'+CONVERT(varchar(10),@wrkid)+' @cmpid,@ordid,@jbid '
exec sp_executesql @mysql, N'@cmpid int, @ordid bigint ,@jbid bigint ', 
@cmpid,@ordid,@jbid
*/
set @mysql='exec order_'+CONVERT(varchar(10),@bizid)+'_startmessage_'+CONVERT(varchar(10),@wrkid)+
' '+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('jobenabled',@bizid,@wrkid,0,@ordid,@jbid,'工作步骤启动后，发送消息','',@mysql) 
end 
end else
begin
if (dbo.checkprocedure('order_'+CONVERT(varchar(10),@bizid)+'_backmessage_'+CONVERT(varchar(10),@wrkid))=0)
begin
/*
set @mysql='exec order_'+CONVERT(varchar(10),@bizid)+'_backmessage_'+CONVERT(varchar(10),@wrkid)+' @cmpid,@ordid,@jbid '
exec sp_executesql @mysql, N'@cmpid int, @ordid bigint ,@jbid bigint ', 
@cmpid,@ordid,@jbid
*/
set @mysql='exec order_'+CONVERT(varchar(10),@bizid)+'_backmessage_'+CONVERT(varchar(10),@wrkid)+
' '+CONVERT(varchar(10),@cmpid)+','+CONVERT(varchar(10),@ordid)+','+CONVERT(varchar(10),@jbid) 
-- insert into mps_task_kernel(tsk_type,tsk_command) values('',@mysql)  
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('jobenabled',@bizid,@wrkid,0,@ordid,@jbid,'工作步骤被回退，发送回退消息','',@mysql) 
end
end 
   end 
   
   -- 开始自动执行
   if (@newwsid>0 or @newstatus>'') 
   begin
set @s1 = CONVERT(varchar(10),@newwsid) 
/*
exec mps_execute_job @cmpid,@jbcuid1, @bizid,@ordbcid,@ordid, @ordtype,@ordno,@ordtitle,@orddate,@ordcreator,
   @ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@wrkid,@jbid,@s1 ,@newstatus ,@jbtext ,0
   */
   set @mysql='exec mps_execute_job '+
   CONVERT(varchar(10),@cmpid)+','+
   CONVERT(varchar(10),@jbcuid1)+', '+
   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),@wrkid)+   ','+
   CONVERT(varchar(10),@jbid)+','''+@s1+''' ,'''+@newstatus+''' ,'''+replace(@jbtext,'''','"')+''' ,0 '
   --insert into mps_task_kernel(tsk_type,tsk_command) values('',@mysql)  
   insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('jobenabled',@bizid,@wrkid,0,@ordid,@jbid,'工作步骤启动后，自动执行当前工作','',@mysql) 
   end else
   begin
     -- 开启流程步骤自动化
     exec mps_create_task_java_item_biz @cmpid , @bizid , @ordid , @wrkid , @jbid 
   end 
   
   -- 如果本项工作没有后面的link，则结束
   if ( not exists (select 1 from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid)) 
   begin
update mps_job with(rowlock)  set jb_wsid=1 , jb_wsstatus='完成' 
where jb_id=@jbid and jb_wsid=0 
   end 
set nocount off
end