IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_get_nextjob_info_bak]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_get_nextjob_info_bak]
GO
CREATE procedure [dbo].[mps_get_nextjob_info_bak](
@cmpid int ,
@useruid int ,
@bizid int ,
@ordid bigint,
@wrkid int,  
@jbid bigint ,
@wsid int,  -- 如果<=0说明要自动去找
@freewrkadvise varchar(50) = default 
)
as 
begin
set nocount on


-- 预判下一步工作情况及执行人等系统

-- 如果当前工作步骤，还有没有创建完的，则不用向后去寻找
if (exists (select 1 from mps_job_available with(nolock) where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and 
jbn_wrkid=@wrkid and jbn_created=1))
begin
-- t1 自由流程
select wrk_id,wrk_title , wrk_type,'allowfwd1' wrk_style from  mps_flowwork with(nolock) 
where wrk_id=@wrkid  and wrk_type=10 and wrk_keyparams like '%allowfwd%' 
-- t2 流程建议 人员清单
select 1 none1 where 0=1 
-- t3 自由程序人员选择方式
select distinct @wrkid wrkid , 'wrkfree' pptype,0 ppgroup,mp_ppid+mp_objid1 useruid , mp_caption  cu_callname , '' cu_depart 
from  mps_permission with(nolock) 
where mp_cmpid=@cmpid and mp_type='wrkfree' and mp_ctgid=@wrkid and mp_ppid=980  
return 
end 

declare @ws table(wsid int , done int) 

declare @keys varchar(300) 
declare @shape varchar(40)
declare @wrk_flwid int 
declare @wrk_type int 

declare @mysql nvarchar(MAX)  


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 @wrk_type=wrk_type,@keys=wrk_keyparams,@shape=wrk_shape ,@wrk_flwid=wrk_flwid from mps_flowwork with(nolock)  where wrk_id=@wrkid 

declare @wrk table(wrkid int , done int) 


declare @mywrk table(orderi int  identity(1, 1) , wrkid int , wrktype int , done int , counti int default 0 ) 
if (@wsid>0) 
begin
insert into @mywrk(wrkid,wrktype,done) 
select wrk_id , wrk_type ,  1 from mps_flowwork with(nolock) , mps_flowworkstatus with(nolock) 
where ws_id=@wsid and  wrk_id=ws_twrkid and wrk_keyparams not like '%auto%' and wrk_keyparams not like '%nomanual%' 
and   (wrk_shape<>'top' and wrk_shape<>'back' and wrk_shape<>'backany' and wrk_shape<>'subflow' and wrk_shape<>'job' and wrk_shape<>'end' and wrk_shape<>'abolish' and wrk_shape<>'wait')

insert into @wrk(wrkid,done)  
select wrk_id ,  1 from mps_flowwork with(nolock) , mps_flowworkstatus with(nolock) 
where ws_id=@wsid and  wrk_id=ws_twrkid  
and   (wrk_shape<>'top' and wrk_shape<>'back' and wrk_shape<>'backany' and wrk_shape<>'subflow' and wrk_shape<>'job' and wrk_shape<>'end' and wrk_shape<>'abolish' and wrk_shape<>'wait')
end 
insert into @wrk(wrkid,done) values(@wrkid,1) 
 
declare @ws_id int 
declare @ws_condition nvarchar(4000)
declare @ws_caption nvarchar(50)
declare @ws_tshape varchar(50) 
declare @ws_twrkid int 
declare @ws_swrkid int 
declare @operation varchar(50)  
set @operation = '' 
declare @balance int 
while (exists (select 1 from @wrk where done=1)) 
begin
declare status1 cursor LOCAL  FORWARD_ONLY READ_ONLY 
for select distinct  ws_id,ws_condition,ws_caption ,ws_tshape , ws_twrkid ,ws_swrkid
from mps_flowworkstatus  with(nolock)  , @wrk , mps_flowwork with(nolock) where ws_swrkid=wrkid and done=1 and len(ws_condition)>5 and wrk_id=wrkid and 
(wrkid=@wrkid or (wrkid<>@wrkid and (wrk_keyparams like '%auto%' or wrk_keyparams like '%nomanual%')))

