IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizmodify_detail]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizmodify_detail]
GO
create procedure [dbo].[mps_gen_bizmodify_detail](
@cmpid int,
@bizid int,
@ord2tag int 
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @detail varchar(100)
declare @ds1 varchar(100)
declare @dss nvarchar(MAX) 
declare @order2 varchar(100)
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @ord2 varchar(10) 
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) 
declare @procparam nvarchar(MAX) 
set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_modify_'+@ord2)=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_modify_'+@ord2+N'(
@ord2id bigint, 
@cmpid int, 
@branchid bigint, 
@userdpid bigint, 
@userotid bigint, 
@useruid int, 
@jbid bigint, 
@ordid bigint, 
@ord2tag int,@sourcefield varchar(50)=default, '+char(13)+char(10)
    set @s1 = @dbname+@tbname+'_'+@ord2   
    exec mps_gen_proc_params_default @s1,'ord2_id,ord2_ordid,ord2_tag,ord2_updated,ord2_group,ord2_date',@procparam output 
--- 生成参数清单
if (len(@procparam)<=6) set @procparam = '@sys_nouse_v1 int' 
set @sql = @sql+@procparam+N' ) 
as  
begin  
set nocount on  
declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '''' 
declare @sys_recid bigint 
set @sys_recid = 0 
 declare @operation varchar(50)
 declare @errorinfo nvarchar(4000) 
 set @operation = ''continue'' 
set @errorinfo = '''' 
 --- 以下是预处理事件中的临时表
 declare @needreturn int 
 set @needreturn = 1
 if (@ord2id=0)  -- 如果=0
 begin
 set @ord2id=-1
 set @needreturn = 0
 exec '+@tbname+'_'+@ord2+'_newdetail @cmpid , @branchid , @userdpid, @useruid , @userotid, @jbid , @ordid ,@ord2tag ,@ord2id output , '''' 
 if (@ord2id<=0) set @ord2id=0 
 end 
 '
 
 if (dbo.mps_sys_checkkeywords('crm.beforemodifyord2,crm.aftermodifyord2',@bizid,0,'','')<>0)   -- 没有事件
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','ord2_id,ord2_ordid,ord2_tag,ord2_updated,ord2_group,ord2_date',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+N'update '+@dbname+@tbname+'_'+@ord2+' set '+char(13)+char(10)+@ss+','+char(13)+char(10)+
 'ord2_updated=getdate() '+char(13)+char(10)+
 'where ord2_id=@ord2id ' 
 end else 
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','ord2_id,ord2_ordid,ord2_tag,ord2_updated,ord2_group,ord2_date',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#old'' '+char(13)+char(10)+
'select * into #old from '+@dbname+@tbname+'_'+@ord2+N' where ord2_id=@ord2id '+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) +
'exec drop_temp_table ''#new'' '+char(13)+char(10)+
'select * ,null recid , case when ((@sourcefield is null) or (@sourcefield = ''_none_'')) then '''' else @sourcefield end sourcefield into #new from #old '+char(13)+char(10)+
'update #new set '+char(13)+char(10) +  @ss + char(13)+char(10) 

set @sql = @sql+N'update #new set recid=ord2_id '+char(13)+char(10) 

if (dbo.mps_sys_checkkeywords('crm.beforemodifyord2,crm.aftermodifyord2',@bizid,0,'#new','insert')=0) 
begin
set @ds1 = @dbname+@tbname+'_'+@ord2 
exec mps_gen_default_code @ds1 , '#new' , 'ord2_id' , @dss output 
set @sql = @sql+char(13)+char(10)+ @dss+char(13)+char(10) 
end

if (dbo.mps_sys_checkkeywords('crm.beforemodifyord2,crm.aftermodifyord2',@bizid,0,'#new','delete')=0)
set @sql = @sql+N'declare @detailnew_old  table (ord2_id bigint) '+char(13)+char(10)+
'insert  into @detailnew_old (ord2_id) select ord2_id from #new ' +char(13)+char(10)
 end 
 
 if (dbo.mps_sys_checkkeywords('crm.beforemodifyord2',@bizid,0,'','')=0) 
 begin
 set @sql = @sql+'exec '+@tbname+'_modify_'+@ord2+'_before @cmpid,@branchid,@userdpid,@useruid,@userotid,@jbid,@ordid,@ord2tag,@ord2id,@operation output,@errorinfo output ' 
 end
 
 set @sql = @sql +N'
 if (@operation=''continue'') 
 begin
 set @nonedate=@nonedate   -- here useless 
 '
 set @s1 = @dbname+@tbname+'_'+@ord2  
 if (dbo.mps_sys_checkkeywords('crm.beforemodifyord2',@bizid,0,'#new','insert')=0) 
