IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_biz_auto]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_biz_auto]
GO
-- 为业务单生成自动化 multiSQL 
create procedure [dbo].[mps_gen_biz_auto](
@cmpid int,
@bizid int
)
as
begin
set nocount on
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 @web varchar(1000) 
select top 1 @web=biz_auto2_webservice from mps_biztype with(nolock) where biz_id=@bizid 
set @web = isnull(@web,'')
if (@web>'')
begin
delete mps_sql where sql_cmpid=@cmpid and sql_name='order_'+convert(varchar(10),@bizid)+'_import_ord' 
return 
end 
declare @tbext varchar(1000) 


if (dbo.mps_sys_checkkeywords('crm.bizauto',@bizid,0,'','')<>0) 
begin
delete mps_sql where sql_cmpid=@cmpid and sql_name='order_'+convert(varchar(10),@bizid)+'_import_ord' 
return 
end

set @sql = dbo.mps_sys_getusercode('crm.bizauto',@bizid,0)
set @sql = ltrim(@sql) 
declare @tsql table(index1 int identity(1, 1),dbname varchar(50), sql nvarchar(max) , islast int) 
-- 将SQL分解开
while (@sql>'') 
begin
set @cntname='' 
set @i = charindex('[[',@sql) 
if (@i=1) 
begin
set @sql = substring(@sql,3,len(@sql)) 
set @i = charindex(']]',@sql) 
if (@i>0) 
begin
set @cntname = substring(@sql,1,@i-1) 
set @sql = substring(@sql,@i+2,len(@sql)) 
end 
set @i = charindex('[[',@sql) 
end 

if (@i>1) 
begin
set @ss = substring(@sql,1,@i-1) 
set @sql = substring(@sql,@i,len(@sql)) 
end else 
begin
set @ss = @sql 
set @sql = '' 
end
set @ss = rtrim(ltrim(@ss)) 
if (len(@ss)>10) 
begin
if (@cntname='current') set @cntname='' 
insert into @tsql(dbname,sql,islast) values(@cntname,@ss,1) 
end 
end 
 
set @i = 0 
select @i = max(index1) from @tsql where dbname='' 
set @i = isnull(@i,0) 
update @tsql set islast=0 where index1=@i 
 

