IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_wrk1alarm]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_wrk1alarm]
GO
create procedure [dbo].[mps_gen_wrk1alarm](
@cmpid int,
@bizid int ,
@wrkid int,
@almid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @detail nvarchar(100) 
declare @ds1 varchar(100)
declare @dss nvarchar(MAX)  
declare @detailold nvarchar(100) 
declare @order2 nvarchar(100) 
declare @i int 
declare @j int 
declare @jold int 
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 @almsql nvarchar(4000) 
declare @almsql2 nvarchar(4000) 
declare @alm_tmpid int 
declare @alm_category nvarchar(100) 
declare @alm_createmode int
declare @alm_tobizid int 
declare @alm_title nvarchar(100) 
select @almsql=alm_sql , @alm_tmpid=alm_tmpid ,@alm_category=alm_category ,@alm_title=alm_title,
@alm_createmode=alm_createmode , @alm_tobizid=alm_tobizid 
from mps_alarm where alm_id=@almid 
set @alm_createmode = ISNULL(@alm_createmode,0) 
set @alm_tobizid = ISNULL(@alm_tobizid,0) 


declare @sql nvarchar(MAX) 

declare @ss nvarchar(MAX) 
declare @s0 nvarchar(1000)

if (len(@almsql)<=5 or @alm_tmpid=0 or @alm_category='') 
begin
set @sql = 'order_'+convert(varchar(10),@bizid)+'_alarm_'+ convert(varchar(10),@wrkid)+'_'+convert(varchar(10),@almid) 
if (dbo.checkprocedure(@sql)=0) 
begin
set @sql = N'drop procedure '+@sql 
execute(@sql)  
end
return 
end

set @almsql2 = @almsql 


set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure('order_'+convert(varchar(10),@bizid)+'_alarm_'+convert(varchar(10),@wrkid)+'_'+ convert(varchar(10),@almid))=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure order_'+convert(varchar(10),@bizid)+'_alarm_'+convert(varchar(10),@wrkid)+'_'+ convert(varchar(10),@almid)+'(
     @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) 
 
 --- 以下是预处理事件中的临时表
 '
 
 if (charindex('#master',@almsql)>0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#master'' '+char(13)+char(10)+
'select * into #master from '+@dbname+@tbname+N' where ord_id=@ordid '
 end
 
 if (charindex('#account',@almsql)>0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#account'' '+char(13)+char(10)+
'create table #account(acctype varchar(30),acccategory varchar(30),accdate varchar(30),fiidreal int,fiid int,objidreal bigint,objid bigint,origin int,value int,used int,suspended int,ready int,changed int,available int,actual int) '+char(13)+char(10) 
 end
 
 if (charindex('#job',@almsql)>0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#job'' '+char(13)+char(10)+
'select * into #job from mps_job where jb_id=@jbid '
 end
 if (charindex('#romaster',@almsql)>0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#romaster'' '+char(13)+char(10)+
'select * into #romaster from '+@dbname+@tbname+N' where ord_id=@ordid '
 end
 
 -- 以下代码是生成子表临时表
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1
declare @ord2tag int 
FETCH NEXT FROM c1 INTO @ord2tag 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag) 
set @detailold = '#rodetail'+convert(varchar(10),@ord2tag) 
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag) 
if (charindex(@detailold,@almsql)>0) 
begin
 set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@detailold+''' '+char(13)+char(10)+
'select *  into '+@detailold+' from '+@order2+N' where ord2_ordid=@ordid  '+char(13)+char(10) 
end 
if (charindex(@detail,@almsql)>0) 
begin
 set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@detail+''' '+char(13)+char(10)+
'select * into '+@detail+' from '+@order2+N' where ord2_ordid=@ordid  '+char(13)+char(10) 
 end
 FETCH NEXT FROM c1 INTO @ord2tag 
END
CLOSE c1
DEALLOCATE c1 

set @sql=@sql+N'
-- 生成人员信息 
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

 ---- 以下预警事件代码 --------------
 '+char(13)+char(10) 
 
set @almsql2 = REPLACE(@almsql2,'''{template}''','{template}')
set @j = CHARINDEX('{template}',@almsql2) 
while (@j>0)
begin
set @i = CHARINDEX('select',@almsql2) 
if (@i<@j and @i>0) 
begin
set @jold = CHARINDEX('select',substring(@almsql2,@i+6,4000)) 
if (@jold=0 or @jold>@j) -- 说明是最后一个了
begin
set @sql=@sql + substring(@almsql2,1,@i-1)
set @almsql2 = substring(@almsql2,@i,4000)
break 
end else
begin
set @sql=@sql + substring(@almsql2,1,@i+5)
set @almsql2 = substring(@almsql2,@i+6,4000)
end 
end else
begin
break 
end
end 
set @almsql2 = REPLACE(@almsql2,'#user','#user INNER JOIN mps_login lgn on cuid=lgn.cu_id ')

set @j = CHARINDEX('{template}',@almsql2) 
if (@j>0)
begin
set @almsql = SUBSTRING(@almsql2,@j+10,4000) 
set @almsql2 = SUBSTRING(@almsql2,1,@j+9) 
end else
begin
set @sql = @sql+@almsql2 
set @almsql=''
set @almsql2=''
end 

-- @almsql2 是可以直接来替换 select {template}
-- @almsql 是最后的代码  

 declare @icc int 
 set @icc = 0 
 declare @msgcount int 
 declare @mpname nvarchar(100) 
declare @mpcaption nvarchar(100) 
declare @mpid int , @emp varchar(20) , @mb nvarchar(4000) , @wxtype varchar(10) , @wxcode varchar(50)  , @wxlen varchar(10) 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
 

select @mpid=mp_id , @emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code 
from    ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where mp_id=@alm_tmpid and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  

-- 发公众号 
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @ss = @almsql2
set @ss = REPLACE(@ss,'select',
 '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,'+convert(varchar(10),@mpid)+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end,') 
set @ss = REPLACE(@ss,'{template}',@mb) 
 set @sql=@sql+@ss+@almsql+char(13)+char(10)
 end 
 
 set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
 select @mpid=mp_id , @emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code 
from    ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where mp_id=@alm_tmpid and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @ss = @almsql2
set @ss = REPLACE(@ss,'select',
 '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,'+convert(varchar(10),@mpid)+',getdate(),'''+@wxtype+''',
substring(cu_qyh , '+@wxlen+',50),') 
set @ss = REPLACE(@ss,'{template}',@mb) 
 set @sql=@sql+@ss+@almsql+char(13)+char(10)
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=mp_id , @emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code 
from    ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where mp_id=@alm_tmpid and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @ss = @almsql2
set @ss = REPLACE(@ss,'select',
 '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,'+convert(varchar(10),@mpid)+',getdate(),'''+@wxtype+''',
substring(cu_ding , '+@wxlen+',50),') 
set @ss = REPLACE(@ss,'{template}',@mb) 
 set @sql=@sql+@ss+@almsql+char(13)+char(10)
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=mp_id , @emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code 
from    ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where mp_id=@alm_tmpid and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @ss = @almsql2
set @sql=@sql+char(13)+char(10)+
'delete #user where len(cu_mobile)<11 '+char(13)+char(10)+
'if ('+convert(varchar(10),@msgcount)+'>0) delete #user where (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)>'''' ' +char(13)+char(10) 
set @ss = REPLACE(@ss,'select',
 '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,'+convert(varchar(10),@mpid)+',getdate(),'''+@wxtype+''',
cu_mobile,') 
set @ss = REPLACE(@ss,'{template}',@mb) 
 set @sql=@sql+@ss+@almsql+char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=mp_id , @emp=mp_scope , @mb=mp_pctext , @wxtype='PC' , @wxcode='' 
from    ebs_kernel.dbo.pub_messagetemplate 
where mp_id=@alm_tmpid and len(mp_pctext)>10  
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @ss = @almsql2
set @ss = REPLACE(@ss,'select',
 '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,'+convert(varchar(10),@almid)+',@identkey,''alarm'','''+@alm_title+''','''+@alm_category+''',@ordid,@jbid,''biz'',') 
 
set @ss = REPLACE(@ss,'{template}',@mb) 
 set @sql=@sql+@ss+@almsql+char(13)+char(10)
end 




 set @sql=@sql+char(13)+char(10)
 +
 '
 ---- 以上预警事件代码 --------------
 
 -- 调整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 
 
 
 -- 生成新业务'+char(13)+char(10) 
 
 if (@alm_tobizid>0) 
 begin
 set @sql=@sql+char(13)+char(10)+
 'if (@sourceobjids>'''' and charindex('','',@sourceobjids)=0) set @sourceobjids = @sourceobjids+'',0'' '+char(13)+char(10)+
 'select top 1 @branchid=ord_bcid,@userdpid=ord_createdpid,@useruid=ord_createcuid from '+ @dbname+@tbname+' where ord_id=@ordid '+char(13)+char(10)+
 'set @branchid = isnull(@branchid,0) '+char(13)+char(10)+
 'set @userdpid = isnull(@userdpid,0) '+char(13)+char(10)+
 'set @useruid = isnull(@useruid,0) '+char(13)+char(10)+
 'if (@sourceobjids='''') '+char(13)+char(10)+
 'begin '+char(13)+char(10)+ 
 'set @sourceobjids=convert(varchar(10),@ordid) '+char(13)+char(10)+
 'exec order_'+convert(varchar(10),@alm_tobizid)+'_newbiz @cmpid,@branchid,@userdpid,0,@useruid,'+
 '@jbid,@bizid,''biz'',@sourceobjids,0,0,2,'''','''','''',@newordid output '+char(13)+char(10)+
 'end else '+char(13)+char(10)+
 'begin '+char(13)+char(10)+
 'exec order_'+convert(varchar(10),@alm_tobizid)+'_newbiz @cmpid,@branchid,@userdpid,0,@useruid,'+
 '@jbid,@bizid,''biz'',@sourceobjids,0,0,2,'''','''','''',@newordid output '+char(13)+char(10)+
 'end '+char(13)+char(10) 
 end 
 
 set @sql = @sql+char(13)+char(10)+'
 ---------------------
--- 清除临时表 
exec drop_temp_table ''#user'' 
'+char(13)+char(10)
if (charindex('#master',@almsql)>0) 
 set @sql = @sql+N'exec drop_temp_table ''#master'' '+char(13)+char(10) 
 
 if (charindex('#account',@almsql)>0) 
 set @sql = @sql+N'exec drop_temp_table ''#account'' '+char(13)+char(10) 
 
 if (charindex('#job',@almsql)>0) 
 set @sql = @sql+N'exec drop_temp_table ''#job'' '+char(13)+char(10) 
 
 if (charindex('#romaster',@almsql)>0) 
 set @sql = @sql+N'exec drop_temp_table ''#romaster'' '+char(13)+char(10) 
 
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag) 
set @detailold = '#rodetail'+convert(varchar(10),@ord2tag) 
if (charindex(@detail,@almsql)>0) 
 set @sql = @sql+N'exec drop_temp_table '''+@detail+'''' +char(13)+char(10) 
 if (charindex(@detailold,@almsql)>0) 
 set @sql = @sql+N'exec drop_temp_table '''+@detailold+'''' +char(13)+char(10) 
 FETCH NEXT FROM c1 INTO @ord2tag 
END
CLOSE c1
DEALLOCATE c1 

set @sql = @sql +N'
set nocount off 
end
'
if (@icc>0) 
begin
exec myprint @sql 
execute(@sql)
end 
set nocount off 
end