IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_752_get_zbjddb]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_752_get_zbjddb]
GO
CREATE PROCEDURE userfunc_752_get_zbjddb(@cmpid int,   @branchid int,  @userdpid int,  @useruid int,   @userotid int,  @usersecurity int,@ctgid int,@ctgid2 int,@objid bigint,@detail varchar(50),@viewid varchar(10),@date varchar(10),@types varchar(50),@branch varchar(20),@area varchar(50),@store varchar(50),@grade varchar(20))
as
begin



       set nocount on;
    declare @sql nvarchar(1000);
    DECLARE @partdata table(name varchar(128),value decimal(10,1));

              create table #temp (
    title varchar(50),
    title1 varchar(50),
    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 @sql = 'insert into #temp(title,title1,ord_zdxl,ord_zdxl1,ord_zdmsr,ord_zdmsr1,ord_pjmsr,ord_pjmsr1,ord_zmsr,ord_zmsr1,ord_ydsr,ord_ydsr1) select ';
    
    if @viewid = 'branchz' 
    begin
            set @sql = @sql + '[分公司名称],[分公司简称],'
    end 
    else if @viewid = 'branchf' 
    begin
            set @sql = @sql + '[区域],[区域简称],'
    end 
    else if @viewid = 'area'
    begin 
       set @sql = @sql + '[门店号],[门店简称],'
    end
    else if @viewid = 'store'
    begin 
       set @sql = @sql + '[营业员],[营业员],'
    end      
          
    set @sql = @sql + ' sum([终端预算量]),sum([终端完成量]),sum([终端毛收入预算量]),sum([终端毛收入完成量]),'
          + 'sum([配件毛收入预算量]),sum([配件毛收入完成量]),sum([总毛收入预算量]),sum([总毛收入完成量]),sum([移动毛收入预算量]),sum([移动毛收入完成量])'
          
    if @viewid = 'store'
        set @sql = @sql + ' FROM [DBTC].[datas].[dbo].[dd_date_yg]'
    else 
        set @sql = @sql + ' FROM [DBTC].[datas].[dbo].[dd_date_md]'
          
    set @sql = @sql + ' where [数据日期] =''' + @date + ''' and  [商圈类型] in (' +  @types + ')';
    
    if @grade != '全部类别' and @viewid != 'store'
    begin
       set @sql = @sql + ' and [门店类别] =''' +  @grade + '''';
    end
    
    if @viewid = 'branchz' 
    begin
       set @sql = @sql + ' group by [分公司名称],[分公司简称]';
    end 
    else if @viewid = 'branchf' 
    begin
       set @sql = @sql + ' and [分公司名称] = ''' + @branch + '''';
       set @sql = @sql + ' group by [区域],[区域简称] ';
    end 
    else if @viewid = 'area'
    begin 
       set @sql = @sql + ' and [分公司名称] = ''' + @branch + ''' and [区域] = ''' + @area + '''';
       set @sql = @sql + ' group by [门店号],[门店简称] ';
    end
    else if @viewid = 'store'
    begin 
       set @sql = @sql + ' and [分公司名称] = ''' + @branch + ''' and [门店号] = ''' + @store + '''';
       set @sql = @sql + ' group by [营业员],[营业员] ';
    end
    
    exec(@sql);
    
    if @detail='终端销量'
    begin
        insert into @partdata(name,value)
        select (title+'@'+title1),(case when ord_zdxl > 0 then (ord_zdxl1/ord_zdxl)*100 else 0 end) from #temp
    end 
    else if @detail='终端毛收入'
    begin
        insert into @partdata(name,value)
        select (title+'@'+title1),(case when ord_zdmsr > 0 then (ord_zdmsr1/ord_zdmsr)*100 else 0 end) from #temp
    end  
    else if @detail='配件毛收入'
    begin
        insert into @partdata(name,value)
        select (title+'@'+title1),(case when ord_pjmsr > 0 then (ord_pjmsr1/ord_pjmsr)*100 else 0 end) from #temp
    end  
    else if @detail='总毛利收入'
    begin
        insert into @partdata(name,value)
        select (title+'@'+title1),(case when ord_zmsr > 0 then (ord_zmsr1/ord_zmsr)*100 else 0 end) from #temp
    end  
    else if @detail='移动毛收入'
    begin
        insert into @partdata(name,value)
        select (title+'@'+title1),(case when ord_ydsr > 0 then (ord_ydsr1/ord_ydsr)*100 else 0 end) from #temp
    end  

    select 'chart_zbjddb' as tableid,* from @partdata order by value desc
    


end