IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_jobinfo]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_jobinfo]
GO
 
 
CREATE procedure [dbo].[mps_g_jobinfo](
@cmpid int ,
@jbid bigint 
) as
begin
set nocount on 
declare @wrkid int 
declare @ordid bigint 
declare @bizid int
declare @bcid int
declare @dpid int
declare @useruid int
declare @otid int
declare @roleid int
declare @otlevel int
declare @managelevel int
declare @professionlevel int

select @wrkid = jb_wrkid , @ordid=jb_ordid  ,
@bizid = jb_crmid ,
@bcid = jb_bcid , 
@dpid = jb_dpid,
@useruid = jb_cuid,
@otid = cu_otid , 
@roleid = cu_function , 
@otlevel = cu_otlevel ,
@managelevel = cu_managelevel ,
@professionlevel = cu_professionlevel 
from mps_job with(nolock) ,mps_login with(nolock)  where jb_id=@jbid and jb_cuid=cu_id 

declare @wrk_keyparams varchar(1000) 
declare @wrk_flwid int

select @wrk_flwid=wrk_flwid , @wrk_keyparams=wrk_keyparams from mps_flowwork with(nolock)  where wrk_id=@wrkid 

-- 如果当前工作步骤只有一个分支，在前端提交的时候，就不用再次确认了。
-- t1
select 0 nextwrkcount , * from mps_job with(nolock)  where jb_id=@jbid and jb_cmpid=@cmpid 
declare @t table(id int identity(1,1) , wsid int , wscaption nvarchar(50) , wspickvalues nvarchar(1000) , jbid bigint) 

-- 如果禁止人工选择，
-- 生成需要调整的工作状态  wrk_keyparams nomanual 
if (charindex('%nomanual%',@wrk_keyparams)>0)   
begin
insert into @t(wsid,wscaption, wspickvalues,jbid ) values(0,'(系统自动)','',0)
end else  -- 如果有SQL,则应该全部都有SQL
if (exists (select ws_id from mps_flowworkstatus with(nolock)  where ws_swrkid=@wrkid and len(ws_condition)>5))  
begin
insert into @t(wsid,wscaption, wspickvalues,jbid ) values(0,'(系统自动)','',0)
end else -- 没有SQL并且允许人员选择
begin
insert into @t(wsid,wscaption, wspickvalues,jbid )  
select ws_id , ws_caption,ws_pickvalues,0 from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_condition='' and (ws_tshape!='top'  and ws_tshape!='back'  and ws_tshape!='backany'  and ws_tshape!='end' and ws_tshape!='abolish' and ws_tshape!='job' and substring(ws_caption,1,1)!=' ' and substring(ws_caption,1,1)!='　'  and ws_caption>'')
end

-- 如果有其它分支，则要加上来

insert into @t(wsid,wscaption, wspickvalues ,jbid)  
select ws_id , ws_caption,ws_pickvalues,0 from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_condition='' and (ws_tshape='top')
insert into @t(wsid,wscaption, wspickvalues,jbid )  
select ws_id , ws_caption,ws_pickvalues ,0 from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_condition='' and (ws_tshape='back')

if (exists (select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_condition='' and (ws_tshape='backany')))  
begin
declare @f table(fjbid bigint) 
declare @ic1 int 
declare @ic2 int 
insert into @f(fjbid) select jr_fjbid from mps_job_relation  with(nolock) where jr_jbid=@jbid 
select @ic1=count(distinct fjbid) from @f 
while (@ic1=@ic1)
begin
insert into @f(fjbid) select jr_fjbid from mps_job_relation with(nolock) ,@f where jr_jbid=fjbid 
select @ic2=count(distinct fjbid) from @f 
if (@ic2=@ic1)
begin
break  
end else 
begin
set @ic1=@ic2 
end
end
declare @wsid3 int 
declare @pck3 nvarchar(1000) 
select  @wsid3=ws_id , @pck3=ws_pickvalues  from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_condition='' and (ws_tshape='backany')
declare @ff table(jbid bigint) 
insert into @ff(jbid) select distinct  fjbid from @f 
insert into @t(wsid,wscaption, wspickvalues,jbid )  
select @wsid3 , '返回至'+jb_title+'('+jb_executor+')',@pck3,jb_id from mps_job with(nolock) ,@ff where jb_id=jbid order by jb_enddate 
end
insert into @t(wsid,wscaption, wspickvalues,jbid )  
select ws_id , ws_caption,ws_pickvalues,0 from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_condition='' and (ws_tshape='end')
insert into @t(wsid,wscaption, wspickvalues ,jbid)  
select ws_id , ws_caption,ws_pickvalues,0 from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid and ws_condition='' and (ws_tshape='abolish')

if (not exists (select * from @t)) 
begin
if (exists (select ws_id from mps_flowworkstatus  with(nolock) where ws_swrkid=@wrkid))  
begin
insert into @t(wsid,wscaption,wspickvalues,jbid) values(0,'(系统自动)','',0)
end 
end

-- t2 所有工作状态 
select * from @t order by id 

-- 返回JS事件   t3 
select a.* from mps_extsqljs a  with(nolock) 
where cd_objid=@wrkid and (cd_name='wrk.aftermodifyjs' or cd_name='wrk.beforemodifyjs' or cd_name='wrk.afterloadjs' or cd_name='wrk.beforeloadjs') 
union all 
select a.* from mps_extsqljs a  with(nolock)  , @t 
where cd_objid=wsid and (cd_name='lnk.afterapply' or cd_name='lnk.beforeapply')

 
set nocount off
end