IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_239_newbiz_before]') AND type = 'P')
DROP PROCEDURE [dbo].[order_239_newbiz_before]
GO

-- 生成时间:2018-06-19 16:32:08 by ebs-core  
CREATE  procedure order_239_newbiz_before(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ordid bigint,
     @stype varchar(20),
     @sbizid int,
     @sord2tag int,
     @sord2id bigint,
     @sordidstr varchar(max),
     @extparam1 nvarchar(1000),
     @extparam2 nvarchar(1000),
     @extparam3 nvarchar(1000),
     @operation varchar(50) output,
     @errorinfo nvarchar(4000) output
  ) 
as  
begin  
set nocount on  
if ((@extparam1 is null) or (@extparam1='_none_'))  set @extparam1 = '' 
  if ((@extparam2 is null) or (@extparam2='_none_'))  set @extparam2 = '' 
  if ((@extparam3 is null) or (@extparam3='_none_'))  set @extparam3 = '' 
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '' 
declare @sys_recid bigint 
set @sys_recid = 0 
declare @newordid bigint  
declare @sordids table(ordid bigint)  
 set @newordid = @ordid  
 declare @i int , @curInd int , @strlen int , @ordid_s varchar ( 20 )  
 declare @sordid bigint  
 set @sordid = 0  
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,'1910-1-1')
 if ( charindex ( ',' ,@sordidstr ) = 0 and @sordidstr > '' )  
 begin  
 set @sordid = convert ( bigint ,@sordidstr )  
 insert into @sordids ( ordid ) values ( @sordid )  
 end   
 else 
 begin  
 select @curInd = 1 , @strlen = LEN ( @sordidstr )  
 while ( @curInd <= @strlen )  
 begin  
 select @i = charindex ( ',' ,@sordidstr , @curInd )  
 if ( @i > 0 )  
 begin  
 select @ordid_s = SUBSTRING ( @sordidstr , @curInd , @i  - @curInd )  
 insert into @sordids ( ordid ) values ( convert ( bigint , @ordid_s ) )  
 set @curInd = @i  + 1  
 end  
 else if ( @curInd <= @strlen )  
 begin  
 select @ordid_s = SUBSTRING ( @sordidstr , @curInd , @strlen  - @curInd  + 1 )  
 insert into @sordids ( ordid ) values ( convert ( bigint , @ordid_s ) )  
 break  
 end  
 end  
 end 
 set @operation = 'continue'
 set @errorinfo = '' 
 --- 以下是预处理事件中的临时表
 
exec drop_temp_table '#detail104' 
select *,null recid into #detail104 from order_239_104 where ord2_ordid=@newordid  

 alter table #detail104 add default 0 for ord2_fbizid   alter table #detail104 add default 0 for ord2_ford2tag   alter table #detail104 add default 0 for ord2_order   alter table #detail104 add default 0 for ord2_rbizid   alter table #detail104 add default 0 for ord2_rord2tag   alter table #detail104 add default 0 for ord2_tag   alter table #detail104 add default 0 for ord2_cuid   alter table #detail104 add default '1900-1-1' for ord2_date   alter table #detail104 add default '1900-1-1' for ord2_updated   alter table #detail104 add default 0 for ord2_fatherord2id   alter table #detail104 add default 0 for ord2_ford2id   alter table #detail104 add default 0 for ord2_fordid   alter table #detail104 add default 0 for ord2_ordid   alter table #detail104 add default 0 for ord2_rord2id   alter table #detail104 add default 0 for ord2_rordid   alter table #detail104 add default '' for ord2_code   alter table #detail104 add default '' for ord2_ftype   alter table #detail104 add default '' for ord2_key   alter table #detail104 add default '' for ord2_rtype   alter table #detail104 add default '' for ord2_tags   alter table #detail104 add default '' for ord2_caption   alter table #detail104 add default '' for ord2_gh   alter table #detail104 add default '' for ord2_group   alter table #detail104 add default '' for ord2_lj   alter table #detail104 add default '' for ord2_qjsj   alter table #detail104 add default '' for ord2_sbsj   alter table #detail104 add default '' for ord2_status   alter table #detail104 add default '' for ord2_xm 
update #detail104 set recid=ord2_id 

 ---- 以下是开发人员写的 before new order 事件 --------------
 insert into #detail104 (ord2_cuid,ord2_xm,ord2_gh)  select b.cu_id,b.cu_callname,b.cu_account  from mps_login b  where  cu_callname='张航' 

 ---- 以上是开发人员写的 before new order 事件 --------------
 
 if (@operation='continue') 
 begin
 set @nonedate = @nonedate 
 
 set @ord2group2='' 
 select top 1 @ord2group2=ord2_group from #detail104 where ord2_group>'' 
 set @ord2group2 = isnull(@ord2group2 , '') 

 insert into order_239_104(ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid,ord2_fbizid,ord2_ford2tag,ord2_order,ord2_rbizid,ord2_rord2tag,ord2_cuid,ord2_fatherord2id,ord2_ford2id,ord2_fordid,ord2_rord2id,ord2_rordid,ord2_code,ord2_ftype,ord2_key,ord2_rtype,ord2_tags,ord2_caption,ord2_gh,ord2_lj,ord2_qjsj,ord2_sbsj,ord2_status,ord2_xm) 
 select case when new.ord2_date>getdate()-40000 then new.ord2_date else getdate() end ,getdate(),104,
 case when new.ord2_group>'' then new.ord2_group else @ord2group2 end , @ordid,
new.ord2_fbizid,new.ord2_ford2tag,new.ord2_order,new.ord2_rbizid,new.ord2_rord2tag,new.ord2_cuid,new.ord2_fatherord2id,new.ord2_ford2id,new.ord2_fordid,new.ord2_rord2id,new.ord2_rordid,new.ord2_code,new.ord2_ftype,new.ord2_key,new.ord2_rtype,new.ord2_tags,new.ord2_caption,new.ord2_gh,new.ord2_lj,new.ord2_qjsj,new.ord2_sbsj,new.ord2_status,new.ord2_xm
 from #detail104 new where recid is null 

end 
--- 清除临时表 
exec drop_temp_table '#detail104'

set nocount off 
end