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

if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'','')<>0) 
begin
set @sql = 'drop procedure '+@tbname+'_'+@ord2+'_deletedetail_after' 
if (dbo.checkprocedure(@tbname+'_'+@ord2+'_deletedetail_after')=0) execute(@sql)  
return 
end


set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_'+@ord2+'_deletedetail_after')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+'_'+@ord2+'_deletedetail_after(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ordid bigint,
     @ord2tag int,
     @ord2id bigint
  ) 
as  
begin  
set nocount on  
declare @autoapplycode varchar(50) 
  set @autoapplycode =  ''''
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '''' 
declare @sys_recid bigint 
set @sys_recid = 0 
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
 --- 以下是预处理事件中的临时表
 --- #old   在主体中已经生成
 '
 
 if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#task','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#task'' '+char(13)+char(10)+
'create table #task(objtype varchar(20),ctgid int,objid bigint,objid2 bigint,cuid int,infotype nvarchar(50),info nvarchar(4000),jbid bigint) '
 end
 
 if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#master','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#master'' '+char(13)+char(10)+
'select * into #master from '+@dbname+@tbname+N' where ord_id=@ordid '
 end
 
 if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#account','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#account'' '+char(13)+char(10)+
'create table #account(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
 
 if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#romaster','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#romaster'' '+char(13)+char(10)+
'select * into #romaster from '+@dbname+@tbname+N' where ord_id=@ordid '
 end
 
 -- 以下代码是生成子表临时表
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @detailold = '#rodetail'+convert(varchar(10),@ord2tag2) 
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detailold,'')=0) 
begin
 set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@detailold+''' '+char(13)+char(10)+
'select *  into '+@detailold+' from '+@order2+N' where ord2_ordid=@ordid  '+char(13)+char(10) 
end
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detail,'')=0) 
begin
 set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@detail+''' '+char(13)+char(10)+
'select *,null recid into '+@detail+' from '+@order2+N' where ord2_ordid=@ordid  '+char(13)+char(10) 
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detail,'insert')=0) 
begin
set @ds1 = @order2 
exec mps_gen_default_code @ds1 , @detail , 'ord2_id' , @dss output 
set @sql = @sql+char(13)+char(10)+ @dss+char(13)+char(10) 
end
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detail,'insert,update')=0)
set @sql = @sql+N'update '+@detail+N' set recid=ord2_id '+char(13)+char(10)
 
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detail,'delete')=0)
set @sql = @sql+N'declare @detail'+convert(varchar(10),@ord2tag2)+'_old  table (ord2_id bigint) '+char(13)+char(10)+
'insert  into @detail'+convert(varchar(10),@ord2tag2)+'_old (ord2_id) select ord2_id from '+@detail +char(13)+char(10)
 end
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 
 
 
 declare @usercode nvarchar(max) 
set @usercode = dbo.mps_sys_getusercode('crm.afterdeleteord2',@bizid,0) 
if (charindex('applycurrentflow()',@usercode)>0) 
begin
set @usercode = replace(@usercode,'applycurrentflow()','set @autoapplycode=dbo.applycurrentflow(0,0,0)')
end else 
if (charindex('applycurrentflow(',@usercode)>0) 
begin
set @usercode = replace(@usercode,'applycurrentflow(','set @autoapplycode=dbo.applycurrentflow(')
end else 
if (charindex('applycurrentflow',@usercode)>0) 
begin
set @usercode = replace(@usercode,'applycurrentflow','set @autoapplycode=dbo.applycurrentflow(0,0,0)')
end 

set @sql=@sql+N'
 ---- 以下是开发人员写的 after delte details order 事件 --------------
 '
 + @usercode +N'
 ---- 以上是开发人员写的 after delte details order 事件 --------------
 
 ' 
 if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#master','update')=0) 
 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',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+char(13)+char(10)+
