IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizimport_web_prev]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizimport_web_prev]
GO
create procedure [dbo].[mps_gen_bizimport_web_prev](
@cmpid int,
@bizid int
)
as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @s1 nvarchar(100) 
declare @detail nvarchar(100) 
declare @ds1 varchar(100)
declare @dss varchar(MAX)  
declare @detailold nvarchar(100) 
declare @order2 nvarchar(100) 
declare @i int 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
declare @ord2tag2 int 
declare @sql nvarchar(MAX) 
declare @ss nvarchar(MAX) 
declare @ss1 nvarchar(MAX) 
declare @cntname varchar(1000) 


declare @websqlprev nvarchar(max)=''
declare @websvcid int = 0

declare @tbext varchar(1000) 
set @tbext = 'o_'+convert(varchar(10),@bizid)+'_imp_'

declare @web varchar(1000) 
select top 1 @web=biz_auto_webservice from mps_biztype with(nolock) where biz_id=@bizid 
set @web = isnull(@web,'')
if (@web='') 
begin
return 
end

if (@web>'') 
begin
select top 1 @websvcid=svc_id, @websqlprev=svc_prevsql from ebs_kernel.dbo.pub_webservice with(nolock) where svc_cmpid=@cmpid and svc_name=@web 
set @websqlprev = ISNULL(@websqlprev,'') 
set @websvcid = ISNULL(@websvcid,0) 
end 

set @web = @web+'_biz_'+convert(varchar(10),@bizid)+'_prev'
if (dbo.mps_sys_checkkeywords('crm.impcond',@bizid,0,'','')<>0) 
begin
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@web+']') and OBJECTPROPERTY(id, N'IsProcedure') = 1) 
set @ss='drop procedure '+@web
execute(@ss) 
delete mps_sql with(rowlock) where sql_cmpid=@cmpid and sql_name=@web 
return 
end

set @sql = dbo.mps_sys_getusercode('crm.impcond',@bizid,0)
set @sql = ltrim(@sql) 
 
set @i = 0 
 
set @ss = @sql 

BEGIN
set @ss1 = @ss 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_master'
set @sql = '' 
set @sql = @sql+' 
set @cmpid= '+convert(varchar(10),@cmpid)+'
     declare @bizid int ='+convert(varchar(10),@bizid)+'
     
 --- 以下是预处理事件中的临时表
 '
 -- 对@ss 进行一些函数替换
 if (charindex('#master',@ss1)>0) 
 begin
 set @ss=REPLACE(@ss,'#master',@tbext) 
 set @ds1 = @dbname+@tbname 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'select *   into '+@tbext+' from '+@dbname+@tbname+N' with(nolock) where ord_id=0 '+char(13)+char(10)
exec mps_gen_default_code @ds1 , @tbext , 'ord_id' , @dss output 
 
set @sql = @sql+@dss+char(13)+char(10) 
 
 end
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_romaster'
 if (charindex('#romaster',@ss1)>0) 
 begin
 set @ss=REPLACE(@ss,'#romaster',@tbext) 
 set @ds1 = @dbname+@tbname 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'select *   into '+@tbext+' from '+@dbname+@tbname+N' with(nolock) where ord_id=0 '+char(13)+char(10)
set @dss = '' 
 
