IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_shopinfo]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_shopinfo]
GO
create proc [dbo].[mps_g_shopinfo]
@cmpid bigint,
@model bigint,
@province nvarchar(100),
@municipal nvarchar(100),
@county nvarchar(100),
@type nvarchar(100),
@bcid bigint,
@pagesize int ,
@pagecount int,
@pageobjids varchar(8000) 
as
begin
set nocount on
declare @amount int 
set @amount=@pagecount*@pagesize

declare @pobjs table(nid int identity(1,1) , lgid bigint)
declare @s1 varchar(8000)
declare @s2 varchar(100) 
declare @i int
set @s1 = @pageobjids 
if (@s1='_none_') set @s1=''
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 
  if (@s2>'') insert into @pobjs(lgid) values(convert(bigint,@s2)) 
end
if (exists (select * from @pobjs))
begin
select *,(select b.dp_depart from mps_depart  b where b.dp_id=a.dp_fdpid) dp_fdepart from mps_depart a  , @pobjs where a.dp_id=lgid order by nid 
return ;
end


select count(1) as reccount 
from mps_depart a
where dp_code != '' and (@model=0 and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and a.dp_cmpid=1 and (@bcid=0 or a.dp_bcid=@bcid)) 
or (@model=1 and a.dp_cmpid= @cmpid and a.dp_province = @province and (@municipal ='' or a.dp_municipal = @municipal)
 and (@county ='' or a.dp_county = @county)
 and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and (@bcid=0 or a.dp_bcid=@bcid) ) 
or (@model=2 and (@bcid=0 or a.dp_bcid=@bcid) and a.dp_cmpid= @cmpid and a.dp_note=@type and a.dp_isdepart = 1) 
or (@model=3 and (@bcid=0 or a.dp_bcid=@bcid) and a.dp_cmpid= @cmpid and a.dp_managearea = @type and a.dp_isdepart = 1) 
or (@model=4 and a.dp_cmpid= @cmpid and a.dp_category1 = @province and (@municipal ='' or a.dp_category2 = @municipal)
 and (@county ='' or a.dp_category3 = @county) 
 and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and (@bcid=0 or a.dp_bcid=@bcid))
or (@model=5 and a.dp_cmpid= @cmpid and a.dp_depart like '%'+@type+'%' and a.dp_isdepart = 1) 

select top(@amount) a.dp_id from mps_depart a 
where dp_code != '' and (@model=0 and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and a.dp_cmpid=1 and (@bcid=0 or a.dp_bcid=@bcid)) 
or (@model=1 and a.dp_cmpid= @cmpid and a.dp_province = @province and (@municipal ='' or a.dp_municipal = @municipal)
 and (@county ='' or a.dp_county = @county)  
and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and (@bcid=0 or a.dp_bcid=@bcid)) 
or (@model=2 and (@bcid=0 or a.dp_bcid=@bcid) and a.dp_cmpid= @cmpid and a.dp_note=@type and a.dp_isdepart = 1) 
or (@model=3 and (@bcid=0 or a.dp_bcid=@bcid) and a.dp_cmpid= @cmpid and a.dp_managearea = @type and a.dp_isdepart = 1)
or (@model=4 and a.dp_cmpid= @cmpid and a.dp_category1 = @province and (@municipal ='' or a.dp_category2 = @municipal)
 and (@county ='' or a.dp_category3 = @county) 
 and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and (@bcid=0 or a.dp_bcid=@bcid))
or (@model=5 and a.dp_cmpid= @cmpid and a.dp_depart like '%'+@type+'%' and a.dp_isdepart = 1)
order by a.dp_order, a.dp_id

select top(@pagesize) *,(select b.dp_depart from mps_depart  b where b.dp_id=a.dp_fdpid) dp_fdepart from mps_depart a
where dp_code != '' and (@model=0 and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and a.dp_cmpid=1 and (@bcid=0 or a.dp_bcid=@bcid)) 
or (@model=1 and a.dp_cmpid= @cmpid and a.dp_province = @province and (@municipal ='' or a.dp_municipal = @municipal)
 and (@county ='' or a.dp_county = @county)  
and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and (@bcid=0 or a.dp_bcid=@bcid)) 
or (@model=2 and (@bcid=0 or a.dp_bcid=@bcid) and a.dp_cmpid= @cmpid and a.dp_note=@type and a.dp_isdepart = 1) 
or (@model=3 and (@bcid=0 or a.dp_bcid=@bcid) and a.dp_cmpid= @cmpid and a.dp_managearea = @type and a.dp_isdepart = 1) 
or (@model=4 and a.dp_cmpid= @cmpid and a.dp_category1 = @province and (@municipal ='' or a.dp_category2 = @municipal)
 and (@county ='' or a.dp_category3 = @county) 
 and a.dp_isdepart = 1 and a.dp_cmpid= @cmpid and (@bcid=0 or a.dp_bcid=@bcid))
or (@model=5 and a.dp_cmpid= @cmpid and a.dp_depart like '%'+@type+'%' and a.dp_isdepart = 1) 
order by a.dp_order, a.dp_id  
set nocount off
end