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

CREATE procedure [dbo].[mps_new_work](
@cmpid int ,
@useruid int ,
@bizid int ,
@ordbcid int,
@ordid bigint,
@ordtype nvarchar(50),
@ordno varchar(50),
@ordtitle nvarchar(100),
@orddate datetime ,
@ordcreator nvarchar(50), 
@backinfo nvarchar(200),
@ftype varchar(10),
@fbizid int,
@fordid bigint,
@ford2tag int ,
@ford2id bigint,
@rtype varchar(10),
@rbizid int,
@rordid bigint,
@rord2tag int ,
@rord2id bigint,
@wrkid int ,
@fjbid bigint ,
@taskid int 
)
as
begin
-- 最新改进：取消了以前的confirm操作，所以有流程人员是预先确认，而不是以前的后期确认。
set nocount on
declare @otlevel int
declare @managelevel int
declare @professionlevel int 
declare @bcid int 
declare @dpid int 
declare @otid int 
declare @roleid int
declare @branch nvarchar(50)
declare @depart nvarchar(50)
declare @callname nvarchar(50)
declare @mysql nvarchar(4000) 

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 @tbname =@dbname+@tbname

declare @newcuid int 
set @newcuid = @useruid 
select top 1 @bcid=lgu_bcid,@dpid=lgu_dpid,@otid=lgu_otid,@roleid=lgu_function 
  , @branch=lgu_branch , @depart=lgu_depart , @callname=lgu_name  , @otlevel=lgu_otlevel ,
  @managelevel=lgu_managelevel , @professionlevel=lgu_professionlevel 
 from mps_login_role with(nolock) where lgu_cuid=@useruid  and   lgu_bcid=@ordbcid 
set @bcid = isnull(@bcid,0)
set @dpid = isnull(@dpid,0)
set @otid = isnull(@otid,0)
if (@dpid=0)   -- 如果找不到人，则从员工表中获取
begin
select top 1 @bcid=cu_bcid,@dpid=cu_dpid,@otid=cu_otid,@roleid=cu_function 
  , @branch=cu_branch , @depart=cu_depart , @callname=cu_name ,@otlevel=cu_otlevel ,
  @managelevel=cu_managelevel , @professionlevel=cu_professionlevel 
 from mps_employee with(nolock) where cu_id=@useruid   
end

declare @wrk_start int 
declare @wrk_type int  
select top 1  @wrk_type=wrk_type,@wrk_start=wrk_start from mps_flowwork with(nolock)  where wrk_id=@wrkid 
set @wrk_start = ISNULL(@wrk_start,0) 

declare @enabled int 
set @enabled = 0 
set @taskid = ISNULL(@taskid,0) 
if (@taskid=0) 
begin
if (@wrk_start>0) 
begin
if (not exists (select 1 from mps_job_task with(nolock)  where wt_wrkid=@wrkid and  wt_bizid=@bizid and wt_ordid=@ordid and wt_type='job')) 
begin
insert into mps_job_task(wt_begin,wt_wrkid,wt_cmpid,wt_bizid,wt_ordid,wt_useruid,wt_jbid,wt_task,wt_interval,wt_mbid,wt_type)
values(GETDATE()+@wrk_start/(60.00*24.00) , @wrkid , @cmpid,@bizid,@ordid,@useruid,0,
'exec mps_new_work '+convert(varchar(10),@cmpid)+','+
convert(varchar(10),@useruid )+','+
convert(varchar(10),@bizid )+','+
convert(varchar(10),@ordbcid)+','+
convert(varchar(10),@ordid)+','+
''''+@ordtype+''','+
''''+@ordno+''','+
''''+@ordtitle +''','+
''''+convert(varchar(19),@orddate,120)+''','+
''''+@ordcreator+''','+ 
''''+@backinfo+''','+
''''+@ftype+''','+
convert(varchar(10),@fbizid)+','+
convert(varchar(10),@fordid)+','+
convert(varchar(10),@ford2tag)+','+
convert(varchar(10),@ford2id)+','+
''''+@rtype+''','+
convert(varchar(10),@rbizid)+','+
convert(varchar(10),@rordid)+','+
convert(varchar(10),@rord2tag)+','+
convert(varchar(10),@rord2id)+','+
convert(varchar(10),@wrkid)+','+
convert(varchar(10),@fjbid)+',@taskid',5,0 ,'job'
)
return 
end else 
begin
return 
end 
end 
end 

