IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_do_unconfirm_job]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_do_unconfirm_job]
GO
CREATE procedure [dbo].[mps_do_unconfirm_job]( 
@cmpid int , 
@useruid int , 
@ordbcid int ,
@bizid int , 
@ordid bigint ,
@execs varchar(8000),
@coops varchar(8000) 
) as
begin 
set nocount on 

declare @t1 table(jbtype1 varchar(30), jbid1 bigint , grp1 int , cuid1 int) 
declare @s varchar(8000) 
declare @s1 varchar(100) 
declare @s2 varchar(100) 
declare @i int 

set @s = @execs 
while (@s>'') 
begin
set @i = charindex('|',@s) 
if (@i>0) 
begin
set @s1 = substring(@s,1,@i-1)
set @s = substring(@s,@i+1,len(@s)-@i) 
end else 
begin
set @s1 = @s 
set @s = '' 
end
-- jbid,cuid,grpid
set @i = charindex(',',@s1) 
set @s2 = substring(@s1,1,@i-1) 
set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
set @i = charindex(',',@s1) 
if (@i>0)
begin
insert into @t1(jbtype1,jbid1,grp1,cuid1) 
select 'wrkexecutor',convert(bigint,@s2),convert(int,substring(@s1,@i+1,len(@s1)-@i)),convert(int,substring(@s1,1,@i-1)) 
end
end

set @s = @coops 
while (@s>'') 
begin
set @i = charindex('|',@s) 
if (@i>0) 
begin
set @s1 = substring(@s,1,@i-1)
set @s = substring(@s,@i+1,len(@s)-@i) 
end else 
begin
set @s1 = @s 
set @s = '' 
end
-- jbid,cuid,grpid
set @i = charindex(',',@s1) 
set @s2 = substring(@s1,1,@i-1) 
set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
set @i = charindex(',',@s1) 
if (@i>0)
begin
insert into @t1(jbtype1,jbid1,grp1,cuid1) 
select 'wrkcoops',convert(bigint,@s2),convert(int,substring(@s1,@i+1,len(@s1)-@i)),convert(int,substring(@s1,1,@i-1)) 
end
end


-- @t1 table(jbtype1 varchar(30), jbid1 bigint , grp1 int , cuid1 int) 

declare @j1 table(jjbid bigint ,jwrktype int) 
insert into @j1(jjbid ,jwrktype)
select jb_id , jb_wrktype 
from mps_job 
where jb_cmpid=@cmpid and jb_ordid=@ordid  and jb_cuid=@useruid and jb_crmid=@bizid 
and jb_back=1 and jb_isauto=1 and jb_confirm=1 and jb_wsid=0  

declare @t2 table(nid2 int identity(1, 1),jbtype2 varchar(30),jbid2 bigint , grp2 int , cuid2 int ,flag2 int ,  
bcid2 int,dpid2 int,branch2 nvarchar(50),depart2 nvarchar(50),name2 nvarchar(50),wrktype2 int) 

insert into @t2(jbtype2,jbid2,grp2,cuid2,flag2,bcid2,dpid2,branch2,depart2,name2,wrktype2)
select jp_type , jjbid, jp_group , jp_cuid , 0 , 0,0,'','','',jwrktype 
from @j1,mps_job_users 
where jjbid=jp_jbid and  
(jp_type='wrkexecutor' or jp_type='wrkcoops')

insert into @t2(jbtype2,jbid2,grp2,cuid2,flag2,bcid2,dpid2,branch2,depart2,name2,wrktype2 )
select jbtype1,jjbid,grp1,cuid1,0 , 0,0,'','','',jwrktype 
from @j1 , @t1 
where not exists (select 1 from @t2 where jbtype2=jbtype1 and jbid2=jjbid and grp2=grp1 and cuid2=cuid1 ) 
 

-- 先纠正一下，有没有执行人找不到的情况 ，也就是说，只有一条记录的情况
declare @o table(grp int , jbid bigint , ic1 int) 
insert into @o(grp,jbid,ic1) 
select grp2 , jbid2 , count(1) 
from @t2 
where jbtype2='wrkexecutor'  
group by grp2 , jbid2  

delete @o where ic1<>1  

update @t2 
set cuid2=cuid1 
from @t1 , @o 
where jbtype2='wrkexecutor' and jbtype1='wrkexecutor' and 
grp2=grp1 and jbid2=jbid1 and grp=grp2 and jbid=jbid2 
 

-- t1中有，t2中没有，则认为是多余的
update @t2 
set flag2=1 
from @t1  
where jbtype1=jbtype2 and 
grp2=grp1 and jbid2=jbid1  and cuid1=cuid2 


-- 删除没有用的
delete mps_job_users 
from @t2 
where flag2=0 and jp_type=jbtype2 and 
jp_group=grp2 and jp_jbid=jbid2 and jp_cuid=cuid2 

delete @t2 where flag2=0 or jbtype2='wrkcoops'
-- 删除
 

-- fixed userinfo 
update @t2 set
name2 = lgu_name ,
depart2 = lgu_depart , 
branch2 = lgu_branch ,
dpid2 = lgu_dpid ,
bcid2 = lgu_bcid 
from mps_login_role 
where lgu_bcid=@ordbcid and lgu_cuid=cuid2 

-- fixed again
update @t2 set
name2 = cu_name ,
depart2 = cu_depart , 
branch2 = cu_branch ,
dpid2 = cu_dpid ,
bcid2 = cu_bcid 
from mps_employee 
where cu_cmpid=@cmpid and cu_id=cuid2 and dpid2<=0 

-- 将每一条记录回写到job表中
declare @t3 table(nid3 int , jbid3 bigint) 
insert into @t3(nid3,jbid3)  
select min(nid2) , jbid2 
from @t2 
group by jbid2 

