IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_document_search]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_document_search]
GO

CREATE proc [dbo].[mps_g_document_search] 
@key1 nvarchar(100),@key2 nvarchar(100),@key3 nvarchar(100),@key4 nvarchar(100),
@key5 nvarchar(100),@key6 nvarchar(100),@key7 nvarchar(100),@key8 nvarchar(100),
@key9 nvarchar(100),@key10 nvarchar(100),
@dcid int ,
@useruid int ,
@usersecurity int ,
@branchid int,
@userdpid int,
@userotid int ,
@order varchar(40),
@objid bigint,
@cmpid int,
@begindate varchar(100),
@enddate varchar(100)
as
begin
    set nocount on
    declare @i bigint, @l bigint, @r bigint,@now datetime, @s bigint;
    --判断是否选择了种类
    
    declare @dc table(dcid int) 
    if (@dcid>0) 
    begin
        insert into @dc(dcid) values(@dcid) 
    end else 
    begin
        insert into @dc(dcid) select dc_id from mps_docctg where dc_cmpid=@cmpid and dc_tag=(-1)*@dcid 
    end 
    -- 将子目录加入
    insert into @dc(dcid) 
    select dc_id from mps_docctg , @dc 
    where dc_father = dcid and 
    (not exists (select 1 from @dc where dcid=dc_id))
    
    insert into @dc(dcid) 
    select dc_id from mps_docctg , @dc 
    where dc_father = dcid and 
    (not exists (select 1 from @dc where dcid=dc_id))
    
    insert into @dc(dcid) 
    select dc_id from mps_docctg , @dc 
    where dc_father = dcid and 
    (not exists (select 1 from @dc where dcid=dc_id))
    
    insert into @dc(dcid) 
    select dc_id from mps_docctg , @dc 
    where dc_father = dcid and 
    (not exists (select 1 from @dc where dcid=dc_id))

    declare @t table
    (
        id bigint identity(1,1),
        doc_id bigint,
        doc_title nvarchar(255),
        doc_click bigint,
        doc_icon1 varchar(max),
        doc_icon2 varchar(max),
        doc_like bigint,
        doc_create datetime,
        doc_dcid bigint,
        doc_share bigint,
        doc_abstract nvarchar(400),
doc_forum int
    )
    set @i = @objid
    if (@i=0)
        begin
            set @l = 99999999
            set @r = 99999999
            set @i = 99999999
            set @now = getdate()
            set @s = 99999999
        end
    else 
        begin
            select @l = doc_like,@r = doc_read,@now = doc_create, @s = doc_share from mps_document where doc_id = @i
        end
    if @order = 'time'   
    begin
        if(@i=99999999)
        begin
                select top 15 a.*,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum
