IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_265_85_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_265_85_query]
GO
CREATE  procedure order_265_85_query(
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@bizid int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@ord_ch_min nvarchar(100), @ord_ch_max nvarchar(100),@ord_status_min nvarchar(30), @ord_status_max nvarchar(30),@ord_no_min varchar(50), @ord_no_max varchar(50),@ord_gcs_min nvarchar(100), @ord_gcs_max nvarchar(100),@ord_hhbm_min nvarchar(100), @ord_hhbm_max nvarchar(100),@ord_khxm_min nvarchar(100), @ord_khxm_max nvarchar(100),@ord_sjh_min nvarchar(100), @ord_sjh_max nvarchar(100),@ord_bmje_min int,@ord_bmje_max int,@ord_date_min datetime,@ord_date_max datetime,@ord_mdbm_min nvarchar(100), @ord_mdbm_max nvarchar(100)) 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_265 a  with(nolock) ,#pobjs
where ord_id=ordid
order by nid 

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


if (@ord_ch_min is null or @ord_ch_min = '_none_') set @ord_ch_min = '' 
if (@ord_ch_max is null or @ord_ch_max = '_none_') set @ord_ch_max = '' 

if (@ord_status_min is null or @ord_status_min = '_none_') set @ord_status_min = '' 
if (@ord_status_max is null or @ord_status_max = '_none_') set @ord_status_max = '' 

if (@ord_no_min is null or @ord_no_min = '_none_') set @ord_no_min = '' 
if (@ord_no_max is null or @ord_no_max = '_none_') set @ord_no_max = '' 

if (@ord_gcs_min is null or @ord_gcs_min = '_none_') set @ord_gcs_min = '' 
if (@ord_gcs_max is null or @ord_gcs_max = '_none_') set @ord_gcs_max = '' 

if (@ord_hhbm_min is null or @ord_hhbm_min = '_none_') set @ord_hhbm_min = '' 
if (@ord_hhbm_max is null or @ord_hhbm_max = '_none_') set @ord_hhbm_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_sjh_min is null or @ord_sjh_min = '_none_') set @ord_sjh_min = '' 
if (@ord_sjh_max is null or @ord_sjh_max = '_none_') set @ord_sjh_max = '' 

if (@ord_bmje_min is null or @ord_bmje_min = -999999999) set @ord_bmje_min = -999999999 
if (@ord_bmje_max is null or @ord_bmje_max = -999999999) set @ord_bmje_max = 999999999


                if (@ord_date_min is null or @ord_date_min <= @nonedate) set @ord_date_min = '1900-01-01' 
                
if (@ord_date_max is null or @ord_date_max <= @nonedate) set @ord_date_max =  '2299-12-30 23:59:59'
else set @ord_date_max = @ord_date_max+ '23:59:59'
                

if (@ord_mdbm_min is null or @ord_mdbm_min = '_none_') set @ord_mdbm_min = '' 
if (@ord_mdbm_max is null or @ord_mdbm_max = '_none_') set @ord_mdbm_max = '' 

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

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

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

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

if @ord_hhbm_min>'' 
begin 
if (charindex('%',@ord_hhbm_min) = 0 and @ord_hhbm_min!='_empty_') 
 begin
set @ord_hhbm_min = '%'+@ord_hhbm_min+'%';
 end else if (@ord_hhbm_min='_empty_') set @ord_hhbm_min='' 
end else
begin 
set @ord_hhbm_min = '%' 
end
if @ord_hhbm_max>'' 
begin 
if (charindex('%', @ord_hhbm_max) = 0 and @ord_hhbm_max!='_empty_') 
begin
set@ord_hhbm_max = '%'+@ord_hhbm_max+'%';
 end else if (@ord_hhbm_max='_empty_') set @ord_hhbm_max='' 
end else
begin 
set @ord_hhbm_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_sjh_min>'' 
begin 
if (charindex('%',@ord_sjh_min) = 0 and @ord_sjh_min!='_empty_') 
 begin
set @ord_sjh_min = '%'+@ord_sjh_min+'%';
 end else if (@ord_sjh_min='_empty_') set @ord_sjh_min='' 
end else
begin 
set @ord_sjh_min = '%' 
end
if @ord_sjh_max>'' 
begin 
if (charindex('%', @ord_sjh_max) = 0 and @ord_sjh_max!='_empty_') 
begin
set@ord_sjh_max = '%'+@ord_sjh_max+'%';
 end else if (@ord_sjh_max='_empty_') set @ord_sjh_max='' 
end else
begin 
set @ord_sjh_max = '%' 
end

if @ord_mdbm_min>'' 
begin 
if (charindex('%',@ord_mdbm_min) = 0 and @ord_mdbm_min!='_empty_') 
 begin
set @ord_mdbm_min = '%'+@ord_mdbm_min+'%';
 end else if (@ord_mdbm_min='_empty_') set @ord_mdbm_min='' 
