IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_199_dolinkdata_2388]') AND type = 'P')
DROP PROCEDURE [dbo].[order_199_dolinkdata_2388]
GO
create procedure order_199_dolinkdata_2388(
@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
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) 
declare @bcid int 
set @bcid=@ordbcid 



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 = '' 
-- 以下是用户扩展的程序  ---- 

-- 以上是用户扩展的程序  ---- 
if (@operation = 'continue') 
begin 


if (@ftype='biz' and @fbizid=202 and @ford2tag=0) 
set @newsql='
update old set  old.ord_updated=getdate() ,  old.ord_lxdh1=new.ord_lxdh1 ,  old.ord_dh=new.ord_dh ,  old.ord_jmfs=new.ord_jmfs ,  old.ord_jyfs=new.ord_jyfs ,  old.ord_kdcs=new.ord_kdcs ,  old.ord_kdpq=new.ord_kdpq ,  old.ord_khxm=new.ord_khxm ,  old.ord_qwsr=new.ord_qwsr ,  old.ord_sfzh=new.ord_sfzhm ,  old.ord_tzys=new.ord_tzys ,  old.ord_xb=new.ord_xb ,  old.ord_xcsxy=new.ord_xcsxy
from order_199 new ,order_202 old 
where  new.ord_id=@ordid 
 and old.ord_id=new.ord_fordid
'
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 



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 = '' 
-- 以下是用户扩展的程序  ---- 

-- 以上是用户扩展的程序  ---- 
if (@operation = 'continue') 
begin 

set @newsql='
if (@newobjid<=0) 
begin 
insert into mps_customer2(cc_cmpid,cc_bcid,cc_branch,cc_dpid,cc_depart,cc_tag,cc_caption,cc_ctid,cc_type,cc_code,cc_createcuid,cc_creator,cc_cuid,cc_cuname,cc_name) 
select @cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,2,ct_caption,0,'''',@ident,cu_id,cu_callname,cu_id,cu_callname,''未录入'' from mps_login,mps_csttype where ct_cmpid=@cmpid and ct_tag=2 and  cu_id=@useruid 
select top 1 @newobjid = cc_id from mps_customer2 where cc_cmpid=@cmpid and cc_code=@ident order by cc_id desc 
set @newobjid = ISNULL(@newobjid,0) 
delete mps_customer2_users where ccp_ccid=@newobjid 
if (not exists (select 1  from mps_customer2_users  where ccp_type=''cstview'' and ccp_ccid=@newobjid and ccp_cuid=@useruid)) insert into mps_customer2_users(ccp_cuid,ccp_ccid,ccp_type,ccp_otid) values(@useruid,@newobjid,''cstview'',-1) 
if (not exists (select 1 from mps_customer2_users   where ccp_type=''cstedit'' and ccp_ccid=@newobjid and ccp_cuid=@useruid)) insert into mps_customer2_users(ccp_cuid,ccp_ccid,ccp_type,ccp_otid) values(@useruid,@newobjid,''cstedit'',-1) 
end 
if (@authority>'''') 
begin 
set @authority = '',''+@authority+'',''  
insert into mps_customer2_users(ccp_cuid,ccp_ccid,ccp_otid,ccp_type) 
select distinct ordp_cuid,@newobjid,0,''cstview'' 
from order_199_users  
where ordp_ordid=@ordid and  @authority like ''%,''+ordp_type +'',%'' and  
not exists (select 1 from mps_customer2_users where ccp_cuid=ordp_cuid and ccp_ccid=@newobjid and ccp_type=''cstview'') 
update  mps_customer2 set cc_tag=ct_tag,cc_caption=ct_caption,cc_type=ct_type from mps_csttype where cc_id=@newobjid and cc_ctid=ct_id 
update  mps_customer2 set cc_depart=dp_depart,cc_bcid=dp_bcid  from mps_depart where cc_id=@newobjid and cc_dpid=dp_id 
update  mps_customer2 set cc_branch=bc_company  from mps_groupbranch where cc_id=@newobjid and cc_bcid=bc_id 
update  mps_customer2 set cc_creator=cu_name  from mps_employee where cc_id=@newobjid and cc_createcuid=cu_id 
update  mps_customer2 set cc_cuname=cu_name  from mps_employee where cc_id=@newobjid and cc_cuid=cu_id 
end 

update old set  old.cc_activetime=getdate() ,  old.cc_type=''加盟商'' ,  old.cc_tag=2 ,  old.cc_mobile=new.ord_dh ,  old.cc_creator=new.ord_creator ,  old.cc_createtime=new.ord_bfsj ,  old.cc_createcuid=new.ord_createcuid ,  old.cc_name=new.ord_khxm ,  old.cc_status=''已加盟'' ,  old.cc_ctid=2 ,  old.cc_code=new.ord_khbh
from order_199 new ,mps_customer2 old 
where  cc_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 

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

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

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

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


end