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

-- 生成时间:2018-06-26 17:51:29 by ebs-core  
CREATE  procedure order_20_2_newdetail_after(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ordid bigint,
     @ord2tag int,
     @ord2group nvarchar(50),
     @ord2id bigint 
  ) 
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')
 
 --- 以下是预处理事件中的临时表
 ---   #new 在主体中已经生成
 
exec drop_temp_table '#master' 
select * into #master from order_20 where ord_id=@ordid declare @detailnew_old  table (ord2_id bigint) 
insert  into @detailnew_old (ord2_id) select ord2_id from #new 

exec drop_temp_table '#detail2' 
select *,null recid into #detail2 from order_20_2 where ord2_ordid=@ordid  

 alter table #detail2 add default 0 for ord2_fbizid   alter table #detail2 add default 0 for ord2_ford2tag   alter table #detail2 add default 0 for ord2_jhbz   alter table #detail2 add default 0 for ord2_order   alter table #detail2 add default 0 for ord2_rbizid   alter table #detail2 add default 0 for ord2_rord2tag   alter table #detail2 add default 0 for ord2_tag   alter table #detail2 add default 0 for ord2_zbrs   alter table #detail2 add default 0 for ord2_bgid   alter table #detail2 add default '1900-1-1' for ord2_date   alter table #detail2 add default '1900-1-1' for ord2_updated   alter table #detail2 add default 0 for ord2_fatherord2id   alter table #detail2 add default 0 for ord2_ford2id   alter table #detail2 add default 0 for ord2_fordid   alter table #detail2 add default 0 for ord2_ordid   alter table #detail2 add default 0 for ord2_rord2id   alter table #detail2 add default 0 for ord2_rordid   alter table #detail2 add default '' for ord2_code   alter table #detail2 add default '' for ord2_ftype   alter table #detail2 add default '' for ord2_key   alter table #detail2 add default '' for ord2_rtype   alter table #detail2 add default '' for ord2_tags   alter table #detail2 add default '' for ord2_bmmc   alter table #detail2 add default '' for ord2_bz   alter table #detail2 add default '' for ord2_caption   alter table #detail2 add default '' for ord2_group   alter table #detail2 add default '' for ord2_gw   alter table #detail2 add default '' for ord2_status   alter table #detail2 add default '' for ord2_bznd 
update #detail2 set recid=ord2_id 
declare @detail2_old  table (ord2_id bigint) 
insert  into @detail2_old (ord2_id) select ord2_id from #detail2

 ---- 以下是开发人员写的 after new details order 事件 --------------
 /*[mps_account_type]>>[mps_account] ，[mps_account] 即是编制数据目标表
导入后，查找当前导入的每行编制的当前在编人数，并用编制表中的现有编制替换最掉导入的现有编制
1.创建临时表@bianzhi，将明细数据写入其中
2.根据部门及岗位更细@bianzhi
3.a.如果存在错误，将错误信息写入改行一文本字段，并做特殊处理，下次导入前删除此行，并提交前做后端验证
3.b.如果不存在错误，更新当前编制值
4.结束
*/
declare @bcid bigint

declare @branch table (bcid bigint,bcname nvarchar(20))
declare @bianzhi table (ord2id bigint ,
                        dpname nvarchar(100),
                        dpid bigint default 0,
                        otname nvarchar(100),
                        otid bigint default 0 ,
                        dtid bigint default 0)

insert @branch(bcid,bcname)
select bc_id ,bc_company from mps_branch 
union
select 0 bc_id ,'集团总部' bc_compamy

select @bcid = bcid 
from #master, @branch 
where bcid = ord_bcid

delete #detail2 where isnull(ord2_bznd,'') = ''

insert into @bianzhi(ord2id,dpname,otname) 
select ord2_id,ord2_bmmc,ord2_gw from #new

update @bianzhi 
set dpid = dp_id
from mps_depart with(nolock)
where dp_depart = dpname
and dp_bcid = @bcid

