IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_45_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_45_data]
GO
CREATE PROCEDURE report_45_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_fgs varchar(512)
,@qry_date1 varchar(512)
,@qry_date2 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)
create table #tempt(
tableid varchar(10),
ord_wgmdmc varchar(50),--违规门店名称
ord_wgmdcs bigint,--通报违规次数
ord_je bigint, --扣款
ord_mdid bigint --门店id
)

if(@qry_fgs=7)
    begin
        insert into #tempt(tableid,ord_wgmdmc,ord_mdid)
        select distinct 'tb_report',ord_dpmc,ord_gssymdid 
        from order_272 
        where ord_dpmc like '%天府新区%'
        and ord_no not like '%临%'
        and ord_status != '已作废'
        and ord_date >= @qry_date1 + ' 00:00:00' 
        and  ord_date <= @qry_date2 + ' 23:59:59'
    end
else if(@qry_fgs=18)
    begin
    insert into #tempt(tableid,ord_wgmdmc,ord_mdid)
        select distinct 'tb_report',ord_dpmc,ord_gssymdid 
        from order_272 
        where ord_zbxsyfgsid= @qry_fgs
        and ord_dpmc not like '%天府新区%'
        and ord_no not like '%临%'
        and ord_status != '已作废'
        and ord_date >= @qry_date1 + ' 00:00:00' 
        and  ord_date <= @qry_date2 + ' 23:59:59'
    end
else
    begin
        insert into #tempt(tableid,ord_wgmdmc,ord_mdid)
        select distinct 'tb_report',ord_dpmc,ord_gssymdid 
        from order_272 
        where ord_zbxsyfgsid= @qry_fgs
        and ord_no not like '%临%'
        and ord_status != '已作废'
        and ord_date >= @qry_date1 + ' 00:00:00' 
        and  ord_date <= @qry_date2 + ' 23:59:59'
    end


update #tempt set ord_wgmdcs = (select count(10) from order_272 
where ord_gssymdid=#tempt.ord_mdid 
and  ord_no not like '%临%'
and ord_status != '已作废'
and ord_date >= @qry_date1 + ' 00:00:00' 
and  ord_date <= @qry_date2 + ' 23:59:59')


update #tempt set ord_je = (select sum(convert (int,(substring(ord2_dsr,1,len(ord2_dsr)-1))))+sum(convert (int,(substring(ord2_dzc,1,len(ord2_dzc)-1))))+sum(convert (int,(substring(ord2_qydd,1,len(ord2_qydd)-1)))) from order_272_2 
where ord2_ordid in (select distinct ord_id from order_272 
where ord_gssymdid=#tempt.ord_mdid 
and  ord_no not like '%临%'
and ord_status != '已作废' 
and ord2_jcqk = '不合格' 
and ord_date >= @qry_date1 + ' 00:00:00' 
and  ord_date <= @qry_date2 + ' 23:59:59'))


select * from #tempt
END