IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_get_nextjob_info]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_get_nextjob_info]
GO
 
 
CREATE procedure [dbo].[mps_get_nextjob_info](
@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 @wrkuser table(wrkid int , useruid int) 
insert into @wrkuser(wrkid,useruid) 
select distinct jb_wrkid ,jb_cuid from mps_job with(nolock) where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and 
jb_isauto=1 and jb_back=1  
declare @nwrkid int , @nwsid int , @nwrkid2 int , @nuseruid int  , @swrkid int 
declare @wrks table(orderi int  identity(1, 1) ,wrkid int , wsid int , done int , autoi2 int ) 
declare @wrkneedusers table(orderi int  identity(1, 1) ,swrkid int,wrkid int ,wrktype int,params varchar(500),usercount int , user1 int , wrklevel int , autoi int ) 
-- wrklevel 表示当前步骤是第几层，原则是，只需要显示第一层的工作，除非某层是自动执行或忽略 
-- autoi =0 表示是自动执行、不需要人工干涉、人相同时跳 

-- 这个临时表用于存放：哪些步骤是需要人来做，并且备选人个数，如果只有一个人，则将这个人的ID写上去
if (@wsid>0) 
begin
insert into @wrks(wrkid,wsid,done,autoi2) 
select @wrkid , @wsid ,0 , 1 
-- 将下一步工作加进来 
set @nwrkid=0 
select top 1 @nwrkid=ws_twrkid  from mps_flowworkstatus with(nolock) where ws_id=@wsid and 
ws_tshape<>'job' and ws_tshape<>'top' and ws_tshape<>'back' and ws_tshape<>'backany' and 
ws_tshape<>'subflow' and ws_tshape<>'end' and  ws_tshape<>'abolish' and ws_tshape<>'wait'
if (@nwrkid>0) 
begin
-- 判断一下,当前工作步骤是否是自动执行的
set @nwrkid2=0 
select @nwrkid2=wrk_id from mps_flowwork with(nolock) 
where wrk_id=@nwrkid and  wrk_keyparams not like '%auto%' and wrk_keyparams not like '%nomanual%' 

if (@nwrkid2>0)  -- 说明这一步是需要人为操作。因此要将这一步加入待选人员操作表
begin
insert into @wrkneedusers(swrkid,wrkid,wrktype,params,usercount,user1,wrklevel,autoi) 
select @wrkid,wrk_id ,wrk_type, wrk_keyparams,-1 , 0 , 0 , 1 from mps_flowwork with(nolock) 
where wrk_id=@nwrkid   -- and wrk_type<10 
end 
-- 将@nwrkid 
if (@nwrkid2>0) -- 如果不是自动执行
begin
-- 将有条件的加进来
insert into @wrks(wrkid,wsid,done,autoi2) 
select @nwrkid2 , ws_id ,1 , 1 
from mps_flowworkstatus WITH(NOLOCK) 
where ws_swrkid=@nwrkid2 and (len(ws_condition)>5 or  ws_caption='') and 
ws_tshape<>'job' and ws_tshape<>'top' and ws_tshape<>'back' and ws_tshape<>'backany' and 
ws_tshape<>'subflow' and ws_tshape<>'end' and  ws_tshape<>'abolish' and ws_tshape<>'wait'  
end else
begin
-- 将所有的状态加进来
insert into @wrks(wrkid,wsid,done,autoi2) 
select @nwrkid , ws_id ,1 , 0 
from mps_flowworkstatus WITH(NOLOCK) 
where ws_swrkid=@nwrkid and  
ws_tshape<>'job' and ws_tshape<>'top' and ws_tshape<>'back' and ws_tshape<>'backany' and 
ws_tshape<>'subflow' and ws_tshape<>'end' and  ws_tshape<>'abolish' and ws_tshape<>'wait'  
end 
end 
end 
-- 将当前步骤中，其它线上的，可以被执行的，也加上来
insert into @wrks(wrkid,wsid,done,autoi2) 
select @wrkid , ws_id ,1 , 1 
from mps_flowworkstatus WITH(NOLOCK) 
where ws_id<>@wsid and ws_swrkid=@wrkid and (len(ws_condition)>5 or  ws_caption='') and 
ws_tshape<>'job' and ws_tshape<>'top' and ws_tshape<>'back' and ws_tshape<>'backany' and 
ws_tshape<>'subflow' and ws_tshape<>'end' and  ws_tshape<>'abolish' and ws_tshape<>'wait'  

    
   
    
    -- 计算出当前业务单，表名
    declare @db varchar(100) 
select @db=biz_database  from mps_biztype with(nolock) where biz_id=@bizid 
if (@db>'') set @db=@db+'.dbo.' 
set @db=@db+'order_'+ltrim(rtrim(str(@bizid)))  


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 @mysql nvarchar(max) 
declare @operation varchar(50)  
set @operation = '' 

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)  

