IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_abolish_order]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_abolish_order]
GO
CREATE procedure [dbo].[mps_abolish_order](
@cmpid int ,
@useruid int,
@bizid int,
@ordid bigint,
@reason nvarchar(1000)=default 
)
as
begin
declare @db varchar(50)
declare @tbid int
select @db=biz_database , @tbid=biz_table from   mps_biztypewhere biz_id=@bizid 
if (@tbid>0) select @db=biz_database , @tbid=biz_id from   mps_biztypewhere biz_id=@tbid 
if (@db>'') set @db=@db+'.dbo.' ;
if (@tbid=0) set @db=@db+'order_'+ltrim(rtrim(str(@bizid))) else set @db=@db+'order_'+ltrim(rtrim(str(@tbid))) 

declare @sql nvarchar(1000) 

set @sql = '
insert into mps_system_operation_log(op_cmpid,op_operation,op_objtype,op_objid,op_objid2,op_tid,
op_tcaption,op_reason,op_useruid,op_branch,op_bcid,op_depart,op_dpid,op_name) 
select @cmpid,''abolish'',''biz'',@bizid,0,@ordid,ord_no+''/''+ord_title,@reason,
@useruid,cu_branch, cu_bcid,cu_depart,cu_dpid,cu_callname 
from '+@db+' , mps_login where ord_id=@ordid and cu_id=@useruid ' 

exec sp_executesql @sql, N'@cmpid int ,@bizid int ,@ordid bigint , @useruid int,@reason nvarchar(1000) ', 
@cmpid,@bizid,@ordid,@useruid,@reason 

set @sql = 'update '+@db+' set ord_finished=-1,ord_status=''已作废'' where ord_id='+ltrim(rtrim(str(@ordid))) 
execute(@sql) 


delete mps_job_users 
from mps_job where jb_crmid=@bizid and jb_ordid=@ordid and jp_jbid=jb_id and jb_wsid=0 

update mps_account_operation set accop_back=0 
from mps_job
where accop_jbid=jb_id and jb_crmid=@bizid and jb_ordid=@ordid and jb_wsid=0  and accop_back=1 


delete mps_job_available where jbn_crmid=@bizid and jbn_ordid=@ordid 

update mps_warehouse_operation set whop_back=0 
from mps_job
where whop_jbid=jb_id and jb_crmid=@bizid and jb_ordid=@ordid and jb_wsid=0  and whop_back=1 

delete mps_job_task from mps_job
where jb_crmid=@bizid and jb_ordid=@ordid and 
wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_jbid=jb_id and wt_type='wrkalarm'

delete mps_job where jb_crmid=@bizid and jb_ordid=@ordid and jb_wsid=0 

delete mps_task where tsk_objtype='biz' and tsk_ctgid=@bizid and tsk_objid=@ordid 

delete mps_job_task where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_type='bizalarm'
delete mps_alarm_message where almmsg_objtype='biz' and almmsg_objid=@ordid 
end