declare @operation nvarchar(50) 
declare @errorinfo nvarchar(4000) 
set @operation = 'continue' 
set @errorinfo = '' 

if (dbo.checkprocedure('order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@wrkid)+'_wrknew_start')=0) 
begin
set @mysql='exec order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@wrkid)+'_wrknew_start 
@cmpid, @useruid, @bizid, @ordid, @wrkid, 0, @operation output, @errorinfo output ' 
exec sp_executesql @mysql, N'@cmpid int,@useruid int,@bizid int,@ordid bigint,@wrkid int ,@operation varchar(50) out,@errorinfo nvarchar(4000) out', 
@cmpid,@useruid,@bizid,@ordid,@wrkid,@operation output,@errorinfo output

end 

if (@operation<>'continue') 
begin
if (@taskid<=0)   -- 不成功，则送至任务  并且5分钟后再试
begin
if (not exists (select 1 from mps_job_task with(nolock) where wt_wrkid=@wrkid and  wt_bizid=@bizid and wt_ordid=@ordid and wt_type='job')) 
begin
insert into mps_job_task(wt_begin,wt_wrkid,wt_cmpid,wt_bizid,wt_ordid,wt_useruid,wt_jbid,wt_task,wt_interval,wt_mbid ,wt_type)
values(GETDATE()+5.00/(60.00*24.00) , @wrkid , @cmpid,@bizid,@ordid,@useruid,0,
'exec mps_new_work '+convert(varchar(10),@cmpid)+','+
convert(varchar(10),@useruid )+','+
convert(varchar(10),@bizid )+','+
convert(varchar(10),@ordbcid)+','+
convert(varchar(10),@ordid)+','+
''''+@ordtype+''','+
''''+@ordno+''','+
''''+@ordtitle +''','+
''''+convert(varchar(19),@orddate,120)+''','+
''''+@ordcreator+''','+ 
''''+@backinfo+''','+
''''+@ftype+''','+
convert(varchar(10),@fbizid)+','+
convert(varchar(10),@fordid)+','+
convert(varchar(10),@ford2tag)+','+
convert(varchar(10),@ford2id)+','+
''''+@rtype+''','+
convert(varchar(10),@rbizid)+','+
convert(varchar(10),@rordid)+','+
convert(varchar(10),@rord2tag)+','+
convert(varchar(10),@rord2id)+','+
convert(varchar(10),@wrkid)+','+
convert(varchar(10),@fjbid)+',@taskid' , 5,0 , 'job' 
)
end
end 
return 
end 

if (@taskid>0) 
begin
delete mps_job_task where wt_id=@taskid 
end 

-- 本工作有没有存在模板
declare @tmpid int 
select @tmpid=tmp_id from mps_template with(nolock) 
where tmp_cmpid=@cmpid and tmp_type='wrkform' and tmp_objid=@wrkid 
and len(convert(varchar(1000),tmp_form))>50 

set @tmpid=isnull(@tmpid,0) 

declare @orderstatus nvarchar(30) 
declare @orderkey varchar(30) 
exec mps_get_order_status @bizid,@ordid , @orderstatus output , @orderkey output 

declare @oldjbid bigint 
select @oldjbid = max(jb_id) from mps_job with(nolock) 

declare @jbnid bigint 
declare @jbnlevel int 
declare @jbnwrkcreated int  -- 这个变量表示：有预步骤，并且已经创建过wrkid的步骤的。那么有很事件就不需要再处理了。
set @jbnwrkcreated = 1 
-- 首先要决定下一步到底该建哪一个步
-- 1 先根据 @fjbid 去找，有没有与之相关的，需要建的步骤，如果有，就建之，若没有，则找一个没有建的来建
select top 1 @jbnid=jbn_id , @jbnlevel=jbn_level from mps_job_available with(nolock) 
where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_wrkid=@wrkid and jbn_created=1 and jbn_jbid=@fjbid 
order by jbn_id 
set @jbnid = isnull(@jbnid,0) 
if (@jbnid=0) 
begin
select top 1 @jbnid=jbn_id, @jbnlevel=jbn_level from mps_job_available with(nolock) 
where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_wrkid=@wrkid and jbn_created=1 and jbn_jbid<@fjbid 
order by jbn_id 
end 
if (exists (select 1 from mps_job_available with(nolock) where jbn_cmpid=@cmpid and jbn_crmid=@bizid and jbn_ordid=@ordid and jbn_wrkid=@wrkid and jbn_created=0)) 
begin
set @jbnwrkcreated = 0
end 
set @jbnlevel = ISNULL(@jbnlevel,0) 







declare @execsql nvarchar(4000) 
declare @auditsql nvarchar(4000) 
declare @coopsql nvarchar(4000) 
declare @viewsql nvarchar(4000) 
declare @logsql nvarchar(4000) 
declare @freesql nvarchar(4000) 

declare @myi int 
declare @extsql nvarchar(4000) 

declare @users table(pptype varchar(30), ppgroup int , useruid int) 
declare @tags varchar(100)
set @tags  = ''  
-- 如果有预处理步骤，才会做下面的，以下还有很多类似的判断
if (@jbnid=0) 
begin
select top 1 @execsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkexecutor' 
if (@tags>'') set @tags = @tags +',' 
set @tags  = @tags + 'wrkexecutor'
end 
if (@jbnwrkcreated=1) 
begin
select top 1 @auditsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkaudit' 
select top 1 @coopsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkcoop' 
select top 1 @viewsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkview' 
select top 1 @logsql = convert(nvarchar(4000),cd_text) from mps_extsqljs with(nolock) where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrklog' 
if (@tags>'') set @tags = @tags +',' 
set @tags  = @tags + 'wrkaudit,wrkcoop,wrkview,wrklog'
end 
set @execsql = isnull(@execsql,'') 
set @auditsql = isnull(@auditsql,'') 
set @coopsql = isnull(@coopsql,'') 
set @viewsql = isnull(@viewsql,'') 
set @logsql = isnull(@logsql,'') 
set @freesql = isnull(@freesql,'') 

set @execsql = replace(@execsql,'#user','#permit') 
set @auditsql = replace(@auditsql,'#user','#permit') 
set @coopsql = replace(@coopsql,'#user','#permit') 
set @viewsql = replace(@viewsql,'#user','#permit') 
set @logsql = replace(@logsql,'#user','#permit') 
set @freesql = replace(@freesql,'#user','#permit') 

declare @newsql nvarchar(4000) 
declare @newsql2 nvarchar(4000) 

set @newsql = @execsql+@auditsql+@coopsql+@viewsql+@logsql+@freesql 
if (len(@newsql)>20) 
begin



set @extsql = '' 
if (CHARINDEX('#master',@newsql)>0) 
begin
set @extsql = 'exec drop_temp_table ''#master'' '+char(13)+char(10) 
set @extsql = @extsql +' select * into #master from '+@tbname+' with(nolock) where ord_id='+convert(varchar(10),@ordid) +char(13)+char(10) 
end 
set @myi = 1 
while (@myi<300) 
begin
 if (charindex('#detail'+convert(varchar(10),@myi),@newsql)>0) 
 begin
set @extsql = 'exec drop_temp_table ''#detail'+convert(varchar(10),@myi)+''' '+char(13)+char(10) 
set @extsql = @extsql +' select * into #detail'+convert(varchar(10),@myi)+' from '+@tbname+'_'+convert(varchar(10),@myi)+' with(nolock) where ord2_ordid='+convert(varchar(10),@ordid) +char(13)+char(10) 
 end
 set @myi = @myi + 1
end

exec drop_temp_table '#permittion' 
create table #permittion(ptype varchar(30), ctgid int , pid int , pobjid1 int , pobjid2 int) 
exec drop_temp_table '#permit' 
create table #permit(type varchar(30),cuid int) 
if (len(@execsql)>10) 
begin
set @execsql = @extsql + @execsql
exec sp_executesql @execsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @fjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkexecutor' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@auditsql)>10) 
begin
set @auditsql = @extsql + @auditsql
exec sp_executesql @auditsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @fjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkaudit' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@coopsql)>10) 
begin
set @coopsql = @extsql + @coopsql
exec sp_executesql @coopsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @fjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkcoops' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@viewsql)>10) 
begin
set @viewsql = @extsql + @viewsql
exec sp_executesql @viewsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @fjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkview' else type end,@wrkid,5,cuid,0 from #permit v
delete #permit

if (len(@logsql)>10) 
begin
set @logsql = @extsql + @logsql
exec sp_executesql @logsql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @fjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrklog' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

if (len(@freesql)>10) 
begin
set @freesql = @extsql + @freesql
exec sp_executesql @freesql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @fjbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkfree' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

exec drop_temp_table '#permit' 
end

create table #myuser(pptype varchar(30),cuid int, grp int) 

if (@tags>'') 
exec mps_gen_powerusers @cmpid,@bcid,@dpid,@useruid,@otid,@roleid,@otlevel,@managelevel,@professionlevel,
'biz',@bizid,@ordid,@fjbid,@wrkid,@tags  


insert into @users( pptype ,ppgroup,useruid) 
select distinct pptype ,grp , cuid from #myuser 

drop table #myuser

exec drop_temp_table '#permittion' 
exec drop_temp_table '#permit' 

    --- 正常情况下，系统能唯一确人员清单，对于找不到的情况，系统要做缺省处理，也不再做unconfirm了
    -- @wrk_type 
    -- 如果没有人员，则将@useruid加入
    if (@jbnid=0) 
    begin
if (not exists (select 1 from @users where pptype='wrkexecutor')) 
begin
insert into @users( pptype ,ppgroup,useruid) 
select 'wrkexecutor' , 0 ,  @useruid 
end 
if (@wrk_type=0) -- 串签
begin
select top 1 @jbnid = useruid from @users where  pptype='wrkexecutor' 
delete @users where pptype='wrkexecutor'  and useruid<>@jbnid 
set @jbnid=0 
end 
end 

-----------------------------------------------------
if (@jbnid>0) -- 发现了预定义的流程
begin
delete @users where pptype = 'wrkexecutor' 
insert into @users( pptype ,ppgroup,useruid) 
select distinct 'wrkexecutor' , 0 ,  jbnu_cuid
from mps_job_available_users with(nolock)  
where jbnu_jbnid=@jbnid 
end 
create table #newjbs(newjbid bigint,jbcuid int) 

-- 判断一下，如果已经存在相同的工作，则不再增加

delete @users from mps_job with(nolock) where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and jb_wrkid=@wrkid and jb_wsid=0 and jb_cuid=useruid 
declare @hasusers int 
set @hasusers = 1 
if (exists (select 1 from @users)) set @hasusers = 0 
if (@hasusers=0) 
begin
insert into mps_job(jb_cmpid,jb_bcid,jb_dpid,jb_cuid,jb_executor,jb_crmid,jb_ordtype,jb_ordid,jb_ordno,jb_fjbid , 
jb_ordtitle,jb_orddate,jb_ordcreator,jb_wrkid,jb_title,jb_date,jb_enddate,jb_backinfo,jb_ordstatus,jb_ordkey,
jb_isauto,jb_sign, jb_branch,jb_depart ,jb_wrktype,jb_execnum,
jb_templateid,jb_enabled,jb_createcuid,jb_creator,jb_confirm,jb_limitedtime,jb_showinundo,jb_jbnid , jb_isfwd) 
select @cmpid,cu_bcid,cu_dpid,cu_id,cu_callname,@bizid,@ordtype,@ordid,@ordno,
case when @fjbid=-999999999 then 0 else @fjbid end,
@ordtitle,@orddate,@ordcreator,@wrkid,wrk_title,getdate(),'1900-1-1',@backinfo,@orderstatus,@orderkey,
   case when charindex('auto',wrk_keyparams)>0 then 0 else 1 end ,
case when charindex('sign',wrk_keyparams)>0 then 0 else 1 end ,@branch,@depart  , wrk_type ,wrk_executor_num , 
@tmpid,@enabled,@useruid,@callname   , 
0 ,ISNULL(wrk_limited,0) ,
case when charindex('hideinundo',wrk_keyparams)>0 then 1 else 0 end  , case when @jbnlevel>1 then 0 else @jbnid end , case when @jbnlevel>1 then 0 else 1 end 
from mps_flowwork  with(nolock) , mps_login with(nolock) , @users 
where wrk_id=@wrkid  and  useruid=cu_id and pptype = 'wrkexecutor'  
insert into #newjbs(newjbid,jbcuid) select jb_id,jb_cuid from mps_job with(nolock) where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and jb_id>@oldjbid and jb_wrkid=@wrkid and jb_wsid=0 
if (@jbnid>0) 
begin
if (@jbnlevel>1) 
begin
delete mps_job_available   where jbn_id=@jbnid 
end else 
begin
update mps_job_available with(rowlock) set jbn_created=0 where jbn_id=@jbnid 
end 
end 

declare @newjbid bigint 
select top 1 @newjbid=newjbid from #newjbs 
set @newjbid = isnull(@newjbid,0) 

-- 将所有的人员插入到jobusers 
delete @users from  mps_job_users with(nolock) 
where jp_type=pptype and jp_group=ppgroup and jp_jbid=@newjbid and jp_cuid=useruid 
insert into mps_job_users(jp_type,jp_group,jp_jbid,jp_cuid,jp_sjbid) 
select distinct pptype,ppgroup,@newjbid,useruid,case when @fjbid=-999999999 then 0 else @fjbid end from @users


-- 需要纠正前面的关系
insert into mps_job_relation(jr_jbid,jr_fjbid) 
select newjbid , @fjbid from #newjbs 

 
-- 执行工作新建前
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,@newjbid,@operation output ,@errorinfo  output 
end
--exec mps_wrk_beforenew @cmpid,@useruid,@bizid,@ordid ,@wrkid,@newjbid ,@operation output , @errorinfo output 
if (@operation='abort') 
begin
delete mps_job from #newjbs where jb_id=newjbid 
if (@errorinfo='') set @errorinfo = 'error' 
select @errorinfo 
return 
end

set @mysql = 'insert into '+ @tbname+'_users(ordp_type,ordp_ordid,ordp_cuid,ordp_otid) '+
'select ''bizview'',@ordid,jbcuid,-1 from #newjbs where not exists (select 1 from '+ @tbname+
'_users   where ordp_type=''bizview'' and ordp_ordid=@ordid and ordp_cuid=jbcuid ) ' 
exec sp_executesql @mysql, N'@ordid bigint',@ordid 

declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select newjbid,jbcuid  from   #newjbs  
  open status1
  declare @jbid bigint
  declare @jbcuid int 
  FETCH NEXT FROM status1 INTO @jbid  , @jbcuid 
  WHILE (@@FETCH_STATUS = 0)
  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  
FETCH NEXT FROM status1 INTO @jbid  , @jbcuid 
  END
  CLOSE status1
DEALLOCATE status1 
drop table #newjbs 
end 
set nocount off
end