IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_prdnew]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_prdnew]
GO
--产品触发器
CREATE procedure [dbo].[mps_gen_prdnew](
@cmpid int,
@pcid 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 = @pcid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'prd',@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 9,prd_pcid,0,0,prd_id from inserted '+char(13)+char(10) 
  -- 判断有没有用户定义的触发
  if (dbo.mps_sys_checkkeywords('prd.aftermodify_trg',@pcid,0,'','')=0) 
  begin
  set @sql=@sql+char(13)+char(10)+char(13)+char(10)+
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('prd.aftermodify_trg',@pcid,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 9,prd_pcid,0,-1,prd_id from deleted '+char(13)+char(10) 
  -- 判断有没有用户定义的触发
  if (dbo.mps_sys_checkkeywords('prd.afterdelete_trg',@pcid,0,'','')=0) 
  begin
  set @sql=@sql+char(13)+char(10)+ 
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('prd.afterdelete_trg',@pcid,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 9,prd_pcid,0,1,prd_id from inserted '+char(13)+char(10) 
  -- 判断有没有用户定义的触发
  if (dbo.mps_sys_checkkeywords('prd.aftercreate_trg',@pcid,0,'','')=0) 
  begin
  set @sql=@sql+char(13)+char(10)+ 
  '-- 以下是用户定义的语句 '+char(13)+char(10)+
  ''+dbo.mps_sys_getusercode('prd.aftercreate_trg',@pcid,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 pc2_tag  from mps_prdctg2  where pc2_pcid=@pcid 
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('prd.afternewprd2_trg',@pcid,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('prd.afternewprd2_trg',@pcid,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('prd.aftermodifyprd2_trg',@pcid,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('prd.aftermodifyprd2_trg',@pcid,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('prd.afterdeleteprd2_trg',@pcid,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('prd.afterdeleteprd2_trg',@pcid,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+'_newprd')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_newprd(
     @cmpid int, 
     @branchid int, 
     @userdpid int, 
     @userotid int, 
     @useruid int, 
     @pcid int,
     @returndatasets int ,  
     @extparam1 nvarchar(1000),
     @extparam2 nvarchar(1000),
     @extparam3 nvarchar(1000),
     @newprdid bigint output  
  ) 
  as  
  begin  
  set nocount on  
  
 
  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 @newprdid = 0  
  
  declare @bizcode nvarchar(100)    
  select   @bizcode=pc_bmcode  from mps_prdctg where  pc_id=@pcid    
  declare @ccno varchar(50)     
select top 1 @newprdid=prd_id from '+@dbname+@tbname+' where prd_cmpid=@cmpid and prd_pcid=@pcid and prd_createcuid=@useruid and (prd_code like ''%临%'' or prd_code = '''') 
  set @newprdid = ISNULL(@newprdid,0) 
  if (@newprdid>0) 
  begin
update '+@dbname+@tbname+' set prd_createtime = GETDATE() , prd_updated = GETDATE() where prd_id=@newprdid 
  end else 
  begin
  insert into '+@dbname+@tbname+N'(prd_cmpid,prd_bcid,prd_branch,prd_dpid,prd_depart,prd_cuid,prd_cuname,prd_tag,prd_caption,prd_pcid,prd_type,prd_title,
  prd_createdpid,prd_createdepart,prd_createcuid,prd_creator,prd_createtime,prd_updated) 
  select @cmpid,bc_id,bc_company,dp_id,dp_depart,cu_id,cu_callname,pc_tag,pc_caption,pc_id,pc_type, '''',
  dp_id,dp_depart,cu_id,cu_callname,getdate(),getdate() 
from mps_prdctg,mps_login,mps_depart,mps_groupbranch 
where pc_id=@pcid and cu_id=@useruid and dp_id=@userdpid and bc_id=@branchid  and bc_cmpid=@cmpid  
select top 1 @newprdid = prd_id from '+ @dbname+@tbname +' where prd_cmpid=@cmpid and prd_pcid=@pcid and prd_createcuid=@useruid and prd_title='''' order by prd_id desc  
  
set @newprdid = isnull(@newprdid,0) 
-- 生成订单编号（临时） 
if (@bizcode>'''') 
begin
exec mps_bm_getorset @cmpid,''prd'',@pcid,@newprdid,@useruid,@bizcode,2,@ccno output,1   
end else 
begin
set @ccno = '''' 
end 
update '+@dbname+@tbname+N' set prd_code = @ccno where prd_id = @newprdid   
   
  -- why here otid=-1 , it''s a secret , I don''t know yet 
  if (not exists (select 1 from '+@dbname+@tbname+'_users where prdp_type=''prdview'' and prdp_prdid=@newprdid and prdp_cuid=@useruid)) 
  insert into '+@dbname+@tbname+'_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) 
  select distinct ''prdview'',@newprdid,@useruid,-1  
  if (not exists (select 1 from '+@dbname+@tbname+'_users where prdp_type=''prdedit'' and prdp_prdid=@newprdid and prdp_cuid=@useruid)) 
  insert into '+@dbname+@tbname+'_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) 
  select distinct ''prdedit'',@newprdid,@useruid,-1  
  end 
   
  -- 返回客户数据  

  if (@returndatasets=0) select * from  '+@dbname+@tbname+' where prd_id=@newprdid 
  if (@returndatasets=2) select @newprdid 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'_newprd'',
''@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@pcid int,@returndatasets int,@extparam1 nvarchar(1000),@extparam2 nvarchar(1000),@extparam3 nvarchar(1000),@newprdid bigint ''' 
execute(@sql)
set nocount off
end