update @bianzhi 
set otid = ot_id
from mps_title with(nolock)
where ot_title = otname

update @bianzhi 
set dtid = dt_id
from mps_depart_title with(nolock)
where dt_otid = otid 
and dt_dpid = dpid 

update #new
set ord2_bz = '错误：部门【'+dpname+'】下不存在岗位【'+otname+'】',
    ord2_status = '错误'
from @bianzhi
where dtid = 0
and ord2_id = ord2id 

update #new 
set ord2_bgid = dtid 
from @bianzhi
where ord2id = ord2_id

update #new
set ord2_bz = '错误：部门+岗位重复',
ord2_status =  '错误'
from (select dtid  from  @bianzhi where dtid != 0 group by dtid having count(10)>1) rpt
where rpt.dtid = ord2_bgid

merge into #new  as t
using mps_account as s 
on t.ord2_bgid = s.acc_objid and ord2_bznd = s.acc_date and t.ord2_status != '错误'
when matched 
then update set t.ord2_zbrs = s.acc_value/100
;




 ---- 以上是开发人员写的 after new details order 事件 --------------
 
 
update old set 
ord_branch=case when new.ord_branch is null or new.ord_branch = '_none_'  then old.ord_branch else new.ord_branch end,ord_bzrs=case when new.ord_bzrs is null or new.ord_bzrs = -999999999  then old.ord_bzrs else new.ord_bzrs end,ord_cbid=case when new.ord_cbid is null or new.ord_cbid = -999999999  then old.ord_cbid else new.ord_cbid end,ord_createbizids=case when new.ord_createbizids is null or new.ord_createbizids = '_none_'  then old.ord_createbizids else new.ord_createbizids end,ord_createcuid=case when new.ord_createcuid is null or new.ord_createcuid = -999999999  then old.ord_createcuid else new.ord_createcuid end,ord_createdepart=case when new.ord_createdepart is null or new.ord_createdepart = '_none_'  then old.ord_createdepart else new.ord_createdepart end,ord_createdpid=case when new.ord_createdpid is null or new.ord_createdpid = -999999999  then old.ord_createdpid else new.ord_createdpid end,ord_creator=case when new.ord_creator is null or new.ord_creator = '_none_'  then old.ord_creator else new.ord_creator end,ord_cuid=case when new.ord_cuid is null or new.ord_cuid = -999999999  then old.ord_cuid else new.ord_cuid end,ord_depart=case when new.ord_depart is null or new.ord_depart = '_none_'  then old.ord_depart else new.ord_depart end,ord_dpid=case when new.ord_dpid is null or new.ord_dpid = -999999999  then old.ord_dpid else new.ord_dpid end,ord_empname=case when new.ord_empname is null or new.ord_empname = '_none_'  then old.ord_empname else new.ord_empname end,ord_enddate=case when new.ord_enddate is null or new.ord_enddate = @nonedate then old.ord_enddate else new.ord_enddate end,ord_fbizid=case when new.ord_fbizid is null or new.ord_fbizid = -999999999  then old.ord_fbizid else new.ord_fbizid end,ord_fjbid=case when new.ord_fjbid is null or new.ord_fjbid = -999999999  then old.ord_fjbid else new.ord_fjbid end,ord_ford2id=case when new.ord_ford2id is null or new.ord_ford2id = -999999999  then old.ord_ford2id else new.ord_ford2id end,ord_ford2tag=case when new.ord_ford2tag is null or new.ord_ford2tag = -999999999  then old.ord_ford2tag else new.ord_ford2tag end,ord_fordid=case when new.ord_fordid is null or new.ord_fordid = -999999999  then old.ord_fordid else new.ord_fordid end,ord_ftype=case when new.ord_ftype is null or new.ord_ftype = '_none_'  then old.ord_ftype else new.ord_ftype end,ord_key=case when new.ord_key is null or new.ord_key = '_none_'  then old.ord_key else new.ord_key end,ord_no=case when new.ord_no is null or new.ord_no = '_none_'  then old.ord_no else new.ord_no end,ord_noi=case when new.ord_noi is null or new.ord_noi = -999999999  then old.ord_noi else new.ord_noi end,ord_rbizid=case when new.ord_rbizid is null or new.ord_rbizid = -999999999  then old.ord_rbizid else new.ord_rbizid end,ord_rord2id=case when new.ord_rord2id is null or new.ord_rord2id = -999999999  then old.ord_rord2id else new.ord_rord2id end,ord_rord2tag=case when new.ord_rord2tag is null or new.ord_rord2tag = -999999999  then old.ord_rord2tag else new.ord_rord2tag end,ord_rordid=case when new.ord_rordid is null or new.ord_rordid = -999999999  then old.ord_rordid else new.ord_rordid end,ord_rtype=case when new.ord_rtype is null or new.ord_rtype = '_none_'  then old.ord_rtype else new.ord_rtype end,ord_security=case when new.ord_security is null or new.ord_security = -999999999  then old.ord_security else new.ord_security end,ord_status=case when new.ord_status is null or new.ord_status = '_none_'  then old.ord_status else new.ord_status end,ord_style=case when new.ord_style is null or new.ord_style = -999999999  then old.ord_style else new.ord_style end,ord_template=case when new.ord_template is null or new.ord_template = '_none_'  then old.ord_template else new.ord_template end,ord_templateid=case when new.ord_templateid is null or new.ord_templateid = -999999999  then old.ord_templateid else new.ord_templateid end,ord_title=case when new.ord_title is null or new.ord_title = '_none_'  then old.ord_title else new.ord_title end,ord_type=case when new.ord_type is null or new.ord_type = '_none_'  then old.ord_type else new.ord_type end,ord_year=case when new.ord_year is null or new.ord_year = -999999999  then old.ord_year else new.ord_year end
from order_20 old , #master new  where old.ord_id=new.ord_id 

 set @ord2group2='' 
 select top 1 @ord2group2=ord2_group from #detail2 where ord2_group>'' 
 set @ord2group2 = isnull(@ord2group2 , '') 

 insert into order_20_2(ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid,ord2_fbizid,ord2_ford2tag,ord2_jhbz,ord2_order,ord2_rbizid,ord2_rord2tag,ord2_zbrs,ord2_bgid,ord2_fatherord2id,ord2_ford2id,ord2_fordid,ord2_rord2id,ord2_rordid,ord2_code,ord2_ftype,ord2_key,ord2_rtype,ord2_tags,ord2_bmmc,ord2_bz,ord2_caption,ord2_gw,ord2_status,ord2_bznd) 
 select case when new.ord2_date>getdate()-40000 then new.ord2_date else getdate() end ,getdate(),2,
    case when new.ord2_group>'' then new.ord2_group else @ord2group2 end , @ordid,new.ord2_fbizid,new.ord2_ford2tag,new.ord2_jhbz,new.ord2_order,new.ord2_rbizid,new.ord2_rord2tag,new.ord2_zbrs,new.ord2_bgid,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_bmmc,new.ord2_bz,new.ord2_caption,new.ord2_gw,new.ord2_status,new.ord2_bznd
 from #detail2 new where recid is null 

