IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_34_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_34_data]
GO
CREATE PROCEDURE report_34_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_viewid varchar(512)
,@qry_types varchar(512)
,@qry_date varchar(512)
,@qry_branch varchar(512)
,@qry_area varchar(512)
,@qry_store varchar(512)
,@qry_grade 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)
declare @sql1 nvarchar(1000);
declare @sql2 nvarchar(1000);

declare @viewid varchar(10)
--declare @qrydate varchar(10)
--SELECT @qrydate = CONVERT(varchar(10), DATEADD(DAY,-1,GETDATE()) , 120)
select @viewid = @qry_viewid

create table #temp1 (
    branch varchar(100),
    area varchar(100),
    store varchar(100),
    ord_jrxl int default 0,
    ord_ljts int default 0,
    ord_jrcl decimal(10,2) default 0
)

create table #temp2 (
    ord_jdzb varchar(10) default 0,
    ord_jd decimal(10,2) default 0
)

create table #temp21 (
    ord_zdxl decimal(18,2) default 0,  --预算量
    ord_zdxl1 decimal(18,2) default 0, --实际量
    ord_zdmsr decimal(18,2) default 0,
    ord_zdmsr1 decimal(18,2) default 0,
    ord_pjmsr decimal(18,2) default 0,
    ord_pjmsr1 decimal(18,2) default 0,
    ord_zmsr decimal(18,2) default 0,
    ord_zmsr1 decimal(18,2) default 0,
    ord_ydsr decimal(18,2) default 0,
    ord_ydsr1 decimal(18,2) default 0
)

set @sql1 = 'insert into #temp1(ord_jrxl,ord_ljts,ord_jrcl)'
          + ' SELECT sum([当日销售量]),sum([累计销售量]),sum([总毛收入])/10000.00'
          + ' FROM [DBTC].[datas].[dbo].[dd_zbgl_date]'
          + ' where [商圈类型] in (' +  @qry_types + ')';
          
set @sql2 = 'insert into #temp21(ord_zdxl,ord_zdxl1,ord_zdmsr,ord_zdmsr1,ord_pjmsr,ord_pjmsr1,ord_zmsr,ord_zmsr1,ord_ydsr,ord_ydsr1)'
          + ' select sum([终端预算量]),sum([终端完成量]),sum([终端毛收入预算量]),sum([终端毛收入完成量]),'
          + 'sum([配件毛收入预算量]),sum([配件毛收入完成量]),sum([总毛收入预算量]),sum([总毛收入完成量]),'
          + 'sum([移动毛收入预算量]),sum([移动毛收入完成量])'
          + ' FROM [DBTC].[datas].[dbo].[dd_date_md]'
          + ' where [数据日期] =''' + @qry_date + ''' and [商圈类型] in (' +  @qry_types + ')';

if @viewid = 'branchf' 
begin
   set @sql1 = @sql1 + ' and [分公司名称] =''' +  @qry_branch + ''''
   set @sql2 = @sql2 + ' and [分公司名称] =''' +  @qry_branch + ''''
end 
else if @viewid = 'area'
begin 
   set @sql1 = @sql1 + ' and [分公司名称] =''' +  @qry_branch + ''' and [区域] = ''' + @qry_area + '''';
   set @sql2 = @sql2 + ' and [分公司名称] =''' +  @qry_branch + ''' and [区域] = ''' + @qry_area + '''';
end
else if @viewid = 'store'
begin 
   set @sql1 = @sql1 + ' and [分公司名称] =''' +  @qry_branch + ''' and [门店号] = ''' + @qry_store + '''';
   set @sql2 = @sql2 + ' and [分公司名称] =''' +  @qry_branch + ''' and [门店号] = ''' + @qry_store + '''';
end

if @qry_grade != '全部类别' and @viewid != 'store'
begin
    set @sql1 = @sql1 + ' and [门店类别] =''' +  @qry_grade + '''';
    set @sql2 = @sql2 + ' and [门店类别] =''' +  @qry_grade + '''';
end



exec(@sql1);
exec(@sql2);

insert into #temp2(ord_jdzb,ord_jd)
values('终端销量',(select case when ord_zdxl > 0 then (ord_zdxl1/ord_zdxl)*100 else 0 end from #temp21))
insert into #temp2(ord_jdzb,ord_jd)
values('终端毛收入',(select case when ord_zdmsr > 0 then (ord_zdmsr1/ord_zdmsr)*100 else 0 end from #temp21))
insert into #temp2(ord_jdzb,ord_jd)
values('配件毛收入',(select case when ord_pjmsr > 0 then (ord_pjmsr1/ord_pjmsr)*100 else 0 end from #temp21))
insert into #temp2(ord_jdzb,ord_jd)
values('总毛利收入',(select case when ord_zmsr > 0 then (ord_zmsr1/ord_zmsr)*100 else 0 end from #temp21))
insert into #temp2(ord_jdzb,ord_jd)
values('移动毛收入',(select case when ord_ydsr > 0 then (ord_ydsr1/ord_ydsr)*100 else 0 end from #temp21))

select 'tb_report1' as tableid,ord_jrxl,ord_ljts,ord_jrcl from #temp1
select 'chart_byjd' as tableid,* from #temp2 order by ord_jd desc




END