update mps_job set 
jb_bcid=bcid2 , jb_dpid=dpid2,jb_cuid=cuid2 , 
jb_branch=branch2 , jb_depart=depart2 , jb_executor=name2 ,
jb_confirm=0 
from @t2 , @t3 
where nid2=nid3 and jbid2=jb_id 


declare @jbs table(jbid bigint) 
insert into @jbs(jbid) 
select distinct jb_id from mps_job , @t2 , @t3 where nid2=nid3 and jbid2=jb_id  
-- 将t3 删除
delete @t2 from @t3  where nid2=nid3 

-- 如果是汇签或任签，则生成新的job记录
insert into mps_job(jb_bcid,jb_branch,jb_dpid,jb_depart,jb_cuid,jb_executor,
jb_cmpid,jb_crmid,jb_ordtype,jb_ordid,jb_ordno, jb_ordtitle,jb_orddate,jb_ordcreator,
jb_wrkid,jb_title,jb_date,jb_enddate,jb_isauto,jb_templateid,jb_sign,jb_back,
jb_enabled,jb_createcuid,jb_creator,jb_fjbid,jb_confirm,jb_limitedtime,jb_elapsedtime,
jb_ordstatus,jb_ordkey,jb_wrktype,jb_execnum)
select distinct bcid2,branch2,dpid2,depart2,cuid2,name2,
jb_cmpid,jb_crmid,jb_ordtype,jb_ordid,jb_ordno, jb_ordtitle,jb_orddate,jb_ordcreator,
jb_wrkid,jb_title,jb_date,jb_enddate,jb_isauto,jb_templateid,jb_sign,jb_back,
jb_enabled,jb_createcuid,jb_creator,jb_fjbid,jb_confirm,jb_limitedtime,jb_elapsedtime,
jb_ordstatus,jb_ordkey,jb_wrktype,jb_execnum
from @t2 , mps_job 
where jbtype2='wrkexecutor' and (wrktype2=1 or wrktype2=2) and jbid2=jb_id 


-- 接着[mps_new_work] 中未继续的程序 
-- 执行工作新建前
declare @operation varchar(50) 
declare @errorinfo nvarchar(4000) 
declare @mysql nvarchar(4000) 
declare @wrkid int 
declare @ordtype nvarchar(50)
declare @ordno varchar(50) 
declare @ordtitle nvarchar(200) 
declare @orddate datetime 
declare @ordcreator  nvarchar(50)
declare @ftype varchar(30)
declare @fbizid int
declare @fordid bigint
declare @ford2tag int
declare @ford2id bigint
declare @rtype varchar(30)
declare @rbizid int
declare @rordid bigint
declare @rord2tag int
declare @rord2id bigint

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 

set @mysql = 'select top 1 @ordtype=ord_type , @ordno=ord_no,@ordtitle=ord_title,@orddate=ord_date,@ordcreator=ord_creator,
@ftype=ord_ftype,@fbizid=ord_fbizid,@fordid=ord_fordid,@ford2tag=ord_ford2tag,@ford2id=ord_ford2id,
@rtype=ord_rtype,@rbizid=ord_rbizid,@rordid=ord_rordid,@rord2tag=ord_rord2tag,@rord2id=ord_rord2id 
from '+@dbname+@tbname+' where ord_id=@ordid ' 
exec sp_executesql @mysql, N'@ordid bigint, @ordtype nvarchar(50) output ,
@ordno varchar(50) output ,
@ordtitle nvarchar(200) output ,
@orddate datetime output ,
@ordcreator  nvarchar(50) output ,
@ftype varchar(30) output ,
@fbizid int output ,
@fordid bigint output ,
@ford2tag int output ,
@ford2id bigint output ,
@rtype varchar(30) output ,
@rbizid int output ,
@rordid bigint output ,
@rord2tag int output ,
@rord2id bigint output ', 
@ordid,
@ordtype  output ,
@ordno  output ,
@ordtitle  output ,
@orddate  output ,
@ordcreator  output ,
@ftype  output ,
@fbizid  output ,
@fordid  output ,
@ford2tag  output ,
@ford2id  output ,
@rtype  output ,
@rbizid  output ,
@rordid  output ,
@rord2tag  output ,
@rord2id  output  

declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select jbid,jb_wrkid,jb_cuid  from @jbs  , mps_job  where jb_id=jbid 
  open status1
  declare @jbid bigint
  declare @jbcuid int 
  FETCH NEXT FROM status1 INTO @jbid  , @wrkid ,@jbcuid 
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
   set @operation = 'continue' 
set @errorinfo = '' 
set @mysql = 'order_'+CONVERT(varchar(10),@bizid)+'_'+CONVERT(varchar(10),@wrkid)+'_wrknew_before' ;
if (dbo.checkprocedure(@mysql)=0)
begin
set @mysql = 'exec '+@mysql+' @cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@operation output,@errorinfo output '
exec sp_executesql @mysql, N'@cmpid int, @useruid int ,@bizid int ,@ordid bigint ,@wrkid int ,@jbid bigint ,@operation varchar(50) output,@errorinfo nvarchar(4000) output', 
@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@operation output ,@errorinfo  output 
end
if (@operation='abort') 
begin
delete mps_job where jb_id=@jbid 
if (@errorinfo='') set @errorinfo = 'error' 
end else 
begin
exec mps_job_enabled @cmpid ,@jbcuid ,@bizid ,@ordbcid,@ordid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,
@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@wrkid,@jbid  
   end 
   FETCH NEXT FROM status1 INTO @jbid  , @wrkid ,@jbcuid 
  END
  CLOSE status1
DEALLOCATE status1 

set nocount off 
end