IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_cstnew]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_cstnew]
GO
create procedure [dbo].[mps_gen_cstnew](
@cmpid int,
@ctid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @i int 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @mctid int 
set @mctid = @ctid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'cst',@mctid,0,@tbname output,@fkey output ,@dbname output 

declare @sql nvarchar(MAX) 

-- 补充一段程序，用于生成存储过程
declare @trg int 
declare @trn varchar(60) 
set @trn = @dbname+@tbname+'_update'
set @trg = 1 
exec mps_sys_checktrigger @trn , @trg output 
if (@trg=0) set @sql = 'alter ' else set @sql='create ' 
set @sql=@sql+' trigger '+@tbname+'_update on '+@tbname+' for update as
begin
   insert into mps_sync(sk_type,sk_bizid,sk_tag2,sk_operation,sk_objid) 
   select 2,cc_ctid,0,0,cc_id from inserted '+char(13)+char(10) 
  -- 判断有没有用户定义的触发
  if (dbo.mps_sys_checkkeywords('cst.aftermodify_trg',@ctid,0,'','')=0) 
  begin
  set @sql=@sql+char(13)+char(10)+char(13)+char(10)+
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('cst.aftermodify_trg',@ctid,0)+char(13)+char(10)+
  '-- 用户定义的语句完毕 ' 
end   
  
  set @sql=@sql+char(13)+char(10)+N'
end'

set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
  
  set @trg = 1 
  set @trn = @dbname+@tbname+'_delete'
exec mps_sys_checktrigger @trn , @trg output 
if (@trg=0) set @sql = 'alter ' else set @sql='create ' 
set @sql=@sql+' trigger '+@tbname+'_delete on '+@tbname+' for delete as
begin
   insert into mps_sync(sk_type,sk_bizid,sk_tag2,sk_operation,sk_objid) 
   select 2,cc_ctid,0,-1,cc_id from deleted '+char(13)+char(10) 
  -- 判断有没有用户定义的触发
  if (dbo.mps_sys_checkkeywords('cst.afterdelete_trg',@ctid,0,'','')=0) 
  begin
  set @sql=@sql+char(13)+char(10)+ 
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('cst.afterdelete_trg',@ctid,0)+char(13)+char(10)+
  '-- 用户定义的语句完毕 ' 
end   
  
  set @sql=@sql+char(13)+char(10)+N'
end'

set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
  
  set @trg = 1 
  set @trn = @dbname+@tbname+'_insert'
exec mps_sys_checktrigger @trn , @trg output 
if (@trg=0) set @sql = 'alter ' else set @sql='create ' 
set @sql=@sql+' trigger '+@tbname+'_insert on '+@tbname+' for insert as
begin
   insert into mps_sync(sk_type,sk_bizid,sk_tag2,sk_operation,sk_objid) 
   select 2,cc_ctid,0,1,cc_id from inserted '+char(13)+char(10) 
  -- 判断有没有用户定义的触发
  if (dbo.mps_sys_checkkeywords('cst.aftercreate_trg',@ctid,0,'','')=0) 
  begin
  set @sql=@sql+char(13)+char(10)+ 
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('cst.aftercreate_trg',@ctid,0)+char(13)+char(10)+
  '-- 用户定义的语句完毕 ' 
end   
  
  set @sql=@sql+char(13)+char(10)+N'
end'

set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
 
 
  -- 以下代码是生成子表的触发器
 
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct ct2_tag  from mps_csttype2  where ct2_ctid=@ctid 
open c1
declare @ord2tag2 int 
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
-- insert 
set @trg = 1 
set @trn = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2)+'_insert'
exec mps_sys_checktrigger @trn , @trg output 
if (dbo.mps_sys_checkkeywords('cst.afternewcst2_trg',@ctid,0,'','')=0) 
begin
if (@trg=0) set @sql = 'alter ' else set @sql='create ' 
set @sql=@sql+' trigger '+@tbname+'_'+convert(varchar(10),@ord2tag2)+'_insert on '+@tbname+'_'+convert(varchar(10),@ord2tag2)+' for insert as
begin '+char(13)+char(10) +
'declare @ord2tag int '+char(13)+char(10)+
'set @ord2tag = '+convert(varchar(10),@ord2tag2) +char(13)+char(10)+
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('cst.afternewcst2_trg',@ctid,0)+' '+char(13)+char(10)+
  '-- 用户定义的语句完毕 
