IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_do_execute_job_auto]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_do_execute_job_auto]
GO
CREATE procedure [dbo].[mps_do_execute_job_auto](
@cmpid int ,
@branchid int ,
@userdpid int,
@useruid int ,
@userotid int , 
@bizid int ,
@ordid bigint ,
@wrkid int
)
as 
begin
set nocount on
 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
if (@bizid=0 or @ordid=0) return 

declare @ordbcid int 
declare @ordtype nvarchar(50) 
declare @ordno varchar(50) 
declare @ordtitle nvarchar(100) 
declare @orddate datetime  
declare @ordcreator nvarchar(50) 

if (@wrkid=0) 
begin
select top 1 @wrkid = jb_wrkid from mps_job with(nolock) 
where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and 
jb_wsid=0 and   jb_back=1 and jb_confirm=0 and jb_enabled=0 
end 

if (@wrkid=0) return 

declare @jbid bigint 
select top 1 @jbid=jb_id ,@ordbcid=jb_bcid , @ordtype=jb_ordtype , @ordno=jb_ordno , @ordtitle=jb_ordtitle , @ordcreator=jb_ordcreator,@orddate=jb_orddate 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_back=1 and jb_confirm=0 and jb_enabled=0 

set @jbid = ISNULL(@jbid,0) 
if (@jbid<=0) return 

declare @wsid int 
declare @status nvarchar(50) 
set @wsid = 0 
select top 1 @wsid=ws_id , @status=ws_caption  
from mps_flowworkstatus 
where ws_swrkid=@wrkid and (ws_tshape!='top' or ws_tshape!='abolish' or ws_tshape!='back' 
or ws_tshape!='end' or ws_tshape!='job' or ws_tshape!='subflow' or ws_tshape!='backany') and 
ws_sql='' 

if (@wsid=0) 
select top 1 @wsid=ws_id , @status=ws_caption 
from mps_flowworkstatus 
where ws_swrkid=@wrkid and (ws_tshape!='top' or ws_tshape!='abolish' or ws_tshape!='back' 
or  ws_tshape!='job' or ws_tshape!='subflow' or ws_tshape!='backany') and 
ws_sql='' 

if (@wsid=0) 
select top 1 @wsid=ws_id , @status=ws_caption 
from mps_flowworkstatus 
where ws_swrkid=@wrkid and (ws_tshape!='top' or  ws_tshape!='back' 
or  ws_tshape!='job' or ws_tshape!='subflow' or ws_tshape!='backany') and 
ws_sql='' 

if (@wsid=0) return 

declare @wsids varchar(10)
set @wsids = CONVERT(varchar(10),@wsid) 

exec mps_do_execute_job 
@cmpid  ,
@branchid  ,
@userdpid ,
@useruid  ,
@userotid  , 
@bizid  ,
@ordbcid ,
@ordid ,
@ordtype ,
@ordno ,
@ordtitle ,
@orddate  ,
@ordcreator ,
'' ,
0 ,
0 ,
0 ,
0,
'',
0,
0,
0 ,
0,
@wrkid,
@jbid,
'0' , --@wsids  , 
'(系统自动)',--@status,
'自动提交',
0 ,
'',
'' 
 
set nocount off
end