set @operation = '' 
open status1
set @balance = 0
FETCH NEXT FROM status1 INTO   @ws_id,@ws_condition,@ws_caption,@ws_tshape,@ws_twrkid ,@ws_swrkid 
WHILE (@@FETCH_STATUS = 0)
 BEGIN
 set @balance = 1
 set @operation='continue' 
 set @ws_caption = replace(@ws_caption,'　','')
 set @ws_caption = ltrim(rtrim(@ws_caption)) 
  
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,@ws_swrkid,@jbid,@ws_id,@ws_id,@ws_caption,@operation output 
   
 if (@operation='continue') 
 begin  -- 当前的根线是要被执行的
 -- 如果这根线对应的wrkid是不需要自动执行的，就登记起来
 if (not exists (select 1 from @mywrk where wrkid=@ws_twrkid)) 
 insert into @mywrk(wrkid,wrktype,done) 
select wrk_id,wrk_type,1 from mps_flowwork with(nolock) 
where wrk_id=@ws_twrkid and wrk_keyparams not like '%auto%' and wrk_keyparams not like '%nomanual%' and  
(wrk_shape<>'top' and wrk_shape<>'back' and wrk_shape<>'backany' and wrk_shape<>'subflow' and wrk_shape<>'job' and wrk_shape<>'end' and wrk_shape<>'abolish' and wrk_shape<>'wait')

if (not exists (select 1 from @wrk where wrkid=@ws_twrkid)) 
insert into @wrk(wrkid,done) 
select wrk_id,1 from mps_flowwork with(nolock) 
where wrk_id=@ws_twrkid and  
(wrk_shape<>'top' and wrk_shape<>'back' and wrk_shape<>'backany' and wrk_shape<>'subflow' and wrk_shape<>'job' and wrk_shape<>'end' and wrk_shape<>'abolish' and wrk_shape<>'wait')
 end
 FETCH NEXT FROM status1 INTO   @ws_id,@ws_condition,@ws_caption,@ws_tshape,@ws_twrkid ,@ws_swrkid 
 update @wrk set done=0 where wrkid=@ws_swrkid 
 END
 CLOSE status1
DEALLOCATE status1 
if (@balance=0) break 
end 

 

--delete @mywrk where wrkid=@wrkid 

-- 以上是根据当前的选择状态，计算出与我当前有关的所以工作步骤

if (charindex('selectflow',@keys)>0) 
begin
-- 将需要调整的流程步骤加上来

delete @mywrk where wrkid=@wrkid 

insert into @mywrk(wrkid,wrktype,done) 
select wrk_id , wrk_type , 1 from mps_flowwork with(nolock) where wrk_flwid=@wrk_flwid and 
wrk_keyparams like '%sltexec%' and wrk_type<10 and (not exists (select  1 from @mywrk where wrkid=wrk_id))
end

-- 如果已经配置好了。则不再处理。
update @mywrk set done=0 
where (exists (select 1 from mps_job_available with(nolock) where jbn_cmpid=@cmpid and jbn_crmid=@bizid and 
jbn_ordid=@ordid and jbn_wrkid=wrkid)) 



-- 处理自由流程
-- 如果当前步骤是自由流程并且允许转发
declare @freet table(orderi int identity(1, 1) , jbnid bigint) 
if (@wrk_type=10 and CHARINDEX('allowfwd',@keys)>0) 
begin
insert into @freet(jbnid) 
select jbn_id from mps_job_available with(nolock)  
where jbn_cmpid=@cmpid and jbn_wrkid=@wrkid and jbn_ordid=@ordid  
if (not exists (select 1 from @freet)) 
begin
insert into @freet(jbnid) 
select jbn_id from mps_job_available  with(nolock)  
where jbn_cmpid=@cmpid and jbn_wrkid=@wrkid and jbn_ident=@freewrkadvise and jbn_ordid=0 
end 
-- t1 自由流程
select wrk_id,wrk_title , wrk_type,'allowfwd' wrk_style from  mps_flowwork with(nolock) where wrk_id=@wrkid  
-- t2 流程建议 人员清单
select a.* , b.* from @freet a,mps_job_available_users b with(nolock) ,mps_login c with(nolock) 
where jbnid=jbnu_jbnid and jbnu_cuid=cu_id  order by orderi  
-- t3 自由程序人员选择方式
select distinct @wrkid wrkid , 'wrkfree' pptype,0 ppgroup,mp_ppid+mp_objid1 useruid , mp_caption  cu_callname , '' cu_depart 
from  mps_permission with(nolock) 
where mp_cmpid=@cmpid and mp_type='wrkfree' and mp_ctgid=@wrkid and mp_ppid=980  
end else
begin
insert into @freet(jbnid) 
select jbn_id from mps_job_available with(nolock) , @mywrk 
where jbn_cmpid=@cmpid and jbn_wrkid=wrkid and jbn_ordid=@ordid and wrktype=10 and done=1 
if (not exists (select 1 from @freet)) 
begin
insert into @freet(jbnid) 
select jbn_id from mps_job_available  with(nolock) , @mywrk 
where jbn_cmpid=@cmpid and jbn_wrkid=wrkid and jbn_ident=@freewrkadvise and jbn_ordid=0 
end 
-- t1 自由流程
select wrk_id,wrk_title , wrk_type ,'wrkfree' wrk_style from @mywrk a , mps_flowwork with(nolock) where wrktype=10 and wrkid=wrk_id order by orderi
-- t2 流程建议 人员清单
select a.* , b.* from @freet a,mps_job_available_users b with(nolock),mps_login c with(nolock) 
where jbnid=jbnu_jbnid and jbnu_cuid=cu_id  order by orderi  