end'
set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
end else
begin
if (@trg=0) 
begin
set @sql = N'drop trigger '+@tbname+N'_'+convert(varchar(10),@ord2tag2)+N'_insert '
set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
end 
end

-- update  
set @trg = 1 
set @trn = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2)+'_update'
exec mps_sys_checktrigger @trn , @trg output 
if (dbo.mps_sys_checkkeywords('cst.aftermodifycst2_trg',@ctid,0,'','')=0) 
begin
if (@trg=0) set @sql = 'alter ' else set @sql='create ' 
set @sql=@sql+' trigger '+@tbname+'_'+convert(varchar(10),@ord2tag2)+'_update on '+@tbname+'_'+convert(varchar(10),@ord2tag2)+' for update as
begin '+char(13)+char(10) +
'declare @ord2tag int '+char(13)+char(10)+
'set @ord2tag = '+convert(varchar(10),@ord2tag2) +char(13)+char(10)+
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('cst.aftermodifycst2_trg',@ctid,0)+' '+char(13)+char(10)+
  '-- 用户定义的语句完毕 
end'
set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
end else
begin
if (@trg=0) 
begin
set @sql = N'drop trigger '+@tbname+N'_'+convert(varchar(10),@ord2tag2)+N'_update '
set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
end 
end

-- insert 
set @trg = 1 
set @trn = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2)+'_delete'
exec mps_sys_checktrigger @trn , @trg output 
if (dbo.mps_sys_checkkeywords('cst.afterdeletecst2_trg',@ctid,0,'','')=0) 
begin
if (@trg=0) set @sql = 'alter ' else set @sql='create ' 
set @sql=@sql+' trigger '+@tbname+'_'+convert(varchar(10),@ord2tag2)+'_delete on '+@tbname+'_'+convert(varchar(10),@ord2tag2)+' for delete as
begin '+char(13)+char(10) +
'declare @ord2tag int '+char(13)+char(10)+
'set @ord2tag = '+convert(varchar(10),@ord2tag2) +char(13)+char(10)+
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('cst.afterdeletecst2_trg',@ctid,0)+' '+char(13)+char(10)+
  '-- 用户定义的语句完毕 
end'
set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
end else
begin
if (@trg=0) 
begin
set @sql = N'drop trigger '+@tbname+N'_'+convert(varchar(10),@ord2tag2)+N'_delete '
set @sql = 'exec '+@dbname+'mps_run_sql '''+replace(@sql,'''','''''')+''' ' 
  execute(@sql) 
end 
end

 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 
 

set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_newcst')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_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 '+@dbname+@tbname+' 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 '+@dbname+@tbname+' set cc_createtime = GETDATE() , cc_updated = GETDATE() where cc_id=@newccid 
  end else 
  begin
  insert into '+@dbname+@tbname+N'(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 '+ @dbname+@tbname +' 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 '+@dbname+@tbname+N' 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 '+@dbname+@tbname+'_users where ccp_type=''cstview'' and ccp_ccid=@newccid and ccp_cuid=@useruid)) 
  insert into '+@dbname+@tbname+'_users(ccp_type,ccp_ccid,ccp_cuid,ccp_otid) 
  select distinct ''cstview'',@newccid,@useruid,-1  
  
  if (not exists (select 1 from '+@dbname+@tbname+'_users where ccp_type=''cstedit'' and ccp_ccid=@newccid and ccp_cuid=@useruid)) 
  insert into '+@dbname+@tbname+'_users(ccp_type,ccp_ccid,ccp_cuid,ccp_otid) 
  select distinct ''cstedit'',@newccid,@useruid,-1  
  end 
   
  -- 返回客户数据  

  if (@returndatasets=0) select * from  '+@dbname+@tbname+' where cc_id=@newccid 
  if (@returndatasets=2) select @newccid newccid   
  set nocount off  
end  
'
exec myprint @sql 
execute(@sql)
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_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 ''' 
execute(@sql)
set nocount off
end