IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_forums_attention]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_forums_attention]
GO
create proc [dbo].[mps_g_forums_attention]
@fmid bigint,
@branchid int,
@userdpid int,
@usertype int,
@useruid int,
@cmpid int
as
begin
set nocount on
--主回复t1
declare @tffmid int
select @tffmid = fm_fffmid from mps_forum where fm_id = @fmid
set @tffmid = ISNULL(@tffmid,0)
if(@tffmid>0) set @tffmid = @tffmid
else set @tffmid = @fmid

select *,
(select count(fm_id) from mps_forum b where b.fm_ffmid = a.fm_id or b.fm_fffmid =a.fm_id) 
as fm_count,
(select count(*) from mps_forum_like 
where fml_fmid = a.fm_id and fml_cmpid =@cmpid and fml_bcid = @branchid and fml_usertype = @usertype and fml_cuid = @useruid and fml_like=0) 
as fm_userlike,
(select count(*) from mps_forum_like 
where fml_fmid = a.fm_id and fml_cmpid =@cmpid and fml_bcid = @branchid and fml_usertype = @usertype and fml_cuid = @useruid and fml_like=1) 
as fm_userdislike,
getdate() as fm_enddate 
from mps_forum a
where fm_id = @tffmid

--子回复t2
select * ,
(select count(fm_id) from mps_forum b where b.fm_ffmid = a.fm_id or b.fm_fffmid =a.fm_id) 
as fm_count,
(select count(*) from mps_forum_like 
where fml_fmid = a.fm_id and fml_cmpid =@cmpid and fml_bcid = @branchid and fml_usertype = @usertype and fml_cuid = @useruid and fml_like=0) 
as fm_userlike,
(select count(*) from mps_forum_like 
where fml_fmid = a.fm_id and fml_cmpid =@cmpid and fml_bcid = @branchid and fml_usertype = @usertype and fml_cuid = @useruid and fml_like=1) 
as fm_userdislike,
getdate() as fm_enddate 
from mps_forum a
where fm_ffmid = @tffmid or fm_fffmid = @tffmid
order by fm_id
--回复的分类t3
declare @ebstype varchar(50),@objid int,@ctgid int,@tag int
select @ebstype = fm_type,@ctgid = fm_ctgid,@objid = fm_objid,@tag = fm_ebstag 
from mps_forum
where fm_id = @tffmid

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 

if @ebstype = 'biz'
begin
declare @str nvarchar(1000)
exec sys_sql_getTableNamePk @cmpid,'biz',@ctgid,0,@tbname output,@fkey output ,@dbname output 
set @str = 'select ord_date createdate,ord_type title,ord_title ,ord_depart ,ord_createdepart depart,ord_no nos,ord_creator author from ' +@dbname+@tbname+ ' where ord_id='+convert(varchar(50),@objid)
exec(@str)
end else
if @ebstype = 'doc'
begin
select doc_title as title,doc_no as nos,doc_create as createdate,doc_author as author,doc_depart as depart 
from mps_document
where doc_dcid = @ctgid and doc_id = @objid
end else
begin
select case when len(convert(nvarchar(max),fm_text))>10 then substring(fm_text,1,10)+'...' else fm_text end as title,
@ebstype+'_ctgid'+convert(varchar(50),@ctgid)+'_objid'+convert(varchar(50),@objid) nos, 
fm_cuname as author , fm_createdtime createdate,fm_depart as depart
from mps_forum 
where fm_id = @tffmid
end
--查询要回复的fmid
/* declare @nowfmid int, @nowcuname nvarchar(100), @nowfffmid int 
select @nowfmid=fm_id,@nowcuname=fm_cuname, @nowfffmid =fm_fffmid from mps_forum 
where fm_ffmid = @fmid and fm_work = 'wrk' and fm_workcuid = @useruid
if(@nowfmid>0)
begin
select @nowfmid fm_id,@nowcuname fm_cuname, @nowfffmid  fm_fffmid
end else
begin */
select fm_id,fm_cuname, fm_fffmid from mps_forum 
where fm_id = @fmid
--end

set nocount off
end