IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_246_dolinkdata_3462]') AND type = 'P')
DROP PROCEDURE [dbo].[order_246_dolinkdata_3462]
GO
create procedure order_246_dolinkdata_3462(
@cmpid int ,
@useruid int ,
@bizid int ,
@ordbcid int,
@ordid bigint,
@jbid bigint,
@ftype varchar(10),
@fbizid int,
@fordid bigint,
@ford2tag int ,
@ford2id bigint,
@rtype varchar(10),
@rbizid int,
@rordid bigint,
@rord2tag int ,
@rord2id bigint 
) 
as 
begin 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
declare @operation varchar(50) 
declare @errorinfo nvarchar(4000)  
declare @newobjid bigint 
declare @ident varchar(40) 
declare @newsql nvarchar(max) 
declare @condition nvarchar(4000) 
declare @authority varchar(200) 
create table #mptypes(mpt varchar(20)) 
declare @bcid int 
set @bcid=@ordbcid 
declare @i int,@curInd int,@strlen int,@tmps varchar(100) ,@srcs varchar(2000) 



set @operation = 'continue' 
set @errorinfo='' 
set @newobjid = 0 
set @ident=convert(varchar(100),convert(decimal(18,11),CURRENT_TIMESTAMP)) 
set @newsql = '' 
set @authority = '' 
set @condition = '' 
-- 以下是用户扩展的程序  ---- 

-- 以上是用户扩展的程序  ---- 
 
set @srcs = @authority  
if (@srcs='') set @srcs='mydocview' 
set @authority=@srcs  
select @curInd=1,@strlen=LEN(@srcs)  
while (@curInd<=@strlen)  
begin  
select @i=charindex(',',@srcs,@curInd)  
if (@i>0)  
begin  
select @tmps=SUBSTRING(@srcs,@curInd,@i-@curInd)  
set @curInd=@i+1  
end else if (@curInd<=@strlen) 
begin  
select @tmps=SUBSTRING(@srcs,@curInd,@strlen-@curInd+1)  
set @curInd=@strlen+1  
end  
if (@tmps>'') insert into #mptypes(mpt) values(@tmps)  
end 
if (@operation = 'continue') 
begin 

set @newsql='
if (@newobjid<=0) 
begin 
insert into mps_document(doc_cmpid,doc_bcid,doc_branch,doc_dpid,doc_depart,doc_tag,doc_cuid,doc_creator,doc_no,doc_title,doc_dcid,doc_security,doc_distroy,doc_deliver,doc_archive) 
select distinct  @cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,0,cu_id,cu_callname,@ident,''未录入'',0 ,5,getdate()+3000,0,0 from mps_login where  cu_id=@useruid 
select top 1 @newobjid = doc_id from mps_document where doc_cmpid=@cmpid and doc_no=@ident order by doc_id desc 
set @newobjid = ISNULL(@newobjid,0) 
end 
insert into mps_document_text(docd_docid,docd_text)
select @newobjid , ord3_text from order_246_0 with(nolock) where ord3_ordid=@ordid and ord3_idname=''context'' 
insert into mps_attach(da_cmpid,da_bcid,da_dpid,da_type,da_objid,da_objid2,da_categoryid,da_category,da_security,
da_filename,da_size,da_ext,da_date,da_cuid,da_uploader,da_realname,da_md5,da_device,da_destory,
da_icopath,da_originname,da_uploaduuid,da_currentchunks,da_success,da_fixpath,da_preview) 
select da_cmpid,da_bcid,da_dpid,''doc'',@newobjid,0,da_categoryid,da_category,da_security,
da_filename,da_size,da_ext,da_date,da_cuid,da_uploader,da_realname,da_md5,da_device,da_destory,
da_icopath,da_originname,da_uploaduuid,da_currentchunks,da_success,da_fixpath,da_preview 
from mps_attach with(nolock) where da_cmpid=@cmpid and da_type=''biz''+convert(varchar(10),@bizid) and da_objid=@ordid 
insert into mps_document_permission(docp_docid,docp_type,docp_ppid,docp_objid1,docp_objid2) 
select distinct @newobjid,''doc246''+case when charindex(''>'',mp_type)>0 then substring(mp_type,3,charindex(''>'',mp_type)-1) else substring(mp_type,3,20) end,mp_ppid,mp_objid1,mp_objid2 
from mps_permission with(nolock) , #mptypes  where mp_cmpid=@cmpid and mp_type=mpt+''>246'' and mp_ctgid=@ordid  
update order_246 with(rowlock) set ord_sobjid=@newobjid where ord_id=@ordid 
if (@authority>'''') 
begin 
insert into mps_document_users(docp_cuid,docp_docid,docp_otid,docp_type) 
select distinct ordp_cuid,@newobjid,-1,substring(mpt,3,20) 
from order_246_users , #mptypes 
where ordp_ordid=@ordid and  ordp_type=mpt and  
not exists (select 1 from mps_document_users where docp_cuid=ordp_cuid and docp_docid=@newobjid and docp_type=substring(mpt,3,20)) 
update  mps_document set doc_depart=dp_depart,doc_bcid=dp_bcid  from mps_depart where doc_id=@newobjid and doc_dpid=dp_id 
update  mps_document set doc_branch=bc_company  from mps_groupbranch where doc_id=@newobjid and doc_bcid=bc_id 
update  mps_document set doc_creator=cu_name  from mps_employee where doc_id=@newobjid and doc_cuid=cu_id 
end 

update old set  old.doc_author=new.ord_creator ,  old.doc_title=new.ord_bt ,  old.doc_tag=0 ,  old.doc_dcid=86 ,  old.doc_cuid=new.ord_cuid ,  old.doc_creator=new.ord_creator ,  old.doc_create=new.ord_date ,  old.doc_no=new.ord_no ,  old.doc_source=new.ord_createdepart
from order_246 new ,mps_document old 
where  doc_id=@newobjid and  new.ord_id=@ordid 

'
if (@condition>'') set @newsql=@newsql+' and ('+@condition+') ' 

exec sp_executesql @newsql, N'@cmpid int,@useruid int,@bizid int,@ordbcid int,@ordid bigint,@jbid bigint,@ftype varchar(10),@fbizid int,@fordid bigint,@ford2tag int ,@ford2id bigint,@rtype varchar(10),@rbizid int,@rordid bigint,@rord2tag int ,@rord2id bigint,@newobjid bigint,@ident varchar(40),@authority varchar(300),@bcid int ', @cmpid,@useruid,@bizid,@ordbcid,@ordid,@jbid,@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@newobjid,@ident,@authority,@bcid 
end 

--- 下面这段程序，是完成帐务处理------

--- 帐务处理完毕 ------

--- 下面这段程序，是完成物资帐务处理------

--- 物资帐务处理完毕 ------

drop table #mptypes 


end