IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_ebsreport]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_ebsreport]
GO
create procedure [dbo].[mps_gen_ebsreport](
@cmpid int,
@rptid 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 @almsql nvarchar(4000) 
declare @almsql2 nvarchar(4000) 
declare @alm_category nvarchar(100) 
declare @alm_title nvarchar(100) 
declare @alm_tmpid int 

select top 1 @almsql=convert(nvarchar(4000),cd_text)  from mps_extsqljs where cd_objid=@rptid and cd_name='rptsend' 
set @almsql=isnull(@almsql,'') 
 
select @alm_tmpid=rpt_tmpid , @alm_title=rpt_title, @alm_category = case when rpt_categorysub='' then rpt_category else rpt_categorysub end 
from mps_report where rpt_id=@rptid 


declare @sql nvarchar(MAX) 

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

if (len(@almsql)<=5 or @alm_tmpid<=0) 
begin
set @sql = 'report_'+convert(varchar(10),@cmpid)+'_'+convert(varchar(10),@rptid) 
if (dbo.checkprocedure(@sql)=0) 
begin
set @sql = N'drop procedure '+@sql 
execute(@sql)  
end
delete mps_job_task where wt_cmpid=@cmpid and wt_bizid=0 and wt_ordid=0 and wt_mbid=@rptid and wt_wrkid=0 and wt_type='report'
return 
end

set @almsql2 = @almsql 


set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure('report_'+convert(varchar(10),@cmpid)+'_'+ convert(varchar(10),@rptid))=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure report_'+convert(varchar(10),@cmpid)+'_'+ convert(varchar(10),@rptid)+'(
     @cmpid int, 
     @rptid 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 =rpt_identkey from mps_report where rpt_id=@rptid 
 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) 
 
 --- 以下是预处理事件中的临时表
 '
 
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'',0,0,0,@rptid,''report'' 
select distinct cuid into #user from #myuser
drop table #myuser

 ---- 以下预警事件代码 --------------
 '+char(13)+char(10) 
 
set @almsql2 = REPLACE(@almsql2,'''{template}''','{template}')
set @almsql2 = REPLACE(@almsql2,'#user','#user INNER JOIN mps_login lgn on cuid=lgn.cu_id ')
set @i = CHARINDEX('select',@almsql2) 
if (@i>0)
begin
set @sql = @sql + substring(@almsql2,1,@i-1)  
set @almsql2 =substring(@almsql2,@i,4000)  
set @j = CHARINDEX('{template}',@almsql2) 
set @almsql = substring(@almsql2,@j-30,4000)  
set @j = CHARINDEX(@almsql,@almsql2) 
if (@j>0) set @sql = @sql + substring(@almsql2,1,@j-1)  
set @almsql2 = @almsql 
end else 
begin
set @sql = @sql + @almsql2 
set @almsql2 = '' 
end

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 @msgcount int 
 declare @icc int 
 set @icc = 0 
 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<>''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 @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<>''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 @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_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<>''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 @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_qyhid=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<>''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 @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<>''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),@rptid)+',@identkey,''alarm'','''+@alm_title+''','''+@alm_category+''',''rpt'',@rptid,0,') 
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<>''abort'' and @identkey<>''remove'') 
 begin
 update mps_report set rpt_identkey=@identkey where rpt_id=@rptid 
 end 
 if (@identkey=''remove'') 
 begin
 update mps_report set rpt_identkey=''''  where rpt_id=@rptid  
 end 
 
 '
 set @sql = @sql+char(13)+char(10)+'
 ---------------------
--- 清除临时表 
exec drop_temp_table ''#user'' 
'+char(13)+char(10)

 

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

set @sql = 'report_'+convert(varchar(10),@cmpid)+'_'+convert(varchar(10),@rptid) 
if (dbo.checkprocedure(@sql)=0) 
begin
if (not exists (select 1 from mps_job_task where wt_cmpid=@cmpid and wt_mbid=@rptid and wt_type='report')) 
insert into mps_job_task(wt_begin,wt_wrkid,wt_cmpid,wt_bizid,wt_ordid,wt_task,wt_interval,wt_mbid,wt_type) 
values(getdate(),0,@cmpid,0,0,
'exec report_'+convert(varchar(10),@cmpid)+'_'+convert(varchar(10),@rptid)+
' '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@rptid),60,@rptid,'report')
end

set nocount off 
end