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

-- 生成时间:2018-05-28 18:55:10 by ebs-core  
CREATE  procedure order_48_3117_wrklink_after(
     @cmpid int, 
     @useruid int,
     @bizid int, 
     @ordid bigint, 
     @wrkid int, 
     @jbid bigint, 
     @wsid int, 
     @wsstatus nvarchar(50) 
  ) 
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 
  
 --- 以下是预处理事件中的临时表
 
exec drop_temp_table '#task' 
create table #task(objtype varchar(20),ctgid int,objid bigint,objid2 bigint,cuid int,infotype nvarchar(50),info nvarchar(4000)) 
exec drop_temp_table '#master' 
select * into #master from order_48 where ord_id=@ordid 
exec drop_temp_table '#romaster' 
select * into #romaster from order_48 where ord_id=@ordid 
exec drop_temp_table '#rodetail105' 
select *  into #rodetail105 from order_48_105 where ord2_ordid=@ordid  

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

 alter table #detail105 add default 0 for ord2_fbizid   alter table #detail105 add default 0 for ord2_ford2tag   alter table #detail105 add default 0 for ord2_order   alter table #detail105 add default 0 for ord2_rbizid   alter table #detail105 add default 0 for ord2_rord2tag   alter table #detail105 add default 0 for ord2_sqdcs   alter table #detail105 add default 0 for ord2_tag   alter table #detail105 add default 0 for ord2_yqdcs   alter table #detail105 add default '1900-1-1' for ord2_date   alter table #detail105 add default '1900-1-1' for ord2_updated   alter table #detail105 add default 0 for ord2_llfs   alter table #detail105 add default 0 for ord2_zhpfs   alter table #detail105 add default 0 for ord2_scfs   alter table #detail105 add default 0 for ord2_fatherord2id   alter table #detail105 add default 0 for ord2_ford2id   alter table #detail105 add default 0 for ord2_fordid   alter table #detail105 add default 0 for ord2_ordid   alter table #detail105 add default 0 for ord2_rord2id   alter table #detail105 add default 0 for ord2_rordid   alter table #detail105 add default 0 for ord2_pxcuid   alter table #detail105 add default 0 for ord2_pxid   alter table #detail105 add default '' for ord2_code   alter table #detail105 add default '' for ord2_ftype   alter table #detail105 add default '' for ord2_key   alter table #detail105 add default '' for ord2_rtype   alter table #detail105 add default '' for ord2_tags   alter table #detail105 add default '' for ord2_bmqk   alter table #detail105 add default '' for ord2_caption   alter table #detail105 add default '' for ord2_group   alter table #detail105 add default '' for ord2_mdlx   alter table #detail105 add default '' for ord2_mdmc   alter table #detail105 add default '' for ord2_pxjg   alter table #detail105 add default '' for ord2_xm   alter table #detail105 add default '' for ord2_ygbh   alter table #detail105 add default '' for ord2_bz   alter table #detail105 add default '' for ord2_lxdh   alter table #detail105 add default '' for ord2_zj   alter table #detail105 add default '' for ord2_ksfs   alter table #detail105 add default '' for ord2_qdxq   alter table #detail105 add default '' for ord2_sfbm   alter table #detail105 add default '' for ord2_sfz   alter table #detail105 add default '' for ord2_sfzs   alter table #detail105 add default '' for ord2_sspq   alter table #detail105 add default '' for ord2_status 
update #detail105 set recid=ord2_id 

 ---- 以下是开发人员写的 before link job 事件 --------------
 insert into #task(objtype,ctgid,objid,objid2,cuid,infotype ,info)
select distinct 'biz',ord_bizid,ord_id,ord2_id,ord2_pxcuid,'培训课程参与','请确认是否参与培训课程'+ord_no+'/'+ord_title
from #rodetail105,#romaster

--update d set d.ord2_yqdcs = n.ord_qdcs from #detail105 d,#master n where n.ord_id = d.ord2_ordid 
 ---- 以上是开发人员写的 before libk job 事件 --------------
 
  
 