end else
begin 
set @ord_mdbm_min = '%' 
end
if @ord_mdbm_max>'' 
begin 
if (charindex('%', @ord_mdbm_max) = 0 and @ord_mdbm_max!='_empty_') 
begin
set@ord_mdbm_max = '%'+@ord_mdbm_max+'%';
 end else if (@ord_mdbm_max='_empty_') set @ord_mdbm_max='' 
end else
begin 
set @ord_mdbm_max = '%' 
end
 
    
if (@branchid <> 0)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_265 a  with(nolock) ,order_265_authority  with(nolock)  
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and ord_ch like @ord_ch_min 
and ord_ch like @ord_ch_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_gcs like @ord_gcs_min 
and ord_gcs like @ord_gcs_max 
and ord_hhbm like @ord_hhbm_min 
and ord_hhbm like @ord_hhbm_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_bmje>=@ord_bmje_min 
and ord_bmje<=@ord_bmje_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_mdbm like @ord_mdbm_min 
and ord_mdbm like @ord_mdbm_max 
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_265 a  with(nolock) ,order_265_authority  with(nolock)  
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and ord_ch like @ord_ch_min 
and ord_ch like @ord_ch_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_gcs like @ord_gcs_min 
and ord_gcs like @ord_gcs_max 
and ord_hhbm like @ord_hhbm_min 
and ord_hhbm like @ord_hhbm_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_bmje>=@ord_bmje_min 
and ord_bmje<=@ord_bmje_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_mdbm like @ord_mdbm_min 
and ord_mdbm like @ord_mdbm_max 
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_265 a  with(nolock) ,order_265_authority  with(nolock)  
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and ord_ch like @ord_ch_min 
and ord_ch like @ord_ch_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_gcs like @ord_gcs_min 
and ord_gcs like @ord_gcs_max 
and ord_hhbm like @ord_hhbm_min 
and ord_hhbm like @ord_hhbm_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_bmje>=@ord_bmje_min 
and ord_bmje<=@ord_bmje_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_mdbm like @ord_mdbm_min 
and ord_mdbm like @ord_mdbm_max 
and (ord_finished>=1)
-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_265 a  with(nolock) ,order_265_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and ord_ch like @ord_ch_min 
and ord_ch like @ord_ch_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_gcs like @ord_gcs_min 
and ord_gcs like @ord_gcs_max 
and ord_hhbm like @ord_hhbm_min 
and ord_hhbm like @ord_hhbm_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_bmje>=@ord_bmje_min 
and ord_bmje<=@ord_bmje_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_mdbm like @ord_mdbm_min 
and ord_mdbm like @ord_mdbm_max 
and (ord_finished>=1) order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_265 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_265_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end else
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_265 a  with(nolock) ,order_265_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and ord_ch like @ord_ch_min 
and ord_ch like @ord_ch_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_gcs like @ord_gcs_min 
and ord_gcs like @ord_gcs_max 
and ord_hhbm like @ord_hhbm_min 
and ord_hhbm like @ord_hhbm_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_bmje>=@ord_bmje_min 
and ord_bmje<=@ord_bmje_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_mdbm like @ord_mdbm_min 
and ord_mdbm like @ord_mdbm_max 
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_265 a  with(nolock) ,order_265_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and ord_ch like @ord_ch_min 
and ord_ch like @ord_ch_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_gcs like @ord_gcs_min 
and ord_gcs like @ord_gcs_max 
and ord_hhbm like @ord_hhbm_min 
and ord_hhbm like @ord_hhbm_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_bmje>=@ord_bmje_min 
and ord_bmje<=@ord_bmje_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_mdbm like @ord_mdbm_min 
and ord_mdbm like @ord_mdbm_max 
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_265 a  with(nolock) ,order_265_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and ord_ch like @ord_ch_min 
and ord_ch like @ord_ch_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_gcs like @ord_gcs_min 
and ord_gcs like @ord_gcs_max 
and ord_hhbm like @ord_hhbm_min 
and ord_hhbm like @ord_hhbm_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_bmje>=@ord_bmje_min 
and ord_bmje<=@ord_bmje_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_mdbm like @ord_mdbm_min 
and ord_mdbm like @ord_mdbm_max 
and (ord_finished>=1)
-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_265 a  with(nolock) ,order_265_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and ord_ch like @ord_ch_min 
and ord_ch like @ord_ch_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_gcs like @ord_gcs_min 
and ord_gcs like @ord_gcs_max 
and ord_hhbm like @ord_hhbm_min 
and ord_hhbm like @ord_hhbm_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_bmje>=@ord_bmje_min 
and ord_bmje<=@ord_bmje_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_mdbm like @ord_mdbm_min 
and ord_mdbm like @ord_mdbm_max 
and (ord_finished>=1) order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_265 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_265_authority a with(nolock)  , #pobjs 
--where 
  --  nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
drop table #pobjs
set nocount off end