IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_execute_job]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_execute_job]
GO
 
CREATE procedure [dbo].[mps_execute_job](
@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 ,
@wsids varchar(200) ,  -- 以前是wsid  为了支持状态名相同，WS不同的情况  就改为了字符串，中间有,
@wsstatus nvarchar(50) ,
@jbtext nvarchar(500),
@gotojbid bigint 
)
as 
begin
set nocount on
 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
-- 需要进行一个简单的逻辑判断  如果当前步骤有FWD

declare @keys varchar(300) 
declare @shape varchar(40)
declare @bm nvarchar(100) 
declare @newno varchar(50)
declare @wrk_bizrelationnvarchar(4000)
declare @mysql nvarchar(MAX)  

declare @long float 
declare @lati float 
declare @loca nvarchar(200) 

declare @jobupdated int 
set @jobupdated = 1 

declare @currentwsid int 
declare @currentstatus nvarchar(50) 

declare @abort int = 1 
-- 如果@abort=0 表示不能向后继续

declare @db varchar(100) 
declare @tbid int 
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)))

select @keys=wrk_keyparams,@shape=wrk_shape,@wrk_bizrelation=wrk_bizrelation  from mps_flowwork with(nolock)  where wrk_id=@wrkid 


if (charindex('genordno',@keys)>0 and (charindex('临',@ordno)>0 or @ordno='' or charindex('(T)',@ordno)>0)) 
begin
select @bm = biz_code from mps_biztype  with(nolock) where biz_id=@bizid 
exec mps_bm_getorset @cmpid,'biz',@bizid,@ordid,@useruid,@bm,0,@newno output,1
if (charindex('临',@newno)<=0) 
begin
update mps_job with(rowlock) set jb_ordno=@newno   
where jb_cmpid=@cmpid and jb_ordid=@ordid and jb_ordno<>@newno and jb_crmid=@bizid 
set @ordno = @newno 
end 
-- 这里，才表示业务有效
if (dbo.checkprocedure('order_'+convert(varchar(10),@bizid)+'_alarm_task')=0) 
begin
/*
set @mysql = 'exec order_'+convert(varchar(10),@bizid)+'_alarm_task @cmpid,@bizid,@ordid' 
exec sp_executesql @mysql, N'@cmpid int, @bizid int , @ordid bigint',@cmpid,@bizid,@ordid 
*/
set @mysql = 'exec order_'+convert(varchar(10),@bizid)+'_alarm_task '+convert(varchar(10),@cmpid)+
','+convert(varchar(10),@bizid)+','+convert(varchar(10),@ordid) 
--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('jobexecute',@bizid,@wrkid,0,@ordid,@jbid,'业务生效，生成业务预警','',@mysql) 
end 
-- 计算本业务单权限 
set @mysql = 'exec mps_sys_calc_bizpower_data_1 '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@bizid)+','+convert(varchar(10),@ordid)
--insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('jobexecute',@bizid,@wrkid,0,@ordid,@jbid,'计算业务单权限','',@mysql) 
end 
else 
if (charindex('begin',@shape)=1)
begin
if (ISNULL(@newno,'')>'') 
begin
set @mysql = ' update '+@db+' with(rowlock)  set ord_date=getdate(),ord_no=@newno,ord_finished=case ord_finished when 0 then 1 else ord_finished end where ord_id=@ordid ' 
exec sp_executesql @mysql, N'@newno varchar(50),@ordid bigint', @newno ,@ordid 
set @newno = ISNULL(@newno,'') 
update mps_job  with(rowlock)  set jb_ordno=@newno , jb_enddate=GETDATE() 
where jb_cmpid=@cmpid and jb_ordid=@ordid and jb_ordno<>@newno and jb_crmid=@bizid 
set @ordno = @newno 
end 

end 

if (charindex('begin',@shape)=1)
begin
-- 这里，将起用业务单自动化
exec mps_create_task_java_item_biz @cmpid , @bizid , @ordid , 0 , 0 
end 