from mps_document a, @dc 
                where doc_id< @i and dcid=doc_dcid  
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_create desc,doc_id
                
                select top 3 a.*,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum from mps_document a , @dc  where doc_icon2 != '' and  dcid=doc_dcid 
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_create desc,doc_id
        end
        else
        begin
            ---查询出1000条小于等于当前点赞量的
                insert into @t(doc_id,doc_title,doc_icon1,doc_icon2,doc_like,doc_click,doc_create,doc_dcid,doc_share,doc_abstract,doc_forum) 
                select top 1000 doc_id,doc_title,doc_icon1,doc_icon2,doc_like,doc_read,doc_create,doc_dcid,doc_share,doc_abstract,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum
                from mps_document , @dc where doc_create <= @now and  dcid=doc_dcid 
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_create desc,doc_id
            -- 查询出十五条
            select @l = id from @t where doc_id = @i
            select top 15 * from @t where id>@l order by id
            select top 3 * from @t where doc_icon2 != '' and id>@l order by id
        end
    end 
    else if @order = 'like'
    begin
        if(@i=99999999)
        begin
                select top 15 a.*,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum from mps_document a, @dc 
                where doc_id< @i and dcid=doc_dcid  
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_like desc,doc_id
                
                select top 3 a.*,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum  from mps_document a , @dc  where doc_icon2 != '' and  dcid=doc_dcid 
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_like desc,doc_id
                

        end
        else
        begin
            ---查询所有
            insert into @t(doc_id,doc_title,doc_icon1,doc_icon2,doc_like,doc_click,doc_create,doc_dcid,doc_share,doc_abstract,doc_forum) 
            select top 1000 doc_id,doc_title,doc_icon1,doc_icon2,doc_like,doc_read,doc_create,doc_dcid,doc_share,doc_abstract,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum
            from mps_document , @dc where doc_like <= @l and  dcid=doc_dcid 
            and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
            and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
            and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
            and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
            and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
            and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
            and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
            and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
            and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
            and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
            and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
            order by doc_like desc,doc_id
            -- 查询出十五条
            select @l = id from @t where doc_id = @i
            select top 15 * from @t where id>@l order by id
            select top 3 * from @t where doc_icon2 != '' and id>@l order by id
        end
    end
    else if @order = 'share'
    begin
        if(@i=99999999)
        begin
                select top 15 a.*,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum  from mps_document a, @dc 
                where doc_id< @i and dcid=doc_dcid  
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_share desc,doc_id
                
                select top 3 a.*,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum  from mps_document a , @dc  where doc_icon2 != '' and  dcid=doc_dcid 
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_share desc,doc_id
        end
        else
        begin
            ---查询出1000条小于等于当前点赞量的
                insert into @t(doc_id,doc_title,doc_icon1,doc_icon2,doc_like,doc_click,doc_create,doc_dcid,doc_share,doc_abstract,doc_forum) 
                select top 1000 doc_id,doc_title,doc_icon1,doc_icon2,doc_like,doc_read,doc_create,doc_dcid,doc_share,doc_abstract,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum
                from mps_document , @dc where doc_share <= @s and  dcid=doc_dcid 
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_share desc,doc_id
            -- 查询出十五条
            select @s = id from @t where doc_id = @i
            select top 15 * from @t where id>@s order by id
            select top 3 * from @t where doc_icon2 != '' and id>@s order by id
        end
    end 
    else
    begin
        if(@i=99999999)
        begin
                select top 15 a.*,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum  from mps_document a, @dc 
                where doc_id< @i and dcid=doc_dcid  
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_click desc,doc_id
                select top 3 a.*,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum  from mps_document a , @dc  where doc_icon2 != '' and  dcid=doc_dcid 
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_click desc,doc_id
        end
        else
        begin
            ---查询出1000条小于等于当前阅读量的
                insert into @t(doc_id,doc_title,doc_icon1,doc_icon2,doc_like,doc_click,doc_create,doc_dcid,doc_share,doc_abstract,doc_forum) 
                select top 1000 doc_id,doc_title,doc_icon1,doc_icon2,doc_like,doc_read,doc_create,doc_dcid,doc_share,doc_abstract,(select count(*) from mps_forum where fm_type='doc' and fm_ctgid = doc_dcid  and fm_objid= doc_id) as doc_forum
                from mps_document ,@dc where doc_click <= @r and   dcid=doc_dcid
                and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
                and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
                and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
                and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
                and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
                and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
                and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
                and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
                and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
                and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
                and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
                order by doc_click desc,doc_id

            select @r = id from @t where doc_id = @i
            select top 15 * from @t where  id>@r order by id
            select top 3 * from @t where doc_icon2 != '' and id>@r order by id
        end
    end

    select count(*) as doc_nums from mps_document a, @dc 
    where doc_id< @i and dcid=doc_dcid  
    and (@key1 = '' or doc_title like '%'+ @key1 +'%' or doc_label like '%'+ @key1 +'%' or doc_abstract like '%'+ @key1 +'%' or doc_source like '%'+ @key1 +'%') 
    and (@key2 = '' or doc_title like '%'+ @key2 +'%' or doc_label like '%'+ @key2 +'%' or doc_abstract like '%'+ @key2 +'%' or doc_source like '%'+ @key2 +'%')
    and (@key3 = '' or doc_title like '%'+ @key3 +'%' or doc_label like '%'+ @key3 +'%' or doc_abstract like '%'+ @key3 +'%' or doc_source like '%'+ @key3 +'%')
    and (@key4 = '' or doc_title like '%'+ @key4 +'%' or doc_label like '%'+ @key4 +'%' or doc_abstract like '%'+ @key4 +'%' or doc_source like '%'+ @key4 +'%')
    and (@key5 = '' or doc_title like '%'+ @key5 +'%' or doc_label like '%'+ @key5 +'%' or doc_abstract like '%'+ @key5 +'%' or doc_source like '%'+ @key5 +'%')
    and (@key6 = '' or doc_title like '%'+ @key6 +'%' or doc_label like '%'+ @key6 +'%' or doc_abstract like '%'+ @key6 +'%' or doc_source like '%'+ @key6 +'%')
    and (@key7 = '' or doc_title like '%'+ @key7 +'%' or doc_label like '%'+ @key7 +'%' or doc_abstract like '%'+ @key7 +'%' or doc_source like '%'+ @key7 +'%')
    and (@key8 = '' or doc_title like '%'+ @key8 +'%' or doc_label like '%'+ @key8 +'%' or doc_abstract like '%'+ @key8 +'%' or doc_source like '%'+ @key8 +'%')
    and (@key9 = '' or doc_title like '%'+ @key9 +'%' or doc_label like '%'+ @key9 +'%' or doc_abstract like '%'+ @key9 +'%' or doc_source like '%'+ @key9 +'%')
    and (@key10 = '' or doc_title like '%'+ @key10 +'%' or doc_label like '%'+ @key10 +'%' or doc_abstract like '%'+ @key10 +'%' or doc_source like '%'+ @key10 +'%')
    and (@begindate = '' or doc_create >= @begindate) and (@enddate = '' or doc_create <= @enddate)
    set nocount off
end