update old set 
ord_createcuid=case when new.ord_createcuid is null or new.ord_createcuid = -999999999  then old.ord_createcuid else new.ord_createcuid 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_cuid=case when new.ord_cuid is null or new.ord_cuid = -999999999  then old.ord_cuid else new.ord_cuid 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_fbizid=case when new.ord_fbizid is null or new.ord_fbizid = -999999999  then old.ord_fbizid else new.ord_fbizid 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_sxrs=case when new.ord_sxrs is null or new.ord_sxrs = -999999999  then old.ord_sxrs else new.ord_sxrs 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_bmrs=case when new.ord_bmrs is null or new.ord_bmrs = -999999999  then old.ord_bmrs else new.ord_bmrs end,ord_qdcs=case when new.ord_qdcs is null or new.ord_qdcs = -999999999  then old.ord_qdcs else new.ord_qdcs end,ord_kkrs=case when new.ord_kkrs is null or new.ord_kkrs = -999999999  then old.ord_kkrs else new.ord_kkrs 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_rord2tag=case when new.ord_rord2tag is null or new.ord_rord2tag = -999999999  then old.ord_rord2tag else new.ord_rord2tag 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_style=case when new.ord_style is null or new.ord_style = -999999999  then old.ord_style else new.ord_style 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_jsrq=case when new.ord_jsrq is null or new.ord_jsrq = @nonedate then old.ord_jsrq else new.ord_jsrq end,ord_ksrq=case when new.ord_ksrq is null or new.ord_ksrq = @nonedate then old.ord_ksrq else new.ord_ksrq end,ord_jkrq=case when new.ord_jkrq is null or new.ord_jkrq = @nonedate then old.ord_jkrq else new.ord_jkrq end,ord_dbfs=case when new.ord_dbfs is null or new.ord_dbfs = -999999999  then old.ord_dbfs else new.ord_dbfs 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_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_fordid=case when new.ord_fordid is null or new.ord_fordid = -999999999  then old.ord_fordid else new.ord_fordid 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_rordid=case when new.ord_rordid is null or new.ord_rordid = -999999999  then old.ord_rordid else new.ord_rordid end,ord_jscuid=case when new.ord_jscuid is null or new.ord_jscuid = -999999999  then old.ord_jscuid else new.ord_jscuid end,ord_pqid=case when new.ord_pqid is null or new.ord_pqid = -999999999  then old.ord_pqid else new.ord_pqid 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_rtype=case when new.ord_rtype is null or new.ord_rtype = '_none_'  then old.ord_rtype else new.ord_rtype end,ord_branch=case when new.ord_branch is null or new.ord_branch = '_none_'  then old.ord_branch else new.ord_branch 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_createdepart=case when new.ord_createdepart is null or new.ord_createdepart = '_none_'  then old.ord_createdepart else new.ord_createdepart 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_depart=case when new.ord_depart is null or new.ord_depart = '_none_'  then old.ord_depart else new.ord_depart 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_tj2=case when new.ord_tj2 is null or new.ord_tj2 = '_none_'  then old.ord_tj2 else new.ord_tj2 end,ord_tj2code=case when new.ord_tj2code is null or new.ord_tj2code = '_none_'  then old.ord_tj2code else new.ord_tj2code end,ord_tj3=case when new.ord_tj3 is null or new.ord_tj3 = '_none_'  then old.ord_tj3 else new.ord_tj3 end,ord_bz=case when new.ord_bz is null or new.ord_bz = '_none_'  then old.ord_bz else new.ord_bz 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_jhym=case when new.ord_jhym is null or new.ord_jhym = '_none_'  then old.ord_jhym else new.ord_jhym end,ord_tjsx=case when new.ord_tjsx is null or new.ord_tjsx = '_none_'  then old.ord_tjsx else new.ord_tjsx end,ord_xxbf=case when new.ord_xxbf is null or new.ord_xxbf = '_none_'  then old.ord_xxbf else new.ord_xxbf end,ord_pxlx=case when new.ord_pxlx is null or new.ord_pxlx = '_none_'  then old.ord_pxlx else new.ord_pxlx end,ord_kjbh=case when new.ord_kjbh is null or new.ord_kjbh = '_none_'  then old.ord_kjbh else new.ord_kjbh end,ord_kjmc=case when new.ord_kjmc is null or new.ord_kjmc = '_none_'  then old.ord_kjmc else new.ord_kjmc end,ord_pxdd=case when new.ord_pxdd is null or new.ord_pxdd = '_none_'  then old.ord_pxdd else new.ord_pxdd end,ord_pxdx=case when new.ord_pxdx is null or new.ord_pxdx = '_none_'  then old.ord_pxdx else new.ord_pxdx 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_template=case when new.ord_template is null or new.ord_template = '_none_'  then old.ord_template else new.ord_template end,ord_js=case when new.ord_js is null or new.ord_js = '_none_'  then old.ord_js else new.ord_js end,ord_jsmc=case when new.ord_jsmc is null or new.ord_jsmc = '_none_'  then old.ord_jsmc else new.ord_jsmc end,ord_kclx=case when new.ord_kclx is null or new.ord_kclx = '_none_'  then old.ord_kclx else new.ord_kclx end,ord_kcmc=case when new.ord_kcmc is null or new.ord_kcmc = '_none_'  then old.ord_kcmc else new.ord_kcmc end,ord_kcnr=case when new.ord_kcnr is null or new.ord_kcnr = '_none_'  then old.ord_kcnr else new.ord_kcnr end,ord_kczt=case when new.ord_kczt is null or new.ord_kczt = '_none_'  then old.ord_kczt else new.ord_kczt end
from order_48 old , #master new  where old.ord_id=new.ord_id 

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

 insert into order_48_105(ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid,ord2_fbizid,ord2_ford2tag,ord2_order,ord2_rbizid,ord2_rord2tag,ord2_sqdcs,ord2_yqdcs,ord2_llfs,ord2_zhpfs,ord2_scfs,ord2_fatherord2id,ord2_ford2id,ord2_fordid,ord2_rord2id,ord2_rordid,ord2_pxcuid,ord2_pxid,ord2_code,ord2_ftype,ord2_key,ord2_rtype,ord2_tags,ord2_bmqk,ord2_caption,ord2_mdlx,ord2_mdmc,ord2_pxjg,ord2_qdxq,ord2_ygbh,ord2_bz,ord2_lxdh,ord2_zj,ord2_ksfs,ord2_sfbm,ord2_sfz,ord2_sfzs,ord2_sspq,ord2_status,ord2_xm) 
 select case when new.ord2_date>getdate()-40000 then new.ord2_date else getdate() end ,getdate(),105,
    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_sqdcs,new.ord2_yqdcs,new.ord2_llfs,new.ord2_zhpfs,new.ord2_scfs,new.ord2_fatherord2id,new.ord2_ford2id,new.ord2_fordid,new.ord2_rord2id,new.ord2_rordid,new.ord2_pxcuid,new.ord2_pxid,new.ord2_code,new.ord2_ftype,new.ord2_key,new.ord2_rtype,new.ord2_tags,new.ord2_bmqk,new.ord2_caption,new.ord2_mdlx,new.ord2_mdmc,new.ord2_pxjg,new.ord2_qdxq,new.ord2_ygbh,new.ord2_bz,new.ord2_lxdh,new.ord2_zj,new.ord2_ksfs,new.ord2_sfbm,new.ord2_sfz,new.ord2_sfzs,new.ord2_sspq,new.ord2_status,new.ord2_xm
 from #detail105 new where recid is null 