declare @operation nvarchar(50) 
declare @errorinfo nvarchar(4000) 
set @operation = 'continue' 
set @errorinfo = '' 
declare @wsid int 
declare @wss varchar(200) 
declare @s1 varchar(20) 
declare @i int 
set @long=0
set @lati=0
set @loca = '' 
set @i = CHARINDEX('{geoposition}',@jbtext) 
if (@i>0) 
begin
set @errorinfo = SUBSTRING(@jbtext , @i + 13 , 200) 
set @jbtext = SUBSTRING(@jbtext,1,@i-1) 
set @i = CHARINDEX('|',@errorinfo) 
if (@i>0) 
begin
set @long = convert(float,SUBSTRING(@errorinfo,1,@i-1))
set @errorinfo = SUBSTRING(@errorinfo , @i + 1 , 200) 
set @i = CHARINDEX('|',@errorinfo) 
if (@i>0)
begin
set @lati = convert(float,SUBSTRING(@errorinfo,1,@i-1))
set @loca = SUBSTRING(@errorinfo , @i + 1 , 200) 
end 
end 
end else 
begin
set @loca='' 
set @long=0
set @lati=0 
end 
set @errorinfo='' 
set @wss = @wsids 
declare @linkcount  int 
set @linkcount = 0 
declare @avwsid int
declare @avwstatus nvarchar(50) 
set @avwsid = 0
set @avwstatus = @wsstatus

declare @wsidtable table(wsid int , wsstatus nvarchar(50)) 
while (@wss>'') 
begin
set @i = charindex(',',@wss)
if (@i>0)
begin
set @s1 = substring(@wss,1,@i-1) 
set @wss = substring(@wss , @i+1 , len(@wss)-@i) 
end else 
begin
set @s1 = @wss
set @wss = ''
end
set @wsid = 0 
if (@s1>'') set @wsid=convert(int,@s1) 
if (@wsid>0 and @avwsid=0) 
begin
set @avwsid = @wsid
set @avwstatus = @wsstatus 
end 
if (@wsid>0) set @linkcount = @linkcount + 1 
if (@linkcount=1)   -- 只能一次
begin
-- 工作提交前
set @operation = 'continue' 
set @errorinfo = '' 
set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wrkid)+'_wrkapply_before'
if (dbo.checkprocedure(@mysql)=0)
begin
set @mysql = 'exec '+@mysql+' @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid,@wsstatus,@operation output,@errorinfo output '
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,@wsstatus,@operation output ,@errorinfo  output 
end
if (@operation='abort')
begin
set @abort = 0 
if (@errorinfo='') set @errorinfo='error' 
select @errorinfo errorinfo
select 'error' operation, @errorinfo errorinfo
return 
end
end 

--保存工作记录
if (@linkcount=1) 
begin
update mps_job with(rowlock)  set jb_wsid=@wsid , jb_wsstatus=@wsstatus , jb_text=@jbtext, 
jb_enddate=GETDATE()  ,
jb_location=@loca ,
jb_long = @long ,
jb_lati = @lati ,
jb_ordkey = case when jb_ordkey='hidden' then '' else jb_ordkey end 
where jb_id=@jbid 
delete mps_task with(rowlock) where tsk_cmpid=@cmpid and tsk_objtype='biz' and tsk_objid=@ordid and tsk_jbid=@jbid 
delete mps_task_java with(rowlock) where tsk_objtype='biz' and  tsk_jbid=@jbid 
delete mps_alarm_message with(rowlock) where almmsg_jbid=@jbid 
set @jobupdated = 0  
end 
-- 执行提交后SQL
if (@linkcount=1)
begin
set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wrkid)+'_wrkapply_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,@avwsid,@avwstatus  
end
end 


-- 处理LINk及创建下一步工作    首先处理一下当前的WS  
  if (@wsid>0) 
  begin
insert into @wsidtable(wsid,wsstatus) values(@wsid , @wsstatus) 
 
