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

-- 生成时间:2018-10-15 17:18:43 by ebs-core  
CREATE  procedure mps_customer1_newcst(
     @cmpid int, 
     @branchid int, 
     @userdpid int, 
     @userotid int, 
     @useruid int, 
     @ctid int,
     @fccid bigint ,
     @cccomp nvarchar(50),
     @ccname nvarchar(50),
     @ccmobile varchar(18),
     @ccidentno varchar(20) ,
     @returndatasets int ,  
     @extparam1 nvarchar(1000),
     @extparam2 nvarchar(1000),
     @extparam3 nvarchar(1000),
     @newccid bigint output  
  ) 
  as  
  begin  
  set nocount on  
  set @fccid = isnull(@fccid,0) 
  set @cccomp = isnull(@cccomp,'') 
  set @ccname = isnull(@ccname,'') 
  set @ccmobile = isnull(@ccmobile,'') 
  set @ccidentno = isnull(@ccidentno,'') 
 if (@fccid = -999999999) set @fccid = 0 
 if (@cccomp='_none_') set @cccomp = ''
 if (@ccname='_none_') set @ccname = ''
 if (@ccmobile='_none_') set @ccmobile = ''
 if (@ccidentno='_none_') set @ccidentno = ''
 
  if ((@extparam1 is null) or (@extparam1='_none_'))  set @extparam1 = '' 
  if ((@extparam2 is null) or (@extparam2='_none_'))  set @extparam2 = '' 
  if ((@extparam3 is null) or (@extparam3='_none_'))  set @extparam3 = '' 
  
  
  set @newccid = 0  
  
  declare @bizcode nvarchar(100)    
  select   @bizcode=ct_bmcode  from mps_csttype where  ct_id=@ctid    
  declare @ccno varchar(50)     
select top 1 @newccid=cc_id from mps_customer1 where cc_cmpid=@cmpid and cc_ctid=@ctid and cc_createcuid=@useruid and cc_code like '%临%'  
  set @newccid = ISNULL(@newccid,0) 
  if (@newccid>0) 
  begin
update mps_customer1 set cc_createtime = GETDATE() , cc_updated = GETDATE() where cc_id=@newccid 
  end else 
  begin
  insert into mps_customer1(cc_cmpid,cc_bcid,cc_branch,cc_dpid,cc_depart,cc_cuid,cc_cuname,cc_tag,cc_caption,cc_ctid,cc_type,cc_name,
  cc_company,cc_mobile,cc_identno ,cc_fccid,
  cc_createdpid,cc_createdepart,cc_createcuid,cc_creator,cc_createtime,cc_updated , cc_gender) 
  select @cmpid,bc_id,bc_company,dp_id,dp_depart,cu_id,cu_callname,ct_tag,ct_caption,ct_id,ct_type, @ccname,
  @cccomp,@ccmobile,@ccidentno,@fccid,
  dp_id,dp_depart,cu_id,cu_callname,getdate(),getdate() , -1
from mps_csttype,mps_login,mps_depart,mps_groupbranch 
where ct_id=@ctid and cu_id=@useruid and dp_id=@userdpid and bc_id=@branchid  and bc_cmpid=@cmpid  
select top 1 @newccid = cc_id from mps_customer1 where cc_cmpid=@cmpid and cc_ctid=@ctid and cc_createcuid=@useruid and cc_name='' order by cc_id desc  
  
set @newccid = isnull(@newccid,0) 
if (@newccid>0 and @fccid>0) 
begin
if (not exists (select 1 from mps_customer_relation with(nolock) where ccr_ccid=@newccid and ccr_fccid=@fccid))  
begin
insert into mps_customer_relation(ccr_ccid,ccr_fccid) values(@newccid,@fccid)
end 
end 
--处理before事件 
declare @errorinfo nvarchar(4000)  
  declare @operation varchar(50) 
  set @operation = 'continue'  
  set @errorinfo = '' 

  
-- 生成订单编号（临时） 
exec mps_bm_getorset @cmpid,'cst',@ctid,@newccid,@useruid,@bizcode,2,@ccno output,1   
update mps_customer1 set cc_code = @ccno where cc_id = @newccid   
  
   
  -- why here otid=-1 , it's a secret , I don't know yet 
  if (not exists (select 1 from mps_customer1_users where ccp_type='cstview' and ccp_ccid=@newccid and ccp_cuid=@useruid)) 
  insert into mps_customer1_users(ccp_type,ccp_ccid,ccp_cuid,ccp_otid) 
  select distinct 'cstview',@newccid,@useruid,-1  
  
  if (not exists (select 1 from mps_customer1_users where ccp_type='cstedit' and ccp_ccid=@newccid and ccp_cuid=@useruid)) 
  insert into mps_customer1_users(ccp_type,ccp_ccid,ccp_cuid,ccp_otid) 
  select distinct 'cstedit',@newccid,@useruid,-1  
  end 
   
  -- 返回客户数据  

  if (@returndatasets=0) select * from  mps_customer1 where cc_id=@newccid 
  if (@returndatasets=2) select @newccid newccid   
  set nocount off  
end