-- t3 自由程序人员选择方式
select distinct  wrkid , 'wrkfree' pptype,0 ppgroup,mp_ppid+mp_objid1 useruid , mp_caption  cu_callname , '' cu_depart 
from  mps_permission with(nolock) ,@mywrk
where mp_cmpid=@cmpid and mp_type='wrkfree' and mp_ctgid=wrkid and mp_ppid=980  and wrktype=10  

end 
  
begin
-- 返回有哪些步骤需要我们去配置人员

declare @tbname varchar(50)
set @tbname =@db 


declare @otid int
declare @roleid int
declare @otlevel int
declare @managelevel int
declare @professionlevel int
declare @branchid int  
declare @userdpid int 
select @branchid=cu_bcid,@userdpid=cu_dpid, @otid=cu_otid , @roleid=cu_function , @otlevel=cu_otlevel , @managelevel=cu_managelevel , @professionlevel=cu_professionlevel 
from mps_login with(nolock) where cu_id=@useruid 

declare @execsql nvarchar(4000) 
declare @auditsql nvarchar(4000) 
declare @coopsql nvarchar(4000) 
declare @viewsql nvarchar(4000) 
declare @logsql nvarchar(4000) 
declare @freesql nvarchar(4000)
declare @newsql nvarchar(4000) 
declare @newsql2 nvarchar(4000)

declare @myi int 
declare @extsql nvarchar(4000)  

exec drop_temp_table '#permittion' 
create table #permittion(ptype varchar(30), ctgid int , pid int , pobjid1 int , pobjid2 int) 
exec drop_temp_table '#permit' 
create table #permit(type varchar(30),cuid int) 
create table #myuser(pptype varchar(30),cuid int, grp int) 

declare @users table(wrkid int ,pptype varchar(30), ppgroup int , useruid int) 

declare @tw table(wrkid int)
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select wrk_id  from mps_flowwork with(nolock) , @mywrk where wrk_id=wrkid and wrktype<10 and done=1  
  open status1
  declare @nwrkid int 
  FETCH NEXT FROM status1 INTO @nwrkid 
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
   insert into @tw(wrkid) values(@nwrkid) 
   select top 1 @execsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@nwrkid and cd_objid2=0 and  cd_name='wrkexecutor' 
select top 1 @auditsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@nwrkid and cd_objid2=0 and  cd_name='wrkaudit' 
select top 1 @coopsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@nwrkid and cd_objid2=0 and  cd_name='wrkcoop' 
select top 1 @viewsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@nwrkid and cd_objid2=0 and  cd_name='wrkview' 
select top 1 @logsql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@nwrkid and cd_objid2=0 and  cd_name='wrklog' 
set @execsql = isnull(@execsql,'') 
set @auditsql = isnull(@auditsql,'') 
set @coopsql = isnull(@coopsql,'') 
set @viewsql = isnull(@viewsql,'') 
set @logsql = isnull(@logsql,'') 
set @freesql = isnull(@freesql,'') 