exec mps_execute_job_link @cmpid,@useruid,@bizid,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@wrkid,@jbid,@wsid,@gotojbid
/* 
insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('jobexecute',@bizid,@wrkid,@wsid,@ordid,@jbid,'工作执行，执行LINK任务','','exec mps_execute_job_link '+convert(varchar(10),@cmpid)+
','+convert(varchar(10),@useruid)+','+convert(varchar(10),@bizid)+','+convert(varchar(10),@ordbcid)+
','+convert(varchar(10),@ordid)+','''+convert(varchar(10),@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)+
','+convert(varchar(10),@wsid)+','+convert(varchar(10),@gotojbid))  
*/
  end 
end



-- 处理一下，其它方面的link 
-- 下面的程序，要仔细考虑
update @wsidtable set wsstatus=ws_caption  from mps_flowworkstatus  with(nolock) where ws_id=wsid 
set @currentwsid = @wsid  
set @currentstatus =@wsstatus 
select top 1 @currentwsid=wsid , @currentstatus=wsstatus from @wsidtable 
set @currentwsid = ISNULL(@currentwsid,@wsid)  
set @currentstatus = ISNULL(@currentstatus,@wsstatus) 
declare @hascond int 
set @hascond = 1 
declare @zerowsid int
set @zerowsid =  1 
if (not exists (select 1 from @wsidtable)) 
begin
insert into @wsidtable(wsid,wsstatus) values(0,'') 
set @zerowsid =  0 
end
if (exists (select 1 from mps_flowworkstatus  with(nolock) 
where ws_swrkid=@wrkid and ws_tshape<>'backany'  -- and @wsid=0  我觉得是没什么用的，任何时候，都建议要执行
and len(ws_condition)>5 
)) set @hascond=0 

declare @at table(wsid9 int) 
if (@zerowsid=0) -- 是自动执行
begin
-- 把有SQL的选出来，把没有SQL，并且=job的选择出来的，把没有sql，status=''的选出来
if (@hascond=0) 
begin
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_tshape<>'backany'  and  len(ws_condition)>5  
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_tshape='job'  and  len(ws_condition)<=1  
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_caption=''  and  len(ws_condition)<=1  
end else 
begin -- 如果没有SQL，则把job的选择出来的  status=''的选出来 , 同时再选择一个不是back的出来
insert into @at(wsid9) 
select top 1 ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_caption>''  and  len(ws_condition)<=1  and ws_tshape<>'job' and 
ws_tshape<>'backany' and ws_tshape<>'back' and ws_tshape<>'top' and ws_tshape<>'abolish'
if (not exists (select 1 from @at)) 
insert into @at(wsid9) 
select top 1 ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_caption>''  and  len(ws_condition)<=1  and ws_tshape<>'job' and 
ws_tshape<>'backany' and ws_tshape<>'back' and ws_tshape<>'top'  
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_tshape='job'  and  len(ws_condition)<=1  
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_caption=''  and  len(ws_condition)<=1  
end 
end else -- 不是自动执行
begin
-- 把有SQL的选出来，把没有SQL，并且=job的选择出来的，把没有sql，status=''的选出来
if (@hascond=0) 
begin
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_tshape<>'backany'  and  len(ws_condition)>5  
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_tshape='job'  and  len(ws_condition)<=1  
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_caption=''  and  len(ws_condition)<=1  
end else  --把job的选择出来的  status=''的选出来
begin  
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_tshape='job'  and  len(ws_condition)<=1  
insert into @at(wsid9) 
select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_caption=''  and  len(ws_condition)<=1  
end 
end 
-- 

declare status1 cursor LOCAL  FORWARD_ONLY READ_ONLY 
for select distinct  ws_id,ws_condition,ws_caption ,ws_tshape from mps_flowworkstatus  with(nolock)  , @at where ws_id=wsid9 

declare @wsid2 int 
declare @ws_condition nvarchar(4000)
declare @ws_caption nvarchar(50)
declare @ws_tshape varchar(50) 
 
set @operation = '' 
 open status1
