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

-- 生成时间:2018-06-22 17:54:06 by ebs-core  
CREATE  procedure order_69_alarm_2929_13(
     @cmpid int, 
     @bizid int, 
     @ordid bigint,
     @wrkid int,
     @jbid bigint,
     @almid int , 
     @taskid int 
  ) 
as  
begin  
set nocount on  
declare @identkey nvarchar(100)
declare @identkeyold nvarchar(100) 
declare @resultdata1 nvarchar(4000) 
declare @resultdata2 nvarchar(4000) 
declare @resultdata3 nvarchar(4000) 
declare @resultdata4 nvarchar(4000) 
declare @resultdata5 nvarchar(4000) 
declare @resultdata6 nvarchar(4000) 
declare @resultdata7 nvarchar(4000) 
declare @resultdata8 nvarchar(4000)
declare @resultdata9 nvarchar(4000)  
set @resultdata1 = '' 
set @resultdata2 = '' 
set @resultdata3 = '' 
set @resultdata4 = '' 
set @resultdata5 = '' 
set @resultdata6 = '' 
set @resultdata7 = '' 
set @resultdata8 = '' 
set @resultdata9 = ''  
 
select @identkey =wt_identkey  from mps_job_task where  wt_id=@taskid 
 set @identkey = isnull(@identkey,'') 
 set @identkeyold=@identkey
 
 --select @identkey =alm_identkey from mps_alarm where alm_id=@almid 
 --set @identkeyold=@identkey
 --set @identkey = isnull(@identkey,'none') 
 --if (@identkey='') set @identkey = 'noidentkey'
 
 declare @almtitle nvarchar(100) 
 set @almtitle = ''  
 declare @almcategory nvarchar(100) 
 set @almcategory = ''  
 declare @sourceobjids varchar(MAX) 
 set @sourceobjids = '' 
 
 declare @newordid bigint 
 declare @branchid int 
 declare @userdpid int 
 declare @userotid int 
 declare @useruid int 
 declare @stype varchar(20) 
 declare @sordids varchar(max) 
 
 --- 以下是预处理事件中的临时表
 
exec drop_temp_table '#master' 
select * into #master from order_69 where ord_id=@ordid 
exec drop_temp_table '#job' 
select * into #job from mps_job where jb_id=@jbid 
-- 生成人员信息 
create table #myuser(pptype varchar(30),cuid int, grp int) 
exec mps_gen_powerusers @cmpid,0,0,0,0,0,0,0,0,'biz',@bizid,@ordid,0,@almid,'wrkalarm' 
select distinct cuid into #user from #myuser
drop table #myuser

 ---- 以下预警事件代码 --------------
 
/***步骤上的预警：
**jb_elapsedtime:本步骤实际耗时，单位为分钟.非工作时段不在计时范围，但需要保证mps_holiday中有数据
**FirstTipMinutes 分钟后提醒一次，然后每隔 IntervalMinutes 分钟提醒一次，并且只计算上班时间。
**以 FirstTipMinutes = 120,IntervalMinutes = 240为例。
*/
/*必须设置*/
insert into #user(cuid)
select jb_cuid from #job

declare 
        @FirstTipMinutes int = 120,        
        @IntervalMinutes int = 240,        
        @elapsedMinutes int = 0,
        @nInterval int = 0 ,
        @notifiedTimes int = 0 

if ISNUMERIC(@identkey)=0
begin
    set @identkey = -1
end
set @notifiedTimes = convert(int,@identkey)

select @elapsedMinutes = jb_elapsedtime from #job 
set @nInterval = floor((@elapsedMinutes-@FirstTipMinutes)/@IntervalMinutes)

if (@elapsedMinutes<@FirstTipMinutes) 
    set @notifiedTimes = -1
else if (@elapsedMinutes>=@FirstTipMinutes and @nInterval>@notifiedTimes and @nInterval<@notifiedTimes+2)
    set @notifiedTimes = @notifiedTimes + 1
