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

-- 生成时间:2018-06-27 14:12:36 by ebs-core  
CREATE  procedure order_153_102_newdetail_before(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ordid bigint,
     @ord2tag int,
     @ord2group nvarchar(50),
     @ord2id bigint,
     @operation varchar(50) output ,
     @errorinfo nvarchar(4000) output
  ) 
as  
begin  
set nocount on  
declare @sys_recid bigint 
set @sys_recid = 0 
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '' 
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,'1910-1-1')
 set @operation = 'continue'
 set @errorinfo = '' 
 --- 以下是预处理事件中的临时表
 ---   #new 在主体中已经生成
 declare @detailnew_old  table (ord2_id bigint) 
insert  into @detailnew_old (ord2_id) select ord2_id from #new 

exec drop_temp_table '#detail102' 
select *,null recid into #detail102 from order_153_102 where ord2_ordid=@ordid  
update #detail102 set recid=ord2_id 
declare @detail102_old  table (ord2_id bigint) 
insert  into @detail102_old (ord2_id) select ord2_id from #detail102

 ---- 以下是开发人员写的 before new details order 事件 --------------
 if (@ord2tag = 102) 
    exec prj_meeting_153_102_logic
/*代码中存在#detail102、 #new才会创建这些临时表，所以本注释千万不能删除
这是 prj_meeting_153_102_logic
    update d
set ord2_sfch = '是',
ord2_sfbm = '否',
ord2_qdcs = isnull(d.ord2_qdcs,0)+1
from #new n,#detail102 d
where (d.ord2_gh = n.ord2_gh) or  (d.ord2_sfzh = n.ord2_sfzh) 

update n
set ord2_sfzh = ord_sfzhm,
ord2_xm = cu_name,
ord2_szdp = cu_depart,
ord2_sjh = cu.cu_mobile,
ord2_sfbm = '是',
ord2_sfch = '是',
ord2_qdcs = 1,
ord2_tsrcuid = cu_id 
from #new n,
order_40 ord,
mps_employee cu,
order_188 ord1
where n.ord2_gh = ord.ord_gh and n.ord2_gh = cu.cu_code and ord.ord_dpbh = ord1.ord_dpbh

delete n from #new n,#detail102 d where ((d.ord2_gh = n.ord2_gh) or  (d.ord2_sfzh = n.ord2_sfzh) ) and d.ord2_id <> n.ord2_id
*/

 ---- 以上是开发人员写的 before new details order 事件 --------------
 
 if (@operation='continue') 
 begin
 set @nonedate =  @nonedate  -- here useless 
 
