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

-- 生成时间:2018-11-23 17:09:55 by ebs-core  
CREATE  procedure order_258_3329_wrkapply_after(
     @cmpid int, 
     @useruid int,
     @bizid int, 
     @ordid bigint, 
     @wrkid int, 
     @jbid bigint, 
     @wsid int, 
     @wsstatus nvarchar(50) 
  ) 
as  
begin  
set nocount on  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @nonedate datetime 
declare @autoapplycode varchar(50) 
  set @autoapplycode =  ''
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 '#master' 
select * into #master from order_258 with(nolock) where ord_id=@ordid 
 ---- 以下是开发人员写的 after apply job 事件 --------------
 update #master 
set ord_hfsj = getdate()
from mps_job s,mps_job f
where s.jb_wrkid = 3329 
and s.jb_crmid = 258 and s.jb_ordid = @ordid
and f.jb_crmid = 258 and f.jb_ordid = @ordid
and s.jb_fjbid = f.jb_id
and f.jb_wsid in (3803,3805)
 ---- 以上是开发人员写的 after apply job 事件 --------------
 
  
 

 declare @authority_old varchar(32)=''
declare @authority_new varchar(32)='' 
select top 1 @authority_old=substring(sys.fn_sqlvarbasetostr(HashBytes('MD5', convert(varchar(100),ord_createcuid) )),3,32) from order_258 with(nolock) where ord_id=@ordid 
set @authority_old = isnull(@authority_old,'') 
select top 1 @authority_new=substring(sys.fn_sqlvarbasetostr(HashBytes('MD5', convert(varchar(100),ord_createcuid) )),3,32) from #master 
set @authority_new = isnull(@authority_new,'') 
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_cbid=case when new.ord_cbid is null or new.ord_cbid = -999999999  then old.ord_cbid else new.ord_cbid end,ord_ccxlh=case when new.ord_ccxlh is null or new.ord_ccxlh = '_none_'  then old.ord_ccxlh else new.ord_ccxlh end,ord_clxq=case when new.ord_clxq is null or new.ord_clxq = '_none_'  then old.ord_clxq else new.ord_clxq 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_dxmbhm=case when new.ord_dxmbhm is null or new.ord_dxmbhm = '_none_'  then old.ord_dxmbhm else new.ord_dxmbhm end,ord_dxnr=case when new.ord_dxnr is null or new.ord_dxnr = '_none_'  then old.ord_dxnr else new.ord_dxnr 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_imh=case when new.ord_imh is null or new.ord_imh = '_none_'  then old.ord_imh else new.ord_imh end,ord_jzdh=case when new.ord_jzdh is null or new.ord_jzdh = '_none_'  then old.ord_jzdh else new.ord_jzdh end,ord_jzxm=case when new.ord_jzxm is null or new.ord_jzxm = '_none_'  then old.ord_jzxm else new.ord_jzxm 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_khdh=case when new.ord_khdh is null or new.ord_khdh = '_none_'  then old.ord_khdh else new.ord_khdh end,ord_khnc=case when new.ord_khnc is null or new.ord_khnc = '_none_'  then old.ord_khnc else new.ord_khnc end,ord_khxm=case when new.ord_khxm is null or new.ord_khxm = '_none_'  then old.ord_khxm else new.ord_khxm 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_sbch=case when new.ord_sbch is null or new.ord_sbch = '_none_'  then old.ord_sbch else new.ord_sbch 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_sobjid=case when new.ord_sobjid is null or new.ord_sobjid = -999999999  then old.ord_sobjid else new.ord_sobjid end,ord_sqsh=case when new.ord_sqsh is null or new.ord_sqsh = '_none_'  then old.ord_sqsh else new.ord_sqsh 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_wtbt=case when new.ord_wtbt is null or new.ord_wtbt = '_none_'  then old.ord_wtbt else new.ord_wtbt end,ord_zxgl=case when new.ord_zxgl is null or new.ord_zxgl = '_none_'  then old.ord_zxgl else new.ord_zxgl end,ord_zxlx=case when new.ord_zxlx is null or new.ord_zxlx = '_none_'  then old.ord_zxlx else new.ord_zxlx end,ord_zxpt=case when new.ord_zxpt is null or new.ord_zxpt = '_none_'  then old.ord_zxpt else new.ord_zxpt end,ord_cppp=case when new.ord_cppp is null or new.ord_cppp = '_none_'  then old.ord_cppp else new.ord_cppp end,ord_cpxh=case when new.ord_cpxh is null or new.ord_cpxh = '_none_'  then old.ord_cpxh else new.ord_cpxh end,ord_gmrq=case when new.ord_gmrq is null or new.ord_gmrq <= @nonedate then old.ord_gmrq else new.ord_gmrq end,ord_khdz=case when new.ord_khdz is null or new.ord_khdz = '_none_'  then old.ord_khdz else new.ord_khdz end,ord_khlx=case when new.ord_khlx is null or new.ord_khlx = '_none_'  then old.ord_khlx else new.ord_khlx end,ord_xiry=case when new.ord_xiry is null or new.ord_xiry = '_none_'  then old.ord_xiry else new.ord_xiry end,ord_zxmd=case when new.ord_zxmd is null or new.ord_zxmd = '_none_'  then old.ord_zxmd else new.ord_zxmd end,ord_hfsj=case when new.ord_hfsj is null or new.ord_hfsj <= @nonedate then old.ord_hfsj else new.ord_hfsj end,ord_hsjg=case when new.ord_hsjg is null or new.ord_hsjg = '_none_'  then old.ord_hsjg else new.ord_hsjg end,ord_khmyd=case when new.ord_khmyd is null or new.ord_khmyd = '_none_'  then old.ord_khmyd else new.ord_khmyd end,ord_spyz=case when new.ord_spyz is null or new.ord_spyz = '_none_'  then old.ord_spyz else new.ord_spyz end,ord_tscljg=case when new.ord_tscljg is null or new.ord_tscljg = '_none_'  then old.ord_tscljg else new.ord_tscljg end,ord_tsdw=case when new.ord_tsdw is null or new.ord_tsdw = '_none_'  then old.ord_tsdw else new.ord_tsdw end,ord_tsdx=case when new.ord_tsdx is null or new.ord_tsdx = '_none_'  then old.ord_tsdx else new.ord_tsdx end,ord_tslx=case when new.ord_tslx is null or new.ord_tslx = '_none_'  then old.ord_tslx else new.ord_tslx end,ord_tsly=case when new.ord_tsly is null or new.ord_tsly = '_none_'  then old.ord_tsly else new.ord_tsly end,ord_tspd=case when new.ord_tspd is null or new.ord_tspd = '_none_'  then old.ord_tspd else new.ord_tspd end,ord_tswt=case when new.ord_tswt is null or new.ord_tswt = '_none_'  then old.ord_tswt else new.ord_tswt end,ord_tsxq=case when new.ord_tsxq is null or new.ord_tsxq = '_none_'  then old.ord_tsxq else new.ord_tsxq end,ord_zgjgfk=case when new.ord_zgjgfk is null or new.ord_zgjgfk = '_none_'  then old.ord_zgjgfk else new.ord_zgjgfk end,ord_jjgcs=case when new.ord_jjgcs is null or new.ord_jjgcs = '_none_'  then old.ord_jjgcs else new.ord_jjgcs end,ord_jjmd=case when new.ord_jjmd is null or new.ord_jjmd = '_none_'  then old.ord_jjmd else new.ord_jjmd end,ord_wtms=case when new.ord_wtms is null or new.ord_wtms = '_none_'  then old.ord_wtms else new.ord_wtms end,ord_wxlx=case when new.ord_wxlx is null or new.ord_wxlx = '_none_'  then old.ord_wxlx else new.ord_wxlx end,ord_yjddsj=case when new.ord_yjddsj is null or new.ord_yjddsj <= @nonedate then old.ord_yjddsj else new.ord_yjddsj end,ord_symddpid=case when new.ord_symddpid is null or new.ord_symddpid = -999999999  then old.ord_symddpid else new.ord_symddpid end,ord_zdmddgcsid=case when new.ord_zdmddgcsid is null or new.ord_zdmddgcsid = -999999999  then old.ord_zdmddgcsid else new.ord_zdmddgcsid end,ord_khid=case when new.ord_khid is null or new.ord_khid = -999999999  then old.ord_khid else new.ord_khid end,ord_xh1=case when new.ord_xh1 is null or new.ord_xh1 = '_none_'  then old.ord_xh1 else new.ord_xh1 end,ord_authority_factor=case when new.ord_authority_factor is null or new.ord_authority_factor = -999999999  then old.ord_authority_factor else new.ord_authority_factor end,ord_zxmddpid=case when new.ord_zxmddpid is null or new.ord_zxmddpid = -999999999  then old.ord_zxmddpid else new.ord_zxmddpid end,ord_psmddpid=case when new.ord_psmddpid is null or new.ord_psmddpid = -999999999  then old.ord_psmddpid else new.ord_psmddpid end,ord_zdmddsyrid=case when new.ord_zdmddsyrid is null or new.ord_zdmddsyrid = -999999999  then old.ord_zdmddsyrid else new.ord_zdmddsyrid end,ord_bzlj=case when new.ord_bzlj is null or new.ord_bzlj = '_none_'  then old.ord_bzlj else new.ord_bzlj end,ord_gcssj=case when new.ord_gcssj is null or new.ord_gcssj = '_none_'  then old.ord_gcssj else new.ord_gcssj end,ord_jbexecutor=case when new.ord_jbexecutor is null or new.ord_jbexecutor = '_none_'  then old.ord_jbexecutor else new.ord_jbexecutor end,ord_jbtitle=case when new.ord_jbtitle is null or new.ord_jbtitle = '_none_'  then old.ord_jbtitle else new.ord_jbtitle end,ord_jsfl=case when new.ord_jsfl is null or new.ord_jsfl = '_none_'  then old.ord_jsfl else new.ord_jsfl end,ord_mddh=case when new.ord_mddh is null or new.ord_mddh = '_none_'  then old.ord_mddh else new.ord_mddh end,ord_mddz=case when new.ord_mddz is null or new.ord_mddz = '_none_'  then old.ord_mddz else new.ord_mddz end,ord_mdfwl=case when new.ord_mdfwl is null or new.ord_mdfwl = '_none_'  then old.ord_mdfwl else new.ord_mdfwl end,ord_shfwl=case when new.ord_shfwl is null or new.ord_shfwl = '_none_'  then old.ord_shfwl else new.ord_shfwl end,ord_wrkid=case when new.ord_wrkid is null or new.ord_wrkid = -999999999  then old.ord_wrkid else new.ord_wrkid end,ord_xh2=case when new.ord_xh2 is null or new.ord_xh2 = '_none_'  then old.ord_xh2 else new.ord_xh2 end,ord_xh3=case when new.ord_xh3 is null or new.ord_xh3 = '_none_'  then old.ord_xh3 else new.ord_xh3 end,ord_xxmb=case when new.ord_xxmb is null or new.ord_xxmb = '_none_'  then old.ord_xxmb else new.ord_xxmb end,ord_yggh=case when new.ord_yggh is null or new.ord_yggh = '_none_'  then old.ord_yggh else new.ord_yggh end,ord_zxgl1=case when new.ord_zxgl1 is null or new.ord_zxgl1 = '_none_'  then old.ord_zxgl1 else new.ord_zxgl1 end,ord_zdmrid=case when new.ord_zdmrid is null or new.ord_zdmrid = -999999999  then old.ord_zdmrid else new.ord_zdmrid end,ord_xzlbxh1id=case when new.ord_xzlbxh1id is null or new.ord_xzlbxh1id = -999999999  then old.ord_xzlbxh1id else new.ord_xzlbxh1id end,ord_xzlbxh2id=case when new.ord_xzlbxh2id is null or new.ord_xzlbxh2id = -999999999  then old.ord_xzlbxh2id else new.ord_xzlbxh2id end,ord_xzlbxh3id=case when new.ord_xzlbxh3id is null or new.ord_xzlbxh3id = -999999999  then old.ord_xzlbxh3id else new.ord_xzlbxh3id end

from order_258 old with(rowlock), #master new  where old.ord_id=new.ord_id 

if (@authority_new<>@authority_old) 
begin
if (not exists (select 1 from order_258_authority_factor with(nolock) where ordaf_value=@authority_new)) 
begin
insert into order_258_authority_factor(ordaf_done,ordaf_ordid,ordaf_value) values(0,@ordid,@authority_new)
end 
end

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

set nocount off 
end