else if (@elapsedMinutes>=@FirstTipMinutes and -1=@notifiedTimes) 
    set @notifiedTimes = @nInterval
else if (@nInterval>@notifiedTimes+2)
begin
    set @notifiedTimes = @nInterval
end

if (@elapsedMinutes>0 and @identkey<>convert(varchar(10),@notifiedTimes) and DATEPART(HH, GETDATE())  BETWEEN  9 AND 22
) 
    set @identkey = convert(varchar(10),@notifiedTimes)
else 
    set @identkey='abort' 

/*业务结束时取消预警，如果不需要取消删除即可*/
if exists(select * from #master where ord_finished = 2 )
begin
    set @identkey='remove'
end
/*end控制提醒时间频次代码结束*/

if (@identkey<>@identkeyold and @identkey>'' and @identkey<>'abort' and @identkey<>'remove') 
 insert into ebs_kernel.dbo.pub_message(
 msg_cmpid,msg_mpid,msg_senddate,msg_type,msg_target,msg_cuid,msg_text) 
 select distinct @cmpid,11,getdate(),'QYH',
substring(cu_qyh , 20,50), jb_cuid ,'你有一项新的工作('+jb_title+')需要及时处理
单据名称：【'+ord_type+'】
单据编号：【'+ord_no+'】
创建人名：【'+ord_creator+'】
创建部门：【'+ord_createdepart+'】
查看详情点我'
+'{title}工作提醒:'+ord_type+char(9)+'('+ord_creator+')'+
+'{url}/order/biztop.mobile.htm?crmid='+CONVERT(varchar,ord_bizid)+'&ordid='+CONVERT(varchar,ord_id)+'&jbid='+CONVERT(varchar,jb_id) from #master,#job,#user INNER JOIN mps_login lgn on cuid=lgn.cu_id 
where DATEPART(HH, GETDATE())  BETWEEN  9 AND 22

if (@identkey<>@identkeyold and @identkey>'' and @identkey<>'abort' and @identkey<>'remove') 
 insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_objid,almmsg_jbid,almmsg_objtype,almmsg_cuid,almmsg_text) 
 select distinct @cmpid,13,@identkey,'alarm','工作提醒','工作事件',@ordid,@jbid,'biz', jb_cuid ,'你有一项新的工作('+jb_title+')需要及时处理
单据名称：【'+ord_type+'】
单据编号：【'+ord_no+'】
创建人：【'+ord_creator+'】
点我查看详情'
+'{url}/order/biztop.mobile.htm?crmid='+CONVERT(varchar,ord_bizid)+'&ordid='+CONVERT(varchar,ord_id)+'&jbid='+CONVERT(varchar,jb_id) from #master,#job,#user INNER JOIN mps_login lgn on cuid=lgn.cu_id 
where DATEPART(HH, GETDATE())  BETWEEN  9 AND 22



 ---- 以上预警事件代码 --------------
 
 -- 调整identkey 
 if (@identkey<>@identkeyold and @identkey>'' and @identkey<>'abort' and @identkey<>'remove') 
 begin
 --update mps_alarm set alm_identkey=@identkey where alm_id=@almid 
 update mps_job_task set wt_identkey=@identkey where wt_id=@taskid 
 end 
 if (@identkey='remove') 
 begin
 delete mps_job_task where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_wrkid=@wrkid and wt_jbid=@jbid and wt_ordid=@ordid and wt_mbid=@almid and wt_type='wrkalarm'
 --update mps_alarm set alm_identkey=''  where alm_id=@almid  
 delete mps_job_task where wt_id=@taskid 
 end 
 
 
 -- 生成新业务


 ---------------------
--- 清除临时表 
exec drop_temp_table '#user' 

exec drop_temp_table '#master' 
exec drop_temp_table '#job' 

set nocount off 
end