IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_employee_all]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_employee_all]
GO
create proc [dbo].[mps_employee_all]
@cmpid int,
@bcid int,
@dpid int,
@key varchar(50),
@type int,
@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 COUNT(1) from mps_employee_subusers with(nolock) where cu_id = cs_cuid) as cu_subcuAcmount from mps_employee a , @pobjs where a.cu_id=lgid order by nid 
return ;
end


  if @type=1
  begin
select count(1) as reccount
from mps_employee a 
where a.cu_cmpid=@cmpid 
and a.cu_bcid=@bcid 
--and exists (select 1 from mps_login b where b.cu_id =a.cu_id)
and a.cu_dpid=@dpid
and (a.cu_code like '%'+@key+'%' 
or a.cu_account like '%'+@key+'%' 
or a.cu_name  like '%'+@key+'%' 
or a.cu_contact like '%'+@key+'%' 
or a.cu_mobile like '%'+@key+'%' 
or a.cu_position like '%'+@key+'%' 
or a.cu_qq like '%'+@key+'%' 
or a.cu_depart like '%'+@key+'%' 
or a.cu_title like '%'+@key+'%' 
or a.cu_depart2 like '%'+@key+'%' 
or a.cu_title2 like '%'+@key+'%') 
and charindex('(X)2',cu_code)=0 
and charindex('(X)2',cu_account)=0 

select top(@amount) a.cu_id 
from mps_employee a 
where a.cu_cmpid=@cmpid 
and a.cu_bcid=@bcid 
--and exists (select 1 from mps_login b where b.cu_id =a.cu_id)
and a.cu_dpid=@dpid
and (a.cu_code like '%'+@key+'%' 
or a.cu_account like '%'+@key+'%' 
or a.cu_name  like '%'+@key+'%' 
or a.cu_contact like '%'+@key+'%' 
or a.cu_mobile like '%'+@key+'%' 
or a.cu_position like '%'+@key+'%' 
or a.cu_qq like '%'+@key+'%' 
or a.cu_depart like '%'+@key+'%' 
or a.cu_title like '%'+@key+'%' 
or a.cu_depart2 like '%'+@key+'%' 
or a.cu_title2 like '%'+@key+'%')
and charindex('(X)2',cu_code)=0 
and charindex('(X)2',cu_account)=0 
order by a.cu_code asc 


select top(@pagesize) *,(select COUNT(1) from mps_employee_subusers with(nolock) where cu_id = cs_cuid) as cu_subcuAcmount from mps_employee a 
where a.cu_cmpid=@cmpid 
and a.cu_bcid=@bcid 
--and exists (select 1 from mps_login b where b.cu_id =a.cu_id)
and a.cu_dpid=@dpid
and (a.cu_code like '%'+@key+'%' 
or a.cu_account like '%'+@key+'%' 
or a.cu_name  like '%'+@key+'%' 
or a.cu_contact like '%'+@key+'%' 
or a.cu_mobile like '%'+@key+'%' 
or a.cu_position like '%'+@key+'%' 
or a.cu_qq like '%'+@key+'%' 
or a.cu_depart like '%'+@key+'%' 
or a.cu_title like '%'+@key+'%' 
or a.cu_depart2 like '%'+@key+'%' 
or a.cu_title2 like '%'+@key+'%')
and charindex('(X)2',cu_code)=0 
and charindex('(X)2',cu_account)=0 
order by a.cu_code asc  
  end
  else 
  if @type=2
  begin
select count(1) as reccount
from mps_employee a 
where cu_cmpid=@cmpid 
and a.cu_bcid=@bcid 
--and exists (select 1 from mps_login b where b.cu_id =a.cu_id)
and (a.cu_code like '%'+@key+'%' 
or a.cu_account like '%'+@key+'%' 
or a.cu_name  like '%'+@key+'%' 
or a.cu_contact like '%'+@key+'%' 
or a.cu_mobile like '%'+@key+'%' 
or a.cu_position like '%'+@key+'%' 
or a.cu_qq like '%'+@key+'%' 
or a.cu_depart like '%'+@key+'%' 
or a.cu_title like '%'+@key+'%' 
or a.cu_depart2 like '%'+@key+'%' 
or a.cu_title2 like '%'+@key+'%')
and charindex('(X)2',cu_code)=0 
and charindex('(X)2',cu_account)=0 

select top(@amount) a.cu_id
from mps_employee a 
where cu_cmpid=@cmpid 
and a.cu_bcid=@bcid 
--and exists (select 1 from mps_login b where b.cu_id =a.cu_id)
and (a.cu_code like '%'+@key+'%' 
or a.cu_account like '%'+@key+'%' 
or a.cu_name  like '%'+@key+'%' 
or a.cu_contact like '%'+@key+'%' 
or a.cu_mobile like '%'+@key+'%' 
or a.cu_position like '%'+@key+'%' 
or a.cu_qq like '%'+@key+'%' 
or a.cu_depart like '%'+@key+'%' 
or a.cu_title like '%'+@key+'%' 
or a.cu_depart2 like '%'+@key+'%' 
or a.cu_title2 like '%'+@key+'%')
and charindex('(X)2',cu_code)=0 
and charindex('(X)2',cu_account)=0 
order by a.cu_code asc



  
select top(@pagesize) *,(select COUNT(1) from mps_employee_subusers with(nolock) where cu_id = cs_cuid) as cu_subcuAcmount from mps_employee a 
where cu_cmpid=@cmpid 
and a.cu_bcid=@bcid 
--and exists (select 1 from mps_login b where b.cu_id =a.cu_id)
and (a.cu_code like '%'+@key+'%' 
or a.cu_account like '%'+@key+'%' 
or a.cu_name  like '%'+@key+'%' 
or a.cu_contact like '%'+@key+'%' 
or a.cu_mobile like '%'+@key+'%' 
or a.cu_position like '%'+@key+'%' 
or a.cu_qq like '%'+@key+'%' 
or a.cu_depart like '%'+@key+'%' 
or a.cu_title like '%'+@key+'%' 
or a.cu_depart2 like '%'+@key+'%' 
or a.cu_title2 like '%'+@key+'%')
and charindex('(X)2',cu_code)=0 
and charindex('(X)2',cu_account)=0 
order by a.cu_code asc  
  end
  set nocount off
end