declare @index int, @islast int 
declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct index1,sql,islast  from @tsql where dbname=''  order by index1 
open c1
FETCH NEXT FROM c1 INTO @index,@ss,@islast 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @ss1 = @ss 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@index)+'_auto_master'
set @sql = '' 
set @sql = @sql+' declare @cmpid int ='+convert(varchar(10),@cmpid)+'
     declare @bizid int ='+convert(varchar(10),@bizid)+'
     declare @ordid bigint={objid} 
     declare @taskid bigint={taskid}
     declare @useruid int = 0 
     declare @userdpid int = 0 
     declare @branchid int = 0
 declare @userotid int = 0  
     declare @operation varchar(50) ='''' 
     declare @errorinfo nvarchar(4000) ='''' 
     declare @genordnoauto int=1 
     declare @autoapplyjob varchar(500)=''''
     select top 1 @useruid=ord_createcuid,@userdpid=ord_createdpid,@branchid=ord_bcid  from '+@dbname+@tbname+' with(nolock) where ord_id=@ordid 
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '''' 
declare @newordid bigint  
 declare @i int 
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
 set @operation = ''continue''
 set @errorinfo = '''' 
 --- 以下是预处理事件中的临时表
 '
 -- 对@ss 进行一些函数替换
 set @ss = replace(@ss,'deletetask()','delete mps_task_kernel where tsk_id=@taskid ') 
 set @ss = replace(@ss,'genordno(','set @genordnoauto=dbo.genordno(') 
 set @ss = replace(@ss,'applycurrentflow(','set @autoapplyjob=dbo.applycurrentflow(') 
 set @ss = replace(@ss,'applycurrentflow()','applycurrentflow(0,0,0)') 
 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' where ord_id=@ordid '+char(13)+char(10)
 end
 if (charindex('#romaster',@ss1)>0) 
 begin
 set @tbext = @tbext+'_old'  ;
 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' where ord_id=@ordid '+char(13)+char(10)
 end
 if (charindex('#task',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@index)+'_auto_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)+'_'+convert(varchar(10),@index)+'_auto_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,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  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)+'_'+convert(varchar(10),@index)+'_auto_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 * , null recid into '+@tbext+' from '+@order2+N' where ord2_ordid=@ordid 
 update '+@tbext+' set recid=ord2_id '+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)+'_'+convert(varchar(10),@index)+'_auto_detail_old'
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' where ord2_ordid=@ordid '+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  就将执行提交操作
 if (@islast=0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@index)+'_auto_master'
 set @sql=@sql+N'
 if (@operation=''continue'') 
 begin
 set @nonedate = @nonedate '+CHAR(13)+CHAR(10)
 -- 需要更新主表
 if (charindex('#master',@ss1)>0 and charindex('update',@ss1)>0 and charindex('#master',@ss1)>charindex('update',@ss1)) 
 begin
 set @s1 = @dbname +@tbname 
 exec mps_sys_gen_updatestatement @s1,'old','new','ord_id,ord_bizid,ord_cmpid,ord_updated,ord_bcid,ord_groupkey,ord_finished,ord_date',@dss output 
 -- 回写主表
 if (len(@dss)>10) 
 set @sql = @sql+char(13)+char(10)+
'update old set '+char(13)+char(10)+@dss + char(13)+char(10)+
'from '+@dbname+@tbname+N' old , '+@tbext+' new  where old.ord_id=new.ord_id ' +char(13)+char(10) 
end 
--- 处理明细表
declare c11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  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)+'_'+convert(varchar(10),@index)+'_auto_detail'
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (charindex(@detail,@ss1)>0 and charindex('insert',@ss1)>0 and charindex(@detail,@ss1)>charindex('insert',@ss1)) 
begin
-- 清理一下明细数据
set @sql = @sql+char(13)+char(10)+
'update '+@tbext+' set ord2_ordid=case when ord2_ordid=0 then  @ordid else ord2_ordid end , ord2_date=case when ord2_date<''1920-01-01'' then getdate() else ord2_date end,ord2_updated=case when ord2_updated<''1920-01-01'' then getdate() else ord2_updated end  '+char(13)+char(10) 
exec mps_gen_insert_code @order2,'','ord2_id,ord2_tag,ord2_group,ord2_ordid',@dss output 
 set @sql = @sql+char(13)+char(10)+
'insert into  '+@order2+'(ord2_tag,ord2_group,ord2_ordid,'+@dss+') '+char(13)+char(10) 
exec mps_gen_insert_code @order2,'new','ord2_id,ord2_tag,ord2_group,ord2_ordid',@dss output 
set @sql = @sql +'
select '+convert(varchar(10),@ord2tag2)+',biz2_caption,ordid,'+@dss+'
from mps_biztype2 with(nolock) ,  '+@tbext+' new 
where biz2_bizid='+convert(varchar(10),@bizid)+' and biz2_tag='+convert(varchar(10),@ord2tag2)+'
and new.ord2_ordid=@ordid and (new.recid is null )  '+char(13)+char(10) 
 end
 if (charindex(@detail,@ss1)>0 and charindex('update',@ss1)>0 and charindex(@detail,@ss1)>charindex('update',@ss1)) 
begin
exec mps_sys_gen_updatestatement @order2,'old','new','ord2_id,ord2_ordid,ord2_tag,ord2_updated',@dss output 
if (len(@dss)>10) 
set @sql = @sql+char(13)+char(10)+
'update old set '+char(13)+char(10)+@dss+char(13)+char(10)+
'from '+@order2+' old , '+@tbext+N' new  '+char(13)+char(10)+
'where old.ord2_id=new.ord2_id and new.recid is not null ' +char(13)+char(10) 
end 
if (charindex(@detail,@ss1)>0 and charindex('delete',@ss1)>0 and charindex(@detail,@ss1)>charindex('delete',@ss1)) 
begin
set @sql = @sql+char(13)+char(10)+
'delete '+@order2+char(13)+char(10)+
'where ord2_ordid=@ordid and  ( not exists (select 1 from '+@tbext+' old where '+@order2+'.ord2_id = old.ord2_id and (old.recid is not null))) '+char(13)+char(10) 
end 
 FETCH NEXT FROM c11 INTO @ord2tag2 
END
CLOSE c11
DEALLOCATE c11 


set @sql = @sql +N'
end 
--- 清除临时表 
'

 
if (charindex('#task',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@index)+'_auto_task'
 set @sql = @sql+char(13)+char(10)+
 ' delete '+@tbext+' where cuid is null or info is null or objid is null  or ctgid is null '+
 ' insert into mps_task(tsk_cmpid,tsk_bcid,tsk_branch,tsk_dpid,tsk_depart,tsk_cuid,tsk_cuname,tsk_date,tsk_type,tsk_objtype,tsk_ctgid,tsk_objid,tsk_objid2,tsk_message,tsk_jbid) '+char(13)+char(10)+
 ' select distinct cu_cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,getdate(),'+char(13)+char(10)+
 'case when infotype is null then ''普通任务'' else infotype end , '+char(13)+char(10)+
 'case when objtype is null or objtype='''' then ''biz'' else objtype end , '+char(13)+char(10)+
 'case when ctgid is null or ctgid=0 then '+convert(varchar(10),@bizid)+' else ctgid end , '+char(13)+char(10)+
 'case when objid  is null or objid=0 then @newordid else objid end , '+char(13)+char(10)+
 'case when objid2  is null then 0 else objid2 end , '+char(13)+char(10)+
 'case when info  is null then '''' else info end , '+char(13)+char(10)+
 'case when jbid  is null or jbid=0 then 0 else jbid end   '+char(13)+char(10)+
 'from '+@tbext+' , mps_login where cu_id=cuid '+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10) 
 end
 
if (charindex('#master',@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table ''#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@index)+'_auto_master''' + char(13)+char(10) 
end 
if (charindex('#romaster',@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table ''#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@index)+'_auto_master_old''' + char(13)+char(10) 
end 
 
 declare c11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  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)+'_'+convert(varchar(10),@index)+'_auto_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)+'_'+convert(varchar(10),@index)+'_auto_detail_old'
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 
if (charindex('#account',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@index)+'_auto_account'
 set @sql =@sql+ 'exec drop_temp_table '''+@tbext+'''' + char(13)+char(10) 
 end
 set @sql =@sql + N'
if (@autoapplyjob!='''') 
begin
declare @ordbcid int
select top 1 @ordbcid=ord_bcid,@userdpid=case when @userdpid=0 then ord_createdpid else @userdpid end ,@useruid = case when @useruid=0 then ord_createcuid else @useruid end 
from '+@dbname+@tbname+' with(nolock) where ord_id=@ordid 
    exec mps_do_execute_job_auto @cmpid ,@ordbcid ,@userdpid ,@useruid,0 , @autoapplyjob,@bizid ,@ordid  
end 

'
end
update @tsql set sql=@sql where index1=@index 
 FETCH NEXT FROM c1 INTO @index,@ss,@islast 
end  
  CLOSE c1
DEALLOCATE c1
  
  declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct index1,dbname,sql,islast  from @tsql  order by index1 
open c1
set @sql = '' 
set @ss1 = '' 

declare @cnti int = 0 
select @cnti = count(1) from @tsql 
set @i=0

FETCH NEXT FROM c1 INTO @index,@cntname,@ss,@islast 
WHILE (@@FETCH_STATUS = 0)
BEGIN
if (@cntname='') 
begin
if (@cnti>1)  set @cntname='[[current]]' 
end else 
begin
set @cntname = '[['+@cntname+']]' 
set @i = 1 
end 
set @sql = @sql+@cntname+char(13)+char(10)+@ss+char(13)+char(10)  
set @ss1 = @ss1+char(13)+char(10)+@ss+char(13)+char(10)  
FETCH NEXT FROM c1 INTO @index,@cntname,@ss,@islast 
end  
CLOSE c1
DEALLOCATE c1
 
 if (@cnti<=1 and @i=1) set @cnti = 2  
  
  
 
 
 set @web = 'order_'+CONVERT(varchar(10),@bizid)+'_import_ord'
 delete mps_sql where sql_name=@web and sql_cmpid=@cmpid 
 
 if (@cnti<=1) 
 begin
 set @sql = replace(@sql,'bigint={objid}','bigint=@objid')
 set @sql = replace(@sql,'declare @taskid bigint={taskid}','')
 set @dss = 'create '
 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@web+']') and OBJECTPROPERTY(id, N'IsProcedure') = 1) set @dss = 'alter ' 
 set @sql = @dss+' procedure '+@web+ '(@objid bigint , @taskid bigint) as 
 begin 
 set nocount on 
 '+@sql+'
 set nocount off 
 end '
 
 exec myprint @sql
 execute(@sql)
 insert into mps_sql(sql_cmpid,sql_name,sql_type,sql_text,sql_note,sql_params) 
 select @cmpid,@web,'MSSQL',@web+' {objid},{taskid}','自动引入','bigint|bigint' 
end else 
begin

 
exec myprint @sql
insert into mps_sql(sql_cmpid,sql_name,sql_type,sql_text,sql_note) 
 select @cmpid,@web,'MSSQL',@sql,'自动引入' 
end
 
 
 set @ss1 = @ss1+@ss 
 set @sql = @sql+@ss 

--set @ss1 = 'create procedure bb as '+@ss1
--execute(@ss1)
set nocount off 
end