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

-- 生成时间:2018-11-30 11:09:30 by ebs-core  
CREATE  procedure order_260_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_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_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_key varchar(30)=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_security int=default,
@ord_sobjid bigint=default,
@ord_status nvarchar(30)=default,
@ord_style int=default,
@ord_sxly nvarchar(100)=default,
@ord_template nvarchar(50)=default,
@ord_templateid int=default,
@ord_title nvarchar(100)=default,
@ord_type nvarchar(50)=default,
@ord_xm nvarchar(100)=default,
@ord_wxlx nvarchar(100)=default,
@ord_bjbz nvarchar(100)=default,
@ord_fwbid int=default,
@ord_fwbmc nvarchar(100)=default,
@ord_fwlx nvarchar(100)=default,
@ord_gjrq datetime=default,
@ord_imei nvarchar(100)=default,
@ord_jjmd nvarchar(100)=default,
@ord_jjrq datetime=default,
@ord_mdbh nvarchar(100)=default,
@ord_mddpid bigint=default,
@ord_sfzh nvarchar(100)=default,
@ord_sjh nvarchar(100)=default,
@ord_sjmc nvarchar(100)=default,
@ord_sn nvarchar(100)=default,
@ord_ys nvarchar(100)=default,
@ord_zdlx nvarchar(100)=default,
@ord_zdpp nvarchar(100)=default,
@ord_zdxh nvarchar(100)=default,
@ord_fwfyy decimal(18,2)=default,
@ord_gzfl1j nvarchar(100)=default,
@ord_gzfl2j nvarchar(100)=default,
@ord_gzms nvarchar(100)=default,
@ord_hxfs nvarchar(100)=default,
@ord_rjclff nvarchar(100)=default,
@ord_sjwcsj datetime=default,
@ord_tcck nvarchar(100)=default,
@ord_trck nvarchar(100)=default,
@ord_wgzk nvarchar(100)=default,
@ord_wlfyy decimal(18,2)=default,
@ord_wxbz nvarchar(2000)=default,
@ord_wxgcs nvarchar(100)=default,
@ord_wxgcs2 nvarchar(100)=default,
@ord_wxkssj datetime=default,
@ord_wxmd nvarchar(100)=default,
@ord_xjimei nvarchar(100)=default,
@ord_xjsn nvarchar(100)=default,
@ord_yjclff nvarchar(100)=default,
@ord_zbts nvarchar(100)=default,
@ord_cfsj datetime=default,
@ord_ddsj datetime=default,
@ord_wxwcsj datetime=default,
@ord_dqzt nvarchar(100)=default,
@ord_fcbz nvarchar(100)=default,
@ord_fzbsj datetime=default,
@ord_khqjsj datetime=default,
@ord_mdsjsj datetime=default,
@ord_zbfcsj datetime=default,
@ord_zbfdsj datetime=default,
@ord_zbsjsj datetime=default,
@ord_zbsjsjfc datetime=default,
@ord_dqbmsyrcuid bigint=default,
@ord_zdmddgcsid bigint=default,
@ord_dqmdgcsid bigint=default,
@ord_czfwbid bigint=default,
@ord_sxfj nvarchar(100)=default,
@ord_ssgzmsid bigint=default,
@ord_xzppxhid bigint=default,
@ord_xzsjid bigint=default,
@ord_rjclid bigint=default,
@ord_yjclid bigint=default,
@ord_cjjj nvarchar(100)=default,
@ord_xzxhid bigint=default,
@ord_xzsjppid bigint=default,
@ord_xzppid bigint=default,
@ord_cjclff nvarchar(100)=default,
@ord_cxshbh nvarchar(100)=default,
@ord_return2head int=default,
@ord_tzqjsj datetime=default,
@ord_fcsc decimal(18,2)=default,
@ord_fwsc decimal(18,2)=default,
@ord_qjjsr nvarchar(100)=default,
@ord_wxje nvarchar(100)=default,
@ord_wxzq decimal(18,2)=default,
@ord_authority_factor int=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_260 with(nolock) where ord_id=@ordid  
set @authority_old = isnull(@authority_old,'') 
 --- 以下是预处理事件中的临时表
 update order_260 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_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_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_key=case when @ord_key is null or @ord_key = '_none_'  then ord_key else @ord_key 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_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_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_sxly=case when @ord_sxly is null or @ord_sxly = '_none_'  then ord_sxly else @ord_sxly 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_xm=case when @ord_xm is null or @ord_xm = '_none_'  then ord_xm else @ord_xm end,ord_wxlx=case when @ord_wxlx is null or @ord_wxlx = '_none_'  then ord_wxlx else @ord_wxlx end,ord_bjbz=case when @ord_bjbz is null or @ord_bjbz = '_none_'  then ord_bjbz else @ord_bjbz end,ord_fwbid=case when @ord_fwbid is null or @ord_fwbid = -999999999  then ord_fwbid else @ord_fwbid end,ord_fwbmc=case when @ord_fwbmc is null or @ord_fwbmc = '_none_'  then ord_fwbmc else @ord_fwbmc end,ord_fwlx=case when @ord_fwlx is null or @ord_fwlx = '_none_'  then ord_fwlx else @ord_fwlx end,ord_gjrq=case when @ord_gjrq is null or @ord_gjrq <= @nonedate then ord_gjrq else @ord_gjrq end,ord_imei=case when @ord_imei is null or @ord_imei = '_none_'  then ord_imei else @ord_imei end,ord_jjmd=case when @ord_jjmd is null or @ord_jjmd = '_none_'  then ord_jjmd else @ord_jjmd end,ord_jjrq=case when @ord_jjrq is null or @ord_jjrq <= @nonedate then ord_jjrq else @ord_jjrq end,ord_mdbh=case when @ord_mdbh is null or @ord_mdbh = '_none_'  then ord_mdbh else @ord_mdbh end,ord_mddpid=case when @ord_mddpid is null or @ord_mddpid = -999999999  then ord_mddpid else @ord_mddpid end,ord_sfzh=case when @ord_sfzh is null or @ord_sfzh = '_none_'  then ord_sfzh else @ord_sfzh end,ord_sjh=case when @ord_sjh is null or @ord_sjh = '_none_'  then ord_sjh else @ord_sjh end,ord_sjmc=case when @ord_sjmc is null or @ord_sjmc = '_none_'  then ord_sjmc else @ord_sjmc end,ord_sn=case when @ord_sn is null or @ord_sn = '_none_'  then ord_sn else @ord_sn end,ord_ys=case when @ord_ys is null or @ord_ys = '_none_'  then ord_ys else @ord_ys end,ord_zdlx=case when @ord_zdlx is null or @ord_zdlx = '_none_'  then ord_zdlx else @ord_zdlx end,ord_zdpp=case when @ord_zdpp is null or @ord_zdpp = '_none_'  then ord_zdpp else @ord_zdpp end,ord_zdxh=case when @ord_zdxh is null or @ord_zdxh = '_none_'  then ord_zdxh else @ord_zdxh end,ord_fwfyy=case when @ord_fwfyy is null or @ord_fwfyy = -999999999  then ord_fwfyy else @ord_fwfyy end,ord_gzfl1j=case when @ord_gzfl1j is null or @ord_gzfl1j = '_none_'  then ord_gzfl1j else @ord_gzfl1j end,ord_gzfl2j=case when @ord_gzfl2j is null or @ord_gzfl2j = '_none_'  then ord_gzfl2j else @ord_gzfl2j end,ord_gzms=case when @ord_gzms is null or @ord_gzms = '_none_'  then ord_gzms else @ord_gzms end,ord_hxfs=case when @ord_hxfs is null or @ord_hxfs = '_none_'  then ord_hxfs else @ord_hxfs end,ord_rjclff=case when @ord_rjclff is null or @ord_rjclff = '_none_'  then ord_rjclff else @ord_rjclff end,ord_sjwcsj=case when @ord_sjwcsj is null or @ord_sjwcsj <= @nonedate then ord_sjwcsj else @ord_sjwcsj end,ord_tcck=case when @ord_tcck is null or @ord_tcck = '_none_'  then ord_tcck else @ord_tcck end,ord_trck=case when @ord_trck is null or @ord_trck = '_none_'  then ord_trck else @ord_trck end,ord_wgzk=case when @ord_wgzk is null or @ord_wgzk = '_none_'  then ord_wgzk else @ord_wgzk end,ord_wlfyy=case when @ord_wlfyy is null or @ord_wlfyy = -999999999  then ord_wlfyy else @ord_wlfyy end,ord_wxbz=case when @ord_wxbz is null or @ord_wxbz = '_none_'  then ord_wxbz else @ord_wxbz end,ord_wxgcs=case when @ord_wxgcs is null or @ord_wxgcs = '_none_'  then ord_wxgcs else @ord_wxgcs end,ord_wxgcs2=case when @ord_wxgcs2 is null or @ord_wxgcs2 = '_none_'  then ord_wxgcs2 else @ord_wxgcs2 end,ord_wxkssj=case when @ord_wxkssj is null or @ord_wxkssj <= @nonedate then ord_wxkssj else @ord_wxkssj end,ord_wxmd=case when @ord_wxmd is null or @ord_wxmd = '_none_'  then ord_wxmd else @ord_wxmd end,ord_xjimei=case when @ord_xjimei is null or @ord_xjimei = '_none_'  then ord_xjimei else @ord_xjimei end,ord_xjsn=case when @ord_xjsn is null or @ord_xjsn = '_none_'  then ord_xjsn else @ord_xjsn end,ord_yjclff=case when @ord_yjclff is null or @ord_yjclff = '_none_'  then ord_yjclff else @ord_yjclff end,ord_zbts=case when @ord_zbts is null or @ord_zbts = '_none_'  then ord_zbts else @ord_zbts end,ord_cfsj=case when @ord_cfsj is null or @ord_cfsj <= @nonedate then ord_cfsj else @ord_cfsj end,ord_ddsj=case when @ord_ddsj is null or @ord_ddsj <= @nonedate then ord_ddsj else @ord_ddsj end,ord_wxwcsj=case when @ord_wxwcsj is null or @ord_wxwcsj <= @nonedate then ord_wxwcsj else @ord_wxwcsj end,ord_dqzt=case when @ord_dqzt is null or @ord_dqzt = '_none_'  then ord_dqzt else @ord_dqzt end,ord_fcbz=case when @ord_fcbz is null or @ord_fcbz = '_none_'  then ord_fcbz else @ord_fcbz end,ord_fzbsj=case when @ord_fzbsj is null or @ord_fzbsj <= @nonedate then ord_fzbsj else @ord_fzbsj end,ord_khqjsj=case when @ord_khqjsj is null or @ord_khqjsj <= @nonedate then ord_khqjsj else @ord_khqjsj end,ord_mdsjsj=case when @ord_mdsjsj is null or @ord_mdsjsj <= @nonedate then ord_mdsjsj else @ord_mdsjsj end,ord_zbfcsj=case when @ord_zbfcsj is null or @ord_zbfcsj <= @nonedate then ord_zbfcsj else @ord_zbfcsj end,ord_zbfdsj=case when @ord_zbfdsj is null or @ord_zbfdsj <= @nonedate then ord_zbfdsj else @ord_zbfdsj end,ord_zbsjsj=case when @ord_zbsjsj is null or @ord_zbsjsj <= @nonedate then ord_zbsjsj else @ord_zbsjsj end,ord_zbsjsjfc=case when @ord_zbsjsjfc is null or @ord_zbsjsjfc <= @nonedate then ord_zbsjsjfc else @ord_zbsjsjfc end,ord_dqbmsyrcuid=case when @ord_dqbmsyrcuid is null or @ord_dqbmsyrcuid = -999999999  then ord_dqbmsyrcuid else @ord_dqbmsyrcuid end,ord_zdmddgcsid=case when @ord_zdmddgcsid is null or @ord_zdmddgcsid = -999999999  then ord_zdmddgcsid else @ord_zdmddgcsid end,ord_dqmdgcsid=case when @ord_dqmdgcsid is null or @ord_dqmdgcsid = -999999999  then ord_dqmdgcsid else @ord_dqmdgcsid end,ord_czfwbid=case when @ord_czfwbid is null or @ord_czfwbid = -999999999  then ord_czfwbid else @ord_czfwbid end,ord_sxfj=case when @ord_sxfj is null or @ord_sxfj = '_none_'  then ord_sxfj else @ord_sxfj end,ord_ssgzmsid=case when @ord_ssgzmsid is null or @ord_ssgzmsid = -999999999  then ord_ssgzmsid else @ord_ssgzmsid end,ord_xzppxhid=case when @ord_xzppxhid is null or @ord_xzppxhid = -999999999  then ord_xzppxhid else @ord_xzppxhid end,ord_xzsjid=case when @ord_xzsjid is null or @ord_xzsjid = -999999999  then ord_xzsjid else @ord_xzsjid end,ord_rjclid=case when @ord_rjclid is null or @ord_rjclid = -999999999  then ord_rjclid else @ord_rjclid end,ord_yjclid=case when @ord_yjclid is null or @ord_yjclid = -999999999  then ord_yjclid else @ord_yjclid end,ord_cjjj=case when @ord_cjjj is null or @ord_cjjj = '_none_'  then ord_cjjj else @ord_cjjj end,ord_xzxhid=case when @ord_xzxhid is null or @ord_xzxhid = -999999999  then ord_xzxhid else @ord_xzxhid end,ord_xzsjppid=case when @ord_xzsjppid is null or @ord_xzsjppid = -999999999  then ord_xzsjppid else @ord_xzsjppid end,ord_xzppid=case when @ord_xzppid is null or @ord_xzppid = -999999999  then ord_xzppid else @ord_xzppid end,ord_cjclff=case when @ord_cjclff is null or @ord_cjclff = '_none_'  then ord_cjclff else @ord_cjclff end,ord_cxshbh=case when @ord_cxshbh is null or @ord_cxshbh = '_none_'  then ord_cxshbh else @ord_cxshbh end,ord_return2head=case when @ord_return2head is null or @ord_return2head = -999999999  then ord_return2head else @ord_return2head end,ord_tzqjsj=case when @ord_tzqjsj is null or @ord_tzqjsj <= @nonedate then ord_tzqjsj else @ord_tzqjsj end,ord_fcsc=case when @ord_fcsc is null or @ord_fcsc = -999999999  then ord_fcsc else @ord_fcsc end,ord_fwsc=case when @ord_fwsc is null or @ord_fwsc = -999999999  then ord_fwsc else @ord_fwsc end,ord_qjjsr=case when @ord_qjjsr is null or @ord_qjjsr = '_none_'  then ord_qjjsr else @ord_qjjsr end,ord_wxje=case when @ord_wxje is null or @ord_wxje = '_none_'  then ord_wxje else @ord_wxje end,ord_wxzq=case when @ord_wxzq is null or @ord_wxzq = -999999999  then ord_wxzq else @ord_wxzq 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_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_260_calc_relation')=0)
begin
exec order_260_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_260 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_260_authority_factor with(nolock) where ordaf_value=@authority_new)) 
begin
insert into order_260_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_260 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=260 and ord_id=@ordid 
end  else 
begin
if (exists (select 1 from mps_order with(nolock) where ord_bizid=260 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_260 b with(nolock) 
where a.ord_bizid=260 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_260 with(nolock) where ord_id=@ordid 
end 
end 
end 
set nocount off 
end