IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_form_scope]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_form_scope]
GO
create procedure [dbo].[mps_g_form_scope](
@cmpid int,
@tmpid int
) as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @type varchar(100)
declare @objid int 
select @type=tmp_type , @objid=tmp_objid from mps_template with(nolock) where tmp_id=@tmpid and tmp_cmpid=@cmpid 
declare @sql nvarchar(max) = ''
select  @sql=@sql+convert(nvarchar(max),tmp_form) from mps_template  with(nolock) 
where tmp_cmpid=@cmpid and tmp_type=@type and tmp_objid=@objid and tmp_main=0 
declare @subs table(md_index int , md_caption nvarchar(200)) 
-- 找到子模板
set @sql = LOWER(@sql) 
declare @i int
declare @j int
declare @s nvarchar(1000) 
declare @s1 nvarchar(1000) 
declare @caption nvarchar(1000)
declare @index1 nvarchar(1000) 
while (1=1)
begin
set @i = CHARINDEX('subform',@sql) 
if (@i>0) 
begin
set @j = 0
while (@j<@i) 
begin
set @j=CHARINDEX('<',@sql) 
if (@j<@i) 
begin
set @sql = SUBSTRING(@sql,@j+1,LEN(@sql))
set @i = CHARINDEX('subform',@sql) 
set @j = 0 
end 
end 
set @i = CHARINDEX('>',@sql) 
if (@i>0) 
begin
set @s = SUBSTRING(@sql,1,@i-1) 
set @sql = SUBSTRING(@sql,@i+1,LEN(@sql)) 
-- 现在的数据是：subform caption='dsa' index='0' 现在，只要分析出caption 和 index即可
set @i = CHARINDEX('caption=',@s) 
if (@i>0) 
begin
set @caption = SUBSTRING(@s,@i + 8,len(@s)) 
set @i = CHARINDEX(' ',@caption) 
if (@i>0) 
begin
set @caption = SUBSTRING(@caption ,  1 , @i - 1 ) 
end 
set @caption = LTRIM(rtrim(@caption)) 
set @caption = REPLACE(@caption,'"','')
set @caption = REPLACE(@caption,'''','')
set @caption = LTRIM(rtrim(@caption)) 
end else 
begin
set @caption=''
end 
set @i = CHARINDEX('index=',@s) 
if (@i>0) 
begin
set @index1 = SUBSTRING(@s,@i + 6,len(@s)) 
set @i = CHARINDEX(' ',@index1) 
if (@i>0) 
begin
set @index1 = SUBSTRING(@index1 ,  1 , @i - 1 ) 
end 
set @index1 = LTRIM(rtrim(@index1)) 
set @index1 = REPLACE(@index1,'"','')
set @index1 = REPLACE(@index1,'''','')
set @index1 = LTRIM(rtrim(@index1)) 
end else 
begin
set @index1=''
end
if (@index1='') set @index1='1' 
if (@index1='0') set @index1='1' 
if (not exists (select 1 from @subs where md_index=CONVERT(int,@index1))) 
begin
insert into @subs(md_index,md_caption) 
values(CONVERT(int,@index1),@caption) 
end 
end else 
begin
break  
end 
end else
begin
break  
end 
end 
insert into @subs(md_index,md_caption) values(0,'主模板') 
update @subs set md_caption = '子模板'+CONVERT(varchar(10),md_index) where md_caption='' 

-- 返回有效的流程步骤 
declare @wrks table(nid int identity(1,1), wrk_id int,wrk_caption nvarchar(200)) 
if (@type='biz') 
begin
insert into @wrks(wrk_id,wrk_caption) 
select wrk_id , wrk_title 
from mps_flowwork with(nolock) 
where wrk_bizid=@objid and wrk_type<10 and charindex('auto',wrk_keyparams)<=0 and charindex('nomanual',wrk_keyparams)<=0 and 
wrk_shape<>'end' and wrk_shape<>'top' and wrk_shape<>'back' and wrk_shape<>'backany' and wrk_shape<>'abolish'
and wrk_shape<>'subflow' and wrk_shape<>'job' and wrk_title>'' 
order by wrk_flwid , wrk_order 

insert into @wrks(wrk_id,wrk_caption) values(0,'流程结束后')
end 
select * from @subs order by md_index 
select * from @wrks order by nid 
select tmp_id , tmp_scope , tmp_main, tmp_wrks from mps_template where tmp_id=@tmpid 

set nocount off
end