IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizcheck]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizcheck]
GO
create procedure [dbo].[mps_gen_bizcheck](
@cmpid int,
@bizid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @detail nvarchar(100) 
declare @ds1 varchar(100)
declare @dss nvarchar(MAX)  
declare @detailold nvarchar(100) 
declare @order2 nvarchar(100) 
declare @i int 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @ord2tag2 int 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 

declare @sql nvarchar(MAX) 
declare @ss nvarchar(MAX) 

if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'','')<>0) 
begin
set @sql = 'drop procedure '+@tbname+'_check' 
if (dbo.checkprocedure(@tbname+'_check')=0) execute(@sql)  
return 
end

set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_check')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_check(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ordid bigint,
     @operation varchar(50) output,
     @errorinfo nvarchar(4000) output
  ) 
as  
begin  
set nocount on  
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '''' 
declare @sys_recid bigint 
set @sys_recid = 0 
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
 set @operation = ''continue''
 set @errorinfo = '''' 
 --- 以下是预处理事件中的临时表
 '
 
 if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'#master','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#master'' '+char(13)+char(10)+
'select * into #master from '+@dbname+@tbname+N' where ord_id=@ordid  '+char(13)+char(10) 
 end
 
 if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'#account','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#account'' '+char(13)+char(10)+
'create table #account(acctype varchar(30),acccategory varchar(30),accdate varchar(30),fiidreal int,fiid int,objidreal bigint,objid bigint,origin int,value int,used int,suspended int,ready int,changed int,available int,actual int) '+char(13)+char(10) 
 end
 
 if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'#romaster','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#romaster'' '+char(13)+char(10)+
'select * into #romaster from '+@dbname+@tbname+N' where ord_id=@ordid  '+char(13)+char(10) 
 end
 
 if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'#task','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#task'' '+char(13)+char(10)+
'create table #task(objtype varchar(20),ctgid int,objid bigint,objid2 bigint,cuid int,infotype nvarchar(50),info nvarchar(4000),jbid bigint) '
 end
 
 -- 以下代码是生成子表临时表
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @detailold = '#rodetail'+convert(varchar(10),@ord2tag2) 
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,@detailold,'')=0) 
begin
set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@detailold+''' '+char(13)+char(10)+
'select * into '+@detailold+' from '+@order2+N' where ord2_ordid=@ordid  '+char(13)+char(10) 
end
if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,@detail,'')=0) 
begin
set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@detail+''' '+char(13)+char(10)+
'select * into '+@detail+' from '+@order2+N' where ord2_ordid=@ordid  '+char(13)+char(10) 
end
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 

set @sql=@sql+N'
 ---- 以下是开发人员写的 check order 事件 --------------
 '
 + dbo.mps_sys_getusercode('crm.check',@bizid,0) +N'
 ---- 以上是开发人员写的 check order 事件 --------------
 
 if (@operation=''continue'') 
 begin
 set @sys_recid=@sys_recid  -- here useless 
 ' 

set @sql = @sql +N'
end 
--- 清除临时表 
'

 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @detailold = '#rodetail'+convert(varchar(10),@ord2tag2) 
if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,@detail,'')=0) 
 set @sql = @sql+N'exec drop_temp_table '''+@detail+'''' +char(13)+char(10) 
 if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,@detailold,'')=0) 
 set @sql = @sql+N'exec drop_temp_table '''+@detailold+'''' +char(13)+char(10) 
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 

if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'#task','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 ' delete #task where cuid is null or info is null or objid is null  or ctgid is null '+
 ' insert into mps_task(tsk_cmpid,tsk_bcid,tsk_branch,tsk_dpid,tsk_depart,tsk_cuid,tsk_cuname,tsk_date,tsk_type,tsk_objtype,tsk_ctgid,tsk_objid,tsk_objid2,tsk_message,tsk_jbid) '+char(13)+char(10)+
 ' select distinct cu_cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,getdate(),'+char(13)+char(10)+
 'case when infotype is null then ''普通任务'' else infotype end , '+char(13)+char(10)+
 'case when objtype is null or objtype='''' then ''biz'' else objtype end , '+char(13)+char(10)+
 'case when ctgid is null or ctgid=0 then '+convert(varchar(10),@bizid)+' else ctgid end , '+char(13)+char(10)+
 'case when objid  is null or objid=0 then @ordid else objid end , '+char(13)+char(10)+
 'case when objid2  is null then 0 else objid2 end , '+char(13)+char(10)+
 'case when info  is null then '''' else info end  , '+char(13)+char(10)+
 'case when jbid  is null or jbid=0 then 0 else jbid end   '+char(13)+char(10)+
 'from #task , mps_login where cu_id=cuid '+char(13)+char(10)+
 'exec drop_temp_table ''#task'' '+char(13)+char(10) 
 end
 
 if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'#master','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#master'' '+char(13)+char(10) 
 end
 
 if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'#account','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#account'' '+char(13)+char(10) 
 end
 
 if (dbo.mps_sys_checkkeywords('crm.check',@bizid,0,'#romaster','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#romaster'' '+char(13)+char(10) 
 end
 
set @sql = @sql +N'
set nocount off 
end
'
exec myprint @sql 
execute(@sql)
set nocount off 
end