update old set 
ord2_bmmc=case when new.ord2_bmmc is null or new.ord2_bmmc = '_none_'  then old.ord2_bmmc else new.ord2_bmmc end,ord2_bz=case when new.ord2_bz is null or new.ord2_bz = '_none_'  then old.ord2_bz else new.ord2_bz end,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_group=case when new.ord2_group is null or new.ord2_group = '_none_'  then old.ord2_group else new.ord2_group end,ord2_gw=case when new.ord2_gw is null or new.ord2_gw = '_none_'  then old.ord2_gw else new.ord2_gw end,ord2_jhbz=case when new.ord2_jhbz is null or new.ord2_jhbz = -999999999  then old.ord2_jhbz else new.ord2_jhbz 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_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_status=case when new.ord2_status is null or new.ord2_status = '_none_'  then old.ord2_status else new.ord2_status 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_zbrs=case when new.ord2_zbrs is null or new.ord2_zbrs = -999999999  then old.ord2_zbrs else new.ord2_zbrs end,ord2_bgid=case when new.ord2_bgid is null or new.ord2_bgid = -999999999  then old.ord2_bgid else new.ord2_bgid end,ord2_bznd=case when new.ord2_bznd is null or new.ord2_bznd = '_none_'  then old.ord2_bznd else new.ord2_bznd end
from order_20_2 old , #detail2 new  
where old.ord2_id=new.ord2_id and (new.recid is not null)   

