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

-- 生成时间:2018-11-23 17:09:54 by ebs-core  
CREATE  procedure order_258_modify(
@cmpid int,
@branchid int,
@userdpid int,
@useruid int,
@userotid int,
@jbid bigint,
@ordid bigint,@sourcefield varchar(50)=default, 
@ord_branch nvarchar(50)=default,
@ord_cbid bigint=default,
@ord_ccxlh nvarchar(100)=default,
@ord_clxq nvarchar(100)=default,
@ord_createbizids nvarchar(200)=default,
@ord_createcuid int=default,
@ord_createdepart nvarchar(50)=default,
@ord_createdpid int=default,
@ord_creator nvarchar(50)=default,
@ord_cuid int=default,
@ord_depart nvarchar(50)=default,
@ord_dpid int=default,
@ord_dxmbhm nvarchar(100)=default,
@ord_dxnr nvarchar(100)=default,
@ord_empname nvarchar(50)=default,
@ord_enddate datetime=default,
@ord_fbizid int=default,
@ord_fjbid bigint=default,
@ord_ford2id bigint=default,
@ord_ford2tag int=default,
@ord_fordid bigint=default,
@ord_ftype varchar(10)=default,
@ord_imh nvarchar(100)=default,
@ord_jzdh nvarchar(100)=default,
@ord_jzxm nvarchar(100)=default,
@ord_key varchar(30)=default,
@ord_khdh nvarchar(100)=default,
@ord_khnc nvarchar(100)=default,
@ord_khxm nvarchar(100)=default,
@ord_no varchar(50)=default,
@ord_noi int=default,
@ord_rbizid int=default,
@ord_rord2id bigint=default,
@ord_rord2tag int=default,
@ord_rordid bigint=default,
@ord_rtype varchar(10)=default,
@ord_sbch nvarchar(100)=default,
@ord_security int=default,
@ord_sobjid bigint=default,
@ord_sqsh nvarchar(100)=default,
@ord_status nvarchar(30)=default,
@ord_style int=default,
@ord_template nvarchar(50)=default,
@ord_templateid int=default,
@ord_title nvarchar(100)=default,
@ord_type nvarchar(50)=default,
@ord_wtbt nvarchar(100)=default,
@ord_zxgl nvarchar(100)=default,
@ord_zxlx nvarchar(100)=default,
@ord_zxpt nvarchar(100)=default,
@ord_cppp nvarchar(100)=default,
@ord_cpxh nvarchar(100)=default,
@ord_gmrq datetime=default,
@ord_khdz nvarchar(100)=default,
@ord_khlx nvarchar(100)=default,
@ord_xiry nvarchar(100)=default,
@ord_zxmd nvarchar(100)=default,
@ord_hfsj datetime=default,
@ord_hsjg nvarchar(100)=default,
@ord_khmyd nvarchar(100)=default,
@ord_spyz nvarchar(100)=default,
@ord_tscljg nvarchar(100)=default,
@ord_tsdw nvarchar(100)=default,
@ord_tsdx nvarchar(100)=default,
@ord_tslx nvarchar(100)=default,
@ord_tsly nvarchar(100)=default,
@ord_tspd nvarchar(100)=default,
@ord_tswt nvarchar(100)=default,
@ord_tsxq nvarchar(100)=default,
@ord_zgjgfk nvarchar(100)=default,
@ord_jjgcs nvarchar(100)=default,
@ord_jjmd nvarchar(100)=default,
@ord_wtms nvarchar(100)=default,
@ord_wxlx nvarchar(100)=default,
@ord_yjddsj datetime=default,
@ord_symddpid bigint=default,
@ord_zdmddgcsid bigint=default,
@ord_khid bigint=default,
@ord_xh1 nvarchar(100)=default,
@ord_authority_factor int=default,
@ord_zxmddpid bigint=default,
@ord_psmddpid bigint=default,
@ord_zdmddsyrid bigint=default,
@ord_bzlj nvarchar(100)=default,
@ord_gcssj nvarchar(100)=default,
@ord_jbexecutor nvarchar(50)=default,
@ord_jbtitle nvarchar(50)=default,
@ord_jsfl nvarchar(100)=default,
@ord_mddh nvarchar(100)=default,
@ord_mddz nvarchar(100)=default,
@ord_mdfwl nvarchar(100)=default,
@ord_shfwl nvarchar(100)=default,
@ord_wrkid int=default,
@ord_xh2 nvarchar(100)=default,
@ord_xh3 nvarchar(100)=default,
@ord_xxmb nvarchar(100)=default,
@ord_yggh nvarchar(100)=default,
@ord_zxgl1 nvarchar(100)=default,
@ord_zdmrid bigint=default,
@ord_xzlbxh1id bigint=default,
@ord_xzlbxh2id bigint=default,
@ord_xzlbxh3id bigint=default ) 
as  
begin  
set nocount on  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @nonedate datetime 
set @nonedate = CONVERT(datetime,'1910-1-1')
 declare @operation varchar(50)