update old set 
ord2_fbizid=case when new.ord2_fbizid is null or new.ord2_fbizid = -999999999  then old.ord2_fbizid else new.ord2_fbizid 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_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_rord2tag=case when new.ord2_rord2tag is null or new.ord2_rord2tag = -999999999  then old.ord2_rord2tag else new.ord2_rord2tag end,ord2_sqdcs=case when new.ord2_sqdcs is null or new.ord2_sqdcs = -999999999  then old.ord2_sqdcs else new.ord2_sqdcs end,ord2_yqdcs=case when new.ord2_yqdcs is null or new.ord2_yqdcs = -999999999  then old.ord2_yqdcs else new.ord2_yqdcs end,ord2_llfs=case when new.ord2_llfs is null or new.ord2_llfs = -999999999  then old.ord2_llfs else new.ord2_llfs end,ord2_zhpfs=case when new.ord2_zhpfs is null or new.ord2_zhpfs = -999999999  then old.ord2_zhpfs else new.ord2_zhpfs end,ord2_scfs=case when new.ord2_scfs is null or new.ord2_scfs = -999999999  then old.ord2_scfs else new.ord2_scfs 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_ford2id=case when new.ord2_ford2id is null or new.ord2_ford2id = -999999999  then old.ord2_ford2id else new.ord2_ford2id 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_rord2id=case when new.ord2_rord2id is null or new.ord2_rord2id = -999999999  then old.ord2_rord2id else new.ord2_rord2id 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_pxcuid=case when new.ord2_pxcuid is null or new.ord2_pxcuid = -999999999  then old.ord2_pxcuid else new.ord2_pxcuid end,ord2_pxid=case when new.ord2_pxid is null or new.ord2_pxid = -999999999  then old.ord2_pxid else new.ord2_pxid 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_ftype=case when new.ord2_ftype is null or new.ord2_ftype = '_none_'  then old.ord2_ftype else new.ord2_ftype 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_rtype=case when new.ord2_rtype is null or new.ord2_rtype = '_none_'  then old.ord2_rtype else new.ord2_rtype 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_bmqk=case when new.ord2_bmqk is null or new.ord2_bmqk = '_none_'  then old.ord2_bmqk else new.ord2_bmqk 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_group=case when new.ord2_group is null or new.ord2_group = '_none_'  then old.ord2_group else new.ord2_group end,ord2_mdlx=case when new.ord2_mdlx is null or new.ord2_mdlx = '_none_'  then old.ord2_mdlx else new.ord2_mdlx end,ord2_mdmc=case when new.ord2_mdmc is null or new.ord2_mdmc = '_none_'  then old.ord2_mdmc else new.ord2_mdmc end,ord2_pxjg=case when new.ord2_pxjg is null or new.ord2_pxjg = '_none_'  then old.ord2_pxjg else new.ord2_pxjg 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_ygbh=case when new.ord2_ygbh is null or new.ord2_ygbh = '_none_'  then old.ord2_ygbh else new.ord2_ygbh 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_lxdh=case when new.ord2_lxdh is null or new.ord2_lxdh = '_none_'  then old.ord2_lxdh else new.ord2_lxdh end,ord2_zj=case when new.ord2_zj is null or new.ord2_zj = '_none_'  then old.ord2_zj else new.ord2_zj end,ord2_ksfs=case when new.ord2_ksfs is null or new.ord2_ksfs = '_none_'  then old.ord2_ksfs else new.ord2_ksfs end,ord2_qdxq=case when new.ord2_qdxq is null or new.ord2_qdxq = '_none_'  then old.ord2_qdxq else new.ord2_qdxq 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_sfz=case when new.ord2_sfz is null or new.ord2_sfz = '_none_'  then old.ord2_sfz else new.ord2_sfz 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_sspq=case when new.ord2_sspq is null or new.ord2_sspq = '_none_'  then old.ord2_sspq else new.ord2_sspq end,ord2_status=case when new.ord2_status is null or new.ord2_status = '_none_'  then old.ord2_status else new.ord2_status end
from order_48_105 old , #detail105 new  
where old.ord2_id=new.ord2_id and (new.recid is not null)   

 
--- 清除临时表 
exec drop_temp_table '#master' 
exec drop_temp_table '#romaster' 
exec drop_temp_table '#detail105'
exec drop_temp_table '#rodetail105'

 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) 
 select distinct cu_cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,getdate(),
case when infotype is null then '普通任务' else infotype end , 
case when objtype is null or objtype='' then 'biz' else objtype end , 
case when ctgid is null or ctgid=0 then 48 else ctgid end , 
case when objid  is null or objid=0 then @ordid else objid end , 
case when objid2  is null then 0 else objid2 end , 
case when info  is null then '' else info end 
from #task , mps_login where cu_id=cuid 
exec drop_temp_table '#task' 

set nocount off 
end