'update old set '+char(13)+char(10)+@ss + char(13)+char(10)+
'from '+@dbname+@tbname+N' old , #master new  where old.ord_id=new.ord_id ' +char(13)+char(10) 
 end
 
 -- 回写明细表
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detail,'insert')=0) 
begin
set @sql = @sql+char(13)+char(10)+
' set @ord2group2='''' '+char(13)+char(10)+
' select top 1 @ord2group2=ord2_group from '+@detail+N' where ord2_group>'''' '+char(13)+char(10)+
' set @ord2group2 = isnull(@ord2group2 , '''') '+char(13)+char(10) 
set @ds1=@order2  
 exec mps_gen_insert_code @ds1,'','ord2_id,ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid',@dss output 
 if (len(@dss)>5) 
 begin
 set @sql = @sql+char(13)+char(10)+
' insert into '+@ds1+'(ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid,'+@dss+') '+char(13)+char(10)+
' select case when new.ord2_date>getdate()-40000 then new.ord2_date else getdate() end ,getdate(),'+
 convert(varchar(10),@ord2tag2)+','+char(13)+char(10)+
'    case when new.ord2_group>'''' then new.ord2_group else @ord2group2 end , @ordid,'
 exec mps_gen_insert_code @ds1,'new','ord2_id,ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid',@dss output 
 set @sql = @sql+@dss+char(13)+char(10)+
' from '+@detail+' new where recid is null '+char(13)+char(10) 
end 
end 


if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detail,'update')=0) 
begin
  exec mps_sys_gen_updatestatement @order2,'old','new','ord2_id,ord2_ordid,ord2_tag,ord2_updated,ord2_date',@ss output 
  if (len(@ss)>10) 
set @sql = @sql+char(13)+char(10)+
'update old set '+char(13)+char(10)+@ss+char(13)+char(10)+
'from '+@order2+' old , '+@detail+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 (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detail,'delete')=0) 
set @sql = @sql+char(13)+char(10)+
'delete '+@order2+char(13)+char(10)+
'from  (select ord2_id from @detail'+CONVERT(varchar(10),@ord2tag2)+'_old  where ord2_id not in ( '+char(13)+char(10)+
'select recid from '+@detail+')) a where '+@order2+'.ord2_id=a.ord2_id '+char(13)+char(10) 
 
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1

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

 if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#master','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#master'' '+char(13)+char(10) 
 
 if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#account','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#account'' '+char(13)+char(10) 
 
 if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#romaster','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#romaster'' '+char(13)+char(10) 
 
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @detailold = '#rodetail'+convert(varchar(10),@ord2tag2) 
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detail,'')=0) 
set @sql = @sql+N'exec drop_temp_table '''+@detail+''' '+char(13)+char(10) 
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,@detailold,'')=0) 
set @sql = @sql+N'exec drop_temp_table '''+@detailold+''' '+char(13)+char(10) 
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 

if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid,0,'#task','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 ' delete #task 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 @ordid 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 #task , mps_login where cu_id=cuid '+char(13)+char(10)+
 'exec drop_temp_table ''#task'' '+char(13)+char(10) 
 end
 
 if (charindex('applycurrentflow',@usercode)>0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 '--以下代码是自动执行工作步骤 '+char(13)+char(10) +
 'if (@autoapplycode<>'''') '+char(13)+char(10) +
 'begin '+char(13)+char(10) +
 'exec mps_do_execute_job_auto2  '+char(13)+char(10) +
'@cmpid  , '+char(13)+char(10) +
'@branchid , '+char(13)+char(10)+ 
'@userdpid , '+char(13)+char(10) +
'@useruid , '+char(13)+char(10) +
'@userotid , '+char(13)+char(10) +
'@autoapplycode , '+convert(varchar(10),@bizid)+','+char(13)+char(10) +
'@ordid  '+char(13)+char(10) +
 'end ' +char(13)+char(10) 
 end
 
set @sql = @sql +N'
set nocount off 
end
'
exec myprint @sql 
execute(@sql) 
set nocount off 
end