declare @errorinfo nvarchar(4000) 
 set @operation = 'continue' 
set @errorinfo = '' 
declare @authority_old varchar(32)=''
declare @authority_new varchar(32)=''
-- 由于无法评估权限因子，所以，每次都要来比较  

  -- 加一个判断逻辑，如果当前步骤被做了，或者当前步骤被删除了（如果任签），则不再保存
  if (not exists (select 1 from mps_job with(nolock) where jb_id=@jbid and jb_wsid=0))
begin
select 'ok' operation, '' errorinfo 
return  
end 

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,'') 
 --- 以下是预处理事件中的临时表
 update order_258 with(rowlock) set 
ord_branch=case when @ord_branch is null or @ord_branch = '_none_'  then ord_branch else @ord_branch end,ord_cbid=case when @ord_cbid is null or @ord_cbid = -999999999  then ord_cbid else @ord_cbid end,ord_ccxlh=case when @ord_ccxlh is null or @ord_ccxlh = '_none_'  then ord_ccxlh else @ord_ccxlh end,ord_clxq=case when @ord_clxq is null or @ord_clxq = '_none_'  then ord_clxq else @ord_clxq end,ord_createbizids=case when @ord_createbizids is null or @ord_createbizids = '_none_'  then ord_createbizids else @ord_createbizids end,ord_createcuid=case when @ord_createcuid is null or @ord_createcuid = -999999999  then ord_createcuid else @ord_createcuid end,ord_createdepart=case when @ord_createdepart is null or @ord_createdepart = '_none_'  then ord_createdepart else @ord_createdepart end,ord_createdpid=case when @ord_createdpid is null or @ord_createdpid = -999999999  then ord_createdpid else @ord_createdpid end,ord_creator=case when @ord_creator is null or @ord_creator = '_none_'  then ord_creator else @ord_creator end,ord_cuid=case when @ord_cuid is null or @ord_cuid = -999999999  then ord_cuid else @ord_cuid end,ord_depart=case when @ord_depart is null or @ord_depart = '_none_'  then ord_depart else @ord_depart end,ord_dpid=case when @ord_dpid is null or @ord_dpid = -999999999  then ord_dpid else @ord_dpid end,ord_dxmbhm=case when @ord_dxmbhm is null or @ord_dxmbhm = '_none_'  then ord_dxmbhm else @ord_dxmbhm end,ord_dxnr=case when @ord_dxnr is null or @ord_dxnr = '_none_'  then ord_dxnr else @ord_dxnr end,ord_empname=case when @ord_empname is null or @ord_empname = '_none_'  then ord_empname else @ord_empname end,ord_enddate=case when @ord_enddate is null or @ord_enddate <= @nonedate then ord_enddate else @ord_enddate end,ord_fbizid=case when @ord_fbizid is null or @ord_fbizid = -999999999  then ord_fbizid else @ord_fbizid end,ord_fjbid=case when @ord_fjbid is null or @ord_fjbid = -999999999  then ord_fjbid else @ord_fjbid end,ord_ford2id=case when @ord_ford2id is null or @ord_ford2id = -999999999  then ord_ford2id else @ord_ford2id end,ord_ford2tag=case when @ord_ford2tag is null or @ord_ford2tag = -999999999  then ord_ford2tag else @ord_ford2tag end,ord_fordid=case when @ord_fordid is null or @ord_fordid = -999999999  then ord_fordid else @ord_fordid end,ord_ftype=case when @ord_ftype is null or @ord_ftype = '_none_'  then ord_ftype else @ord_ftype end,ord_imh=case when @ord_imh is null or @ord_imh = '_none_'  then ord_imh else @ord_imh end,ord_jzdh=case when @ord_jzdh is null or @ord_jzdh = '_none_'  then ord_jzdh else @ord_jzdh end,ord_jzxm=case when @ord_jzxm is null or @ord_jzxm = '_none_'  then ord_jzxm else @ord_jzxm end,ord_key=case when @ord_key is null or @ord_key = '_none_'  then ord_key else @ord_key end,ord_khdh=case when @ord_khdh is null or @ord_khdh = '_none_'  then ord_khdh else @ord_khdh end,ord_khnc=case when @ord_khnc is null or @ord_khnc = '_none_'  then ord_khnc else @ord_khnc end,ord_khxm=case when @ord_khxm is null or @ord_khxm = '_none_'  then ord_khxm else @ord_khxm end,ord_no=case when @ord_no is null or @ord_no = '_none_'  then ord_no else @ord_no end,ord_noi=case when @ord_noi is null or @ord_noi = -999999999  then ord_noi else @ord_noi end,ord_rbizid=case when @ord_rbizid is null or @ord_rbizid = -999999999  then ord_rbizid else @ord_rbizid end,ord_rord2id=case when @ord_rord2id is null or @ord_rord2id = -999999999  then ord_rord2id else @ord_rord2id end,ord_rord2tag=case when @ord_rord2tag is null or @ord_rord2tag = -999999999  then ord_rord2tag else @ord_rord2tag end,ord_rordid=case when @ord_rordid is null or @ord_rordid = -999999999  then ord_rordid else @ord_rordid end,ord_rtype=case when @ord_rtype is null or @ord_rtype = '_none_'  then ord_rtype else @ord_rtype end,ord_sbch=case when @ord_sbch is null or @ord_sbch = '_none_'  then ord_sbch else @ord_sbch end,ord_security=case when @ord_security is null or @ord_security = -999999999  then ord_security else @ord_security end,ord_sobjid=case when @ord_sobjid is null or @ord_sobjid = -999999999  then ord_sobjid else @ord_sobjid end,ord_sqsh=case when @ord_sqsh is null or @ord_sqsh = '_none_'  then ord_sqsh else @ord_sqsh end,ord_status=case when @ord_status is null or @ord_status = '_none_'  then ord_status else @ord_status end,ord_style=case when @ord_style is null or @ord_style = -999999999  then ord_style else @ord_style end,ord_template=case when @ord_template is null or @ord_template = '_none_'  then ord_template else @ord_template end,ord_templateid=case when @ord_templateid is null or @ord_templateid = -999999999  then ord_templateid else @ord_templateid end,ord_title=case when @ord_title is null or @ord_title = '_none_'  then ord_title else @ord_title end,ord_type=case when @ord_type is null or @ord_type = '_none_'  then ord_type else @ord_type end,ord_wtbt=case when @ord_wtbt is null or @ord_wtbt = '_none_'  then ord_wtbt else @ord_wtbt end,ord_zxgl=case when @ord_zxgl is null or @ord_zxgl = '_none_'  then ord_zxgl else @ord_zxgl end,ord_zxlx=case when @ord_zxlx is null or @ord_zxlx = '_none_'  then ord_zxlx else @ord_zxlx end,ord_zxpt=case when @ord_zxpt is null or @ord_zxpt = '_none_'  then ord_zxpt else @ord_zxpt end,ord_cppp=case when @ord_cppp is null or @ord_cppp = '_none_'  then ord_cppp else @ord_cppp end,ord_cpxh=case when @ord_cpxh is null or @ord_cpxh = '_none_'  then ord_cpxh else @ord_cpxh end,ord_gmrq=case when @ord_gmrq is null or @ord_gmrq <= @nonedate then ord_gmrq else @ord_gmrq end,ord_khdz=case when @ord_khdz is null or @ord_khdz = '_none_'  then ord_khdz else @ord_khdz end,ord_khlx=case when @ord_khlx is null or @ord_khlx = '_none_'  then ord_khlx else @ord_khlx end,ord_xiry=case when @ord_xiry is null or @ord_xiry = '_none_'  then ord_xiry else @ord_xiry end,ord_zxmd=case when @ord_zxmd is null or @ord_zxmd = '_none_'  then ord_zxmd else @ord_zxmd end,ord_hfsj=case when @ord_hfsj is null or @ord_hfsj <= @nonedate then ord_hfsj else @ord_hfsj end,ord_hsjg=case when @ord_hsjg is null or @ord_hsjg = '_none_'  then ord_hsjg else @ord_hsjg end,ord_khmyd=case when @ord_khmyd is null or @ord_khmyd = '_none_'  then ord_khmyd else @ord_khmyd end,ord_spyz=case when @ord_spyz is null or @ord_spyz = '_none_'  then ord_spyz else @ord_spyz end,ord_tscljg=case when @ord_tscljg is null or @ord_tscljg = '_none_'  then ord_tscljg else @ord_tscljg end,ord_tsdw=case when @ord_tsdw is null or @ord_tsdw = '_none_'  then ord_tsdw else @ord_tsdw end,ord_tsdx=case when @ord_tsdx is null or @ord_tsdx = '_none_'  then ord_tsdx else @ord_tsdx end,ord_tslx=case when @ord_tslx is null or @ord_tslx = '_none_'  then ord_tslx else @ord_tslx end,ord_tsly=case when @ord_tsly is null or @ord_tsly = '_none_'  then ord_tsly else @ord_tsly end,ord_tspd=case when @ord_tspd is null or @ord_tspd = '_none_'  then ord_tspd else @ord_tspd end,ord_tswt=case when @ord_tswt is null or @ord_tswt = '_none_'  then ord_tswt else @ord_tswt end,ord_tsxq=case when @ord_tsxq is null or @ord_tsxq = '_none_'  then ord_tsxq else @ord_tsxq end,ord_zgjgfk=case when @ord_zgjgfk is null or @ord_zgjgfk = '_none_'  then ord_zgjgfk else @ord_zgjgfk end,ord_jjgcs=case when @ord_jjgcs is null or @ord_jjgcs = '_none_'  then ord_jjgcs else @ord_jjgcs end,ord_jjmd=case when @ord_jjmd is null or @ord_jjmd = '_none_'  then ord_jjmd else @ord_jjmd end,ord_wtms=case when @ord_wtms is null or @ord_wtms = '_none_'  then ord_wtms else @ord_wtms end,ord_wxlx=case when @ord_wxlx is null or @ord_wxlx = '_none_'  then ord_wxlx else @ord_wxlx end,ord_yjddsj=case when @ord_yjddsj is null or @ord_yjddsj <= @nonedate then ord_yjddsj else @ord_yjddsj end,ord_symddpid=case when @ord_symddpid is null or @ord_symddpid = -999999999  then ord_symddpid else @ord_symddpid end,ord_zdmddgcsid=case when @ord_zdmddgcsid is null or @ord_zdmddgcsid = -999999999  then ord_zdmddgcsid else @ord_zdmddgcsid end,ord_khid=case when @ord_khid is null or @ord_khid = -999999999  then ord_khid else @ord_khid end,ord_xh1=case when @ord_xh1 is null or @ord_xh1 = '_none_'  then ord_xh1 else @ord_xh1 end,ord_authority_factor=case when @ord_authority_factor is null or @ord_authority_factor = -999999999  then ord_authority_factor else @ord_authority_factor end,ord_zxmddpid=case when @ord_zxmddpid is null or @ord_zxmddpid = -999999999  then ord_zxmddpid else @ord_zxmddpid end,ord_psmddpid=case when @ord_psmddpid is null or @ord_psmddpid = -999999999  then ord_psmddpid else @ord_psmddpid end,ord_zdmddsyrid=case when @ord_zdmddsyrid is null or @ord_zdmddsyrid = -999999999  then ord_zdmddsyrid else @ord_zdmddsyrid end,ord_bzlj=case when @ord_bzlj is null or @ord_bzlj = '_none_'  then ord_bzlj else @ord_bzlj end,ord_gcssj=case when @ord_gcssj is null or @ord_gcssj = '_none_'  then ord_gcssj else @ord_gcssj end,ord_jbexecutor=case when @ord_jbexecutor is null or @ord_jbexecutor = '_none_'  then ord_jbexecutor else @ord_jbexecutor end,ord_jbtitle=case when @ord_jbtitle is null or @ord_jbtitle = '_none_'  then ord_jbtitle else @ord_jbtitle end,ord_jsfl=case when @ord_jsfl is null or @ord_jsfl = '_none_'  then ord_jsfl else @ord_jsfl end,ord_mddh=case when @ord_mddh is null or @ord_mddh = '_none_'  then ord_mddh else @ord_mddh end,ord_mddz=case when @ord_mddz is null or @ord_mddz = '_none_'  then ord_mddz else @ord_mddz end,ord_mdfwl=case when @ord_mdfwl is null or @ord_mdfwl = '_none_'  then ord_mdfwl else @ord_mdfwl end,ord_shfwl=case when @ord_shfwl is null or @ord_shfwl = '_none_'  then ord_shfwl else @ord_shfwl end,ord_wrkid=case when @ord_wrkid is null or @ord_wrkid = -999999999  then ord_wrkid else @ord_wrkid end,ord_xh2=case when @ord_xh2 is null or @ord_xh2 = '_none_'  then ord_xh2 else @ord_xh2 end,ord_xh3=case when @ord_xh3 is null or @ord_xh3 = '_none_'  then ord_xh3 else @ord_xh3 end,ord_xxmb=case when @ord_xxmb is null or @ord_xxmb = '_none_'  then ord_xxmb else @ord_xxmb end,ord_yggh=case when @ord_yggh is null or @ord_yggh = '_none_'  then ord_yggh else @ord_yggh end,ord_zxgl1=case when @ord_zxgl1 is null or @ord_zxgl1 = '_none_'  then ord_zxgl1 else @ord_zxgl1 end,ord_zdmrid=case when @ord_zdmrid is null or @ord_zdmrid = -999999999  then ord_zdmrid else @ord_zdmrid end,ord_xzlbxh1id=case when @ord_xzlbxh1id is null or @ord_xzlbxh1id = -999999999  then ord_xzlbxh1id else @ord_xzlbxh1id end,ord_xzlbxh2id=case when @ord_xzlbxh2id is null or @ord_xzlbxh2id = -999999999  then ord_xzlbxh2id else @ord_xzlbxh2id end,ord_xzlbxh3id=case when @ord_xzlbxh3id is null or @ord_xzlbxh3id = -999999999  then ord_xzlbxh3id else @ord_xzlbxh3id end,
ord_finished=case when ord_finished=0 then 1 else ord_finished end ,
ord_updated=getdate() 
where ord_id=@ordid 
 if (@operation='continue') 
 begin
 set @nonedate = @nonedate 
 
 if (@ord_fordid>0) -- 说明有可能在修改上级关系
