IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_forum_message_biz]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_forum_message_biz]
GO
create procedure [dbo].[mps_forum_message_biz](
@cmpid int,
@ctgid int,
@ordid int,
@fmid int,
@ebstype varchar(100),
@useruid int 
)
as
begin
set nocount on

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@ctgid,0,@tbname output,@fkey output ,@dbname output 


declare @sql nvarchar(MAX) 
set @sql = ''

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 ,@mpname=mp_name,@mpcaption=mp_caption  
from ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where mp_caption = '业务单评论' 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 @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select '+convert(varchar(50),@cmpid)+','+convert(varchar(10),@mpid)+','+@mb+',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 , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+'mps_forum,' 
set @sql=@sql+'mps_login  
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id='+convert(varchar(50),@ordid)+'  and ord_createcuid != '+ convert(varchar(100),@useruid) + '  and  '
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+' fm_id = '+convert(varchar(50),@fmid)+' and ' 
set @sql=@sql+' cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +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 ,@mpname=mp_name,@mpcaption=mp_caption  
from ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where mp_caption = '业务单评论'  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 @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select '+convert(varchar(50),@cmpid)+','+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+'mps_forum,' 
set @sql=@sql+'mps_login  
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id='+convert(varchar(50),@ordid)+'  and ord_createcuid != '+ convert(varchar(100),@useruid) + '  and  '
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+' fm_id = '+convert(varchar(50),@fmid)+' and ' 
set @sql=@sql+' cu_type=0 and cu_qyh>'''' ' +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 ,@mpname=mp_name,@mpcaption=mp_caption  
from ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where mp_caption = '业务单评论'  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 @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select '+convert(varchar(50),@cmpid)+','+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+'mps_forum,' 
set @sql=@sql+'mps_login  
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id='+convert(varchar(50),@ordid)+'  and ord_createcuid != '+ convert(varchar(100),@useruid) + '  and  '
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+' fm_id = '+convert(varchar(50),@fmid)+' and ' 
set @sql=@sql+'cu_type=0 and cu_ding>'''' ' +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 ,@mpname=mp_name,@mpcaption=mp_caption  
from ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where mp_caption = '业务单评论' 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 @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select '+convert(varchar(50),@cmpid)+','+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+'mps_forum,' 
set @sql=@sql+'mps_login  
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id='+convert(varchar(50),@ordid)+'  and ord_createcuid != '+ convert(varchar(100),@useruid) + '  and  '
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+' fm_id = '+convert(varchar(50),@fmid)+' and ' 
set @sql=@sql+' and cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +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  ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where mp_caption = '业务单评论' and len(mp_pctext)>10  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select '+convert(varchar(50),@cmpid)+','+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , '+convert(varchar(50),@ordid)+',0 ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+'mps_forum,' 
set @sql=@sql+'mps_login  
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id='+convert(varchar(50),@ordid)+'  and ord_createcuid != '+ convert(varchar(100),@useruid) + '  and  '
if (CHARINDEX('fm_',@mb)>0) set @sql=@sql+' fm_id = '+convert(varchar(50),@fmid)+' and ' 
set @sql=@sql+' cu_type=0  '+char(13)+char(10) 
end 
print(@sql)
exec(@sql)
end