declare @thiswrkkeyparam varchar(500) 
declare @thiswrktype int 
declare @thiswrkflwid int 
select @thiswrkflwid=wrk_flwid, @thiswrkkeyparam = wrk_keyparams , @thiswrktype=wrk_type   from mps_flowwork with(nolock) where wrk_id=@wrkid 

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 @tuser table(wrkid1 int,useruid1 int, usercount1 int) 
declare @param varchar(500)
   while (1=1) 
   begin
if (not exists (select 1 from @wrks where done=1 )) 
begin
if (charindex('selectflow',@thiswrkkeyparam)>0) 
begin
-- 将需要调整的流程步骤加上来
insert into @wrkneedusers(swrkid,wrkid,wrktype,params,usercount,user1) 
select @wrkid,wrk_id ,wrk_type,wrk_keyparams,-1 , 0 from mps_flowwork with(nolock) 
where wrk_flwid=@thiswrkflwid and 
wrk_keyparams like '%sltexec%' and (not exists (select  1 from @wrkneedusers where wrkid=wrk_id))
end
end 
-- 如果已经有预设好的人员，则不需要再计算了
delete @tuser
insert into @tuser(wrkid1,useruid1,usercount1)
select wrkid, MIN(jbnu_cuid) , COUNT(1) 
from mps_job_available with(nolock) , mps_job_available_users with(nolock), @wrkneedusers 
where  jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_wrkid=wrkid and 
wrktype<10 and jbn_id=jbnu_id and usercount=-1 
group by wrkid 
update @wrkneedusers set usercount=usercount1 ,
user1 = case when usercount1=1 then useruid1 else 0 end 
from @tuser 
where wrkid=wrkid1 

-- 将@wrkneedusers表中的步骤，人员计算出来
-----  在计算过程中，如果发现只有一个人，并且在前面的步骤中已经出现了，则需要再向后计算
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select wrkid , params from  @wrkneedusers where    usercount=-1 and wrktype<10 -- 自由流程是不需要计算的
  open status1
FETCH NEXT FROM status1 INTO @nwrkid , @param
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
   
   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 '+@db+' 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 '+@db+'_'+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 

set @nwrkid2 = 0 
set @nuseruid = 0 
select @nwrkid2=COUNT(*) , @nuseruid=MIN(useruid) from @users where wrkid=@nwrkid and pptype='wrkexecutor'
set @nuseruid = ISNULL(@nuseruid , 0) 
update @wrkneedusers set usercount=@nwrkid2 , user1=@nuseruid where wrkid=@nwrkid 

-- 如果人有一个人  将这个人加入@wrkuser
if (@nwrkid2=1)
begin
insert into @wrkuser(wrkid,useruid) select @nwrkid , @nuseruid 
end 
-- 如果人员存在，则将继续找
if (@nwrkid2=1 and CHARINDEX('nodup',@param)<=0 and (exists (select 1 from @wrkuser where wrkid<>@nwrkid and useruid=@nuseruid))) 
begin
insert into @wrks(wrkid,wsid,done) 
select @nwrkid , ws_id ,1
from mps_flowworkstatus WITH(NOLOCK) 
where ws_swrkid=@nwrkid and  
ws_tshape<>'job' and ws_tshape<>'top' and ws_tshape<>'back' and ws_tshape<>'backany' and 
ws_tshape<>'subflow' and ws_tshape<>'end' and  ws_tshape<>'abolish' and ws_tshape<>'wait'  
end 

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

-- 以上是计算出所有的人来


set @nwrkid = 0 
set @nwsid = 0 
select top 1 @nwrkid=wrkid , @nwsid=wsid ,
@ws_caption = ws_caption , @ws_condition=ws_condition 
from @wrks , mps_flowworkstatus with(nolock) where done=1  and ws_id=wsid 
order by orderi 

if (@nwrkid=0) break 

update @wrks set done=0 where wrkid=@nwrkid  and wsid=@nwsid

-- 以下程序，u判断这根线，是不是能运行

set @operation='continue' 
set @ws_caption = replace(@ws_caption,'　','')
set @ws_caption = ltrim(rtrim(@ws_caption)) 
 
