IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_do_execute_job]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_do_execute_job]
GO
CREATE procedure [dbo].[mps_do_execute_job](
@cmpid int ,
@branchid int ,
@userdpid int,
@useruid int ,
@userotid 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 ,
@freecuids varchar(2000),  -- 如果是自由流程，这里将是所有的ID  2,3|4,5|5,6
-- 如果是预流程人员 200:2,3|100:3:4|120:1 
@freeident varchar(50)  -- 当前条件下，可以标准化的自由流程码
)
as 
begin
set nocount on
if (not exists (select 1 from mps_job with(nolock) where jb_id=@jbid and 
jb_wsid=0 )) 
begin
select 0  unconfirm
return 
end 
declare @nwrkid int 
declare @freeratio int 
set @freeratio = 1 
-- 确定一下，当前工作是不是从alaible中生成来的
declare @oldjbnid bigint 
declare @level int
select top 1 @oldjbnid = jb_jbnid from mps_job with(nolock) where jb_id=@jbid 
set @oldjbnid = ISNULL(@oldjbnid,0) 
if (@oldjbnid>0) 
begin
select top 1 @level = jbn_level from mps_job_available with(nolock) where jbn_id=@oldjbnid 
end  

set @level = ISNULL(@level,0) 
set @level = @level + 1 
if (@freecuids='_none_') set @freecuids='' 
if (@freecuids>'') 
begin
/*
delete mps_job_available where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_wrkid=@wrkid 
if (@freeident>'') 
begin
delete mps_job_available where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=0 and jbn_wrkid=@wrkid and jbn_ident=@freeident 
end  
*/
declare @s1 varchar(2000) 
declare @s2 varchar(500) 
declare @s3 varchar(500) 
declare @i int 
declare @jbnid bigint 
declare @jbnid2 bigint 
set @jbnid= 0 
set @jbnid2= 0 
set @s1 = @freecuids 
while (@s1>'') 
begin
set @i = CHARINDEX('|',@s1) 
if (@i>0) 
begin
set @s2 = SUBSTRING(@s1,1,@i-1) 
set @s1 = SUBSTRING(@s1,@i+1,len(@s1)-@i) 
end else
begin
set @s2 = @s1 
set @s1 = '' 
end 
if (@s2>'') 
begin
set @i = CHARINDEX(':',@s2) 
if (@i>0) 
begin
set @s3 = SUBSTRING(@s2,1,@i-1) 
set @s2 = SUBSTRING(@s2,@i+1,LEN(@s2)-@i) 
if (@s3>'') 
begin
set @nwrkid = CONVERT(int,@s3)  
set @freeratio = 1  
end else 
begin
set @nwrkid = @wrkid  
set @freeratio = 1 
end 
end else 
begin
set @s3='' 
set @nwrkid = @wrkid  
set @freeratio = 1 
end 
insert into mps_job_available(jbn_cmpid,jbn_crmid,jbn_ordid,jbn_wrkid,jbn_ident,jbn_level,jbn_jbid) 
values(@cmpid,@bizid,@ordid,@nwrkid * @freeratio,'',@level,@jbid) 
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=@nwrkid * @freeratio  
order by jbn_id desc 
if (@freeident>'') 
begin
insert into mps_job_available(jbn_cmpid,jbn_crmid,jbn_ordid,jbn_wrkid,jbn_ident) 
values(@cmpid,@bizid,0,@nwrkid * @freeratio,@freeident) 
select top 1 @jbnid2=jbn_id from mps_job_available with(nolock) where 
jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=0 and jbn_wrkid=@nwrkid * @freeratio and jbn_ident=@freeident 
order by jbn_id desc 
end 
end 
while (@s2>'') 
begin
set @i = CHARINDEX(',',@s2) 
if (@i>0) 
begin
set @s3 = SUBSTRING(@s2,1,@i-1) 
set @s2 = SUBSTRING(@s2,@i+1,len(@s2)-@i) 
end else
begin
set @s3 = @s2 
set @s2 = '' 
end   
if (@s3>'') 
begin
insert into mps_job_available_users(jbnu_jbnid,jbnu_cuid,jbnu_bcid,jbnu_branch,jbnu_dpid,jbnu_depart,jbnu_name) 
select top 1 @jbnid,cu_id,cu_bcid ,cu_branch,cu_dpid ,cu_depart , cu_callname from mps_login  with(nolock) 
where cu_type=0 and cu_cmpid=@cmpid and cu_id=convert(int,@s3) 
if (@freeident>'') 
begin
insert into mps_job_available_users(jbnu_jbnid,jbnu_cuid,jbnu_bcid,jbnu_branch,jbnu_dpid,jbnu_depart,jbnu_name) 
select top 1 @jbnid2,cu_id,cu_bcid ,cu_branch,cu_dpid ,cu_depart , cu_callname from mps_login  with(nolock) 
where cu_type=0 and cu_cmpid=@cmpid and cu_id=convert(int,@s3) 
end 
end 
end 
end 
     end 
     
if (dbo.checkprocedure('order_'+CONVERT(varchar(10),@bizid)+'_check')=0) 
begin
declare @sql nvarchar(4000) 
declare @operation varchar(50)  
declare @errorinfo nvarchar(4000)  
set @operation='continue' 
set @errorinfo = '' 
set @sql = N' exec order_'+CONVERT(varchar(10),@bizid)+
'_check @cmpid,@branchid,@userdpid,@useruid,@userotid,@jbid,@ordid,@operation output , @errorinfo output ' 
exec sp_executesql @sql, N'@cmpid int, @branchid int,@userdpid int,@useruid int,@userotid int,@jbid bigint,@ordid bigint,@operation varchar(50) output , @errorinfo nvarchar(4000) output', 
@cmpid,@branchid,@userdpid,@useruid,@userotid,@jbid,@ordid,@operation output , @errorinfo output  
if (@operation='abort') 
begin
if (@errorinfo='') set @errorinfo='数据验证失败'
select 'abort' operation , @errorinfo errorinfo 
return 
end 
end 

exec mps_execute_job @cmpid   ,
@useruid   ,
@bizid   ,
@ordbcid,
@ordid  ,
@ordtype ,
@ordno ,
@ordtitle ,
@orddate  ,
@ordcreator ,
@ftype ,
@fbizid,
@fordid,
@ford2tag,
@ford2id ,
@rtype ,
@rbizid,
@rordid ,
@rord2tag,
@rord2id ,
@wrkid,
@jbid ,
@wsids ,
@wsstatus ,
@jbtext ,
@gotojbid

-- 提取需要确认的人员清单 

/*
-- 检查有没有需要我确认的内容
declare @t table(jbid bigint , jbtitle nvarchar(100)) 
insert into @t(jbid,jbtitle) 
select jb_id,jb_title from mps_job  with(nolock) 
where jb_cmpid=@cmpid and jb_ordid=@ordid  and jb_crmid=@bizid 
and jb_back=1 and jb_isauto=1 and jb_confirm=1 and jb_wsid=0 

declare @me table(myjbid bigint) 
insert into @me(myjbid) 
select jb_id from mps_job  with(nolock) 
where jb_cmpid=@cmpid and jb_ordid=@ordid  and jb_crmid=@bizid 
and jb_back=1 and jb_isauto=1  and jb_cuid=@useruid 

-- t1
select COUNT(1)  unconfirm
from @t,mps_job_users  with(nolock) , @me 
where jbid=jp_jbid and  (jp_type='wrkexecutor' or jp_type='wrkcoops')  and jp_sjbid=myjbid 
*/
-- t1 
select 0   unconfirm 

set nocount off
end