IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_17_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_17_data]
GO
CREATE PROCEDURE report_17_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@md_jbr varchar(512)
,@md_clzt varchar(512)
,@md_fgcs varchar(512)
,@md_branch varchar(512)
,@md_wxysj varchar(512)
,@md_rqlx varchar(512)
,@md_jdrq varchar(512)
,@md_wcrq varchar(512)
,@md_xqlx varchar(512)
,@md_djzt varchar(512)

AS
BEGIN
 set nocount on 

declare @dataIds table(objid int)
if @objids!='_NONE' and @objids!='_none_' and @objids!='' 
insert into @dataIds(objid)
select value from dbo.fn_split_int(@objids)
if @tableid='tb_report1'
begin
    if @pagesize=10000
    begin
       select 
ord_no , ord_date ,ord_branch,ord_xqlx , ord_xqms ,ord_creator , ord_whry , ord_clzt , ord_clym , ord_clhs1, ord_zhmyd , ord_fgcs , ord_wxysj2, 
ord_mxysj  = case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END
,ord_dfqk,ord_whwcsj
 from 
order_243 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@md_jbr = '' or ord_whry like '%' + @md_jbr + '%')
and (@md_clzt = '' or ord_clzt = @md_clzt or (@md_clzt = '未响应' and ord_clzt = ''))
and (@md_fgcs = '' or ord_fgcs = @md_fgcs)
and (@md_branch = '' or ord_branch = @md_branch)
and 
((@md_wxysj = '超过24小时' and (case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END) >= 24 )
or (@md_wxysj = '超过48小时' and (case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END) >= 48 ) 
or (@md_wxysj = ''))
and ((@md_rqlx = '建单日期' and ord_date >= @md_jdrq + ' 00:00:00' and ord_date <= @md_wcrq + ' 23:59:59')
or (@md_rqlx = '完成日期' and ord_whwcsj >= @md_jdrq + ' 00:00:00' and ord_whwcsj <= @md_wcrq + ' 23:59:59'))
and (@md_xqlx = '' or ord_xqlx = @md_xqlx)
and (@md_djzt = '' or @md_djzt = (case when ord_status = '已完成' then '已完成' else '未完成' end))

       return
    end
    if @objids!='_none_'
    begin
       select 
ord_no , ord_date ,ord_branch,ord_xqlx , ord_xqms ,ord_creator , ord_whry , ord_clzt , ord_clym , ord_clhs1, ord_zhmyd , ord_fgcs , ord_wxysj2, 
ord_mxysj  = case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END
,ord_dfqk,ord_whwcsj
 from @dataIds,order_243 where objid=ord_id
       return
    end

end
--select 'tb_report1' tableid, * from order_234 where ord_status != '已作废' and charindex('临',ord_no) = 0

select count(*) as dataCount from 
order_243 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@md_jbr = '' or ord_whry like '%' + @md_jbr + '%')
and (@md_clzt = '' or ord_clzt = @md_clzt or (@md_clzt = '未响应' and ord_clzt = ''))
and (@md_fgcs = '' or ord_fgcs = @md_fgcs)
and (@md_branch = '' or ord_branch = @md_branch)
and 
((@md_wxysj = '超过24小时' and (case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END) >= 24 )
or (@md_wxysj = '超过48小时' and (case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END) >= 48 ) 
or (@md_wxysj = ''))
and ((@md_rqlx = '建单日期' and ord_date >= @md_jdrq + ' 00:00:00' and ord_date <= @md_wcrq + ' 23:59:59')
or (@md_rqlx = '完成日期' and ord_whwcsj >= @md_jdrq + ' 00:00:00' and ord_whwcsj <= @md_wcrq + ' 23:59:59'))
and (@md_xqlx = '' or ord_xqlx = @md_xqlx)
and (@md_djzt = '' or @md_djzt = (case when ord_status = '已完成' then '已完成' else '未完成' end))

select top 99990 ord_id as dataIds from 
order_243 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@md_jbr = '' or ord_whry like '%' + @md_jbr + '%')
and (@md_clzt = '' or ord_clzt = @md_clzt or (@md_clzt = '未响应' and ord_clzt = ''))
and (@md_fgcs = '' or ord_fgcs = @md_fgcs)
and (@md_branch = '' or ord_branch = @md_branch)
and 
((@md_wxysj = '超过24小时' and (case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END) >= 24 )
or (@md_wxysj = '超过48小时' and (case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END) >= 48 ) 
or (@md_wxysj = ''))
and ((@md_rqlx = '建单日期' and ord_date >= @md_jdrq + ' 00:00:00' and ord_date <= @md_wcrq + ' 23:59:59')
or (@md_rqlx = '完成日期' and ord_whwcsj >= @md_jdrq + ' 00:00:00' and ord_whwcsj <= @md_wcrq + ' 23:59:59'))
and (@md_xqlx = '' or ord_xqlx = @md_xqlx)
and (@md_djzt = '' or @md_djzt = (case when ord_status = '已完成' then '已完成' else '未完成' end))
 order by ord_id
select top 10 'tb_report1' tableid ,
ord_no , ord_date ,ord_branch,ord_xqlx , ord_xqms ,ord_creator , ord_whry , ord_clzt , ord_clym , ord_clhs1, ord_zhmyd , ord_fgcs , ord_wxysj2, 
ord_mxysj  = case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END
,ord_dfqk,ord_whwcsj
 from 
order_243 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@md_jbr = '' or ord_whry like '%' + @md_jbr + '%')
and (@md_clzt = '' or ord_clzt = @md_clzt or (@md_clzt = '未响应' and ord_clzt = ''))
and (@md_fgcs = '' or ord_fgcs = @md_fgcs)
and (@md_branch = '' or ord_branch = @md_branch)
and 
((@md_wxysj = '超过24小时' and (case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END) >= 24 )
or (@md_wxysj = '超过48小时' and (case when ord_clzt = '' then (select datediff(minute,ord_date,GETDATE())/60.00) ELSE 0 END) >= 48 ) 
or (@md_wxysj = ''))
and ((@md_rqlx = '建单日期' and ord_date >= @md_jdrq + ' 00:00:00' and ord_date <= @md_wcrq + ' 23:59:59')
or (@md_rqlx = '完成日期' and ord_whwcsj >= @md_jdrq + ' 00:00:00' and ord_whwcsj <= @md_wcrq + ' 23:59:59'))
and (@md_xqlx = '' or ord_xqlx = @md_xqlx)
and (@md_djzt = '' or @md_djzt = (case when ord_status = '已完成' then '已完成' else '未完成' end))
 order by ord_id

END