begin
set @sql = @sql+char(13)+char(10)+
' set @ord2group2='''' '+char(13)+char(10)+
' select top 1 @ord2group2=ord2_group from #new where ord2_group>'''' '+char(13)+char(10)+
' set @ord2group2 = isnull(@ord2group2 , '''') '+char(13)+char(10) 
set @ds1=@dbname+@tbname+'_'+@ord2  
 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),@ord2tag)+','+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 #new new where recid is null '+char(13)+char(10) 
end 
end

-- 不管什么情况，都要回写
if (dbo.mps_sys_checkkeywords('crm.beforemodifyord2,crm.aftermodifyord2',@bizid,0,'','')=0) 
begin
exec mps_sys_gen_updatestatement @s1,'old','new','ord2_id,ord2_ordid,ord2_tag,ord2_updated,ord2_group,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)+
'ord2_updated=getdate() '+char(13)+char(10)+
 'from '+@s1+' old,#new new where old.ord2_id=new.ord2_id and (new.recid is not null) ' 
 end 
 
 if (dbo.mps_sys_checkkeywords('crm.beforemodifyord2',@bizid,0,'#new','delete')=0) 
set @sql = @sql+char(13)+char(10)+
'delete '+@s1+char(13)+char(10)+
'from  (select ord2_id from @detailnew_old  where ord2_id not in ( '+char(13)+char(10)+
'select recid from #new)) a where '+@s1+'.ord2_id=a.ord2_id '+char(13)+char(10) 

 
 if (dbo.mps_sys_checkkeywords('crm.aftermodifyord2',@bizid,0,'','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec '+@tbname+'_modify_'+@ord2+'_after @cmpid ,@branchid ,@userdpid,@useruid,@userotid,@jbid,@ordid,@ord2tag,@ord2id '+char(13)+char(10)
 end
 
 set @s1 = @dbname+@tbname+'_'+@ord2  
 if (dbo.mps_sys_checkkeywords('crm.aftermodifyord2',@bizid,0,'#new','insert')=0) 
begin
set @sql = @sql+char(13)+char(10)+
' set @ord2group2='''' '+char(13)+char(10)+
' select top 1 @ord2group2=ord2_group from #new where ord2_group>'''' '+char(13)+char(10)+
' set @ord2group2 = isnull(@ord2group2 , '''') '+char(13)+char(10) 
set @ds1=@dbname+@tbname+'_'+@ord2  
 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),@ord2tag)+','+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 #new new where recid is null '+char(13)+char(10) 
end 
end 

 if (dbo.mps_sys_checkkeywords('crm.aftermodifyord2',@bizid,0,'#new','update')=0)
 begin
 set @s1 = @dbname+@tbname+'_'+@ord2  
 exec mps_sys_gen_updatestatement @s1,'old','new','ord2_id,ord2_ordid,ord2_tag,ord2_updated,ord2_group,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)+
'ord2_updated=getdate() '+char(13)+char(10)+
 'from '+@s1+' old,#new new where old.ord2_id=new.ord2_id and (new.recid is not null) ' 
 end
 
 if (dbo.mps_sys_checkkeywords('crm.aftermodifyord2',@bizid,0,'#new','delete')=0) 
set @sql = @sql+char(13)+char(10)+
'delete '+@s1+char(13)+char(10)+
'from  (select ord2_id from @detailnew_old  where ord2_id not in ( '+char(13)+char(10)+
'select recid from #new)) a where '+@s1+'.ord2_id=a.ord2_id '+char(13)+char(10) +
'if ('+@ord2+'>100) '+char(13)+char(10)+
'begin '+char(13)+char(10)+
'delete mps_task where tsk_cmpid=@cmpid and tsk_objtype=''biz'' and tsk_ctgid='+convert(varchar(10),@bizid)+' and tsk_objid=@ordid and tsk_objid2=@ord2id '+char(13)+char(10)+ 
'end '+char(13)+char(10) 
 set @sql=@sql+N'
 if (@needreturn=1) select ''ok'' operation, '''' errorinfo 
 else select * from '+@s1+N' where ord2_id=@ord2id
end  else 
begin
if (@needreturn=1) select @operation operation, @errorinfo errorinfo  
else select 0 none , 1 none1 where 1=0 
end
' 
if (dbo.mps_sys_checkkeywords('crm.beforemodifyord2,crm.aftermodifyord2',@bizid,0,'','')=0)
 begin
 set @sql=@sql+char(13)+char(10)+
'exec drop_temp_table ''#new'' '+char(13)+char(10)+ 
'exec drop_temp_table ''#account'' '+char(13)+char(10)+ 
'exec drop_temp_table ''#old'' '+char(13)+char(10)
 end
set @sql=@sql+N'set nocount off 
end '
exec myprint @sql 
execute(@sql)  
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_modify_'+@ord2+''',
''@ord2id bigint,@cmpid int,@branchid bigint,@userdpid bigint,@userotid bigint,'+ 
  '@useruid int,@jbid bigint,@ordid bigint,@ord2tag int,@sourcefield varchar(50),'+@procparam+'''' 
execute(@sql)
set nocount off 
end