set @mysql = ' 
set @operation = '''' '+CHAR(13)+CHAR(10)
if (LEN(@ws_condition)>5) 
begin
set @mysql = @mysql+' 
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

if (@ws_condition>'') 
set @mysql = @mysql+' and (((('+@ws_condition+'))))'
set @mysql = @mysql+' )) '
end else
begin
set @mysql = @mysql+'
if (1=1) '
end 
set @mysql = @mysql+ ' 
begin
set @operation = ''continue'' 
end else 
begin
set @operation=''abort'' 
end 
'
print @mysql 
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,@nwrkid,@jbid,@nwsid,@nwsid,@ws_caption,@operation output 
  

if (@operation='continue') 
begin  -- 当前的根线是要被执行的
-- 如果这根线对应的wrkid是不需要自动执行的，就登记起来
-- 将下一步工作加进来 
set @nwrkid2=@nwrkid 
set @nwrkid = 0  
select top 1 @nwrkid=ws_twrkid  , @swrkid=ws_swrkid from mps_flowworkstatus with(nolock) where ws_id=@nwsid and 
ws_tshape<>'job' and ws_tshape<>'top' and ws_tshape<>'back' and ws_tshape<>'backany' and 
ws_tshape<>'subflow' and ws_tshape<>'end' and  ws_tshape<>'abolish' and ws_tshape<>'wait'
if (@nwrkid>0) 
begin
-- 判断一下,当前工作步骤是否是自动执行的
set @nwrkid2=0 
select @nwrkid2=wrk_id from mps_flowwork with(nolock) 
where wrk_id=@nwrkid and  wrk_keyparams not like '%auto%' and wrk_keyparams not like '%nomanual%' 

if (@nwrkid2>0) 
begin
insert into @wrkneedusers(swrkid,wrkid,wrktype,params,usercount,user1) 
select @swrkid,wrk_id ,wrk_type,wrk_keyparams,-1 , 0 from mps_flowwork with(nolock) 
where wrk_id=@nwrkid  -- and wrk_type<10 
end 
-- 将@nwrkid 
if (@nwrkid2>0) -- 如果不是自动执行
begin
-- 将有条件的加进来
set @swrkid=0 
select top 1 @nuseruid=user1 , @swrkid=usercount from @wrkneedusers where wrkid=@nwrkid2 and CHARINDEX('nodup',params)<=0 
-- 只有相同人员要跳过的，才找下一步
if (@swrkid=1 and (exists (select 1 from @wrkuser where wrkid<>@nwrkid2 and useruid=@nuseruid))) 
begin
insert into @wrks(wrkid,wsid,done) 
select @nwrkid2 , ws_id ,1
from mps_flowworkstatus WITH(NOLOCK) 
where ws_swrkid=@nwrkid2 and (len(ws_condition)>5 or  ws_caption='') and 
ws_tshape<>'job' and ws_tshape<>'top' and ws_tshape<>'back' and ws_tshape<>'backany' and 
ws_tshape<>'subflow' and ws_tshape<>'end' and  ws_tshape<>'abolish' and ws_tshape<>'wait'  
end 
end else
begin
-- 将所有的状态加进来
insert into @wrks(wrkid,wsid,done) 
select @nwrkid , ws_id ,1
from mps_flowworkstatus WITH(NOLOCK) 
where ws_swrkid=@nwrkid and  
ws_tshape<>'job' and ws_tshape<>'top' and ws_tshape<>'back' and ws_tshape<>'backany' and 
ws_tshape<>'subflow' and ws_tshape<>'end' and  ws_tshape<>'abolish' and ws_tshape<>'wait'  
end 
end 
end
 
   end 
   
   exec drop_temp_table '#permittion' 
   exec drop_temp_table '#permit' 
   drop table #myuser 



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

-- 如果已经配置好了。则不再处理。




-- 处理自由流程
-- 如果当前步骤是自由流程并且允许转发
declare @freet table(orderi int identity(1, 1) , jbnid bigint) 
if (@thiswrktype=10 and CHARINDEX('allowfwd',@thiswrkkeyparam)>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 @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) , @wrkneedusers 
where jbn_cmpid=@cmpid and jbn_wrkid=wrkid and jbn_ordid=@ordid and wrktype=10 and usercount=-1
if (not exists (select 1 from @freet)) 
begin
insert into @freet(jbnid) 
select jbn_id from mps_job_available  with(nolock) , @wrkneedusers 
where jbn_cmpid=@cmpid and jbn_wrkid=wrkid and jbn_ident=@freewrkadvise and @freewrkadvise>'' and jbn_ordid=0 
end 
-- t1 自由流程
select wrk_id,wrk_title , wrk_type ,'wrkfree' wrk_style 
from @wrkneedusers 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) ,@wrkneedusers
where mp_cmpid=@cmpid and mp_type='wrkfree' and mp_ctgid=wrkid and mp_ppid=980  and wrktype=10  

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





 
-- 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 and wrk_type>0) or wrk_type>0) then 0 else 1 end wrk_multi  
from mps_flowwork with(nolock) ,@wrkneedusers where wrk_id=wrkid and wrktype<10  and usercount>0 
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 @wrkneedusers 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 @wrkneedusers , mps_permission with(nolock) 
where mp_cmpid=@cmpid and mp_type='wrkexecutor' and mp_ctgid=wrkid and mp_ppid=980 and wrktype<10  

end  

set nocount off
end