IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizalarm]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizalarm]
GO
create procedure [dbo].[mps_gen_bizalarm](
@cmpid int,
@bizid int
)
as
begin
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 

declare @ord2tag int 
declare @sql nvarchar(MAX) 
declare @sql2 nvarchar(MAX) 
declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct  alm_id  from mps_alarm  where   alm_cmpid=@cmpid  and alm_objtype='biz' and alm_objid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag 
WHILE (@@FETCH_STATUS = 0)
BEGIN
exec mps_gen_biz1alarm @cmpid ,@bizid ,@ord2tag 
 FETCH NEXT FROM c1 INTO @ord2tag 
END
CLOSE c1
DEALLOCATE c1

declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct  wrk_id  from mps_flowwork  where   wrk_bizid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag 
WHILE (@@FETCH_STATUS = 0)
BEGIN
exec mps_gen_wrkalarm @cmpid ,@bizid ,@ord2tag 
 FETCH NEXT FROM c1 INTO @ord2tag 
END
CLOSE c1
DEALLOCATE c1
-- 生成ORDER任务添加程序
declare @s1 nvarchar(4000) 

if (dbo.checkprocedure('order_'+convert(varchar(10),@bizid)+'_alarm_task')=0)
begin
set @sql = N'drop procedure order_'+convert(varchar(10),@bizid)+'_alarm_task'  
execute(@sql)  
end

set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure('order_'+convert(varchar(10),@bizid)+'_alarm_task')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure order_'+convert(varchar(10),@bizid)+'_alarm_task(
     @cmpid int, 
     @bizid int, 
     @ordid bigint
  ) 
as  
begin  
'
declare @ic int 
set @ic = 0
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select alm_id  from mps_alarm  where alm_cmpid=@cmpid and alm_objtype='biz' and alm_objid=@bizid and alm_objid2=0 
open status1
FETCH NEXT FROM status1 INTO @ord2tag 
WHILE (@@FETCH_STATUS = 0)
BEGIN
 set @s1 = 'order_'+convert(varchar(10),@bizid)+'_alarm_'+rtrim(ltrim(str(@ord2tag))) 
if (dbo.checkprocedure(@s1)=0) 
begin
set @ic = @ic + 1
set @sql=@sql+char(13)+char(10)+
' if (not exists (select 1 from mps_job_task where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_wrkid=0 and wt_mbid='+rtrim(ltrim(str(@ord2tag)))+' and wt_jbid=0 and wt_type=''bizalarm'')) '+char(13)+char(10)+
'insert into mps_job_task(wt_begin,wt_wrkid,wt_cmpid,wt_bizid,wt_ordid,wt_useruid,wt_task,wt_jbid,wt_interval,wt_mbid,wt_type) '+char(13)+char(10)+
'values(getdate(),0,'+convert(varchar(10),@cmpid)+','+convert(varchar(10),@bizid)+
',@ordid,0,'''+
'exec '+@s1+' '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@bizid)+
',''+convert(varchar(10),@ordid)+'','+convert(varchar(10),@ord2tag)
+',@taskid'',0,60,'+convert(varchar(10),@ord2tag)+',''bizalarm'') '+char(13)+char(10) 
set @sql2=
'insert into mps_job_task(wt_begin,wt_wrkid,wt_cmpid,wt_bizid,wt_ordid,wt_useruid,wt_task,wt_jbid,wt_interval,wt_mbid,wt_type) '+char(13)+char(10)+
'select getdate(),0,'+convert(varchar(10),@cmpid)+','+convert(varchar(10),@bizid)+
',ord_id,0,'''+
'exec '+@s1+' '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@bizid)+
',''+convert(varchar(10),ord_id)+'','+convert(varchar(10),@ord2tag)
+',@taskid'',0,60,'+convert(varchar(10),@ord2tag)+',''bizalarm'' FROM ' +@dbname+@tbname+' where ord_finished>0 and ord_finished<10 and ' +
' (not exists (select 1 from mps_job_task where wt_cmpid=ord_cmpid and wt_bizid=ord_bizid and wt_ordid=ord_id and wt_type=''bizalarm'' and wt_mbid='+convert(varchar(10),@ord2tag)+' )) '
execute(@sql2)
end 
 FETCH NEXT FROM status1 INTO @ord2tag 
END
CLOSE status1
DEALLOCATE status1 
set @sql=@sql+char(13)+char(10)+
'end'+char(13)+char(10)
if (@ic>0) 
begin
exec myprint @sql 
execute(@sql) 
end 
end