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

-- 生成时间:2018-05-09 09:32:29 by ebs-core  
CREATE  procedure order_202_alarm_5(
     @cmpid int, 
     @bizid int, 
     @ordid bigint,
     @almid int ,
     @taskid int 
  ) 
as  
begin  
set nocount on  
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 = ''  
declare @identkey nvarchar(100) 

 select @identkey =wt_identkey  from mps_job_task where  wt_id=@taskid 
 set @identkey = isnull(@identkey,'') 
 set @identkeyold=@identkey
 
 
 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 @jbid int
 declare @stype varchar(20) 
 declare @sordids varchar(max) 
 
 --- 以下是预处理事件中的临时表
 
exec drop_temp_table '#master' 
select * into #master from order_202 where ord_id=@ordid 
-- 生成人员信息 
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,'bizalarm' 
select distinct cuid into #user from #myuser
drop table #myuser

 ---- 以下预警事件代码 --------------
 
/*start控制提醒时间x频次*/
if(isnull(@identkey,'')='noidentkey' or isdate(isnull(@identkey,''))=0)
set @identkey = convert(nvarchar(100),getdate(),120)
if(isnull(@identkey,'')!='' and datediff(minute,isnull(@identkey,''),getdate())>60)
set @identkey = convert(nvarchar(100),getdate(),120)
set @identkeyold = convert(nvarchar(100),dateadd(minute,2,getdate()),120)
/*end控制提醒时间频次*/

declare @jobinfo table(jb_id bigint,ordcuid bigint,ordid bigint)

insert into @jobinfo(jb_id,ordid,ordcuid)
select jb_id,jb_ordid,ord_bfrid 
from mps_job with(nolock),#master d
where ord_id = jb_ordid
and jb_wrkid = 2156 
and jb_crmid = d.ord_bizid

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,17,getdate(),'QYH',
substring(cu_qyh , 20,50),  ordcuid,'亲，你有一个客户【'+ord_khxm+'】需要及时跟进
上次跟进：【'+convert(nvarchar(100),ord_gjsj,120)+'】
单据名称：【'+ord_type+'】
单据编号：【'+ord_no+'】
创建人名：【'+ord_creator+'】
创建时间：【'+convert(nvarchar(100),ord_date,120)+'】
推送时间：【'+convert(nvarchar(100),getdate(),120)+'】
详情请点我'
+'{title}工作提醒:'+ord_type+char(9)+'('+ord_creator+')'+
+'{url}/order/biztop.mobile.htm?crmid='+CONVERT(varchar(100),ord_bizid)+'&ordid='+CONVERT(varchar(100),ord_id)+'&jbid='+CONVERT(varchar,jb_id) from #master,@jobinfo,mps_login
where (ord_jmsyy='A' or ord_jmsyy = 'B')
and ord_gjjg = '跟进中'
and ordcuid = cu_id
and DATEDIFF(day,ord_gjsj,GETDATE())>=2
and ordid = ord_id
and 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,5,@identkey,'alarm','AB级客户跟进预警','一般性预警',@ordid,0,'biz',  ordcuid,'亲，你有一个客户【'+ord_khxm+'】需要及时跟进，
上次跟进：【'+convert(nvarchar(100),ord_gjsj,120)+'】
单据名称：【'+ord_type+'】
单据编号：【'+ord_no+'】
创建人名：【'+ord_creator+'】
创建时间：【'+convert(nvarchar(100),ord_date,120)+'】
发送时间：【'+convert(nvarchar(100),getdate(),120)+'】
详情请点我'
+'{url}/order/biztop.mobile.htm?crmid='+CONVERT(varchar(100),ord_bizid)+'&ordid='+CONVERT(varchar(100),ord_id)+'&jbid='+CONVERT(varchar,jb_id) from #master,@jobinfo,mps_login
where (ord_jmsyy='A' or ord_jmsyy = 'B')
and ord_gjjg = '跟进中'
and ordcuid = cu_id
and DATEDIFF(day,ord_gjsj,GETDATE())>=2
and ordid = ord_id
and 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=0 and wt_ordid=@ordid and wt_mbid=@almid and wt_type='bizalarm'
 delete mps_job_task where wt_id=@taskid 
 --update mps_alarm set alm_identkey=''  where alm_id=@almid  
 end 
 
 
 -- 生成新业务


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

exec drop_temp_table '#master' 

set nocount off 
end