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

CREATE procedure [dbo].[mps_backto_job](
@cmpid int ,
@useruid int ,
@gotojbid bigint ,
@reason nvarchar(500) 
)
as 
begin

set nocount on
declare @bizid int  
declare @ordid bigint 
declare @ordbcid int 
declare @ordtype nvarchar(100) 
declare @ordno nvarchar(100)
declare @ordtitle nvarchar(200)
declare @orddate datetime
declare @ordcreator nvarchar(50) 

select top 1 @bizid=jb_crmid, @ordid=jb_ordid,@ordbcid=jb_bcid , @ordtitle=jb_ordtitle , @ordno=jb_ordno ,
@orddate=jb_orddate , @ordcreator=jb_ordcreator ,@ordtype=jb_ordtype 
from mps_job where jb_id=@gotojbid 

declare @backjb table(id int identity(1,1),jbid bigint , first int) 
 -- 回到任意步
insert into @backjb(jbid,first) values(@gotojbid,0)

-- 从jid，包括(@jid) 开始，所以后面的工作都要都搜索出来，如果没有做，就删除，如果做了，就设置回退。
declare @ic int 
declare @ic2 int 
select  @ic=0 
set @ic2=0 
begin
while (@ic=@ic)
begin
insert into @backjb(jbid,first) select jr_jbid,1 from mps_job_relation , @backjb,mps_job 
where jr_fjbid=jbid and jr_jbid=jb_id and jb_back=1 
select @ic2=count(distinct jbid) from @backjb 
if (@ic2=@ic) 
begin
break 
end else 
begin
set @ic=@ic2 
end
end
end 
-- 删除没有做的
delete mps_job from @backjb where jb_id=jbid and jb_wsid=0 and jb_wsstatus='' 
-- 删除task 
delete mps_task from @backjb where tsk_cmpid=@cmpid and tsk_objtype='biz' and tsk_jbid=jbid  
-- 删除任务
delete mps_job_task from @backjb where wt_jbid=jbid and wt_type in ('job','wrkalarm')
delete mps_alarm_message from @backjb  where  almmsg_jbid=jbid 
-- 按先进后出原则，回退步骤及数据
declare status1 cursor LOCAL  FORWARD_ONLY READ_ONLY 
for select  jb_wrkid,jb_id from mps_job , @backjb where jb_id=jbid order by jb_enddate  desc , jb_id desc 
declare @jbid3 bigint
declare @wrkid3 int 
declare @operation varchar(200)
declare @errorinfo nvarchar(4000) 
set @operation='continue'
set @errorinfo=''
open status1
FETCH NEXT FROM status1 INTO   @wrkid3 , @jbid3 
WHILE (@@FETCH_STATUS = 0)
 BEGIN
 set @operation='continue'
 -- 执行回退sQL
 exec mps_job_afterback @cmpid,@useruid,@bizid,@ordid,@wrkid3,@jbid3,@operation output 
 if (@operation='continue') 
 begin
 -- 删除日志，同时回滚数据
 set @wrkid3=@wrkid3
 end 
 FETCH NEXT FROM status1 INTO   @wrkid3 , @jbid3 
 END
 CLOSE status1
DEALLOCATE status1 

-- 将所有的
declare @orderstatus nvarchar(30)
declare @orderkey nvarchar(30)
declare @mess nvarchar(200) 
select @mess='被'+cu_callname+'强制回退，理由是:'+@reason+',时间是'+convert(varchar(16),getdate(),120) from mps_login with(nolock) where cu_type=0 and cu_id=@useruid 
update mps_job set jb_back=0 , jb_text = case when jb_text='' then @mess else jb_text+'。'+@mess end from @backjb where jb_id=jbid and jb_back=1 

update mps_account_operation set accop_back=0 
from @backjb where accop_jbid=jbid  and accop_back=1

update mps_warehouse_operation set whop_back=0 
from @backjb where whop_jbid=jbid  and whop_back=1 


delete mps_job_task from @backjb 
where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_jbid=jbid and wt_type in ('job','wrkalarm') 

-- 如果是回退 就需要重新新建一步工作
declare @sql nvarchar(4000) 
declare @tbid int 
declare @jid bigint 
declare @db nvarchar(50) 
begin
set @sql=''  
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))) 
set @sql = 'update '+@db+' set ord_finished=1,ord_status=@ws_status ,  ord_key=@ws_key  where ord_id=@ordid ' 

declare status12 cursor LOCAL  FORWARD_ONLY READ_ONLY 
for select  distinct jb_cuid,jb_wrkid,jb_id , jb_ordstatus,jb_ordkey from mps_job , @backjb where jb_id=jbid  and first=0 
declare @newuseruid int 
declare @newwrkid int 
open status12
FETCH NEXT FROM status12 INTO   @newuseruid , @newwrkid , @jid,@orderstatus,@orderkey 
WHILE (@@FETCH_STATUS = 0)
 BEGIN
 -- 这里是需要重新还原业务状态
 exec sp_executesql @sql, N'@ws_status nvarchar(30) ,@ws_key varchar(30),@ordid bigint', @orderstatus,@orderkey,@ordid 
exec mps_new_work @cmpid ,@newuseruid ,@bizid,
@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,
@mess,'',0,0,0,0,
'',0,0,0,0,@newwrkid,@gotojbid,0
FETCH NEXT FROM status12 INTO   @newuseruid , @newwrkid , @jid,@orderstatus,@orderkey 
 END
 CLOSE status12
DEALLOCATE status12
end
 
set nocount off
end