IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_prd_query_tag]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_prd_query_tag]
GO
CREATE  procedure [dbo].[mps_prd_query_tag](
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@onlyme int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@pctag int , @prd_area_min nvarchar(100), @prd_area_max nvarchar(100),@prd_attachi_min float,@prd_attachi_max float) as begin     set nocount on 
    
    declare @nonedate datetime 
    set @nonedate = CONVERT(datetime,'1910-1-1') 
    declare @s1 varchar(8000) 
    declare @s2 varchar(100) 
    declare @i int 
    declare @amount int 
    select @amount = @pagecount*@pagesize 
    declare @pobjs table(nid int identity(1, 1), ordid bigint) 
    set @s1 = @pageobjids  
    while (@s1>'') 
    begin 
    set @i = charindex(',', @s1) 
    if (@i>0) 
    begin 
    set @s2 = substring(@s1,1,@i-1) 
    set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
    end else  
    begin 
    set @s2 = @s1 
    set @s1 = '' 
    end  
    insert into @pobjs(ordid) values(convert(bigint,@s2)) 
    end 
    if (exists (select top 1 * from @pobjs)) 
    begin 
    select a.* from mps_prd a,    @pobjs
    where prd_id=ordid
    order by nid  
    select a.* from mps_prd_users a , @pobjs where prdp_prdid=ordid and prdp_cuid=@useruid
  order by prdp_type    return 
    end 
if (@prd_area_min is null or @prd_area_min = '_none_') set @prd_area_min = '' 
if (@prd_area_max is null or @prd_area_max = '_none_') set @prd_area_max = '' 

if (@prd_attachi_min is null or @prd_attachi_min = -999999999) set @prd_attachi_min = -999999999 
if (@prd_attachi_max is null or @prd_attachi_max = -999999999) set @prd_attachi_max = 999999999

if @prd_area_min>'' 
begin 
if (charindex('%',@prd_area_min) = 0 and @prd_area_min!='_empty_') 
 begin
set @prd_area_min = '%'+@prd_area_min+'%';
 end else if (@prd_area_min='_empty_') set @prd_area_min='' 
end else
begin 
set @prd_area_min = '%' 
end
if @prd_area_max>'' 
begin 
if (charindex('%', @prd_area_max) = 0 and @prd_area_max!='_empty_') 
begin
set@prd_area_max = '%'+@prd_area_max+'%';
 end else if (@prd_area_max='_empty_') set @prd_area_max='' 
end else
begin 
set @prd_area_max = '%' 
end
 
    
declare @prduse int =0
if @onlyme = 2 set @prduse = 1
else set @prduse = 0

if (@onlyme = 0) 
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from mps_prd a,mps_prd_users , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and (a.prd_cuid=@useruid or a.prd_createcuid=@useruid ) 
and prd_area like @prd_area_min 
and prd_area like @prd_area_max 
and prd_attachi>=@prd_attachi_min 
and prd_attachi<=@prd_attachi_max 
order by prd_updated desc,prd_area,prd_attachi
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.prd_id  from mps_prd a,mps_prd_users , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview'
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and (a.prd_cuid=@useruid or a.prd_createcuid=@useruid ) 
and prd_area like @prd_area_min 
and prd_area like @prd_area_max 
and prd_attachi>=@prd_attachi_min 
and prd_attachi<=@prd_attachi_max 
order by prd_updated desc,prd_area,prd_attachi
select count(1) resultcount  from datalinker where useruid=@useruid 
end else 
begin
select count(1) cnt from mps_prd a,mps_prd_users , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and (a.prd_cuid= @useruid or a.prd_createcuid= @useruid) 
and prd_area like @prd_area_min 
and prd_area like @prd_area_max 
and prd_attachi>=@prd_attachi_min 
and prd_attachi<=@prd_attachi_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) prd_id from mps_prd a ,mps_prd_users , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid
and (a.prd_cuid= @useruid or a.prd_createcuid= @useruid) 
and prd_area like @prd_area_min 
and prd_area like @prd_area_max 
and prd_attachi>=@prd_attachi_min 
and prd_attachi<=@prd_attachi_max 
order by prd_updated desc,prd_area,prd_attachi
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from mps_prd a,@pobjs 
where a.prd_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_prd_users a , @pobjs 
where prdp_prdid=ordid 
and nid<=@pagesize 
and prdp_cuid=@useruid 
order by prdp_type 
end 
end 
else
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from mps_prd a,mps_prd_users  , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and prd_area like @prd_area_min 
and prd_area like @prd_area_max 
and prd_attachi>=@prd_attachi_min 
and prd_attachi<=@prd_attachi_max 
order by prd_updated desc,prd_area,prd_attachi
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.prd_id  from mps_prd a,mps_prd_users  , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and prd_area like @prd_area_min 
and prd_area like @prd_area_max 
and prd_attachi>=@prd_attachi_min 
and prd_attachi<=@prd_attachi_max 
order by prd_updated desc,prd_area,prd_attachi
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from mps_prd a,mps_prd_users  , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and prd_area like @prd_area_min 
and prd_area like @prd_area_max 
and prd_attachi>=@prd_attachi_min 
and prd_attachi<=@prd_attachi_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) prd_id from mps_prd a,mps_prd_users  , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid
and prd_area like @prd_area_min 
and prd_area like @prd_area_max 
and prd_attachi>=@prd_attachi_min 
and prd_attachi<=@prd_attachi_max 
order by prd_updated desc,prd_area,prd_attachi
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from mps_prd a,@pobjs 
where a.prd_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_prd_users a , @pobjs 
where prdp_prdid=ordid 
and nid<=@pagesize 
and prdp_cuid=@useruid 
order by prdp_type 
end 
end
set nocount off end