update old set 
ord2_caption=case when new.ord2_caption is null or new.ord2_caption = '_none_'  then old.ord2_caption else new.ord2_caption end,ord2_code=case when new.ord2_code is null or new.ord2_code = '_none_'  then old.ord2_code else new.ord2_code end,ord2_fatherord2id=case when new.ord2_fatherord2id is null or new.ord2_fatherord2id = -999999999  then old.ord2_fatherord2id else new.ord2_fatherord2id end,ord2_fbizid=case when new.ord2_fbizid is null or new.ord2_fbizid = -999999999  then old.ord2_fbizid else new.ord2_fbizid end,ord2_ford2id=case when new.ord2_ford2id is null or new.ord2_ford2id = -999999999  then old.ord2_ford2id else new.ord2_ford2id end,ord2_ford2tag=case when new.ord2_ford2tag is null or new.ord2_ford2tag = -999999999  then old.ord2_ford2tag else new.ord2_ford2tag end,ord2_fordid=case when new.ord2_fordid is null or new.ord2_fordid = -999999999  then old.ord2_fordid else new.ord2_fordid end,ord2_ftype=case when new.ord2_ftype is null or new.ord2_ftype = '_none_'  then old.ord2_ftype else new.ord2_ftype end,ord2_gh=case when new.ord2_gh is null or new.ord2_gh = '_none_'  then old.ord2_gh else new.ord2_gh end,ord2_group=case when new.ord2_group is null or new.ord2_group = '_none_'  then old.ord2_group else new.ord2_group end,ord2_key=case when new.ord2_key is null or new.ord2_key = '_none_'  then old.ord2_key else new.ord2_key end,ord2_order=case when new.ord2_order is null or new.ord2_order = -999999999  then old.ord2_order else new.ord2_order end,ord2_qdcs=case when new.ord2_qdcs is null or new.ord2_qdcs = -999999999  then old.ord2_qdcs else new.ord2_qdcs end,ord2_rbizid=case when new.ord2_rbizid is null or new.ord2_rbizid = -999999999  then old.ord2_rbizid else new.ord2_rbizid end,ord2_rord2id=case when new.ord2_rord2id is null or new.ord2_rord2id = -999999999  then old.ord2_rord2id else new.ord2_rord2id end,ord2_rord2tag=case when new.ord2_rord2tag is null or new.ord2_rord2tag = -999999999  then old.ord2_rord2tag else new.ord2_rord2tag end,ord2_rordid=case when new.ord2_rordid is null or new.ord2_rordid = -999999999  then old.ord2_rordid else new.ord2_rordid end,ord2_rtype=case when new.ord2_rtype is null or new.ord2_rtype = '_none_'  then old.ord2_rtype else new.ord2_rtype end,ord2_sfbm=case when new.ord2_sfbm is null or new.ord2_sfbm = '_none_'  then old.ord2_sfbm else new.ord2_sfbm end,ord2_sfch=case when new.ord2_sfch is null or new.ord2_sfch = '_none_'  then old.ord2_sfch else new.ord2_sfch end,ord2_sfzh=case when new.ord2_sfzh is null or new.ord2_sfzh = '_none_'  then old.ord2_sfzh else new.ord2_sfzh end,ord2_sfzs=case when new.ord2_sfzs is null or new.ord2_sfzs = '_none_'  then old.ord2_sfzs else new.ord2_sfzs end,ord2_sjh=case when new.ord2_sjh is null or new.ord2_sjh = '_none_'  then old.ord2_sjh else new.ord2_sjh end,ord2_status=case when new.ord2_status is null or new.ord2_status = '_none_'  then old.ord2_status else new.ord2_status end,ord2_szdp=case when new.ord2_szdp is null or new.ord2_szdp = '_none_'  then old.ord2_szdp else new.ord2_szdp end,ord2_tags=case when new.ord2_tags is null or new.ord2_tags = '_none_'  then old.ord2_tags else new.ord2_tags end,ord2_xm=case when new.ord2_xm is null or new.ord2_xm = '_none_'  then old.ord2_xm else new.ord2_xm end,ord2_tsrcuid=case when new.ord2_tsrcuid is null or new.ord2_tsrcuid = -999999999  then old.ord2_tsrcuid else new.ord2_tsrcuid end,ord2_dksj=case when new.ord2_dksj is null or new.ord2_dksj = @nonedate then old.ord2_dksj else new.ord2_dksj end
from order_153_102 old , #detail102 new  
where old.ord2_id=new.ord2_id and (new.recid is not null) 