set @sql = @sql+@dss+char(13)+char(10) 
 
 end
 if (charindex('#task',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_task'
 set @ss=REPLACE(@ss,'#task',@tbext) 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'create table '+@tbext+'task(objtype varchar(20),ctgid int,objid bigint,objid2 bigint,cuid int,infotype nvarchar(50),info nvarchar(4000),jbid bigint) '
 end
 if (charindex('#account',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_account'
 set @ss=REPLACE(@ss,'#account',@tbext) 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'create table '+@tbext+'(acctype varchar(30),acccategory varchar(30),accdate varchar(30),fiidreal int,fiid int,objidreal bigint,objid bigint,origin int,value int,used int,suspended int,ready int,changed int,available int,actual int) '+char(13)+char(10) 
 end
 
 
 -- 以下代码是生成子表临时表
 
 declare c11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  with(nolock) where biz2_bizid=@bizid 
open c11

FETCH NEXT FROM c11 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag2)+'_imp_detail'
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 

if (charindex(@detail,@ss1)>0) 
begin
set @ss=REPLACE(@ss,@detail,@tbext) 
 set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'select * into '+@tbext+' from '+@order2+N' with(nolock) where ord2_ordid=0'+char(13)+char(10) 
 
 set @ds1 = @order2
 set @dss = ''
 exec mps_gen_default_code @order2 , @tbext , 'ord2_id' , @dss output 
 set @sql = @sql+@dss+char(13)+char(10) 
 end
 
 set @detail = '#rodetail'+convert(varchar(10),@ord2tag2) 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag2)+'_imp_rodetail'

if (charindex(@detail,@ss1)>0) 
begin
set @ss=REPLACE(@ss,@detail,@tbext) 
 set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'select * into '+@tbext+' from '+@order2+N' with(nolock) where ord2_ordid=0'+char(13)+char(10) 
 
 set @ds1 = @order2
 set @dss = ''
 set @sql = @sql+@dss+char(13)+char(10) 
 end
 
 FETCH NEXT FROM c11 INTO @ord2tag2 
END
CLOSE c11
DEALLOCATE c11 

set @sql=@sql+N'
-----  用户自定义代码开始
 '+@ss+'
 -----  用户自定义代码结束'+char(13)+char(10) 
 
 -- 如果是最后一条本SQL  就将执行提交操作
   
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_master'
  
  
set @sql = @sql +N'
--- 清除临时表 
'

 
 
 
if (charindex('#master',@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table ''#o_'+convert(varchar(10),@bizid)+'_imp_master''' + char(13)+char(10) 
end 
if (charindex('#romaster',@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table ''#o_'+convert(varchar(10),@bizid)+'_imp_romaster''' + char(13)+char(10) 
end 
 
 declare c11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  with(nolock) where biz2_bizid=@bizid 
open c11
FETCH NEXT FROM c11 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag2)+'_imp_detail'
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (charindex(@detail,@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table '''+@tbext+'''' + char(13)+char(10) 
end
set @detail = '#rodetail'+convert(varchar(10),@ord2tag2) 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag2)+'_imp_rodetail'
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (charindex(@detail,@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table '''+@tbext+'''' + char(13)+char(10) 
end
 FETCH NEXT FROM c11 INTO @ord2tag2 
END
CLOSE c11
DEALLOCATE c11 
 
 
end
end  
  
   
  

 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@web+']') and OBJECTPROPERTY(id, N'IsProcedure') = 1) set @ss1='alter ' else set @ss1='create '
set @ss1 = @ss1+' procedure '+@web+char(13)+
'@cmpid int,'+ char(13)+
'@branchid int,'+ char(13)+
'@userdpid int,'+ char(13)+
'@useruid int ,'+ char(13)+
'@userotid int ,'+ char(13)+
'@taskid bigint ,'+ char(13)+
'@jbid bigint ,'+ char(13)+
'@ctgid int ,'+ char(13)+
'@ctgid2 int ,'+ char(13)+
'@objid bigint ,'+ char(13)+
'@objid2 bigint ,'+ char(13)+
'@md5 varchar(32)'+ char(13)+
'as '+ char(13)+
'begin'+char(13) +char(9)+
'set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED' +char(13)+char(13) +char(9)+@websqlprev+ char(13)+char(10)+char(9)+@sql+char(13)+char(10) +
'set nocount off'+char(13)+
'end' 

set @cntname=@web
set @web = @cntname+' {cmpid},{branchid},{userdpid},{useruid},{userotid},{taskid},{jbid},{ctgid},{ctgid2},{objid},{objid2},{md5}'
set @tbext = 'int|int|int|int|int|bigint|bigint|int|int|bigint|bigint|varchar(32)'

if (not exists(select * from mps_sql where sql_cmpid=@cmpid and sql_name=@cntname))
begin
insert into mps_sql(sql_cmpid,sql_name,sql_type,sql_text,sql_note,sql_sourcetables,sql_targettables,sql_params,sql_keyparam)
values(@cmpid,@cntname,'MSSQL',@web,'','','',@tbext,'')
end else 
begin
update mps_sql set 
sql_text=@web,
sql_sourcetables='',
sql_targettables='',
sql_params=@tbext,
sql_keyparam='' 
where sql_cmpid=@cmpid and sql_name=@cntname
end

 
exec myprint @ss1
execute(@ss1)
set nocount off 
end