IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_263_23_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_263_23_query]
GO
CREATE  procedure order_263_23_query(
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@bizid int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@ord_sjxh_min nvarchar(100), @ord_sjxh_max nvarchar(100),@ord_sjch_min nvarchar(100), @ord_sjch_max nvarchar(100),@ord_ysjch_min nvarchar(100), @ord_ysjch_max nvarchar(100),@ord_sjpp_min nvarchar(100), @ord_sjpp_max nvarchar(100),@ord_sfzh_min nvarchar(2000), @ord_sfzh_max nvarchar(2000),@ord_khxm_min nvarchar(100), @ord_khxm_max nvarchar(100),@ord_lxdh_min nvarchar(2000), @ord_lxdh_max nvarchar(2000)) as begin  
set nocount on  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
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 
create table #pobjs(
nid int  identity(1, 1),
ordid bigint  primary key not null)
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 order_263 a  with(nolock) ,#pobjs
where ord_id=ordid
order by nid 

--select a.* from order_263_authority a , #pobjs 
--where  orda_cuid=@useruid
--drop table #pobjs
return 
end 


if (@ord_sjxh_min is null or @ord_sjxh_min = '_none_') set @ord_sjxh_min = '' 
if (@ord_sjxh_max is null or @ord_sjxh_max = '_none_') set @ord_sjxh_max = '' 

if (@ord_sjch_min is null or @ord_sjch_min = '_none_') set @ord_sjch_min = '' 
if (@ord_sjch_max is null or @ord_sjch_max = '_none_') set @ord_sjch_max = '' 

if (@ord_ysjch_min is null or @ord_ysjch_min = '_none_') set @ord_ysjch_min = '' 
if (@ord_ysjch_max is null or @ord_ysjch_max = '_none_') set @ord_ysjch_max = '' 

if (@ord_sjpp_min is null or @ord_sjpp_min = '_none_') set @ord_sjpp_min = '' 
if (@ord_sjpp_max is null or @ord_sjpp_max = '_none_') set @ord_sjpp_max = '' 

if (@ord_sfzh_min is null or @ord_sfzh_min = '_none_') set @ord_sfzh_min = '' 
if (@ord_sfzh_max is null or @ord_sfzh_max = '_none_') set @ord_sfzh_max = '' 

if (@ord_khxm_min is null or @ord_khxm_min = '_none_') set @ord_khxm_min = '' 
if (@ord_khxm_max is null or @ord_khxm_max = '_none_') set @ord_khxm_max = '' 

if (@ord_lxdh_min is null or @ord_lxdh_min = '_none_') set @ord_lxdh_min = '' 
if (@ord_lxdh_max is null or @ord_lxdh_max = '_none_') set @ord_lxdh_max = '' 

if @ord_sjxh_min>'' 
begin 
if (charindex('%',@ord_sjxh_min) = 0 and @ord_sjxh_min!='_empty_') 
 begin
set @ord_sjxh_min = '%'+@ord_sjxh_min+'%';
 end else if (@ord_sjxh_min='_empty_') set @ord_sjxh_min='' 
end else
begin 
set @ord_sjxh_min = '%' 
end
if @ord_sjxh_max>'' 
begin 
if (charindex('%', @ord_sjxh_max) = 0 and @ord_sjxh_max!='_empty_') 
begin
set@ord_sjxh_max = '%'+@ord_sjxh_max+'%';
 end else if (@ord_sjxh_max='_empty_') set @ord_sjxh_max='' 
end else
begin 
set @ord_sjxh_max = '%' 
end

if @ord_sjch_min>'' 
begin 
if (charindex('%',@ord_sjch_min) = 0 and @ord_sjch_min!='_empty_') 
 begin
set @ord_sjch_min = '%'+@ord_sjch_min+'%';
 end else if (@ord_sjch_min='_empty_') set @ord_sjch_min='' 
end else
begin 
set @ord_sjch_min = '%' 
end
if @ord_sjch_max>'' 
begin 
if (charindex('%', @ord_sjch_max) = 0 and @ord_sjch_max!='_empty_') 
begin
set@ord_sjch_max = '%'+@ord_sjch_max+'%';
 end else if (@ord_sjch_max='_empty_') set @ord_sjch_max='' 
end else
begin 
set @ord_sjch_max = '%' 
end

if @ord_ysjch_min>'' 
begin 
if (charindex('%',@ord_ysjch_min) = 0 and @ord_ysjch_min!='_empty_') 
 begin