begin
if (dbo.checkprocedure('order_258_calc_relation')=0)
begin
exec order_258_calc_relation @cmpid , @ordid 
end
end 
 select 'ok' operation, '' errorinfo 
end  else 
begin
select @operation operation, @errorinfo errorinfo  
end


select top 1 @authority_new=substring(sys.fn_sqlvarbasetostr(HashBytes('MD5', convert(varchar(100),ord_createcuid) )),3,32) from order_258 with(nolock) where ord_id=@ordid 
set @authority_new = isnull(@authority_new,'') 

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 
-- 对项目进行处理
declare @style int 
declare @finished int 
select @style=ord_style , @finished=ord_finished from order_258 with(nolock) where ord_id=@ordid 
if (@style=0)
begin
if (@finished>=10 or @finished<=0) 
begin
delete mps_order with(rowlock) where ord_bizid=258 and ord_id=@ordid 
end  else 
begin
if (exists (select 1 from mps_order with(nolock) where ord_bizid=258 and ord_id=@ordid)) 
begin
update a set 
a.ord_bcid=b.ord_bcid ,
a.ord_branch=b.ord_branch,
a.ord_createdpid=b.ord_createdpid,
a.ord_createdepart=b.ord_createdepart,
a.ord_dpid=b.ord_dpid,
a.ord_depart=b.ord_depart,
a.ord_createcuid=b.ord_createcuid,
a.ord_creator=b.ord_creator,
a.ord_cuid=b.ord_cuid,
a.ord_empname=b.ord_empname,
a.ord_date=b.ord_date,
a.ord_enddate=b.ord_enddate,
a.ord_finished=b.ord_finished,
a.ord_no=b.ord_no,
a.ord_title=b.ord_title,
a.ord_status=b.ord_status,
a.ord_type =b.ord_type
from mps_order a with(rowlock), order_258 b with(nolock) 
where a.ord_bizid=258 and a.ord_id=b.ord_id and b.ord_id=@ordid 
end else
begin
insert into mps_order(
ord_bizid,
ord_id,
ord_cmpid,
ord_bcid,
ord_branch,
ord_createdpid,
ord_createdepart,
ord_dpid,
ord_depart,
ord_createcuid,
ord_creator,
ord_cuid,
ord_empname,
ord_date,
ord_enddate,
ord_finished,
ord_no,
ord_title,
ord_status,
ord_type )
select 
ord_bizid,
ord_id,
ord_cmpid,
ord_bcid,
ord_branch,
ord_createdpid,
ord_createdepart,
ord_dpid,
ord_depart,
ord_createcuid,
ord_creator,
ord_cuid,
ord_empname,
ord_date,
ord_enddate,
ord_finished,
ord_no,
ord_title,
ord_status,
ord_type
from order_258 with(nolock) where ord_id=@ordid 
end 
end 
end 
set nocount off 
end