FETCH NEXT FROM status1 INTO   @wsid2,@ws_condition,@ws_caption,@ws_tshape
WHILE (@@FETCH_STATUS = 0)
 BEGIN
 set @operation='continue' 
 set @ws_caption = replace(@ws_caption,'　','')
 set @ws_caption = ltrim(rtrim(@ws_caption)) 
 if (len(@ws_condition)>5)  -- 如果有条件，不管是哪种方式调用，都是要执行的
 begin
 set @mysql = ' 
 set @operation = '''' 
 if (exists (select 1 from '+@db +'  width(nolock) '
if (charindex('wrk_',@ws_condition)>0) set @mysql=@mysql+',mps_flowwork width(nolock) ' 
if (charindex('cu_',@ws_condition)>0) set @mysql=@mysql+',mps_login width(nolock) ' 
if (charindex('jb_',@ws_condition)>0) set @mysql=@mysql+',mps_job width(nolock) ' 
 set @mysql = @mysql+' where ord_id=@ordid ' 
 if (charindex('wrk_',@ws_condition)>0) set @mysql=@mysql+' and wrk_id=@wrkid' 
 if (charindex('jb_',@ws_condition)>0) set @mysql=@mysql+' and jb_id=@jbid' 
 if (charindex('cu_',@ws_condition)>0) 
 begin
 if (charindex('cu_id',@ws_condition)<=0) 
 set @mysql=@mysql+' and cu_id=@useruid ' 
 end
 set @mysql = @mysql+' and (((('+@ws_condition+'))))'
 set @mysql = @mysql+' )) 
 begin
 set @operation = ''continue'' 
 end else 
 begin
 set @operation=''abort'' 
 end 
 '
 exec sp_executesql @mysql, N'@cmpid int,@useruid int,@bizid int,@ordid bigint,@wrkid int,@jbid bigint,@wsid2 int,@currentwsid int,@currentstatus nvarchar(50),@operation varchar(30) output ', 
 @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid2,@wsid2,@ws_caption,@operation output 
 end else 
 -- 如果没有条件，则要考虑以下几种情况:
 -- 1、所有的分支都没有条件，并且又是被自动调用，那么每根线都要运行
 -- 2、任何时候，都要执行 ws_tshape='job'的那项工作 
 -- 3、如何某一条线没有状态，任何时候都要执行
 begin
 set @operation=@operation  
 end 
 if (@operation='continue') 
 begin
 if (@wsid2>0 and @avwsid=0) 
begin
set @avwsid = @wsid2
end 
set @linkcount = @linkcount + 1 
set @operation = 'continue' 
set @errorinfo = '' 
if (@linkcount=1) -- 只能做一次
begin
set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wrkid)+'_wrkapply_before'
if (dbo.checkprocedure(@mysql)=0)
begin
set @mysql = 'exec '+@mysql+' @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid,@wsstatus,@operation output,@errorinfo output '
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,@wsid2,@wsstatus,@operation output ,@errorinfo  output 
end
end 
if (@operation='abort')
begin
set @abort = 0 
if (@errorinfo='') set @errorinfo='error' 
end



if (@linkcount=1 and @abort=1) 
begin
if (CHARINDEX('自动',@wsstatus)>=1 and @ws_caption>'')
begin
update mps_job  with(rowlock)  set jb_wsid=@wsid2 , jb_wsstatus=@ws_caption , jb_text=@jbtext, 
jb_enddate=GETDATE(),
jb_location=@loca ,
jb_long = @long ,
jb_lati = @lati ,
jb_ordkey = case when jb_ordkey='hidden' then '' else jb_ordkey end 
 where jb_id=@jbid 
delete mps_task with(rowlock) where tsk_cmpid=@cmpid and tsk_objtype='biz' and tsk_objid=@ordid and tsk_jbid=@jbid  
delete mps_task_java with(rowlock) where tsk_objtype='biz' and  tsk_jbid=@jbid 
delete mps_alarm_message with(rowlock) where almmsg_jbid=@jbid  
set @jobupdated = 0 
end 
end 

-- 执行提交后SQL
if (@abort=1 and @linkcount=1)
begin
set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wrkid)+'_wrkapply_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,@avwsid,@avwstatus  
end
end 
/*
 exec mps_execute_job_link @cmpid,@useruid,@bizid,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@wrkid,@jbid,@wsid2,0 
 */
 if (@operation='continue' and @abort=1)
 begin
 insert into mps_task_kernel(tsk_from,tsk_bizid,tsk_wrkid,tsk_wsid,tsk_ordid,tsk_jbid,tsk_reason,tsk_type,tsk_command) values('jobexecute',@bizid,@wrkid,@wsid2,@ordid,@jbid,'工作执行，执行LINK任务','','exec mps_execute_job_link '+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),@wrkid)+','+convert(varchar(10),@jbid)+
 ','+convert(varchar(10),@wsid2)+',0') 
 end 
 end
 FETCH NEXT FROM status1 INTO   @wsid2,@ws_condition,@ws_caption,@ws_tshape
 END
 CLOSE status1
DEALLOCATE status1 


if (@abort=1 and @linkcount>0)
begin
----------- 处理自动新建
-- 先处理 bizrelation 
declare @auto varchar(500)
declare @sql nvarchar(4000) 
declare @s0 nvarchar(500) 
declare @s2 varchar(100) 
set @auto=','
-- n0,r0:供货单,n0,n0,aa202,r1:供货单|n0,r4:出库单,n0,n0,n0,r0:出库单
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('aa',@s0) 
if (@i>0) set @s0 = SUBSTRING(@s0,@i,100) 
set @s1 = substring(@s0,1,2) 
set @s0 = substring(@s0,3,len(@s0)-2) 
if (@s1='aa') 
begin
set @i = charindex(',',@s0) 
if (@i>0) 
begin
set @s2=substring(@s0,@i+1,len(@s0)-@i) 
set @s0=substring(@s0,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(','+@s0+',',@auto)<=0) 
begin
set @auto=@auto+@s0+','+@s2+':' 
end 
end  
end
if (@auto=',') set @auto=''
if (len(@auto)>2) set @auto = substring(@auto , 2, len(@auto) - 1) 
--------------------------

-- 以下开始新建
-- 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 '+@db+' 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 '+@db+' 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 '+@db+' 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 @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)+
     ','''+convert(varchar(100) , @ordid)+''',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
end 

-- 发送消息  执行
if (@abort=1 and @linkcount>0)
begin
if (dbo.checkprocedure('order_'+CONVERT(varchar(10),@bizid)+'_endmessage_'+CONVERT(varchar(10),@wrkid))=0)
begin
/*
set @mysql='exec order_'+CONVERT(varchar(10),@bizid)+'_endmessage_'+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)+'_endmessage_'+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('jobexecute',@bizid,@wrkid,0,@ordid,@jbid,'工作执行完毕，发送消息','',@mysql)  
end 
end

 

--select 'ok' successinfo 
if (@jobupdated = 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_back=1 and jb_wsid=0 and jb_isauto=1)) OR 
(EXISTS (select * from mps_task_kernel  with(nolock) where tsk_bizid=@bizid and tsk_ordid=@ordid and tsk_command like 'exec mps_new_work%')) or 
(EXISTS (select * from mps_job_task  with(nolock) where wt_bizid=@bizid and wt_ordid=@ordid and wt_task like 'exec mps_new_work%')) 
)
begin
set @mysql = 'update '+@db+' with(rowlock)  set ord_finished=1 where ord_id=@ordid and ord_finished=2 '
end else
begin
set @mysql = 'update '+@db+' with(rowlock)  set ord_finished=2 where ord_id=@ordid and ord_finished=1 ' 
delete mps_alarm_message from mps_job   where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and almmsg_jbid=jb_id 
delete mps_job_available  where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid 
end 
exec sp_executesql @mysql, N'@ordid bigint', @ordid
end 
set nocount off
end