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

-- 生成时间:2018-06-22 10:46:07 by ebs-core  
CREATE  procedure order_99_alarm_28(
     @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_99 where ord_id=@ordid 
exec drop_temp_table '#detail2' 
select * into #detail2 from order_99_2 where ord2_ordid=@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控制提醒时间频次*/
declare @IntervalMinutes int = 1440,
        @Jobid bigint = 0 
/*兼容平台以前逻辑*/
if (isnull(@identkey, '') = 'noidentkey' or isdate(isnull(@identkey, '')) = 0)
begin
    set @identkey = convert(nvarchar(100), getdate(), 120)    
end
if (isnull(@identkey, '') != '' and datediff(minute, isnull(@identkey, ''), getdate()) >=@IntervalMinutes
 and DATEPART(HH, GETDATE()) BETWEEN 9 AND 22)
begin
    set @identkey = convert(nvarchar(100), getdate(), 120)
end
/*业务结束时取消预警，如果不需要取消删除即可*/
if exists(select * from #master where ord_finished = 2)
begin
set @identkey='remove'
end
/*end控制提醒时间频次代码结束*/

DECLARE @MINORD2ID BIGINT = 0 
select @MINORD2ID = min(ord2_id) from #detail2 where ord2_zt!='已完成'

select @Jobid = jb_id from mps_job with(nolock) 
where jb_ordid = @ordid 
and jb_crmid = @bizid  
and jb_wsid = 0 

insert into #user(cuid) 
select cu_id from mps_login 
where cu_callname in ('江昆笛', '谢建', '余春梅', '刘小红', '杨友香','倪安华','彭强','蔡鹏飞','龙静')

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,23,getdate(),'QYH',
substring(cu_qyh , 20,50), cuid, '装修进度工作未完成，请及时跟进
店铺名称：【'+ord_dpmc+'】
装修阶段：【'+ord2_jdx+'】
预期时间：【'+convert(nvarchar(100),ord2_jssj,23)+'】
业务编号：【'+ord_no+'】
点击查看详情'
+'{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,@jobid) from #master,#user INNER JOIN mps_login lgn on cuid=lgn.cu_id ,#detail2
where DATEDIFF(day, ord2_jssj, getdate()) >= 0
and DATEPART(HH, GETDATE()) BETWEEN 9 AND 22
AND datediff(day,ord2_jssj,'1900-01-01') != 0
and ord2_zt!= '已完成'
and cu_id = cuid
and ord2_id = @MINORD2ID



 ---- 以上预警事件代码 --------------
 
 -- 调整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' 
exec drop_temp_table '#detail2'

set nocount off 
end