delete order_153_102
from  (select ord2_id from @detail102_old  where ord2_id not in ( 
select recid from #detail102)) a where order_153_102.ord2_id=a.ord2_id 

update old set 
ord2_caption=case when new.ord2_caption is null or new.ord2_caption = '_none_'  then old.ord2_caption else new.ord2_caption end,ord2_code=case when new.ord2_code is null or new.ord2_code = '_none_'  then old.ord2_code else new.ord2_code end,ord2_fatherord2id=case when new.ord2_fatherord2id is null or new.ord2_fatherord2id = -999999999  then old.ord2_fatherord2id else new.ord2_fatherord2id end,ord2_fbizid=case when new.ord2_fbizid is null or new.ord2_fbizid = -999999999  then old.ord2_fbizid else new.ord2_fbizid end,ord2_ford2id=case when new.ord2_ford2id is null or new.ord2_ford2id = -999999999  then old.ord2_ford2id else new.ord2_ford2id end,ord2_ford2tag=case when new.ord2_ford2tag is null or new.ord2_ford2tag = -999999999  then old.ord2_ford2tag else new.ord2_ford2tag end,ord2_fordid=case when new.ord2_fordid is null or new.ord2_fordid = -999999999  then old.ord2_fordid else new.ord2_fordid end,ord2_ftype=case when new.ord2_ftype is null or new.ord2_ftype = '_none_'  then old.ord2_ftype else new.ord2_ftype end,ord2_gh=case when new.ord2_gh is null or new.ord2_gh = '_none_'  then old.ord2_gh else new.ord2_gh end,ord2_group=case when new.ord2_group is null or new.ord2_group = '_none_'  then old.ord2_group else new.ord2_group end,ord2_key=case when new.ord2_key is null or new.ord2_key = '_none_'  then old.ord2_key else new.ord2_key end,ord2_order=case when new.ord2_order is null or new.ord2_order = -999999999  then old.ord2_order else new.ord2_order end,ord2_qdcs=case when new.ord2_qdcs is null or new.ord2_qdcs = -999999999  then old.ord2_qdcs else new.ord2_qdcs end,ord2_rbizid=case when new.ord2_rbizid is null or new.ord2_rbizid = -999999999  then old.ord2_rbizid else new.ord2_rbizid end,ord2_rord2id=case when new.ord2_rord2id is null or new.ord2_rord2id = -999999999  then old.ord2_rord2id else new.ord2_rord2id end,ord2_rord2tag=case when new.ord2_rord2tag is null or new.ord2_rord2tag = -999999999  then old.ord2_rord2tag else new.ord2_rord2tag end,ord2_rordid=case when new.ord2_rordid is null or new.ord2_rordid = -999999999  then old.ord2_rordid else new.ord2_rordid end,ord2_rtype=case when new.ord2_rtype is null or new.ord2_rtype = '_none_'  then old.ord2_rtype else new.ord2_rtype end,ord2_sfbm=case when new.ord2_sfbm is null or new.ord2_sfbm = '_none_'  then old.ord2_sfbm else new.ord2_sfbm end,ord2_sfch=case when new.ord2_sfch is null or new.ord2_sfch = '_none_'  then old.ord2_sfch else new.ord2_sfch end,ord2_sfzh=case when new.ord2_sfzh is null or new.ord2_sfzh = '_none_'  then old.ord2_sfzh else new.ord2_sfzh end,ord2_sfzs=case when new.ord2_sfzs is null or new.ord2_sfzs = '_none_'  then old.ord2_sfzs else new.ord2_sfzs end,ord2_sjh=case when new.ord2_sjh is null or new.ord2_sjh = '_none_'  then old.ord2_sjh else new.ord2_sjh end,ord2_status=case when new.ord2_status is null or new.ord2_status = '_none_'  then old.ord2_status else new.ord2_status end,ord2_szdp=case when new.ord2_szdp is null or new.ord2_szdp = '_none_'  then old.ord2_szdp else new.ord2_szdp end,ord2_tags=case when new.ord2_tags is null or new.ord2_tags = '_none_'  then old.ord2_tags else new.ord2_tags end,ord2_xm=case when new.ord2_xm is null or new.ord2_xm = '_none_'  then old.ord2_xm else new.ord2_xm end,ord2_tsrcuid=case when new.ord2_tsrcuid is null or new.ord2_tsrcuid = -999999999  then old.ord2_tsrcuid else new.ord2_tsrcuid end,ord2_dksj=case when new.ord2_dksj is null or new.ord2_dksj = @nonedate then old.ord2_dksj else new.ord2_dksj end
from order_153_102 old , #new new  
where old.ord2_id=new.ord2_id and (new.recid is not null)  

delete order_153_102
from  (select ord2_id from @detailnew_old  where ord2_id not in ( 
select recid from #new)) a where order_153_102.ord2_id=a.ord2_id 

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

set nocount off 
end