set @execsql = replace(@execsql,'#user','#permit') 
set @auditsql = replace(@auditsql,'#user','#permit') 
set @coopsql = replace(@coopsql,'#user','#permit') 
set @viewsql = replace(@viewsql,'#user','#permit') 
set @logsql = replace(@logsql,'#user','#permit') 
set @freesql = replace(@freesql,'#user','#permit') 

set @newsql = @execsql+@auditsql+@coopsql+@viewsql+@logsql+@freesql 
if (len(@newsql)>20) 
begin

set @extsql = '' 
if (CHARINDEX('#master',@newsql)>0) 
begin
set @extsql = 'exec drop_temp_table ''#master'' '+char(13)+char(10) 
set @extsql = @extsql +' select * into #master from '+@tbname+' where ord_id='+convert(varchar(10),@ordid) +char(13)+char(10) 
end 
set @myi = 1 
while (@myi<300) 
begin
 if (charindex('#detail'+convert(varchar(10),@myi),@newsql)>0) 
 begin
set @extsql = 'exec drop_temp_table ''#detail'+convert(varchar(10),@myi)+''' '+char(13)+char(10) 
set @extsql = @extsql +' select * into #detail'+convert(varchar(10),@myi)+' from '+@tbname+'_'+convert(varchar(10),@myi)+' where ord2_ordid='+convert(varchar(10),@ordid) +char(13)+char(10) 
 end
 set @myi = @myi + 1
end

delete #permittion 
delete #permit 
if (len(@execsql)>10) 
begin
set @execsql = @extsql + @execsql
exec sp_executesql @execsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @nwrkid , @jbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkexecutor' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@auditsql)>10) 
begin
set @auditsql = @extsql + @auditsql
exec sp_executesql @auditsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @nwrkid , @jbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkaudit' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@coopsql)>10) 
begin
set @coopsql = @extsql + @coopsql
exec sp_executesql @coopsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @nwrkid , @jbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkcoops' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@viewsql)>10) 
begin
set @viewsql = @extsql + @viewsql
exec sp_executesql @viewsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @nwrkid , @jbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkview' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@logsql)>10) 
begin
set @logsql = @extsql + @logsql
exec sp_executesql @logsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @nwrkid , @jbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrklog' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

END

delete #myuser
exec mps_gen_powerusers @cmpid,@branchid,@userdpid,@useruid,@otid,@roleid,@otlevel,@managelevel,@professionlevel,
'biz',@bizid,@ordid,@jbid,@nwrkid,'wrkexecutor,wrkview,wrkaudit,wrkcoops,wrklog' 


insert into @users( wrkid,pptype ,ppgroup,useruid) 
select distinct @nwrkid , pptype ,grp , cuid from #myuser 


   FETCH NEXT FROM status1 INTO @nwrkid 
  END
  CLOSE status1
DEALLOCATE status1 

exec drop_temp_table '#permittion' 
exec drop_temp_table '#permit' 
drop table #myuser 
-- t4  可以被调整的流程步骤
select  wrk_id , wrk_order , wrk_code,wrk_title,case when wrk_type=0 then 0 else wrk_executor_num end wrk_executor_num,case when (wrk_executor_num>0 or wrk_type>0) then 0 else 1 end wrk_multi  
from mps_flowwork with(nolock) ,@mywrk where wrk_id=wrkid and wrktype<10 and done=1 order by  orderi,wrk_order , wrk_id 
-- t5  步骤人员
-- 如果不是selectflw 则删除只有一个人的情况
declare @t table(wrkids int , countis int) 
insert into @t(wrkids,countis) 
select wrkid,count(*) from @users  
where pptype='wrkexecutor' 
group by wrkid 
update @mywrk set counti=countis from @t where wrkid=wrkids 

/*
if (charindex('selectflw',@keys)<=0) 
begin
 delete @users from @t where wrkids=wrkid and countis=1 
end 
*/
select wrkid,pptype ,ppgroup,useruid , cu_callname , cu_depart  from mps_login , @users a where cu_id=useruid and pptype='wrkexecutor'

-- t6  看看有没有手工选择的
select distinct wrkid , 'wrkexecutor' pptype,0 ppgroup,mp_ppid+mp_objid1 useruid , mp_caption  cu_callname , '' cu_depart 
from @mywrk , mps_permission with(nolock) 
where mp_cmpid=@cmpid and mp_type='wrkexecutor' and mp_ctgid=wrkid and mp_ppid=980 and wrktype<10 and done=1  

end  

set nocount off
end