IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_forums_mobile]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_forums_mobile]
GO
CREATE proc [dbo].[mps_g_forums_mobile]
@cmpid int,
@ebstag int,
@type varchar(100),
@ctgid bigint,
@objid bigint,
@orderby bigint,
@branchid int,
@userdpid int,
@usertype int,
@useruid bigint,
@pageobjids varchar(4000)
as
begin
set nocount on

declare @str varchar(max) =''
if(@type = 'biz')
begin
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 
set @str = 'select ord_date,ord_type,ord_title,ord_depart,ord_createdepart,ord_no,ord_creator from ' 
+@dbname+@tbname+ ' where ord_id='+convert(varchar(50),@objid)
exec(@str)
end else
if(@type = 'doc')
begin
select doc_create ord_date, doc_title ord_type,'' ord_title,doc_depart ord_depart,
doc_depart ord_createdepart,'' ord_no, doc_creator ord_creator 
from mps_document where doc_id = @objid
end

--创建内存表，存储所有fm_ffmid = 0的评论
declare @pobjs table(nid int identity(1, 1), fmid bigint) 
if (@ebstag=-999999999) set @ebstag = 0
if (@orderby=-999999999) set @orderby = 0

declare @ss1 varchar(8000)
declare @ss2 varchar(100) 
declare @i int
set @ss1 = @pageobjids 
if (@ss1='_none_') set @ss1=''
while (@ss1>'') 
begin
  set @i=charindex(',',@ss1)
  if (@i>0)
  begin
    set @ss2 = substring(@ss1,1,@i-1) 
    set @ss1 = substring(@ss1,@i+1,len(@ss1)-@i)
  end else 
  begin
    set @ss2 = @ss1 
    set @ss1 = '' 
  end 
  if (@ss2>'') insert into @pobjs(fmid) values(convert(bigint,@ss2)) 
end
if (not exists (select top 1 * from @pobjs)) 
begin
-- 返回10条ID
if(@orderby=0)
begin
insert into @pobjs(fmid)
select top 10 fm_id from mps_forum a
where a.fm_cmpid = @cmpid and a.fm_ffmid=0 and a.fm_type = @type and a.fm_ctgid = @ctgid and a.fm_objid=@objid and a.fm_ebstag = @ebstag 
order by a.fm_id asc
end else 
begin
insert into @pobjs(fmid)
select top 10 fm_id from mps_forum a
where a.fm_cmpid = @cmpid and a.fm_ffmid=0 and a.fm_type = @type and a.fm_ctgid = @ctgid and a.fm_objid=@objid and a.fm_ebstag = @ebstag 
order by a.fm_like desc,a.fm_id desc
end
end

--查询出回复
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  , @pobjs 
where a.fm_id=fmid 
order by nid 

--查询出总回复数
select count(*) as totalcount from mps_forum a
where a.fm_cmpid = @cmpid and a.fm_type = @type and a.fm_ctgid = @ctgid and a.fm_objid=@objid and a.fm_ebstag = @ebstag

--查询出主回复的回复
declare @s1 varchar(500) = '',@j  int = 0
declare @s2 varchar(500)
select @s1 = stuff((
select ','+convert(varchar(100),fmid)
from @pobjs for xml path('')),1,1,'')

set @str = ''
while @s1 > ''
begin
set @j = charindex(',', @s1) 
if (@j>0) 
begin 
set @s2 = substring(@s1,1,@j-1) 
set @s1 = substring(@s1,@j+1,len(@s1)-@j) 
end else  
begin 
set @s2 = @s1 
set @s1 = '' 
end 
set @str += ' fm_ffmid = '+ @s2 + ' or fm_fffmid = '+ @s2 + ' or '
end

if @str > '' 
begin
set @str = ' select *,(select count(fm_id) from mps_forum b where b.fm_ffmid = a.fm_id) as fm_count,
(select count(*) from mps_forum_like 
where fml_fmid = a.fm_id and fml_cmpid = '+ convert(varchar(50), @cmpid) +' and fml_bcid = '+ convert(varchar(50), @branchid) +' and fml_usertype = '+ convert(varchar(50), @usertype)+' and fml_cuid = '+ convert(varchar(50), @useruid) +' and fml_like=0) 
as fm_userlike,
(select count(*) from mps_forum_like 
where fml_fmid = a.fm_id and fml_cmpid ='+ convert(varchar(50), @cmpid)+' and fml_bcid = '+ convert(varchar(50), @branchid)+' and fml_usertype = '+ convert(varchar(50), @usertype)+' and fml_cuid = '+ convert(varchar(50), @useruid)+' and fml_like=1) 
as fm_userdislike,
getdate() as fm_enddate from mps_forum a where ' + @str
set @str = substring(@str,1,len(@str)-3) + ' order by fm_id '
exec(@str)
end

if @pageobjids=''
begin
--查询出总的回复id
if(@orderby=0)
begin
select fm_id from mps_forum a
where a.fm_cmpid = @cmpid and a.fm_ffmid=0 and a.fm_type = @type and a.fm_ctgid = @ctgid and a.fm_objid=@objid and a.fm_ebstag = @ebstag 
order by a.fm_id asc
end else
begin
select fm_id from mps_forum a
where a.fm_cmpid = @cmpid and a.fm_ffmid=0 and a.fm_type = @type and a.fm_ctgid = @ctgid and a.fm_objid=@objid and a.fm_ebstag = @ebstag 
order by a.fm_like desc,a.fm_id desc
end



end


set nocount off
end