delete order_20_2
from  (select ord2_id from @detail2_old  where ord2_id not in ( 
select recid from #detail2)) a where order_20_2.ord2_id=a.ord2_id 

 set @ord2group2='' 
 select top 1 @ord2group2=ord2_group from #new where ord2_group>'' 
 set @ord2group2 = isnull(@ord2group2 , '') 

 insert into order_20_2(ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid,ord2_fbizid,ord2_ford2tag,ord2_jhbz,ord2_order,ord2_rbizid,ord2_rord2tag,ord2_zbrs,ord2_bgid,ord2_fatherord2id,ord2_ford2id,ord2_fordid,ord2_rord2id,ord2_rordid,ord2_code,ord2_ftype,ord2_key,ord2_rtype,ord2_tags,ord2_bmmc,ord2_bz,ord2_caption,ord2_gw,ord2_status,ord2_bznd) 
 select case when new.ord2_date>getdate()-40000 then new.ord2_date else getdate() end ,getdate(),2,
    case when new.ord2_group>'' then new.ord2_group else @ord2group2 end , @ordid,new.ord2_fbizid,new.ord2_ford2tag,new.ord2_jhbz,new.ord2_order,new.ord2_rbizid,new.ord2_rord2tag,new.ord2_zbrs,new.ord2_bgid,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_bmmc,new.ord2_bz,new.ord2_caption,new.ord2_gw,new.ord2_status,new.ord2_bznd
 from #new new where recid is null 

update old set 
ord2_bmmc=case when new.ord2_bmmc is null or new.ord2_bmmc = '_none_'  then old.ord2_bmmc else new.ord2_bmmc end,ord2_bz=case when new.ord2_bz is null or new.ord2_bz = '_none_'  then old.ord2_bz else new.ord2_bz end,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_group=case when new.ord2_group is null or new.ord2_group = '_none_'  then old.ord2_group else new.ord2_group end,ord2_gw=case when new.ord2_gw is null or new.ord2_gw = '_none_'  then old.ord2_gw else new.ord2_gw end,ord2_jhbz=case when new.ord2_jhbz is null or new.ord2_jhbz = -999999999  then old.ord2_jhbz else new.ord2_jhbz 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_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_status=case when new.ord2_status is null or new.ord2_status = '_none_'  then old.ord2_status else new.ord2_status 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_zbrs=case when new.ord2_zbrs is null or new.ord2_zbrs = -999999999  then old.ord2_zbrs else new.ord2_zbrs end,ord2_bgid=case when new.ord2_bgid is null or new.ord2_bgid = -999999999  then old.ord2_bgid else new.ord2_bgid end,ord2_bznd=case when new.ord2_bznd is null or new.ord2_bznd = '_none_'  then old.ord2_bznd else new.ord2_bznd end
from order_20_2 old , #new new  
where old.ord2_id=new.ord2_id and (new.recid is not null)  

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

--- 清除临时表 
exec drop_temp_table '#master' 
exec drop_temp_table '#detail2' 

set nocount off 
end