set @ord_ysjch_min = '%'+@ord_ysjch_min+'%';
 end else if (@ord_ysjch_min='_empty_') set @ord_ysjch_min='' 
end else
begin 
set @ord_ysjch_min = '%' 
end
if @ord_ysjch_max>'' 
begin 
if (charindex('%', @ord_ysjch_max) = 0 and @ord_ysjch_max!='_empty_') 
begin
set@ord_ysjch_max = '%'+@ord_ysjch_max+'%';
 end else if (@ord_ysjch_max='_empty_') set @ord_ysjch_max='' 
end else
begin 
set @ord_ysjch_max = '%' 
end

if @ord_sjpp_min>'' 
begin 
if (charindex('%',@ord_sjpp_min) = 0 and @ord_sjpp_min!='_empty_') 
 begin
set @ord_sjpp_min = '%'+@ord_sjpp_min+'%';
 end else if (@ord_sjpp_min='_empty_') set @ord_sjpp_min='' 
end else
begin 
set @ord_sjpp_min = '%' 
end
if @ord_sjpp_max>'' 
begin 
if (charindex('%', @ord_sjpp_max) = 0 and @ord_sjpp_max!='_empty_') 
begin
set@ord_sjpp_max = '%'+@ord_sjpp_max+'%';
 end else if (@ord_sjpp_max='_empty_') set @ord_sjpp_max='' 
end else
begin 
set @ord_sjpp_max = '%' 
end

if @ord_sfzh_min>'' 
begin 
if (charindex('%',@ord_sfzh_min) = 0 and @ord_sfzh_min!='_empty_') 
 begin
set @ord_sfzh_min = '%'+@ord_sfzh_min+'%';
 end else if (@ord_sfzh_min='_empty_') set @ord_sfzh_min='' 
end else
begin 
set @ord_sfzh_min = '%' 
end
if @ord_sfzh_max>'' 
begin 
if (charindex('%', @ord_sfzh_max) = 0 and @ord_sfzh_max!='_empty_') 
begin
set@ord_sfzh_max = '%'+@ord_sfzh_max+'%';
 end else if (@ord_sfzh_max='_empty_') set @ord_sfzh_max='' 
end else
begin 
set @ord_sfzh_max = '%' 
end

if @ord_khxm_min>'' 
begin 
if (charindex('%',@ord_khxm_min) = 0 and @ord_khxm_min!='_empty_') 
 begin
set @ord_khxm_min = '%'+@ord_khxm_min+'%';
 end else if (@ord_khxm_min='_empty_') set @ord_khxm_min='' 
end else
begin 
set @ord_khxm_min = '%' 
end
if @ord_khxm_max>'' 
begin 
if (charindex('%', @ord_khxm_max) = 0 and @ord_khxm_max!='_empty_') 
begin
set@ord_khxm_max = '%'+@ord_khxm_max+'%';
 end else if (@ord_khxm_max='_empty_') set @ord_khxm_max='' 
end else
begin 
set @ord_khxm_max = '%' 
end

if @ord_lxdh_min>'' 
begin 
if (charindex('%',@ord_lxdh_min) = 0 and @ord_lxdh_min!='_empty_') 
 begin
set @ord_lxdh_min = '%'+@ord_lxdh_min+'%';
 end else if (@ord_lxdh_min='_empty_') set @ord_lxdh_min='' 
end else
begin 
set @ord_lxdh_min = '%' 
end
if @ord_lxdh_max>'' 
begin 
if (charindex('%', @ord_lxdh_max) = 0 and @ord_lxdh_max!='_empty_') 
begin
set@ord_lxdh_max = '%'+@ord_lxdh_max+'%';
 end else if (@ord_lxdh_max='_empty_') set @ord_lxdh_max='' 
end else
begin 
set @ord_lxdh_max = '%' 
end
 
    
if (@branchid <> 0)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_263 a  with(nolock) ,mps_job with(nolock)   
where  
 
a.ord_cmpid=@cmpid
and ord_sjxh like @ord_sjxh_min 
and ord_sjxh like @ord_sjxh_max 
and ord_sjch like @ord_sjch_min 
and ord_sjch like @ord_sjch_max 
and ord_ysjch like @ord_ysjch_min 
and ord_ysjch like @ord_ysjch_max 
and ord_sjpp like @ord_sjpp_min 
and ord_sjpp like @ord_sjpp_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
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.ord_id  from order_263 a  with(nolock) ,mps_job with(nolock)   
where  

