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

-- 生成时间:2018-06-26 17:32:25 by ebs-core  
CREATE  procedure order_93_alarm_4(
     @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_93 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控制提醒时间频次*/
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控制提醒时间频次代码结束*/


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 ='冯明静'

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,18,getdate(),'QYH',
substring(cu_qyh , 20,50), cuid, '业务编号：【'+ord_no+'】
 宿舍名称：【'+ord_ssmc+'】
 租赁体系：【'+ord_zltx+'】
 交租日期：【'+convert(nvarchar(100),ord_txjzrq,23)+'】
 当前时间：【'+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)
from #master, #user INNER JOIN mps_login lgn on cuid=lgn.cu_id 
where DATEDIFF(day,getdate(),ord_txjzrq) <= 30
and DATEPART(HH, GETDATE()) BETWEEN 9 AND 22
and ord_status= '可用'
and ord_txjzrq!='' 
and ord_txjzrq !='1900-01-01 00:00:00.000'
and cu_id = cuid
and ord_zltx='营运'


 ---- 以上预警事件代码 --------------
 
 -- 调整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