IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_template]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_template]
GO
 
CREATE procedure [dbo].[mps_save_template] 
@cmpid int ,
@type varchar(10),
@objid int , 
@objid2 int , 
@terminal int ,
@tmpname nvarchar(100),
@texttype varchar(10),
@tmplanguage varchar(20),
@text text ,
@jssql varchar(8000),
@parsesql text  ,
@advtext nvarchar(4000) 
as
begin
declare @tmpid int 
select top 1 @tmpid = tmp_id from mps_template 
where tmp_cmpid=@cmpid and tmp_type=@type and tmp_objid=@objid and tmp_objid2=@objid2 and tmp_caption=@tmpname 
set @tmpid = isnull(@tmpid , 0 ) 
if (@tmpid=0) 
begin
insert into mps_template(tmp_cmpid  ,
   tmp_type,
   tmp_objid,
   tmp_objid2,
   tmp_caption,
   tmp_language,
   tmp_form,
   tmp_js,
   tmp_css,
   tmp_formmobile,
   tmp_jsmobile,
   tmp_cssmobile,
   tmp_flowxml,
   tmp_sql,tmp_jssql) 
select @cmpid , @type , @objid , @objid2 ,@tmpname,  
case when @tmplanguage='_none_' then '' else @tmplanguage end , 
case when @texttype='form' and @terminal=0 then @text else '' end ,
case when @texttype='js' and @terminal=0  then @text else '' end ,
case when @texttype='css' and @terminal=0  then @text else '' end,
case when @texttype='form' and @terminal=1 then @text else '' end ,
case when @texttype='js' and @terminal=1  then @text else '' end ,
case when @texttype='css' and @terminal=1  then @text else '' end  ,
case when @texttype='xml'  then @text else '' end  ,
case when @texttype='sql'  then @text else '' end   ,
case when @jssql='_none_'  then '' else @jssql end 
end else 
begin
update mps_template set 
 tmp_form = case when @texttype='form' and @terminal=0 then @text else tmp_form end ,
   tmp_js = case when @texttype='js' and @terminal=0 then @text else tmp_js end ,
   tmp_css = case when @texttype='css' and @terminal=0 then @text else tmp_css end  ,
   tmp_sql = case when @texttype='sql'   then @text else tmp_sql end  ,
   tmp_flowxml = case when @texttype='xml'   then @text else tmp_flowxml end  ,
   tmp_formmobile = case when @texttype='form' and @terminal=1 then @text else tmp_formmobile end ,
   tmp_jsmobile = case when @texttype='js' and @terminal=1 then @text else tmp_jsmobile end ,
   tmp_cssmobile = case when @texttype='css' and @terminal=1 then @text else tmp_cssmobile end  ,
   tmp_jssql = case when @jssql='_none_'  then tmp_jssql else @jssql end 
where tmp_id=@tmpid 
end 
--生成存储过程
if not CONVERT(varchar,@parsesql)='_none_' 
begin
exec mps_sys_gen_userfunc @cmpid,@tmpid,@parsesql
end
-- 处理advtext 
declare @i int 
declare @s1 nvarchar(500) 
declare @name nvarchar(50) 
declare @title nvarchar(200) 
while (@advtext>'')
begin
set @i = CHARINDEX('|||',@advtext) 
if (@i>0) 
begin
set @s1 = SUBSTRING(@advtext,1,@i-1) 
set @advtext = SUBSTRING(@advtext,@i+3,len(@advtext)-@i-2) 
end else
begin
set @s1 = @advtext 
set @advtext = '' 
end 
if (@s1>'')
begin
set @i = CHARINDEX('||',@s1) 
if (@i>0) 
begin
set @name = SUBSTRING(@s1,1,@i-1) 
set @s1 = SUBSTRING(@s1,@i+2,len(@s1)-@i-1) 
set @i = CHARINDEX('||',@s1) 
if (@i>0) 
begin
set @title = SUBSTRING(@s1,1,@i-1) 
set @s1 = SUBSTRING(@s1,@i+2,len(@s1)-@i-1) 
if (@s1>'') 
begin
if (not exists (select 1 from mps_advertisement where ad_cmpid=@cmpid and ad_name=@name)) 
begin
insert into mps_advertisement(ad_cmpid,ad_name,ad_title,ad_interval) 
values(@cmpid , @name , @title , CONVERT(int,@s1)) 
end 
end
end
end 
end
end 
end