a.ord_cmpid=@cmpid
and ord_sjxh like @ord_sjxh_min 
and ord_sjxh like @ord_sjxh_max 
and ord_sjch like @ord_sjch_min 
and ord_sjch like @ord_sjch_max 
and ord_ysjch like @ord_ysjch_min 
and ord_ysjch like @ord_ysjch_max 
and ord_sjpp like @ord_sjpp_min 
and ord_sjpp like @ord_sjpp_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
select count(1) resultcount  from datalinker  with(nolock) where useruid=@useruid 
end else
begin
select count(1) cnt from order_263 a  with(nolock) ,mps_job with(nolock)   
where  
 
a.ord_cmpid=@cmpid
and ord_sjxh like @ord_sjxh_min 
and ord_sjxh like @ord_sjxh_max 
and ord_sjch like @ord_sjch_min 
and ord_sjch like @ord_sjch_max 
and ord_ysjch like @ord_ysjch_min 
and ord_ysjch like @ord_ysjch_max 
and ord_sjpp like @ord_sjpp_min 
and ord_sjpp like @ord_sjpp_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1)
-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_263 a  with(nolock) ,mps_job with(nolock)  
where  

a.ord_cmpid=@cmpid
and ord_sjxh like @ord_sjxh_min 
and ord_sjxh like @ord_sjxh_max 
and ord_sjch like @ord_sjch_min 
and ord_sjch like @ord_sjch_max 
and ord_ysjch like @ord_ysjch_min 
and ord_ysjch like @ord_ysjch_max 
and ord_sjpp like @ord_sjpp_min 
and ord_sjpp like @ord_sjpp_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_263 a  with(nolock) ,mps_job with(nolock)  ,#pobjs 
where a.ord_id=ordid and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 

and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_263_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end else
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_263 a  with(nolock) ,mps_job with(nolock)  
where  
 
a.ord_cmpid=@cmpid
and ord_sjxh like @ord_sjxh_min 
and ord_sjxh like @ord_sjxh_max 
and ord_sjch like @ord_sjch_min 
and ord_sjch like @ord_sjch_max 
and ord_ysjch like @ord_ysjch_min 
and ord_ysjch like @ord_ysjch_max 
and ord_sjpp like @ord_sjpp_min 
and ord_sjpp like @ord_sjpp_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
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.ord_id  from order_263 a  with(nolock) ,mps_job with(nolock)  
where  

a.ord_cmpid=@cmpid
and ord_sjxh like @ord_sjxh_min 
and ord_sjxh like @ord_sjxh_max 
and ord_sjch like @ord_sjch_min 
and ord_sjch like @ord_sjch_max 
and ord_ysjch like @ord_ysjch_min 
and ord_ysjch like @ord_ysjch_max 
and ord_sjpp like @ord_sjpp_min 
and ord_sjpp like @ord_sjpp_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
select count(1) resultcount  from datalinker  with(nolock) where useruid=@useruid 
end else
begin
select count(1) cnt from order_263 a  with(nolock) ,mps_job with(nolock) 
where  
 
a.ord_cmpid=@cmpid
and ord_sjxh like @ord_sjxh_min 
and ord_sjxh like @ord_sjxh_max 
and ord_sjch like @ord_sjch_min 
and ord_sjch like @ord_sjch_max 
and ord_ysjch like @ord_ysjch_min 
and ord_ysjch like @ord_ysjch_max 
and ord_sjpp like @ord_sjpp_min 
and ord_sjpp like @ord_sjpp_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1)
-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_263 a  with(nolock) ,mps_job with(nolock)  
where  

a.ord_cmpid=@cmpid
and ord_sjxh like @ord_sjxh_min 
and ord_sjxh like @ord_sjxh_max 
and ord_sjch like @ord_sjch_min 
and ord_sjch like @ord_sjch_max 
and ord_ysjch like @ord_ysjch_min 
and ord_ysjch like @ord_ysjch_max 
and ord_sjpp like @ord_sjpp_min 
and ord_sjpp like @ord_sjpp_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_263 a  with(nolock) ,mps_job with(nolock)  ,#pobjs 
where a.ord_id=ordid and  (ord_cmpid=@cmpid and jb_crmid=263 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 

and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_263_authority a with(nolock)  , #pobjs 
--where 
  --  nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
drop table #pobjs
set nocount off end